1- import { isLinkExternal , normalizeRoutePath } from '@vuepress/shared'
1+ import { inferRoutePath , isLinkExternal } from '@vuepress/shared'
22import type { PluginWithOptions } from 'markdown-it'
33import type Token from 'markdown-it/lib/token.mjs'
44import type { MarkdownEnv } from '../../types.js'
@@ -67,7 +67,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
6767
6868 // if `href` attr exists, `token.attrs` is not `null`
6969 const hrefAttr = token . attrs ! [ hrefIndex ]
70- const hrefLink = hrefAttr [ 1 ]
70+ const hrefLink : string = hrefAttr [ 1 ]
7171
7272 // get `base` and `filePathRelative` from `env`
7373 const { base = '/' , filePathRelative = null } = env
@@ -83,7 +83,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
8383
8484 // check if a link is an internal link
8585 const internalLinkMatch = hrefLink . match (
86- / ^ ( (?: . * ) (?: \/ | \. m d | \. h t m l ) ) ( # .* ) ? $ / ,
86+ / ^ ( [ ^ # ? ] * ? (?: \/ | \. m d | \. h t m l ) ) ( [ # ? ] .* ) ? $ / ,
8787 )
8888
8989 if ( ! internalLinkMatch ) {
@@ -97,7 +97,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
9797
9898 // notice that the path and hash are encoded by markdown-it
9999 const rawPath = internalLinkMatch [ 1 ]
100- const rawHash = internalLinkMatch [ 2 ] || ''
100+ const rawHashAndQueries = internalLinkMatch [ 2 ] || ''
101101
102102 // resolve relative and absolute path
103103 const { relativePath, absolutePath } = resolvePaths (
@@ -114,16 +114,19 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
114114 // normalize markdown file path to route path
115115 // we are removing the `base` from absolute path because it should not be
116116 // passed to `<RouteLink>` or `<RouterLink>`
117- const normalizedPath = normalizeRoutePath (
118- absolutePath . replace ( new RegExp ( `^${ base } ` ) , '/' ) ,
117+ const normalizedPath = inferRoutePath (
118+ absolutePath
119+ ? absolutePath . replace ( new RegExp ( `^${ base } ` ) , '/' )
120+ : relativePath ,
119121 )
120122 // replace the original href link with the normalized path
121- hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHash } `
123+ hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHashAndQueries } `
122124 // set `hasOpenInternalLink` to modify the ending tag
123125 hasOpenInternalLink = true
124126 } else {
125- const normalizedPath = normalizeRoutePath ( absolutePath )
126- hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHash } `
127+ const normalizedPath = inferRoutePath ( absolutePath ?? relativePath )
128+ // replace the original href link with the normalized path
129+ hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHashAndQueries } `
127130 }
128131
129132 // extract internal links for file / page existence check
@@ -139,7 +142,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
139142 return self . renderToken ( tokens , idx , options )
140143 }
141144
142- md . renderer . rules . link_close = ( tokens , idx , options , env , self ) => {
145+ md . renderer . rules . link_close = ( tokens , idx , options , _env , self ) => {
143146 // convert ending tag of internal link
144147 if ( hasOpenInternalLink ) {
145148 hasOpenInternalLink = false
0 commit comments