generator.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * Create container AST for node lines
  69. *
  70. * @param {number} number
  71. * @return {Element[]}
  72. */
  73. const createLineNodes = (number) => {
  74. const a = new Array(number)
  75. for (let i = 0; i < number; i++) {
  76. a[i] = {
  77. type: 'element',
  78. tagName: 'span',
  79. properties: { className: [] },
  80. children: [],
  81. }
  82. }
  83. return a
  84. }
  85. /**
  86. * Split multiline text nodes into individual nodes with positioning
  87. * Add a node start and end line position information for each text node
  88. *
  89. * @return { (ast:Element['children']) => Element['children'] }
  90. *
  91. */
  92. const addNodePositionClosure = () => {
  93. let startLineNum = 1
  94. /**
  95. * @param {Element['children']} ast
  96. * @return {Element['children']}
  97. */
  98. const addNodePosition = (ast) => {
  99. return ast.reduce((result, node) => {
  100. if (node.type === 'text') {
  101. const value = /** @type {string} */ (node.value)
  102. const numLines = (value.match(/\n/g) || '').length
  103. if (numLines === 0) {
  104. node.position = {
  105. // column: 1 is needed to avoid error with @next/mdx
  106. // https://github.com/timlrx/rehype-prism-plus/issues/44
  107. start: { line: startLineNum, column: 1 },
  108. end: { line: startLineNum, column: 1 },
  109. }
  110. result.push(node)
  111. } else {
  112. const lines = value.split('\n')
  113. for (const [i, line] of lines.entries()) {
  114. result.push({
  115. type: 'text',
  116. value: i === lines.length - 1 ? line : line + '\n',
  117. position: {
  118. start: { line: startLineNum + i, column: 1 },
  119. end: { line: startLineNum + i, column: 1 },
  120. },
  121. })
  122. }
  123. }
  124. startLineNum = startLineNum + numLines
  125. return result
  126. }
  127. if (Object.prototype.hasOwnProperty.call(node, 'children')) {
  128. const initialLineNum = startLineNum
  129. // @ts-ignore
  130. node.children = addNodePosition(node.children, startLineNum)
  131. result.push(node)
  132. node.position = {
  133. start: { line: initialLineNum, column: 1 },
  134. end: { line: startLineNum, column: 1 },
  135. }
  136. return result
  137. }
  138. result.push(node)
  139. return result
  140. }, [])
  141. }
  142. return addNodePosition
  143. }
  144. /**
  145. * Rehype prism plugin generator that highlights code blocks with refractor (prismjs)
  146. *
  147. * Pass in your own refractor object with the required languages registered:
  148. * https://github.com/wooorm/refractor#refractorregistersyntax
  149. *
  150. * @param {import('refractor/lib/core').Refractor} refractor
  151. * @return {import('unified').Plugin<[Options?], Root>}
  152. */
  153. const rehypePrismGenerator = (refractor) => {
  154. return (options = {}) => {
  155. return (tree) => {
  156. visit(tree, 'element', visitor)
  157. }
  158. /**
  159. * @param {Element} node
  160. * @param {number} index
  161. * @param {Element} parent
  162. */
  163. function visitor(node, index, parent) {
  164. if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
  165. return
  166. }
  167. let meta = /** @type {string} */ (node?.data?.meta || node?.properties?.metastring || '')
  168. // Coerce className to array
  169. if (node.properties.className) {
  170. if (typeof node.properties.className === 'boolean') {
  171. node.properties.className = []
  172. } else if (!Array.isArray(node.properties.className)) {
  173. node.properties.className = [node.properties.className]
  174. }
  175. } else {
  176. node.properties.className = []
  177. }
  178. node.properties.className.push('code-highlight')
  179. const lang = getLanguage(node)
  180. /** @type {Element} */
  181. let refractorRoot
  182. // Syntax highlight
  183. if (lang) {
  184. try {
  185. let rootLang
  186. if (lang?.includes('diff-')){
  187. rootLang=lang.split('-')[1]
  188. } else{
  189. rootLang=lang
  190. }
  191. // @ts-ignore
  192. refractorRoot = refractor.highlight(toString(node), rootLang)
  193. // @ts-ignore className is already an array
  194. parent.properties.className = (parent.properties.className || []).concat(
  195. 'language-' + rootLang
  196. )
  197. } catch (err) {
  198. if (options.ignoreMissing && /Unknown language/.test(err.message)) {
  199. refractorRoot = node
  200. } else {
  201. throw err
  202. }
  203. }
  204. } else {
  205. refractorRoot = node
  206. }
  207. refractorRoot.children = addNodePositionClosure()(refractorRoot.children)
  208. // Add position info to root
  209. if (refractorRoot.children.length > 0) {
  210. refractorRoot.position = {
  211. start: { line: refractorRoot.children[0].position.start.line, column: 0 },
  212. end: {
  213. line: refractorRoot.children[refractorRoot.children.length - 1].position.end.line,
  214. column: 0,
  215. },
  216. }
  217. } else {
  218. refractorRoot.position = {
  219. start: { line: 0, column: 0 },
  220. end: { line: 0, column: 0 },
  221. }
  222. }
  223. const shouldHighlightLine = calculateLinesToHighlight(meta)
  224. const startingLineNumber = calculateStartingLine(meta)
  225. const codeLineArray = createLineNodes(refractorRoot.position.end.line)
  226. const falseShowLineNumbersStr = [
  227. 'showlinenumbers=false',
  228. 'showlinenumbers="false"',
  229. 'showlinenumbers={false}',
  230. ]
  231. for (const [i, line] of codeLineArray.entries()) {
  232. // Default class name for each line
  233. line.properties.className = ['code-line']
  234. // Syntax highlight
  235. const treeExtract = filter(
  236. refractorRoot,
  237. (node) => node.position.start.line <= i + 1 && node.position.end.line >= i + 1
  238. )
  239. line.children = treeExtract.children
  240. // Line number
  241. if (
  242. (meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) ||
  243. options.showLineNumbers) &&
  244. !falseShowLineNumbersStr.some((str) => meta.toLowerCase().includes(str))
  245. ) {
  246. line.properties.line = [(i + startingLineNumber).toString()]
  247. line.properties.className.push('line-number')
  248. }
  249. // Line highlight
  250. if (shouldHighlightLine(i)) {
  251. line.properties.className.push('highlight-line')
  252. }
  253. // Diff classes
  254. if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '-') {
  255. line.properties.className.push('deleted')
  256. } else if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '+') {
  257. line.properties.className.push('inserted')
  258. }
  259. }
  260. // Remove possible trailing line when splitting by \n which results in empty array
  261. if (
  262. codeLineArray.length > 0 &&
  263. toString(codeLineArray[codeLineArray.length - 1]).trim() === ''
  264. ) {
  265. codeLineArray.pop()
  266. }
  267. node.children = codeLineArray
  268. }
  269. }
  270. }
  271. export default rehypePrismGenerator