Skip to content

Commit 073e21d

Browse files
committedFeb 5, 2024
fix(markdown): keep base if link tag is not updated
1 parent 0c79330 commit 073e21d

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed
 

‎packages/markdown/src/plugins/linksPlugin/linksPlugin.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export interface LinksPluginOptions {
2929
/**
3030
* Process links in markdown file
3131
*
32-
* - internal links: convert them into `<VPLink>`
33-
* - external links: add extra attrs and external icon
32+
* - internal links: convert `<a>` tag into internalTag
33+
* - external links: add extra attrs
3434
*/
3535
export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
3636
md,
@@ -106,28 +106,26 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
106106
filePathRelative,
107107
)
108108

109-
// normalize markdown file path to route path
110-
//
111-
// we are removing the `base` from absolute path because it should not be
112-
// passed to `<VPLink>`
113-
//
114-
// '/foo/index.md' => '/foo/'
115-
// '/foo/bar.md' => '/foo/bar.html'
116-
const normalizedPath = normalizeRoutePath(
117-
absolutePath.replace(new RegExp(`^${base}`), '/'),
118-
)
119-
120109
if (['RouterLink', 'VPLink'].includes(internalTag)) {
121110
// convert starting tag of internal link to `internalTag`
122111
token.tag = internalTag
123112
// replace the original `href` attr with `to` attr
124113
hrefAttr[0] = 'to'
114+
// normalize markdown file path to route path
115+
// we are removing the `base` from absolute path because it should not be
116+
// passed to `<VPLink>` or `<RouterLink>`
117+
const normalizedPath = normalizeRoutePath(
118+
absolutePath.replace(new RegExp(`^${base}`), '/'),
119+
)
120+
// replace the original href link with the normalized path
121+
hrefAttr[1] = `${normalizedPath}${rawHash}`
125122
// set `hasOpenInternalLink` to modify the ending tag
126123
hasOpenInternalLink = true
124+
} else {
125+
const normalizedPath = normalizeRoutePath(absolutePath)
126+
hrefAttr[1] = `${normalizedPath}${rawHash}`
127127
}
128128

129-
hrefAttr[1] = `${normalizedPath}${rawHash}`
130-
131129
// extract internal links for file / page existence check
132130
;(env.links ??= []).push({
133131
raw: hrefLink,

‎packages/markdown/src/plugins/linksPlugin/resolvePaths.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@ export const resolvePaths = (
1515
let absolutePath: string
1616
let relativePath: string
1717

18+
// if raw path is absolute
1819
if (rawPath.startsWith('/')) {
19-
// if raw path is absolute
20-
20+
// if raw path is a link to markdown file
2121
if (rawPath.endsWith('.md')) {
22-
// if raw path is a link to markdown file
23-
2422
// prepend `base` to the link
2523
absolutePath = path.join(base, rawPath)
2624
relativePath = removeLeadingSlash(rawPath)
27-
} else {
28-
// if raw path is a link to other kind of file
29-
25+
}
26+
// if raw path is a link to other kind of file
27+
else {
3028
// keep the link as is
3129
absolutePath = rawPath
3230
relativePath = path.relative(base, absolutePath)
3331
}
34-
} else {
35-
// if raw path is relative
32+
}
33+
// if raw path is relative
34+
else {
35+
// if `filePathRelative` is available
3636
if (filePathRelative) {
37-
// if `filePathRelative` is available
38-
3937
// resolve relative path according to `filePathRelative`
4038
relativePath = path.join(
4139
// file path may contain non-ASCII characters
@@ -44,9 +42,9 @@ export const resolvePaths = (
4442
)
4543
// resolve absolute path according to `base`
4644
absolutePath = path.join(base, relativePath)
47-
} else {
48-
// if `filePathRelative` is not available
49-
45+
}
46+
// if `filePathRelative` is not available
47+
else {
5048
// remove leading './'
5149
relativePath = rawPath.replace(/^(?:\.\/)?(.*)$/, '$1')
5250
// just take relative link as absolute link

‎packages/markdown/tests/plugins/linksPlugin.spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,45 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
838838
},
839839
])
840840
})
841+
842+
it('should render to <a> tag correctly', () => {
843+
const md = MarkdownIt({ html: true }).use(linksPlugin, {
844+
internalTag: 'a',
845+
})
846+
const env: MarkdownEnv = {
847+
base: '/base/',
848+
}
849+
850+
const rendered = md.render(source, env)
851+
852+
expect(rendered).toEqual(
853+
[
854+
'<a href="/base/path/to/">md</a>',
855+
'<a href="/base/base/path/to/">md-with-redundant-base</a>',
856+
'<a href="/base/path/to/">html</a>',
857+
]
858+
.map((a) => `<p>${a}</p>`)
859+
.join('\n') + '\n',
860+
)
861+
862+
expect(env.links).toEqual([
863+
{
864+
raw: '/path/to/index.md',
865+
relative: 'path/to/index.md',
866+
absolute: '/base/path/to/index.md',
867+
},
868+
{
869+
raw: '/base/path/to/index.md',
870+
relative: 'base/path/to/index.md',
871+
absolute: '/base/base/path/to/index.md',
872+
},
873+
{
874+
raw: '/base/path/to/index.html',
875+
relative: 'path/to/index.html',
876+
absolute: '/base/path/to/index.html',
877+
},
878+
])
879+
})
841880
})
842881
})
843882

0 commit comments

Comments
 (0)
Please sign in to comment.