index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. return textArray.map((line) => {
  53. return {
  54. type: 'element',
  55. tagName: 'div',
  56. properties: { className: ['code-line'] },
  57. children: [{ type: 'text', value: line }],
  58. }
  59. })
  60. }
  61. /**
  62. * Rehype plugin that highlights code blocks with refractor (prismjs)
  63. *
  64. * Set `showLineNumbers` to `true` to always display line number
  65. *
  66. * Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
  67. *
  68. * @typedef {{ showLineNumbers?: boolean, ignoreMissing?: boolean }} RehypePrismOptions
  69. * @param {RehypePrismOptions} options
  70. * @return {Visitor}
  71. */
  72. const rehypePrism = (options) => {
  73. options = options || {}
  74. return (tree) => {
  75. visit(tree, 'element', visitor)
  76. }
  77. /**
  78. * @param {Node} node
  79. * @param {number} index
  80. * @param {Parent} parent
  81. */
  82. function visitor(node, index, parent) {
  83. if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
  84. return
  85. }
  86. const lang = getLanguage(node)
  87. /** @type {string} */
  88. // @ts-ignore
  89. let meta = node.data && node.data.meta ? node.data.meta : ''
  90. if (lang) {
  91. parent.properties.className = (parent.properties.className || []).concat('language-' + lang)
  92. // Add lang to meta to allow line highlighting even when no lang is specified
  93. meta = `${lang} ${meta}`
  94. }
  95. const shouldHighlightLine = calculateLinesToHighlight(meta)
  96. const codeLineArray = splitLine(toString(node))
  97. for (const [i, line] of codeLineArray.entries()) {
  98. // Code lines
  99. if (meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) || options.showLineNumbers) {
  100. line.properties.line = [(i + 1).toString()]
  101. line.properties.className = [`${line.properties.className} line-number`]
  102. }
  103. // Line highlight
  104. if (shouldHighlightLine(i)) {
  105. line.properties.className = [`${line.properties.className} highlight-line`]
  106. }
  107. // Syntax highlight
  108. if (lang) {
  109. try {
  110. line.children = refractor.highlight(line.children[0].value, lang).children
  111. } catch (err) {
  112. // eslint-disable-next-line no-empty
  113. if (options.ignoreMissing && /Unknown language/.test(err.message)) {
  114. } else {
  115. throw err
  116. }
  117. }
  118. }
  119. }
  120. node.children = codeLineArray
  121. }
  122. }
  123. export default rehypePrism