index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @typedef {import('unist-util-visit').Node & {properties: Object<any, any>}} Node
  3. * @typedef {import('unist-util-visit').Parent & {properties: Object<any, any>}} Parent
  4. * @typedef {import('unist-util-visit').Visitor<Node>} Visitor
  5. */
  6. import { visit } from 'unist-util-visit'
  7. import toString from 'hast-util-to-string'
  8. import { refractor } from 'refractor'
  9. import rangeParser from 'parse-numeric-range'
  10. /**
  11. * @param {Node} node
  12. * @return {string|null}
  13. */
  14. const getLanguage = (node) => {
  15. const className = node.properties.className || []
  16. for (const classListItem of className) {
  17. if (classListItem.slice(0, 9) === 'language-') {
  18. return classListItem.slice(9).toLowerCase()
  19. }
  20. }
  21. return null
  22. }
  23. /**
  24. * Create a closure that determines if we have to highlight the given index
  25. *
  26. * @param {string} meta
  27. * @return { (index:number) => boolean }
  28. */
  29. const calculateLinesToHighlight = (meta) => {
  30. const RE = /{([\d,-]+)}/
  31. if (RE.test(meta)) {
  32. const strlineNumbers = RE.exec(meta)[1]
  33. const lineNumbers = rangeParser(strlineNumbers)
  34. return (index) => lineNumbers.includes(index + 1)
  35. } else {
  36. return () => false
  37. }
  38. }
  39. /**
  40. * Split line to div node with className `code-line`
  41. *
  42. * @param {string} text
  43. * @return {Node[]}
  44. */
  45. const splitLine = (text) => {
  46. // Xdm Markdown parser every code line with \n
  47. const textArray = text.split(/\n/)
  48. // Remove last line \n which results in empty array
  49. if (textArray[textArray.length - 1].trim() === '') {
  50. textArray.pop()
  51. }
  52. // Empty array are actually line segments so we convert them back to newlines
  53. return textArray.map((line) => {
  54. return {
  55. type: 'element',
  56. tagName: 'div',
  57. properties: { className: ['code-line'] },
  58. children: [{ type: 'text', value: line === '' ? '\n' : line }],
  59. }
  60. })
  61. }
  62. /**
  63. * Rehype plugin that highlights code blocks with refractor (prismjs)
  64. *
  65. * Set `showLineNumbers` to `true` to always display line number
  66. *
  67. * Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
  68. *
  69. * @typedef {{ showLineNumbers?: boolean, ignoreMissing?: boolean }} RehypePrismOptions
  70. * @param {RehypePrismOptions} options
  71. * @return {Visitor}
  72. */
  73. const rehypePrism = (options) => {
  74. options = options || {}
  75. return (tree) => {
  76. visit(tree, 'element', visitor)
  77. }
  78. /**
  79. * @param {Node} node
  80. * @param {number} index
  81. * @param {Parent} parent
  82. */
  83. function visitor(node, index, parent) {
  84. if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
  85. return
  86. }
  87. const lang = getLanguage(node)
  88. /** @type {string} */
  89. // @ts-ignore
  90. let meta = node.data && node.data.meta ? node.data.meta : ''
  91. if (lang) {
  92. parent.properties.className = (parent.properties.className || []).concat('language-' + lang)
  93. // Add lang to meta to allow line highlighting even when no lang is specified
  94. meta = `${lang} ${meta}`
  95. }
  96. const shouldHighlightLine = calculateLinesToHighlight(meta)
  97. const codeLineArray = splitLine(toString(node))
  98. for (const [i, line] of codeLineArray.entries()) {
  99. // Code lines
  100. if (meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) || options.showLineNumbers) {
  101. line.properties.line = [(i + 1).toString()]
  102. line.properties.className = [`${line.properties.className} line-number`]
  103. }
  104. // Line highlight
  105. if (shouldHighlightLine(i)) {
  106. line.properties.className = [`${line.properties.className} highlight-line`]
  107. }
  108. // Syntax highlight
  109. if (lang && line.children) {
  110. try {
  111. line.children = refractor.highlight(line.children[0].value, lang).children
  112. } catch (err) {
  113. // eslint-disable-next-line no-empty
  114. if (options.ignoreMissing && /Unknown language/.test(err.message)) {
  115. } else {
  116. throw err
  117. }
  118. }
  119. }
  120. }
  121. node.children = codeLineArray
  122. }
  123. }
  124. export default rehypePrism