markdown.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. var Promise = require('bluebird'),
  3. md = require('markdown-it'),
  4. mdEmoji = require('markdown-it-emoji'),
  5. mdTaskLists = require('markdown-it-task-lists'),
  6. mdAbbr = require('markdown-it-abbr'),
  7. mdAnchor = require('markdown-it-toc-and-anchor').default,
  8. mdFootnote = require('markdown-it-footnote'),
  9. mdExternalLinks = require('markdown-it-external-links'),
  10. mdExpandTabs = require('markdown-it-expand-tabs'),
  11. mdAttrs = require('markdown-it-attrs'),
  12. hljs = require('highlight.js'),
  13. slug = require('slug'),
  14. cheerio = require('cheerio'),
  15. _ = require('lodash');
  16. // Load plugins
  17. var mkdown = md({
  18. html: true,
  19. linkify: true,
  20. typography: true,
  21. highlight(str, lang) {
  22. if (lang && hljs.getLanguage(lang)) {
  23. try {
  24. return '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>';
  25. } catch (err) {
  26. return '<pre><code>' + str + '</code></pre>';
  27. }
  28. }
  29. return '<pre class="hljs"><code>' + hljs.highlightAuto(str).value + '</code></pre>';
  30. }
  31. })
  32. .use(mdEmoji)
  33. .use(mdTaskLists)
  34. .use(mdAbbr)
  35. .use(mdAnchor, {
  36. tocClassName: 'toc',
  37. anchorClassName: 'toc-anchor'
  38. })
  39. .use(mdFootnote)
  40. .use(mdExternalLinks, {
  41. externalClassName: 'external-link',
  42. internalClassName: 'internal-link'
  43. })
  44. .use(mdExpandTabs, {
  45. tabWidth: 4
  46. })
  47. .use(mdAttrs);
  48. // Rendering rules
  49. mkdown.renderer.rules.emoji = function(token, idx) {
  50. return '<i class="twa twa-' + token[idx].markup + '"></i>';
  51. };
  52. mkdown.inline.ruler.push('internal_link', (state) => {
  53. });
  54. /**
  55. * Parse markdown content and build TOC tree
  56. *
  57. * @param {(Function|string)} content Markdown content
  58. * @return {Array} TOC tree
  59. */
  60. const parseTree = (content) => {
  61. let tokens = md().parse(content, {});
  62. let tocArray = [];
  63. //-> Extract headings and their respective levels
  64. for (let i = 0; i < tokens.length; i++) {
  65. if (tokens[i].type !== "heading_close") {
  66. continue;
  67. }
  68. const heading = tokens[i - 1];
  69. const heading_close = tokens[i];
  70. if (heading.type === "inline") {
  71. let content = "";
  72. let anchor = "";
  73. if (heading.children && heading.children[0].type === "link_open") {
  74. content = heading.children[1].content;
  75. anchor = slug(content, {lower: true});
  76. } else {
  77. content = heading.content
  78. anchor = slug(heading.children.reduce((acc, t) => acc + t.content, ""), {lower: true});
  79. }
  80. tocArray.push({
  81. content,
  82. anchor,
  83. level: +heading_close.tag.substr(1, 1)
  84. });
  85. }
  86. }
  87. //-> Exclude levels deeper than 2
  88. _.remove(tocArray, (n) => { return n.level > 2; });
  89. //-> Build tree from flat array
  90. return _.reduce(tocArray, (tree, v) => {
  91. let treeLength = tree.length - 1;
  92. if(v.level < 2) {
  93. tree.push({
  94. content: v.content,
  95. anchor: v.anchor,
  96. nodes: []
  97. });
  98. } else {
  99. let lastNodeLevel = 1;
  100. let GetNodePath = (startPos) => {
  101. lastNodeLevel++;
  102. if(_.isEmpty(startPos)) {
  103. startPos = 'nodes';
  104. }
  105. if(lastNodeLevel === v.level) {
  106. return startPos;
  107. } else {
  108. return GetNodePath(startPos + '[' + (_.at(tree[treeLength], startPos).length - 1) + '].nodes');
  109. }
  110. };
  111. let lastNodePath = GetNodePath();
  112. let lastNode = _.get(tree[treeLength], lastNodePath);
  113. if(lastNode) {
  114. lastNode.push({
  115. content: v.content,
  116. anchor: v.anchor,
  117. nodes: []
  118. });
  119. _.set(tree[treeLength], lastNodePath, lastNode);
  120. }
  121. }
  122. return tree;
  123. }, []);
  124. };
  125. /**
  126. * Parse markdown content to HTML
  127. *
  128. * @param {String} content Markdown content
  129. * @return {String} HTML formatted content
  130. */
  131. const parseContent = (content) => {
  132. let output = mkdown.render(content);
  133. let cr = cheerio.load(output);
  134. cr('table').addClass('table is-bordered is-striped is-narrow');
  135. output = cr.html();
  136. return output;
  137. };
  138. module.exports = {
  139. parse(content) {
  140. return {
  141. html: parseContent(content),
  142. tree: parseTree(content)
  143. };
  144. }
  145. };