Skip to content

Commit

Permalink
fix(markdown-parser): do not add d-heading-* class to content body (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Feb 24, 2022
1 parent 00ccd39 commit 118b06a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 80 deletions.
38 changes: 38 additions & 0 deletions src/runtime/markdown-parser/content.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
7 changes: 3 additions & 4 deletions src/runtime/markdown-parser/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -57,9 +56,9 @@ export async function parse (file: string, userOptions: Partial<MarkdownOptions>
}

/**
* Process content headeings
* Process content headings
*/
const heading = processHeading(body)
const heading = contentHeading(body)

return {
body: {
Expand Down
74 changes: 0 additions & 74 deletions src/runtime/markdown-parser/meta.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/__snapshots__/basic.test.ts.snap
Expand Up @@ -69,7 +69,6 @@ exports[`Basic tests > Get contents index > basic-index-body 1`] = `
},
],
"props": {
"class": "d-heading-title",
"id": "index",
},
"tag": "h1",
Expand Down
Expand Up @@ -14,7 +14,6 @@ exports[`Transformer > Parse > transformer-index-parsed 1`] = `
],
"fmAttributes": undefined,
"props": {
"class": "d-heading-title",
"id": "index",
},
"tag": "h1",
Expand Down

0 comments on commit 118b06a

Please sign in to comment.