From 118b06ae58a70a2b954fa44146bc274ba54e815d Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Thu, 24 Feb 2022 14:18:42 +0330 Subject: [PATCH] fix(markdown-parser): do not add `d-heading-*` class to content body (#40) --- src/runtime/markdown-parser/content.ts | 38 ++++++++++ src/runtime/markdown-parser/index.ts | 7 +- src/runtime/markdown-parser/meta.ts | 74 ------------------- test/unit/__snapshots__/basic.test.ts.snap | 1 - .../__snapshots__/transformer.test.ts.snap | 1 - 5 files changed, 41 insertions(+), 80 deletions(-) delete mode 100644 src/runtime/markdown-parser/meta.ts diff --git a/src/runtime/markdown-parser/content.ts b/src/runtime/markdown-parser/content.ts index e0e410f96..6ad8c149c 100644 --- a/src/runtime/markdown-parser/content.ts +++ b/src/runtime/markdown-parser/content.ts @@ -7,6 +7,7 @@ import remarkMDC from './remark-mdc' import handlers from './handler' import compiler from './compiler' import { flattenNodeText } from './utils/ast' +import { nodeTextContent } from './utils/node' const usePlugins = (plugins: any[], stream: Processor) => plugins.reduce((stream, plugin) => stream.use(plugin[0] || plugin, plugin[1] || undefined), stream) @@ -60,3 +61,40 @@ export function generateBody (content: string, options: MarkdownOptions & { data ) }) } + +export function contentHeading (body: MarkdownRoot) { + let title = '' + let description = '' + const children = body.children + // top level `text` can be ignored + .filter(node => node.type !== 'text') + + if (children.length && children[0].tag === 'h1') { + /** + * Remove node + */ + const node = children.shift()! + + /** + * Generate title + */ + title = nodeTextContent(node) + } + + if (children.length && children[0].tag === 'p') { + /** + * Remove node + */ + const node = children.shift()! + + /** + * Generate description + */ + description = nodeTextContent(node) + } + + return { + title, + description + } +} diff --git a/src/runtime/markdown-parser/index.ts b/src/runtime/markdown-parser/index.ts index 704a09b85..de4332312 100644 --- a/src/runtime/markdown-parser/index.ts +++ b/src/runtime/markdown-parser/index.ts @@ -8,10 +8,9 @@ import rehypeSortAttributeValues from 'rehype-sort-attribute-values' import rehypeSortAttributes from 'rehype-sort-attributes' import rehypeRaw from 'rehype-raw' import { MarkdownOptions, Toc } from '../types' -import { processHeading } from './meta' import { parseFrontMatter } from './frontmatter' import { generateToc } from './toc' -import { generateBody } from './content' +import { contentHeading, generateBody } from './content' export const useDefaultOptions = (): MarkdownOptions => ({ mdc: true, @@ -57,9 +56,9 @@ export async function parse (file: string, userOptions: Partial } /** - * Process content headeings + * Process content headings */ - const heading = processHeading(body) + const heading = contentHeading(body) return { body: { diff --git a/src/runtime/markdown-parser/meta.ts b/src/runtime/markdown-parser/meta.ts deleted file mode 100644 index c8bd71837..000000000 --- a/src/runtime/markdown-parser/meta.ts +++ /dev/null @@ -1,74 +0,0 @@ -import defu from 'defu' -import { MarkdownRoot } from '../types' -import { flatUnwrap, nodeTextContent } from './utils/node' - -export function processHeading (body: MarkdownRoot) { - let title = '' - let description = '' - const children = body.children - // top level `text` can be ignored - .filter(node => node.type !== 'text') - - if (children.length && children[0].tag === 'h1') { - /** - * Remove node - */ - const node = children.shift()! - - /** - * Remove anchor link from H1 tag - */ - node.children = flatUnwrap(node.children, ['a']) - - /** - * Generate title - */ - title = nodeTextContent(node) - - /** - * Inject class - */ - node.props = defu(node.props || {}, { - class: 'd-heading-title' - }) - } - - if (children.length && children[0].tag === 'p') { - /** - * Remove node - */ - const node = children.shift()! - - /** - * Generate description - */ - description = nodeTextContent(node) - - /** - * Inject class - */ - node.props = defu(node.props || {}, { - class: 'd-heading-description' - }) - } - - if (children.length && children[0].tag === 'hr') { - /** - * Remove node - */ - const node = children.shift()! - - /** - * Inject class - */ - node.props = defu(node.props || {}, { - class: 'd-heading-hr' - }) - } - - return { - title, - description, - body - } -} diff --git a/test/unit/__snapshots__/basic.test.ts.snap b/test/unit/__snapshots__/basic.test.ts.snap index 92fa6e179..7d1f58537 100644 --- a/test/unit/__snapshots__/basic.test.ts.snap +++ b/test/unit/__snapshots__/basic.test.ts.snap @@ -69,7 +69,6 @@ exports[`Basic tests > Get contents index > basic-index-body 1`] = ` }, ], "props": { - "class": "d-heading-title", "id": "index", }, "tag": "h1", diff --git a/test/unit/transformer/__snapshots__/transformer.test.ts.snap b/test/unit/transformer/__snapshots__/transformer.test.ts.snap index 56e8e96ca..2a8fff61b 100644 --- a/test/unit/transformer/__snapshots__/transformer.test.ts.snap +++ b/test/unit/transformer/__snapshots__/transformer.test.ts.snap @@ -14,7 +14,6 @@ exports[`Transformer > Parse > transformer-index-parsed 1`] = ` ], "fmAttributes": undefined, "props": { - "class": "d-heading-title", "id": "index", }, "tag": "h1",