From 5a0a11389c6d9a9b83569f7d0065ed22dfd60c34 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 6 Mar 2025 13:14:27 +0100 Subject: [PATCH] !fix(MDC): do not generate title/description and toc --- src/runtime/components/MDC.vue | 13 ++++++++++++- src/runtime/parser/index.ts | 25 +++++++++++++------------ src/types/parser.ts | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/runtime/components/MDC.vue b/src/runtime/components/MDC.vue index 6e16eec9..b8c54cd1 100644 --- a/src/runtime/components/MDC.vue +++ b/src/runtime/components/MDC.vue @@ -72,6 +72,13 @@ const props = defineProps({ cacheKey: { type: String, default: undefined + }, + /** + * Partial parsing (if partial is `true`, title and toc generation will not be generated) + */ + partial: { + type: Boolean, + default: true } }) @@ -81,7 +88,11 @@ const { data, refresh, error } = await useAsyncData(key.value, async () => { if (typeof props.value !== 'string') { return props.value } - return await parseMarkdown(props.value, props.parserOptions) + return await parseMarkdown(props.value, { + ...props.parserOptions, + toc: props.partial ? false : props.parserOptions?.toc, + contentHeading: props.partial ? false : props.parserOptions?.contentHeading + }) }) const body = computed(() => props.excerpt ? data.value?.excerpt : data.value?.body) diff --git a/src/runtime/parser/index.ts b/src/runtime/parser/index.ts index 69d0c4e2..682b3f58 100644 --- a/src/runtime/parser/index.ts +++ b/src/runtime/parser/index.ts @@ -117,28 +117,29 @@ export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {}) }) }) - const result = processedFile?.result as { body: MDCRoot, excerpt: MDCRoot | undefined } + const parsedContent = processedFile?.result as { body: MDCRoot, excerpt: MDCRoot | undefined } // Update data with processor data const data = Object.assign( - inlineOptions.contentHeading !== false ? contentHeading(result.body) : {}, + inlineOptions.contentHeading !== false ? contentHeading(parsedContent.body) : {}, frontmatter, processedFile?.data || {} ) as MDCData - // Generate toc if it is not disabled in front-matter - let toc: Toc | undefined - if (data.toc !== false) { - const tocOption = defu(data.toc || {}, inlineOptions.toc, defaults.toc) - toc = generateToc(result.body, tocOption) + const parsedResult = { data, body: parsedContent.body } as MDCParserResult + + // Generate toc if it is not disabled + const userTocOption = data.toc ?? inlineOptions.toc + if (userTocOption !== false) { + const tocOption = defu({} as Toc, userTocOption, defaults.toc) + parsedResult.toc = generateToc(parsedContent.body, tocOption) } - return { - data, - body: result.body, - excerpt: result.excerpt, - toc + if (parsedContent.excerpt) { + parsedResult.excerpt = parsedContent.excerpt } + + return parsedResult } } diff --git a/src/types/parser.ts b/src/types/parser.ts index 1a288ca4..071c626e 100644 --- a/src/types/parser.ts +++ b/src/types/parser.ts @@ -32,7 +32,7 @@ export interface MDCParseOptions { */ depth?: number searchDepth?: number - } + } | false keepComments?: boolean