Skip to content

Commit

Permalink
fix(link): remove hash before checking if ending by '.md' (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skraye committed Apr 28, 2023
1 parent 79d5181 commit c65a2b4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/runtime/markdown-parser/handler/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export default function link (h: H, node: Node) {
}

function normalizeLink (link: string) {
if (link.endsWith('.md') && (isRelative(link) || (!/^https?/.test(link) && !link.startsWith('/')))) {
return generatePath(link.replace(/\.md$/, ''), { forceLeadingSlash: false })
const match = link.match(/#.+$/)
const hash = match ? match[0] : ''
if (link.replace(/#.+$/, '').endsWith('.md') && (isRelative(link) || (!/^https?/.test(link) && !link.startsWith('/')))) {
return (generatePath(link.replace('.md' + hash, ''), { forceLeadingSlash: false }) + hash)
} else {
return link
}
Expand Down
6 changes: 4 additions & 2 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ export const testMarkdownParser = () => {
'[link1](./../01.foo.md)',
'[link1](../01.foo.md)',
'[link1](../../01.foo.md)',
'[link1](../../01.foo#bar.md)',
'[link1](../../01.foo.md#bar)',
'[link1](../../01.foo/file.md#bar)',
'[link1](../../01.foo.draft.md)',
'[link1](../../_foo.draft.md)'
].join('\n')
Expand All @@ -200,7 +201,8 @@ export const testMarkdownParser = () => {
expect(nodes.shift().props.href).toEqual('./../foo')
expect(nodes.shift().props.href).toEqual('../foo')
expect(nodes.shift().props.href).toEqual('../../foo')
expect(nodes.shift().props.href).toEqual('../../foobar')
expect(nodes.shift().props.href).toEqual('../../foo#bar')
expect(nodes.shift().props.href).toEqual('../../foo/file#bar')
expect(nodes.shift().props.href).toEqual('../../foo')
expect(nodes.shift().props.href).toEqual('../../_foo')
})
Expand Down

0 comments on commit c65a2b4

Please sign in to comment.