Skip to content

Commit

Permalink
fix(markdown): remove extra dash from heading id
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Dec 2, 2022
1 parent 005741a commit 4c37658
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/runtime/markdown-parser/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^-/, '')
}

/**
Expand Down
31 changes: 17 additions & 14 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
'# `<Alert />` ',
'## `<Alert />` -',
'### `<Alert />` \\#',
'### `<Alert />`.'
].join('\n')
}
})
expect(parsed.body.children[0].props.id).toEqual('alert')
expect(parsed.body.children[1].props.id).toEqual('alert')
const headings = [
'# `<Alert />` ',
'## `<Alert />` -',
'### `<Alert />` \\#',
'### `<Alert />`.',
'### 🎨 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')
}
})
})
}

0 comments on commit 4c37658

Please sign in to comment.