Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): remove double and trailing dashes from heading ids #1711

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})
}