feat(markdown): Migrate to comark for markdown rendering

This commit is contained in:
Zoe
2026-08-11 03:18:28 -05:00
parent c9e48687ef
commit 4b99921023
10 changed files with 441 additions and 337 deletions
-65
View File
@@ -1,65 +0,0 @@
import { unified } from 'unified';
import { visit } from 'unist-util-visit';
import remarkParse from 'remark-parse';
import remarkGfm from 'remark-gfm';
import remarkRehype from 'remark-rehype';
import remarkBreaks from 'remark-breaks';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
function remarkSplitBlocks() {
return (tree: any) => {
visit(tree, 'paragraph', (node, index, parent) => {
if (parent == null || index == null) {
return;
}
const breakIndex = node.children.findIndex((child: any) => child.type === 'break');
if (breakIndex === -1) {
return;
}
const beforeBreak = node.children.slice(0, breakIndex);
const afterBreak = node.children.slice(breakIndex + 1);
// Keep content before the break in the current paragraph.
node.children = beforeBreak;
// Insert content after the break as the next sibling of this
// paragraph in its actual parent (root, listItem, blockquote, etc.).
// Previously this only spliced into tree.children and dropped
// nested content when the parent wasn't a direct root child.
if (afterBreak.length > 0) {
const newParagraph = {
type: 'paragraph',
children: afterBreak,
};
parent.children.splice(index + 1, 0, newParagraph);
// Continue at the newly inserted paragraph so further
// hard breaks inside it are split as well.
return index + 1;
}
});
};
}
export default defineNuxtPlugin((nuxtApp) => {
const remark =
unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkMath)
.use(remarkBreaks)
.use(remarkSplitBlocks)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeKatex, { output: 'mathml' });
return {
provide: {
remark,
}
}
})