Skip to content

Commit f8a4a9b

Browse files
committed
fix: 🐛 support async rehype plugin
1 parent f739531 commit f8a4a9b

File tree

6 files changed

+5101
-4960
lines changed

6 files changed

+5101
-4960
lines changed

.changeset/short-keys-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vue-markdown-next': patch
3+
---
4+
5+
fix: support async rehype plugin

packages/docs/example/components.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<template>
2-
<Markdown :content="testContent" :components="components">
2+
<Markdown
3+
:content="testContent"
4+
:components="components"
5+
:rehype-plugins="[[rehypeMermaid, { strategy: 'img-png' }]]"
6+
>
37
<template #hr>
48
<t-divider align="left">Using TDesign Divider</t-divider>
59
</template>
@@ -15,6 +19,7 @@
1519
<script setup>
1620
import { Markdown, VNodeRenderer } from 'vue-markdown-next';
1721
import { Divider as TDivider } from 'tdesign-vue-next';
22+
import rehypeMermaid from 'rehype-mermaid';
1823
import { components } from './components';
1924
2025
const testContent = `
@@ -25,6 +30,15 @@ This is [an example](http://example.com/ "Title") inline link.
2530
2631
<http://example.com/>
2732
33+
\`\`\`mermaid
34+
graph TD;
35+
A-->B;
36+
A-->C;
37+
B-->D;
38+
C-->D;
39+
\`\`\`
40+
41+
2842
- Red
2943
- Green
3044
- Blue

packages/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@vitejs/plugin-vue-jsx": "^4.1.1",
1616
"lodash-es": "^4.17.21",
1717
"postcss": "^8.4.49",
18+
"rehype-mermaid": "^3.0.0",
1819
"remark-gfm": "^4.0.0",
1920
"tdesign-vue-next": "^1.10.6",
2021
"unplugin-auto-import": "^0.19.0",

packages/markdown/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@types/mdast": "^4.0.4",
2929
"@vitejs/plugin-vue": "^5.2.1",
3030
"@vitejs/plugin-vue-jsx": "^4.1.1",
31+
"@vueuse/core": "^12.7.0",
3132
"hast-util-to-jsx-runtime": "^2.3.2",
3233
"html-url-attributes": "^3.0.1",
3334
"remark-parse": "^11.0.0",

packages/markdown/src/markdown.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { VFile } from 'vfile';
99
import { computed, defineComponent, PropType, shallowRef, watch, h } from 'vue';
1010
import { Fragment, JSX } from 'vue/jsx-runtime';
1111
import { Root as HastRoot, Element, Parents } from 'hast';
12+
import { computedAsync } from '@vueuse/core';
1213
import { defaultUrlTransform } from './utils';
1314

1415
// 替换 vue3 中的实现,vue3 实现后续参数中不存在 children,渲染函数无法处理 children 的渲染
@@ -123,7 +124,7 @@ export default defineComponent({
123124
},
124125
);
125126

126-
const renderTree = computed(() => {
127+
const renderTree = computedAsync(async () => {
127128
const file = new VFile();
128129

129130
// 检测类型
@@ -139,7 +140,7 @@ export default defineComponent({
139140
}
140141

141142
const mdAstTree = processor.value.parse(file);
142-
let hastTree = processor.value.runSync(mdAstTree, file);
143+
let hastTree = await processor.value.run(mdAstTree, file);
143144

144145
if (props.className) {
145146
hastTree = {
@@ -226,17 +227,21 @@ export default defineComponent({
226227

227228
// 返回渲染函数结果
228229
return () =>
229-
toJsxRuntime(renderTree.value, {
230-
Fragment,
231-
components: {
232-
...props.components,
233-
...componentsWithSlot.value,
234-
},
235-
ignoreInvalidStyle: true,
236-
jsx: jsxWithChildren,
237-
jsxs: jsxWithChildren,
238-
passKeys: true,
239-
passNode: true,
240-
});
230+
renderTree.value ? (
231+
toJsxRuntime(renderTree.value, {
232+
Fragment,
233+
components: {
234+
...props.components,
235+
...componentsWithSlot.value,
236+
},
237+
ignoreInvalidStyle: true,
238+
jsx: jsxWithChildren,
239+
jsxs: jsxWithChildren,
240+
passKeys: true,
241+
passNode: true,
242+
})
243+
) : (
244+
<></>
245+
);
241246
},
242247
});

0 commit comments

Comments
 (0)