1
- import { isLinkExternal , normalizeRoutePath } from '@vuepress/shared'
1
+ import { inferRoutePath , isLinkExternal } from '@vuepress/shared'
2
2
import type { PluginWithOptions } from 'markdown-it'
3
3
import type Token from 'markdown-it/lib/token.mjs'
4
4
import type { MarkdownEnv } from '../../types.js'
@@ -67,7 +67,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
67
67
68
68
// if `href` attr exists, `token.attrs` is not `null`
69
69
const hrefAttr = token . attrs ! [ hrefIndex ]
70
- const hrefLink = hrefAttr [ 1 ]
70
+ const hrefLink : string = hrefAttr [ 1 ]
71
71
72
72
// get `base` and `filePathRelative` from `env`
73
73
const { base = '/' , filePathRelative = null } = env
@@ -83,7 +83,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
83
83
84
84
// check if a link is an internal link
85
85
const internalLinkMatch = hrefLink . match (
86
- / ^ ( (?: . * ) (?: \/ | \. m d | \. h t m l ) ) ( # .* ) ? $ / ,
86
+ / ^ ( [ ^ # ? ] * ? (?: \/ | \. m d | \. h t m l ) ) ( [ # ? ] .* ) ? $ / ,
87
87
)
88
88
89
89
if ( ! internalLinkMatch ) {
@@ -97,7 +97,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
97
97
98
98
// notice that the path and hash are encoded by markdown-it
99
99
const rawPath = internalLinkMatch [ 1 ]
100
- const rawHash = internalLinkMatch [ 2 ] || ''
100
+ const rawHashAndQueries = internalLinkMatch [ 2 ] || ''
101
101
102
102
// resolve relative and absolute path
103
103
const { relativePath, absolutePath } = resolvePaths (
@@ -114,16 +114,19 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
114
114
// normalize markdown file path to route path
115
115
// we are removing the `base` from absolute path because it should not be
116
116
// 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 ,
119
121
)
120
122
// replace the original href link with the normalized path
121
- hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHash } `
123
+ hrefAttr [ 1 ] = `${ normalizedPath } ${ rawHashAndQueries } `
122
124
// set `hasOpenInternalLink` to modify the ending tag
123
125
hasOpenInternalLink = true
124
126
} 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 } `
127
130
}
128
131
129
132
// extract internal links for file / page existence check
@@ -139,7 +142,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
139
142
return self . renderToken ( tokens , idx , options )
140
143
}
141
144
142
- md . renderer . rules . link_close = ( tokens , idx , options , env , self ) => {
145
+ md . renderer . rules . link_close = ( tokens , idx , options , _env , self ) => {
143
146
// convert ending tag of internal link
144
147
if ( hasOpenInternalLink ) {
145
148
hasOpenInternalLink = false
0 commit comments