mdx: add remark-frontmatter to pipeline, fix rehype-pretty-code line numbers visitor
This commit is contained in:
26
lib/mdx-hast-visitor.js
Normal file
26
lib/mdx-hast-visitor.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* HAST visitor plugin that adds data-line-numbers attribute to code blocks
|
||||
* inside <pre> elements.
|
||||
*/
|
||||
import { visit } from 'unist-util-visit'
|
||||
|
||||
/**
|
||||
* Rehype plugin that adds line number indicators to code blocks.
|
||||
* @returns {import('unified').Plugin<[], import('hast').Root>}
|
||||
*/
|
||||
export default function addLineNumbers() {
|
||||
return function attacher(tree) {
|
||||
visit(tree, 'element', function visitor(node) {
|
||||
if (
|
||||
node.tagName === 'pre' &&
|
||||
node.children?.length === 1 &&
|
||||
node.children[0].type === 'element' &&
|
||||
node.children[0].tagName === 'code'
|
||||
) {
|
||||
const code = node.children[0]
|
||||
code.properties = code.properties || {}
|
||||
code.properties['data-line-numbers'] = ''
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user