mdx: add remark-frontmatter to pipeline, fix rehype-pretty-code line numbers visitor

This commit is contained in:
2026-06-02 12:05:32 -05:00
parent bf34a225c0
commit 5ff65968a3
4 changed files with 43 additions and 7 deletions

26
lib/mdx-hast-visitor.js Normal file
View 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'] = ''
}
})
}
}