Skip to content

Commit 4c37658

Browse files
committed
fix(markdown): remove extra dash from heading id
1 parent 005741a commit 4c37658

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/runtime/markdown-parser/compiler.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export default function (this: any, _options: MarkdownOptions) {
2323

2424
// Remove double dashes and trailing dash from heading ids
2525
if (node.tagName?.startsWith('h') && node.properties.id) {
26-
node.properties.id = node.properties.id.replace(/-+/g, '-').replace(/-$/, '')
26+
node.properties.id = node.properties.id
27+
.replace(/-+/g, '-')
28+
.replace(/-$/, '')
29+
.replace(/^-/, '')
2730
}
2831

2932
/**

test/features/parser-markdown.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,23 @@ export const testMarkdownParser = () => {
181181
})
182182

183183
test('No trailing dashes in heading ids', async () => {
184-
const parsed = await $fetch('/api/parse', {
185-
method: 'POST',
186-
body: {
187-
id: 'content:index.md',
188-
content: [
189-
'# `<Alert />` ',
190-
'## `<Alert />` -',
191-
'### `<Alert />` \\#',
192-
'### `<Alert />`.'
193-
].join('\n')
194-
}
195-
})
196-
expect(parsed.body.children[0].props.id).toEqual('alert')
197-
expect(parsed.body.children[1].props.id).toEqual('alert')
184+
const headings = [
185+
'# `<Alert />` ',
186+
'## `<Alert />` -',
187+
'### `<Alert />` \\#',
188+
'### `<Alert />`.',
189+
'### 🎨 Alert'
190+
]
191+
for (const heading of headings) {
192+
const parsed = await $fetch('/api/parse', {
193+
method: 'POST',
194+
body: {
195+
id: 'content:index.md',
196+
content: heading
197+
}
198+
})
199+
expect(parsed.body.children[0].props.id).toEqual('alert')
200+
}
198201
})
199202
})
200203
}

0 commit comments

Comments
 (0)