Skip to content

Commit

Permalink
fix: use relative .md link (#1556)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahad Birang <farnabaz@gmail.com>
  • Loading branch information
kecrily and farnabaz committed Oct 5, 2022
1 parent f9f161b commit cb7679e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/runtime/markdown-parser/handler/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { H } from 'mdast-util-to-hast'
import { all } from 'mdast-util-to-hast'
import { encode } from 'mdurl'
import type { MdastNode } from 'mdast-util-to-hast/lib'
import { isRelative } from 'ufo'

type Node = MdastNode & {
title: string
Expand All @@ -16,7 +17,7 @@ type Node = MdastNode & {
export default function link (h: H, node: Node) {
const props: any = {
...((node.attributes || {}) as object),
href: encode(node.url)
href: encode(normalizeLink(node.url))
}

if (node.title !== null && node.title !== undefined) {
Expand All @@ -25,3 +26,14 @@ export default function link (h: H, node: Node) {

return h(node, 'a', props, all(h, node))
}

function normalizeLink (link: string) {
if (isRelative(link) || (!/^https?/.test(link) && !link.startsWith('/'))) {
return link.split('/')
.map(x => x.replace(/^[0-9]*\./g, ''))
.join('/')
.replace(/\.md$/g, '')
} else {
return link
}
}

0 comments on commit cb7679e

Please sign in to comment.