Skip to content

Commit bea5e92

Browse files
committed
chore: tweaks
1 parent 6bc3431 commit bea5e92

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

packages/@vuepress/markdown/src/plugins/assetsPlugin.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,28 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
2222
md.renderer.rules.image = (tokens, idx, options, env: MarkdownEnv, self) => {
2323
const token = tokens[idx]
2424

25-
// get the image link and decode link to the origin one
26-
// so that bundler can find the file correctly
25+
// get the image link
2726
const link = token.attrGet('src')
2827

29-
if (link) {
30-
if (/^\.{1,2}\//.test(link) && env.filePathRelative) {
31-
// if the link is relative path, and the `env.filePathRelative` exists
32-
// add `@source` alias to the link
33-
const resolvedLink = `${relativePathPrefix}/${path.join(
34-
path.dirname(env.filePathRelative),
35-
decode(link)
36-
)}`
37-
38-
// replace the original link with absolute path
39-
token.attrSet('src', resolvedLink)
40-
} else {
41-
token.attrSet('src', decode(link))
42-
}
28+
if (!link) {
29+
return rawRule(tokens, idx, options, env, self)
4330
}
4431

32+
// decode link to ensure bundler can find the file correctly
33+
let resolvedLink = decode(link)
34+
35+
// if the link is relative path, and the `env.filePathRelative` exists
36+
// add `@source` alias to the link
37+
if (/^\.{1,2}\//.test(link) && env.filePathRelative) {
38+
resolvedLink = `${relativePathPrefix}/${path.join(
39+
path.dirname(env.filePathRelative),
40+
resolvedLink
41+
)}`
42+
}
43+
44+
// replace the original link with resolved link
45+
token.attrSet('src', resolvedLink)
46+
4547
return rawRule(tokens, idx, options, env, self)
4648
}
4749
}

0 commit comments

Comments
 (0)