Files
blog-frontend-v2/src/components/MarkdocRenderer.tsx
passthem 5e53a213e6
All checks were successful
continuous-integration/drone/push Build is passing
补充 Artalk 和博客文章页
2026-04-05 20:34:57 +08:00

36 lines
961 B
TypeScript

import React, { useEffect, useState } from 'react'
import Markdoc, { type RenderableTreeNode } from '@markdoc/markdoc'
import { toMarkdocTree } from '../lib/markdoc'
import { CodeBlock } from './markdoc/CodeBlock.tsx'
export const MarkdocContent: React.FC<{
content: string
}> = ({ content }) => {
/* 注意一下这个元件是动态的 */
const [tree, setTree] = useState<any>(null)
useEffect(() => {
let active = true
toMarkdocTree(content).then((result) => {
if (active) setTree(result)
})
return () => {
active = false
}
}, [content])
if (!tree) return <div className="loading">... (̀́)و</div>
return <MarkdocTreeRender tree={tree} />
}
export const MarkdocTreeRender: React.FC<{
tree: RenderableTreeNode
}> = ({ tree }) => {
return (
<div className="markdoc-container prose">
{Markdoc.renderers.react(tree, React, { components: { CodeBlock } })}
</div>
)
}