perf: 优化markdown输出
This commit is contained in:
@@ -1,40 +0,0 @@
|
|||||||
<script lang="ts" setup>
|
|
||||||
import type { MarkdownProps } from '../XMarkdownCore/shared/types';
|
|
||||||
import { useShiki } from '@components/XMarkdownCore/hooks/useShiki';
|
|
||||||
import { MarkdownRendererAsync } from '../XMarkdownCore';
|
|
||||||
import { useMarkdownContext } from '../XMarkdownCore/components/MarkdownProvider';
|
|
||||||
import { DEFAULT_PROPS } from '../XMarkdownCore/shared/constants';
|
|
||||||
|
|
||||||
const props = withDefaults(defineProps<MarkdownProps>(), DEFAULT_PROPS);
|
|
||||||
|
|
||||||
const slots = useSlots();
|
|
||||||
const customComponents = useMarkdownContext();
|
|
||||||
const colorReplacementsComputed = computed(() => {
|
|
||||||
return props.colorReplacements;
|
|
||||||
});
|
|
||||||
const needViewCodeBtnComputed = computed(() => {
|
|
||||||
return props.needViewCodeBtn;
|
|
||||||
});
|
|
||||||
useShiki();
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="elx-xmarkdown-container">
|
|
||||||
<MarkdownRendererAsync
|
|
||||||
v-bind="props"
|
|
||||||
:color-replacements="colorReplacementsComputed"
|
|
||||||
:need-view-code-btn="needViewCodeBtnComputed"
|
|
||||||
>
|
|
||||||
<template
|
|
||||||
v-for="(slot, name) in customComponents"
|
|
||||||
:key="name"
|
|
||||||
#[name]="slotProps"
|
|
||||||
>
|
|
||||||
<component :is="slot" v-bind="slotProps" />
|
|
||||||
</template>
|
|
||||||
<template v-for="(_, name) in slots" :key="name" #[name]="slotProps">
|
|
||||||
<slot :name="name" v-bind="slotProps" />
|
|
||||||
</template>
|
|
||||||
</MarkdownRendererAsync>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
@@ -11,7 +11,8 @@ import {
|
|||||||
transformerNotationHighlight,
|
transformerNotationHighlight,
|
||||||
transformerNotationWordHighlight
|
transformerNotationWordHighlight
|
||||||
} from '@shikijs/transformers';
|
} from '@shikijs/transformers';
|
||||||
import { computed, h, reactive, ref, toValue, watch } from 'vue';
|
import { computed, h, reactive, ref, toValue, watch, onUnmounted } from 'vue';
|
||||||
|
import { debounce } from 'lodash-es';
|
||||||
import HighLightCode from '../../components/HighLightCode/index.vue';
|
import HighLightCode from '../../components/HighLightCode/index.vue';
|
||||||
import { SHIKI_SUPPORT_LANGS, shikiThemeDefault } from '../../shared';
|
import { SHIKI_SUPPORT_LANGS, shikiThemeDefault } from '../../shared';
|
||||||
import { useMarkdownContext } from '../MarkdownProvider';
|
import { useMarkdownContext } from '../MarkdownProvider';
|
||||||
@@ -88,16 +89,29 @@ async function generateHtml() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用防抖优化代码块渲染,避免SSE流式更新时频繁高亮
|
||||||
|
const debouncedGenerateHtml = debounce(generateHtml, 100, {
|
||||||
|
leading: false,
|
||||||
|
trailing: true,
|
||||||
|
maxWait: 200
|
||||||
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => props.raw?.content,
|
() => props.raw?.content,
|
||||||
async content => {
|
async content => {
|
||||||
if (content) {
|
if (content) {
|
||||||
await generateHtml();
|
debouncedGenerateHtml();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 清理防抖定时器
|
||||||
|
onUnmounted(() => {
|
||||||
|
debouncedGenerateHtml.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
const runCodeOptions = reactive<ElxRunCodeProps>({
|
const runCodeOptions = reactive<ElxRunCodeProps>({
|
||||||
code: [],
|
code: [],
|
||||||
content: '',
|
content: '',
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import type { PluggableList } from 'unified';
|
|||||||
import type { PropType } from 'vue';
|
import type { PropType } from 'vue';
|
||||||
|
|
||||||
import type { CustomAttrs, SanitizeOptions, TVueMarkdown } from './types';
|
import type { CustomAttrs, SanitizeOptions, TVueMarkdown } from './types';
|
||||||
import { defineComponent, shallowRef, toRefs, watch } from 'vue';
|
import { defineComponent, shallowRef, toRefs, watch, onUnmounted } from 'vue';
|
||||||
// import { useMarkdownContext } from '../components/MarkdownProvider';
|
// import { useMarkdownContext } from '../components/MarkdownProvider';
|
||||||
import { render } from './hast-to-vnode';
|
import { render } from './hast-to-vnode';
|
||||||
import { useMarkdownProcessor } from './useProcessor';
|
import { useMarkdownProcessor } from './useProcessor';
|
||||||
|
import { debounce } from 'lodash-es';
|
||||||
|
|
||||||
export type { CustomAttrs, SanitizeOptions, TVueMarkdown };
|
export type { CustomAttrs, SanitizeOptions, TVueMarkdown };
|
||||||
|
|
||||||
@@ -93,12 +94,42 @@ const vueMarkdownAsyncImpl = defineComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const hast = shallowRef<Root | null>(null);
|
const hast = shallowRef<Root | null>(null);
|
||||||
|
const isProcessing = shallowRef(false);
|
||||||
|
|
||||||
const process = async (): Promise<void> => {
|
const process = async (): Promise<void> => {
|
||||||
const mdast = processor.value.parse(markdown.value);
|
// 避免重复处理
|
||||||
hast.value = (await processor.value.run(mdast)) as Root;
|
if (isProcessing.value) return;
|
||||||
|
isProcessing.value = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const mdast = processor.value.parse(markdown.value);
|
||||||
|
hast.value = (await processor.value.run(mdast)) as Root;
|
||||||
|
} finally {
|
||||||
|
isProcessing.value = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(() => [markdown.value, processor.value], process, { flush: 'sync' });
|
// 使用防抖优化SSE流式更新场景
|
||||||
|
// trailing: true 确保最后一次更新会执行
|
||||||
|
// leading: false 避免首次触发两次渲染
|
||||||
|
const debouncedProcess = debounce(process, 50, {
|
||||||
|
leading: false,
|
||||||
|
trailing: true,
|
||||||
|
maxWait: 200 // 最长等待200ms,避免延迟过大
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => [markdown.value, processor.value],
|
||||||
|
() => {
|
||||||
|
debouncedProcess();
|
||||||
|
},
|
||||||
|
{ flush: 'post' } // 改为post,在DOM更新后执行,避免阻塞UI
|
||||||
|
);
|
||||||
|
|
||||||
|
// 清理防抖定时器
|
||||||
|
onUnmounted(() => {
|
||||||
|
debouncedProcess.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
await process();
|
await process();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user