import Markdoc, { type Config } from '@markdoc/markdoc' import { ensureShikiEngine, shikiRender } from './markdown' const { nodes, Tag } = Markdoc export const markdocConfig: Config = { nodes: { fence: { render: 'CodeBlock', attributes: { ...nodes.fence.attributes, highlightedHtml: { type: String }, }, transform(node, config) { /* * 注意力!!!!!!!!!!!!!! * 注意!!!!!!!!!!!! * * 如果改了这里的逻辑,记得更改 `markdoc-cache.ts` 里的版本号 * 以让对应的缓存能够被更新 * **/ const attributes = node.transformAttributes(config) const highlightedHtml = shikiRender( node.attributes.content, node.attributes.language, ) return new Tag( 'CodeBlock', { ...attributes, highlightedHtml }, node.transformChildren(config), ) }, }, }, } export const renderMarkdocTree = async (content: string) => { const ast = Markdoc.parse(content) await ensureShikiEngine() const tree = Markdoc.transform(ast, markdocConfig) return tree }