Skip to content

Commit

Permalink
fix(markdown): remove double and trailing dashes from heading ids (#1711
Browse files Browse the repository at this point in the history
)
  • Loading branch information
farnabaz committed Nov 30, 2022
1 parent ef92b20 commit 67e1f53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/runtime/markdown-parser/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export default function (this: any, _options: MarkdownOptions) {
if (Array.isArray(node)) {
return node.map(parseAsJSON).filter(Boolean)
}

// Remove double dashes and trailing dash from heading ids
if (node.tagName?.startsWith('h') && node.properties.id) {
node.properties.id = node.properties.id.replace(/-+/g, '-').replace(/-$/, '')
}

/**
* Element node creates an isolated children array to
* allow nested elements
Expand Down
17 changes: 17 additions & 0 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,22 @@ export const testMarkdownParser = () => {
expect(nodes.shift().props.href).toEqual('../../foo')
expect(nodes.shift().props.href).toEqual('../../_foo')
})

test('No trailing dashes in heading ids', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'# `<Alert />` ',
'## `<Alert />` -',
'### `<Alert />` \\#',
'### `<Alert />`.'
].join('\n')
}
})
expect(parsed.body.children[0].props.id).toEqual('alert')
expect(parsed.body.children[1].props.id).toEqual('alert')
})
})
}

0 comments on commit 67e1f53

Please sign in to comment.