Skip to content

Commit

Permalink
fix(mdc-parser): minor fixes in markdown generation
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Jun 21, 2022
1 parent 0ee4420 commit caf9b83
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/runtime/markdown-parser/remark-mdc/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export function stringifyFrontMatter (data: any, content: string = '') {
safe: true
})

if (!Object.keys(data).length) {
return ''
}

return [
FRONTMATTER_DELIMITER,
yaml.dump(data, { lineWidth: -1 }),
yaml.dump(data, { lineWidth: -1 }).trim(),
FRONTMATTER_DELIMITER,
content
].join('\n')
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/markdown-parser/remark-mdc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default <Plugin<Array<RemarkMDCOptions>, Root, Root>> function ({ compone
return async (tree: ComponentNode, { data }: { data: Record<string, any> }) => {
const jobs: Promise<unknown>[] = []
visit<ComponentNode, string[]>(tree, ['textComponent', 'leafComponent', 'containerComponent'], (node) => {
bindNode(node, data)
bindNode(node)
const { instance: handler, options } = components.find(c => c.name === node.name) || {}
if (handler) {
jobs.push(handler(options)(node, data))
Expand Down
12 changes: 10 additions & 2 deletions src/runtime/markdown-parser/remark-mdc/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ function componentContainerSection (node: NodeComponentContainerSection, _: any,

type NodeTextComponent = Parent & { name: string; rawData: string }
function textComponent (node: NodeTextComponent, _: any, context: any) {
let value
context.indexStack = context.stack

const exit = context.enter(node.type)
let value = ':' + (node.name || '') + label(node, context) + attributes(node, context)
value += '\n' + content(node, context)

if (node.name === 'span') {
// Handle span suger syntax
value = `[${content(node, context)}]${attributes(node, context)}`
} else {
value = ':' + (node.name || '') + label(node, context) + attributes(node, context)
}

exit()
return value
}
Expand Down

0 comments on commit caf9b83

Please sign in to comment.