Skip to content

Commit 1a1b86e

Browse files
committed
refactor(markdown): remove relativePathPrefix option from assetsPlugin
1 parent 2cb9174 commit 1a1b86e

File tree

3 files changed

+74
-280
lines changed

3 files changed

+74
-280
lines changed

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ export interface AssetsPluginOptions {
88
* Whether to prepend base to absolute path
99
*/
1010
absolutePathPrependBase?: boolean
11-
12-
/**
13-
* Prefix to add to relative assets links
14-
*/
15-
relativePathPrefix?: string
1611
}
1712

1813
/**
1914
* Plugin to handle assets links
2015
*/
2116
export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
2217
md,
23-
{
24-
absolutePathPrependBase = false,
25-
relativePathPrefix = '@source',
26-
}: AssetsPluginOptions = {},
18+
{ absolutePathPrependBase = false }: AssetsPluginOptions = {},
2719
) => {
2820
// wrap raw image renderer rule
2921
const rawImageRule = md.renderer.rules.image!
@@ -35,10 +27,7 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
3527

3628
if (link) {
3729
// replace the original link with resolved link
38-
token.attrSet(
39-
'src',
40-
resolveLink(link, { env, absolutePathPrependBase, relativePathPrefix }),
41-
)
30+
token.attrSet('src', resolveLink(link, { env, absolutePathPrependBase }))
4231
}
4332

4433
return rawImageRule(tokens, idx, options, env, self)
@@ -57,7 +46,6 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
5746
`${prefix}${quote}${resolveLink(src.trim(), {
5847
env,
5948
absolutePathPrependBase,
60-
relativePathPrefix,
6149
strict: true,
6250
})}${quote}`,
6351
)
@@ -74,7 +62,6 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
7462
`${resolveLink(url.trim(), {
7563
env,
7664
absolutePathPrependBase,
77-
relativePathPrefix,
7865
strict: true,
7966
})}${descriptor.replace(/[ \n]+/g, ' ').trimEnd()}`,
8067
),

packages/markdown/src/plugins/assetsPlugin/resolveLink.ts

+1-23
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,19 @@ import type { MarkdownEnv } from '../../types.js'
55
interface ResolveLinkOptions {
66
env: MarkdownEnv
77
absolutePathPrependBase?: boolean
8-
relativePathPrefix: string
98
strict?: boolean
109
}
1110

1211
export const resolveLink = (
1312
link: string,
14-
{
15-
env,
16-
absolutePathPrependBase = false,
17-
relativePathPrefix,
18-
strict = false,
19-
}: ResolveLinkOptions,
13+
{ env, absolutePathPrependBase = false }: ResolveLinkOptions,
2014
): string => {
2115
// do not resolve data uri
2216
if (link.startsWith('data:')) return link
2317

2418
// decode link to ensure bundler can find the file correctly
2519
let resolvedLink = decode(link)
2620

27-
// check if the link is relative path
28-
const isRelativePath = strict
29-
? // in strict mode, only link that starts with `./` or `../` is considered as relative path
30-
/^\.{1,2}\//.test(link)
31-
: // in non-strict mode, link that does not start with `/` and does not have protocol is considered as relative path
32-
!link.startsWith('/') && !/[A-z]+:\/\//.test(link)
33-
34-
// if the link is relative path, and the `env.filePathRelative` exists
35-
// add `@source` alias to the link
36-
if (isRelativePath && env.filePathRelative) {
37-
resolvedLink = `${relativePathPrefix}/${path.join(
38-
path.dirname(env.filePathRelative),
39-
resolvedLink,
40-
)}`
41-
}
42-
4321
// prepend base to absolute path if needed
4422
if (absolutePathPrependBase && env.base && link.startsWith('/')) {
4523
resolvedLink = path.join(env.base, resolvedLink)

0 commit comments

Comments
 (0)