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.

418 lines
12 KiB

  1. 'use strict'
  2. /* global winston */
  3. const Promise = require('bluebird')
  4. const md = require('markdown-it')
  5. const mdEmoji = require('markdown-it-emoji')
  6. const mdTaskLists = require('markdown-it-task-lists')
  7. const mdAbbr = require('markdown-it-abbr')
  8. const mdAnchor = require('markdown-it-anchor')
  9. const mdFootnote = require('markdown-it-footnote')
  10. const mdExternalLinks = require('markdown-it-external-links')
  11. const mdExpandTabs = require('markdown-it-expand-tabs')
  12. const mdAttrs = require('markdown-it-attrs')
  13. const mdMathjax = require('markdown-it-mathjax')()
  14. const mathjax = require('mathjax-node')
  15. const hljs = require('highlight.js')
  16. const cheerio = require('cheerio')
  17. const _ = require('lodash')
  18. const mdRemove = require('remove-markdown')
  19. // Load plugins
  20. var mkdown = md({
  21. html: true,
  22. breaks: appconfig.features.linebreaks,
  23. linkify: true,
  24. typography: true,
  25. highlight(str, lang) {
  26. if (appconfig.theme.code.colorize && lang && hljs.getLanguage(lang)) {
  27. try {
  28. return '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>'
  29. } catch (err) {
  30. return '<pre><code>' + _.escape(str) + '</code></pre>'
  31. }
  32. }
  33. return '<pre><code>' + _.escape(str) + '</code></pre>'
  34. }
  35. })
  36. .use(mdEmoji)
  37. .use(mdTaskLists)
  38. .use(mdAbbr)
  39. .use(mdAnchor, {
  40. slugify: _.kebabCase,
  41. permalink: true,
  42. permalinkClass: 'toc-anchor nc-icon-outline location_bookmark-add',
  43. permalinkSymbol: '',
  44. permalinkBefore: true
  45. })
  46. .use(mdFootnote)
  47. .use(mdExternalLinks, {
  48. externalClassName: 'external-link',
  49. internalClassName: 'internal-link'
  50. })
  51. .use(mdExpandTabs, {
  52. tabWidth: 4
  53. })
  54. .use(mdAttrs)
  55. if (appconfig.features.mathjax) {
  56. mkdown.use(mdMathjax)
  57. }
  58. // Rendering rules
  59. mkdown.renderer.rules.emoji = function (token, idx) {
  60. return '<i class="twa twa-' + _.replace(token[idx].markup, /_/g, '-') + '"></i>'
  61. }
  62. // Video rules
  63. const videoRules = [
  64. {
  65. selector: 'a.youtube',
  66. regexp: new RegExp(/(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|&v(?:i)?=))([^#&?]*).*/i),
  67. output: '<iframe width="640" height="360" src="https://www.youtube.com/embed/{0}?rel=0" frameborder="0" allowfullscreen></iframe>'
  68. },
  69. {
  70. selector: 'a.vimeo',
  71. regexp: new RegExp(/vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^/]*)\/videos\/|album\/(?:\d+)\/video\/|)(\d+)(?:$|\/|\?)/i),
  72. output: '<iframe src="https://player.vimeo.com/video/{0}" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
  73. },
  74. {
  75. selector: 'a.dailymotion',
  76. regexp: new RegExp(/(?:dailymotion\.com(?:\/embed)?(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[-_0-9a-zA-Z]+(?:#video=)?([a-z0-9]+)?)?/i),
  77. output: '<iframe width="640" height="360" src="//www.dailymotion.com/embed/video/{0}?endscreen-enable=false" frameborder="0" allowfullscreen></iframe>'
  78. },
  79. {
  80. selector: 'a.video',
  81. regexp: false,
  82. output: '<video width="640" height="360" controls preload="metadata"><source src="{0}" type="video/mp4"></video>'
  83. }
  84. ]
  85. // Regex
  86. const textRegex = new RegExp('\\b[a-z0-9-.,' + appdata.regex.cjk + appdata.regex.arabic + ']+\\b', 'g')
  87. const mathRegex = [
  88. {
  89. format: 'TeX',
  90. regex: /\\\[([\s\S]*?)\\\]/g
  91. },
  92. {
  93. format: 'inline-TeX',
  94. regex: /\\\((.*?)\\\)/g
  95. },
  96. {
  97. format: 'MathML',
  98. regex: /<math([\s\S]*?)<\/math>/g
  99. }
  100. ]
  101. // MathJax
  102. mathjax.config({
  103. MathJax: {
  104. jax: ['input/TeX', 'input/MathML', 'output/SVG'],
  105. extensions: ['tex2jax.js', 'mml2jax.js'],
  106. TeX: {
  107. extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']
  108. },
  109. SVG: {
  110. scale: 120,
  111. font: 'STIX-Web'
  112. }
  113. }
  114. })
  115. /**
  116. * Parse markdown content and build TOC tree
  117. *
  118. * @param {(Function|string)} content Markdown content
  119. * @return {Array} TOC tree
  120. */
  121. const parseTree = (content) => {
  122. content = content.replace(/<!--(.|\t|\n|\r)*?-->/g, '')
  123. let tokens = md().parse(content, {})
  124. let tocArray = []
  125. // -> Extract headings and their respective levels
  126. for (let i = 0; i < tokens.length; i++) {
  127. if (tokens[i].type !== 'heading_close') {
  128. continue
  129. }
  130. const heading = tokens[i - 1]
  131. const headingclose = tokens[i]
  132. if (heading.type === 'inline') {
  133. let content = ''
  134. let anchor = ''
  135. if (heading.children && heading.children.length > 0 && heading.children[0].type === 'link_open') {
  136. content = mdRemove(heading.children[1].content)
  137. anchor = _.kebabCase(content)
  138. } else {
  139. content = mdRemove(heading.content)
  140. anchor = _.kebabCase(heading.children.reduce((acc, t) => acc + t.content, ''))
  141. }
  142. tocArray.push({
  143. content,
  144. anchor,
  145. level: +headingclose.tag.substr(1, 1)
  146. })
  147. }
  148. }
  149. // -> Exclude levels deeper than 2
  150. _.remove(tocArray, (n) => { return n.level > 2 })
  151. // -> Build tree from flat array
  152. return _.reduce(tocArray, (tree, v) => {
  153. let treeLength = tree.length - 1
  154. if (v.level < 2) {
  155. tree.push({
  156. content: v.content,
  157. anchor: v.anchor,
  158. nodes: []
  159. })
  160. } else {
  161. let lastNodeLevel = 1
  162. let GetNodePath = (startPos) => {
  163. lastNodeLevel++
  164. if (_.isEmpty(startPos)) {
  165. startPos = 'nodes'
  166. }
  167. if (lastNodeLevel === v.level) {
  168. return startPos
  169. } else {
  170. return GetNodePath(startPos + '[' + (_.at(tree[treeLength], startPos).length - 1) + '].nodes')
  171. }
  172. }
  173. let lastNodePath = GetNodePath()
  174. let lastNode = _.get(tree[treeLength], lastNodePath)
  175. if (lastNode) {
  176. lastNode.push({
  177. content: v.content,
  178. anchor: v.anchor,
  179. nodes: []
  180. })
  181. _.set(tree[treeLength], lastNodePath, lastNode)
  182. }
  183. }
  184. return tree
  185. }, [])
  186. }
  187. /**
  188. * Parse markdown content to HTML
  189. *
  190. * @param {String} content Markdown content
  191. * @return {Promise<String>} Promise
  192. */
  193. const parseContent = (content) => {
  194. let cr = cheerio.load(mkdown.render(content))
  195. if (cr.root().children().length < 1) {
  196. return ''
  197. }
  198. // -> Check for empty first element
  199. let firstElm = cr.root().children().first()[0]
  200. if (firstElm.type === 'tag' && firstElm.name === 'p') {
  201. let firstElmChildren = firstElm.children
  202. if (firstElmChildren.length < 1) {
  203. firstElm.remove()
  204. } else if (firstElmChildren.length === 1 && firstElmChildren[0].type === 'tag' && firstElmChildren[0].name === 'img') {
  205. cr(firstElm).addClass('is-gapless')
  206. }
  207. }
  208. // -> Remove links in headers
  209. cr('h1 > a:not(.toc-anchor), h2 > a:not(.toc-anchor), h3 > a:not(.toc-anchor)').each((i, elm) => {
  210. let txtLink = cr(elm).text()
  211. cr(elm).replaceWith(txtLink)
  212. })
  213. // -> Re-attach blockquote styling classes to their parents
  214. cr('blockquote').each((i, elm) => {
  215. if (cr(elm).children().length > 0) {
  216. let bqLastChild = cr(elm).children().last()[0]
  217. let bqLastChildClasses = cr(bqLastChild).attr('class')
  218. if (bqLastChildClasses && bqLastChildClasses.length > 0) {
  219. cr(bqLastChild).removeAttr('class')
  220. cr(elm).addClass(bqLastChildClasses)
  221. }
  222. }
  223. })
  224. // -> Enclose content below headers
  225. cr('h2').each((i, elm) => {
  226. let subH2Content = cr(elm).nextUntil('h1, h2')
  227. cr(elm).after('<div class="indent-h2"></div>')
  228. let subH2Container = cr(elm).next('.indent-h2')
  229. _.forEach(subH2Content, (ch) => {
  230. cr(subH2Container).append(ch)
  231. })
  232. })
  233. cr('h3').each((i, elm) => {
  234. let subH3Content = cr(elm).nextUntil('h1, h2, h3')
  235. cr(elm).after('<div class="indent-h3"></div>')
  236. let subH3Container = cr(elm).next('.indent-h3')
  237. _.forEach(subH3Content, (ch) => {
  238. cr(subH3Container).append(ch)
  239. })
  240. })
  241. // Replace video links with embeds
  242. _.forEach(videoRules, (vrule) => {
  243. cr(vrule.selector).each((i, elm) => {
  244. let originLink = cr(elm).attr('href')
  245. if (vrule.regexp) {
  246. let vidMatches = originLink.match(vrule.regexp)
  247. if ((vidMatches && _.isArray(vidMatches))) {
  248. vidMatches = _.filter(vidMatches, (f) => {
  249. return f && _.isString(f)
  250. })
  251. originLink = _.last(vidMatches)
  252. }
  253. }
  254. let processedLink = _.replace(vrule.output, '{0}', originLink)
  255. cr(elm).replaceWith(processedLink)
  256. })
  257. })
  258. // Apply align-center to parent
  259. cr('img.align-center').each((i, elm) => {
  260. cr(elm).parent().addClass('align-center')
  261. cr(elm).removeClass('align-center')
  262. })
  263. // Mathjax Post-processor
  264. if (appconfig.features.mathjax) {
  265. return processMathjax(cr.html())
  266. } else {
  267. return Promise.resolve(cr.html())
  268. }
  269. }
  270. /**
  271. * Process MathJax expressions
  272. *
  273. * @param {String} content HTML content
  274. * @returns {Promise<String>} Promise
  275. */
  276. const processMathjax = (content) => {
  277. let matchStack = []
  278. let replaceStack = []
  279. let currentMatch
  280. let mathjaxState = {}
  281. _.forEach(mathRegex, mode => {
  282. do {
  283. currentMatch = mode.regex.exec(content)
  284. if (currentMatch) {
  285. matchStack.push(currentMatch[0])
  286. replaceStack.push(
  287. new Promise((resolve, reject) => {
  288. mathjax.typeset({
  289. math: (mode.format === 'MathML') ? currentMatch[0] : currentMatch[1],
  290. format: mode.format,
  291. speakText: false,
  292. svg: true,
  293. state: mathjaxState,
  294. timeout: 30 * 1000
  295. }, result => {
  296. if (!result.errors) {
  297. resolve(result.svg)
  298. } else {
  299. resolve(currentMatch[0])
  300. winston.warn(result.errors.join(', '))
  301. }
  302. })
  303. })
  304. )
  305. }
  306. } while (currentMatch)
  307. })
  308. return (matchStack.length > 0) ? Promise.all(replaceStack).then(results => {
  309. _.forEach(matchStack, (repMatch, idx) => {
  310. content = content.replace(repMatch, results[idx])
  311. })
  312. return content
  313. }) : Promise.resolve(content)
  314. }
  315. /**
  316. * Parse meta-data tags from content
  317. *
  318. * @param {String} content Markdown content
  319. * @return {Object} Properties found in the content and their values
  320. */
  321. const parseMeta = (content) => {
  322. let commentMeta = new RegExp('<!-- ?([a-zA-Z]+):(.*)-->', 'g')
  323. let results = {}
  324. let match
  325. while ((match = commentMeta.exec(content)) !== null) {
  326. results[_.toLower(match[1])] = _.trim(match[2])
  327. }
  328. return results
  329. }
  330. /**
  331. * Strips non-text elements from Markdown content
  332. *
  333. * @param {String} content Markdown-formatted content
  334. * @return {String} Text-only version
  335. */
  336. const removeMarkdown = (content) => {
  337. return _.join(mdRemove(_.chain(content)
  338. .replace(/<!-- ?([a-zA-Z]+):(.*)-->/g, '')
  339. .replace(/```([^`]|`)+?```/g, '')
  340. .replace(/`[^`]+`/g, '')
  341. .replace(new RegExp('(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?', 'g'), '')
  342. .deburr()
  343. .toLower()
  344. .value()
  345. ).replace(/\r?\n|\r/g, ' ').match(textRegex), ' ')
  346. }
  347. module.exports = {
  348. /**
  349. * Parse content and return all data
  350. *
  351. * @param {String} content Markdown-formatted content
  352. * @return {Object} Object containing meta, html and tree data
  353. */
  354. parse(content) {
  355. return parseContent(content).then(html => {
  356. return {
  357. meta: parseMeta(content),
  358. html,
  359. tree: parseTree(content)
  360. }
  361. })
  362. },
  363. parseContent,
  364. parseMeta,
  365. parseTree,
  366. removeMarkdown
  367. }