Git based wiki inspired by Gollum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.8KB

  1. // Init highlight JS
  2. hljs.initHighlightingOnLoad();
  3. function splitInput(str) {
  4. if (str.slice(0, 3) !== '---') return;
  5. var matcher = /\n(\.{3}|\-{3})/g;
  6. var metaEnd = matcher.exec(str);
  7. return metaEnd && [str.slice(0, metaEnd.index), str.slice(matcher.lastIndex)];
  8. }
  9. /* © 2013 j201
  10. * https://github.com/j201/meta-marked */
  11. // Splits the given string into a meta section and a markdown section if a meta section is present, else returns null
  12. var metaMarked = function(src, opt, callback) {
  13. if (Object.prototype.toString.call(src) !== '[object String]')
  14. throw new TypeError('First parameter must be a string.');
  15. var mySplitInput = splitInput(src);
  16. if (mySplitInput) {
  17. var meta;
  18. try {
  19. meta = jsyaml.safeLoad(mySplitInput[0]);
  20. } catch(e) {
  21. meta = null;
  22. }
  23. return {
  24. meta: meta,
  25. md: mySplitInput[1]
  26. };
  27. } else {
  28. return {
  29. meta: null,
  30. md: src
  31. }
  32. }
  33. };
  34. var markdownit = window.markdownit({
  35. html: true,
  36. linkify: true,
  37. typographer: true,
  38. highlight: function (str, lang) {
  39. if (lang && hljs.getLanguage(lang)) {
  40. try {
  41. return hljs.highlight(lang, str).value;
  42. } catch (__) {
  43. }
  44. }
  45. return ''; // use external default escaping
  46. }
  47. }).use(markdownItAnchor, {
  48. level: 1,
  49. // slugify: string => string,
  50. permalink: false,
  51. // renderPermalink: (slug, opts, state, permalink) => {},
  52. permalinkClass: 'header-anchor',
  53. permalinkSymbol: '¶',
  54. permalinkBefore: false
  55. });
  56. // Markdown Renderer
  57. var MDR = {
  58. meta: null,
  59. md: null,
  60. sanitize: true, // Override
  61. parse: function(md){
  62. return markdownit.render(md);
  63. },
  64. convert: function(md, sanitize) {
  65. if (this.sanitize !== null) {
  66. sanitize = this.sanitize;
  67. }
  68. this.md = md;
  69. this.processMeta();
  70. try {
  71. var html = this.parse(this.md);
  72. } catch(e) {
  73. return this.md;
  74. }
  75. if (sanitize) {
  76. // Causes some problems with inline styles
  77. html = html_sanitize(html, function(url) {
  78. try {
  79. var prot = decodeURIComponent(url.toString());
  80. } catch (e) {
  81. return '';
  82. }
  83. if (prot.indexOf('javascript:') === 0) {
  84. return '';
  85. }
  86. return prot;
  87. }, function(id){
  88. return id;
  89. });
  90. }
  91. this.hook();
  92. return html;
  93. },
  94. processMeta: function() {
  95. var doc = metaMarked(this.md);
  96. this.md = doc.md;
  97. this.meta = doc.meta;
  98. if (this.meta) {
  99. try {
  100. var template = Handlebars.compile(this.md);
  101. this.md = template(this.meta);
  102. } catch(e) {
  103. console.log(e);
  104. }
  105. }
  106. },
  107. hook: function() {
  108. }
  109. };
  110. // Add some custom classes to table tags
  111. markdownit.renderer.rules.table_open = function (tokens, idx, options, env, self) {
  112. tokens[idx].attrPush(['class', 'table table-bordered']);
  113. return self.renderToken(tokens, idx, options);
  114. };