diff --git a/src/runtime/markdown-parser/compiler.ts b/src/runtime/markdown-parser/compiler.ts index 8f6cc1ef2..b34068e81 100644 --- a/src/runtime/markdown-parser/compiler.ts +++ b/src/runtime/markdown-parser/compiler.ts @@ -23,7 +23,10 @@ export default function (this: any, _options: MarkdownOptions) { // 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(/-$/, '') + node.properties.id = node.properties.id + .replace(/-+/g, '-') + .replace(/-$/, '') + .replace(/^-/, '') } /** diff --git a/test/features/parser-markdown.ts b/test/features/parser-markdown.ts index bc12c7a85..00fb09009 100644 --- a/test/features/parser-markdown.ts +++ b/test/features/parser-markdown.ts @@ -181,20 +181,23 @@ export const testMarkdownParser = () => { }) test('No trailing dashes in heading ids', async () => { - const parsed = await $fetch('/api/parse', { - method: 'POST', - body: { - id: 'content:index.md', - content: [ - '# `` ', - '## `` -', - '### `` \\#', - '### ``.' - ].join('\n') - } - }) - expect(parsed.body.children[0].props.id).toEqual('alert') - expect(parsed.body.children[1].props.id).toEqual('alert') + const headings = [ + '# `` ', + '## `` -', + '### `` \\#', + '### ``.', + '### 🎨 Alert' + ] + for (const heading of headings) { + const parsed = await $fetch('/api/parse', { + method: 'POST', + body: { + id: 'content:index.md', + content: heading + } + }) + expect(parsed.body.children[0].props.id).toEqual('alert') + } }) }) }