generator.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @typedef {import('hast').Element} Element
  3. * @typedef {import('hast').Root} Root
  4. * @typedef Options options
  5. * Configuration.
  6. * @property {boolean} [showLineNumbers]
  7. * Set `showLineNumbers` to `true` to always display line number
  8. * @property {boolean} [ignoreMissing]
  9. * Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
  10. */
  11. import { visit } from 'unist-util-visit'
  12. import { toString } from 'hast-util-to-string'
  13. import { filter } from 'unist-util-filter'
  14. import rangeParser from 'parse-numeric-range'
  15. /**
  16. * @param {Element} node
  17. * @return {string|null}
  18. */
  19. const getLanguage = (node) => {
  20. const className = node.properties.className
  21. //@ts-ignore
  22. for (const classListItem of className) {
  23. if (classListItem.slice(0, 9) === 'language-') {
  24. return classListItem.slice(9).toLowerCase()
  25. }
  26. }
  27. return null
  28. }
  29. /**
  30. * Create a closure that determines if we have to highlight the given index
  31. *
  32. * @param {string} meta
  33. * @return { (index:number) => boolean }
  34. */
  35. const calculateLinesToHighlight = (meta) => {
  36. const RE = /{([\d,-]+)}/
  37. // Remove space between {} e.g. {1, 3}
  38. const parsedMeta = meta
  39. .split(',')
  40. .map((str) => str.trim())
  41. .join()
  42. if (RE.test(parsedMeta)) {
  43. const strlineNumbers = RE.exec(parsedMeta)[1]
  44. const lineNumbers = rangeParser(strlineNumbers)
  45. return (index) => lineNumbers.includes(index + 1)
  46. } else {
  47. return () => false
  48. }
  49. }
  50. /**
  51. * Check if we want to start the line numbering from a given number or 1
  52. * showLineNumbers=5, will start the numbering from 5
  53. * @param {string} meta
  54. * @returns {number}
  55. */
  56. const calculateStartingLine = (meta) => {
  57. const RE = /showLineNumbers=(?<lines>\d+)/i
  58. // pick the line number after = using a named capturing group
  59. if (RE.test(meta)) {
  60. const {
  61. groups: { lines },
  62. } = RE.exec(meta)
  63. return Number(lines)
  64. }
  65. return 1
  66. }
  67. /**
  68. * Split line to div node with className `code-line`
  69. *
  70. * @param {string} text
  71. * @return {Element[]}
  72. */
  73. const splitLine = (text) => {
  74. // Xdm Markdown parses every code line with \n
  75. const textArray = text.split(/\n/)
  76. // Remove last line \n which results in empty array
  77. if (textArray[textArray.length - 1].trim() === '') {
  78. textArray.pop()
  79. }
  80. // Empty array are actually line segments so we convert them back to newlines
  81. return textArray.map((line) => {
  82. return {
  83. type: 'element',
  84. tagName: 'span',
  85. properties: { className: ['code-line'] },
  86. children: [{ type: 'text', value: line }],
  87. }
  88. })
  89. }
  90. /**
  91. * Add a node start and end line position information for each text node
  92. *
  93. * @return { (ast:Element['children']) => Element['children'] }
  94. *
  95. */
  96. const addNodePositionClosure = () => {
  97. let startLineNum = 1
  98. /**
  99. * @param {Element['children']} ast
  100. * @return {Element['children']}
  101. */
  102. const addNodePosition = (ast) => {
  103. // @ts-ignore
  104. return ast.reduce((result, node) => {
  105. if (node.type === 'text') {
  106. const value = /** @type {string} */ (node.value)
  107. const numLines = (value.match(/\n/g) || '').length
  108. node.position = {
  109. // column: 0 is to make the ts compiler happy but we do not use this field
  110. start: { line: startLineNum, column: 0 },
  111. end: { line: startLineNum + numLines, column: 0 },
  112. }
  113. startLineNum = startLineNum + numLines
  114. result.push(node)
  115. return result
  116. }
  117. if (Object.prototype.hasOwnProperty.call(node, 'children')) {
  118. const initialLineNum = startLineNum
  119. // @ts-ignore
  120. node.children = addNodePosition(node.children, startLineNum)
  121. result.push(node)
  122. node.position = {
  123. start: { line: initialLineNum, column: 0 },
  124. end: { line: startLineNum, column: 0 },
  125. }
  126. return result
  127. }
  128. result.push(node)
  129. return result
  130. }, [])
  131. }
  132. return addNodePosition
  133. }
  134. /**
  135. * Split multiline text nodes into individual nodes with positioning
  136. *
  137. * @param {Element['children']} ast
  138. * @return {Element['children']}
  139. */
  140. const splitTextByLine = (ast) => {
  141. //@ts-ignore
  142. return ast.reduce((result, node) => {
  143. if (node.type === 'text') {
  144. if (node.value.indexOf('\n') === -1) {
  145. result.push(node)
  146. return result
  147. }
  148. const lines = node.value.split('\n')
  149. for (const [i, line] of lines.entries()) {
  150. result.push({
  151. type: 'text',
  152. value: i === lines.length - 1 ? line : line + '\n',
  153. position: {
  154. start: { line: node.position.start.line + i },
  155. end: { line: node.position.start.line + i },
  156. },
  157. })
  158. }
  159. return result
  160. }
  161. if (Object.prototype.hasOwnProperty.call(node, 'children')) {
  162. // @ts-ignore
  163. node.children = splitTextByLine(node.children)
  164. result.push(node)
  165. return result
  166. }
  167. result.push(node)
  168. return result
  169. }, [])
  170. }
  171. /**
  172. * Rehype prism plugin generator that highlights code blocks with refractor (prismjs)
  173. *
  174. * Pass in your own refractor object with the required languages registered:
  175. * https://github.com/wooorm/refractor#refractorregistersyntax
  176. *
  177. * @param {import('refractor/lib/core').Refractor} refractor
  178. * @return {import('unified').Plugin<[Options?], Root>}
  179. */
  180. const rehypePrismGenerator = (refractor) => {
  181. return (options = {}) => {
  182. return (tree) => {
  183. visit(tree, 'element', visitor)
  184. }
  185. /**
  186. * @param {Element} node
  187. * @param {number} index
  188. * @param {Element} parent
  189. */
  190. function visitor(node, index, parent) {
  191. if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
  192. return
  193. }
  194. let meta = node.data && node.data.meta ? /** @type {string} */ (node.data.meta) : ''
  195. // Coerce className to array
  196. if (node.properties.className) {
  197. if (typeof node.properties.className === 'boolean') {
  198. node.properties.className = []
  199. } else if (!Array.isArray(node.properties.className)) {
  200. node.properties.className = [node.properties.className]
  201. }
  202. } else {
  203. node.properties.className = []
  204. }
  205. node.properties.className.push('code-highlight')
  206. const lang = getLanguage(node)
  207. /** @type {Element} */
  208. let refractorRoot
  209. let langError = false
  210. // Syntax highlight
  211. if (lang) {
  212. try {
  213. // @ts-ignore
  214. refractorRoot = refractor.highlight(toString(node), lang)
  215. // @ts-ignore className is already an array
  216. parent.properties.className = (parent.properties.className || []).concat(
  217. 'language-' + lang
  218. )
  219. } catch (err) {
  220. if (options.ignoreMissing && /Unknown language/.test(err.message)) {
  221. langError = true
  222. refractorRoot = node
  223. } else {
  224. throw err
  225. }
  226. }
  227. } else {
  228. refractorRoot = node
  229. }
  230. const nodeWithPosition = addNodePositionClosure()(refractorRoot.children)
  231. refractorRoot.children = splitTextByLine(nodeWithPosition)
  232. if (refractorRoot.children.length > 0) {
  233. refractorRoot.position = {
  234. start: { line: refractorRoot.children[0].position.start.line, column: 0 },
  235. end: {
  236. line: refractorRoot.children[refractorRoot.children.length - 1].position.end.line,
  237. column: 0,
  238. },
  239. }
  240. }
  241. const shouldHighlightLine = calculateLinesToHighlight(meta)
  242. const startingLineNumber = calculateStartingLine(meta)
  243. const codeLineArray = splitLine(toString(node))
  244. for (const [i, line] of codeLineArray.entries()) {
  245. // Code lines
  246. if (
  247. meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) ||
  248. options.showLineNumbers
  249. ) {
  250. line.properties.line = [(i + startingLineNumber).toString()]
  251. // @ts-ignore
  252. line.properties.className.push('line-number')
  253. }
  254. // Line highlight
  255. if (shouldHighlightLine(i)) {
  256. // @ts-ignore
  257. line.properties.className.push('highlight-line')
  258. }
  259. if (lang === 'diff' && toString(line).substring(0, 1) === '-') {
  260. // @ts-ignore
  261. line.properties.className.push('deleted')
  262. } else if (lang === 'diff' && toString(line).substring(0, 1) === '+') {
  263. // @ts-ignore
  264. line.properties.className.push('inserted')
  265. }
  266. // Syntax highlight
  267. const treeExtract = filter(
  268. refractorRoot,
  269. (node) => node.position.start.line <= i + 1 && node.position.end.line >= i + 1
  270. )
  271. line.children = treeExtract.children
  272. }
  273. node.children = codeLineArray
  274. }
  275. }
  276. }
  277. export default rehypePrismGenerator