From d81a08a26a001698807344c1cbe080f3b8cef5d0 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 27 Jul 2023 00:08:37 +0200 Subject: [PATCH] fix: markdown render compatiblity --- src/module.ts | 5 +- src/runtime/components/ContentSlot.vue | 39 +++++---- src/runtime/markdown-parser/index.ts | 111 ------------------------- 3 files changed, 25 insertions(+), 130 deletions(-) delete mode 100644 src/runtime/markdown-parser/index.ts diff --git a/src/module.ts b/src/module.ts index 521c01c3f..6cc053f5b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -619,7 +619,10 @@ export default defineNuxtModule({ await installModule('nuxt-mdc', { remarkPlugins: contentContext.markdown.remarkPlugins, rehypePlugins: contentContext.markdown.rehypePlugins, - highlight: contentContext.highlight + highlight: contentContext.highlight, + components: { + map: contentContext.markdown.tags + } }) nuxt.options.runtimeConfig.public.content = defu(nuxt.options.runtimeConfig.public.content, { diff --git a/src/runtime/components/ContentSlot.vue b/src/runtime/components/ContentSlot.vue index 7fcdb9444..2f4d364ba 100644 --- a/src/runtime/components/ContentSlot.vue +++ b/src/runtime/components/ContentSlot.vue @@ -1,23 +1,26 @@ - + diff --git a/src/runtime/markdown-parser/index.ts b/src/runtime/markdown-parser/index.ts deleted file mode 100644 index db4491bc9..000000000 --- a/src/runtime/markdown-parser/index.ts +++ /dev/null @@ -1,111 +0,0 @@ -// eslint-disable-next-line import/no-named-as-default -import defu from 'defu' -import remarkEmoji from 'remark-emoji' -import rehypeSlug from 'rehype-slug' -import remarkSqueezeParagraphs from 'remark-squeeze-paragraphs' -import rehypeExternalLinks from 'rehype-external-links' -import remarkGfm from 'remark-gfm' -import rehypeSortAttributeValues from 'rehype-sort-attribute-values' -import rehypeSortAttributes from 'rehype-sort-attributes' -import rehypeRaw from 'rehype-raw' -import { parseFrontMatter } from 'remark-mdc' -import { MarkdownOptions, MarkdownParsedContent, Toc } from '../types' -import { generateToc } from './toc' -import { contentHeading, generateBody } from './content' - -export const useDefaultOptions = (): MarkdownOptions => ({ - mdc: true, - toc: { - depth: 2, - searchDepth: 2 - }, - tags: {}, - remarkPlugins: { - 'remark-emoji': { - instance: remarkEmoji - }, - 'remark-squeeze-paragraphs': { - instance: remarkSqueezeParagraphs - }, - 'remark-gfm': { - instance: remarkGfm - } - }, - rehypePlugins: { - 'rehype-slug': { - instance: rehypeSlug - }, - 'rehype-external-links': { - instance: rehypeExternalLinks - }, - 'rehype-sort-attribute-values': { - instance: rehypeSortAttributeValues - }, - 'rehype-sort-attributes': { - instance: rehypeSortAttributes - }, - 'rehype-raw': { - instance: rehypeRaw, - options: { - passThrough: ['element'] - } - } - } -}) - -export async function parse (file: string, userOptions: Partial = {}) { - const options = defu(userOptions, useDefaultOptions()) as MarkdownOptions - - const { content, data } = parseFrontMatter(file) - - // Compile markdown from file content to JSON - const body = await generateBody(content, { ...options, data }) - - /** - * generate toc if it is not disabled in front-matter - */ - let toc: Toc | undefined - if (data.toc !== false) { - const tocOption = defu(data.toc || {}, options.toc) - toc = generateToc(body, tocOption) - } - - const excerptString = useExcerpt(content) - const excerpt = excerptString - ? await generateBody(excerptString, { ...options, data }) - : undefined - - /** - * Process content headings - */ - const heading = contentHeading(body) - - return <{ meta: Partial, body: MarkdownParsedContent['body'] }> { - body: { - ...body, - toc - }, - meta: { - _empty: content.trim().length === 0, - title: heading.title, - description: heading.description, - excerpt, - ...data - } - } -} - -function useExcerpt (content: string, delimiter = //i) { - if (!delimiter) { - return '' - } - // if enabled, get the excerpt defined after front-matter - let idx = -1 - const match = delimiter.exec(content) - if (match) { - idx = match.index - } - if (idx !== -1) { - return content.slice(0, idx) - } -}