Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/markdown/src/plugins/assetsPlugin/assetsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ export interface AssetsPluginOptions {
* Prefix to add to relative assets links
*/
relativePathPrefix?: string

/**
* The strictness of the determination of relative paths
*
* When different options are set, the criteria for identifying relative paths are as follows:
*
* - true: start with `./` or `../`
* - false: no start with `/` or `<protocol header>`
* - '@-perfix': no start with `/` or `@` or `<protocol header>`
*/
restrictRelativePath?: boolean | '@-perfix'
}

/**
Expand All @@ -23,6 +34,7 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
{
absolutePathPrependBase = false,
relativePathPrefix = '@source',
restrictRelativePath = true,
}: AssetsPluginOptions = {},
) => {
// wrap raw image renderer rule
Expand Down Expand Up @@ -58,7 +70,7 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
env,
absolutePathPrependBase,
relativePathPrefix,
strict: true,
strict: restrictRelativePath,
})}${quote}`,
)
// handle srcset
Expand All @@ -75,7 +87,7 @@ export const assetsPlugin: PluginWithOptions<AssetsPluginOptions> = (
env,
absolutePathPrependBase,
relativePathPrefix,
strict: true,
strict: restrictRelativePath,
})}${descriptor.replace(/[ \n]+/g, ' ').trimEnd()}`,
),
)
Expand Down
15 changes: 9 additions & 6 deletions packages/markdown/src/plugins/assetsPlugin/resolveLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface ResolveLinkOptions {
env: MarkdownEnv
absolutePathPrependBase?: boolean
relativePathPrefix: string
strict?: boolean
strict?: boolean | '@-perfix'
}

export const resolveLink = (
Expand All @@ -25,11 +25,14 @@ export const resolveLink = (
let resolvedLink = decode(link)

// check if the link is relative path
const isRelativePath = strict
? // in strict mode, only link that starts with `./` or `../` is considered as relative path
/^\.{1,2}\//.test(link)
: // in non-strict mode, link that does not start with `/` and does not have protocol is considered as relative path
!link.startsWith('/') && !/[A-z]+:\/\//.test(link)
const isRelativePath =
strict === true
? /^\.{1,2}\//.test(link)
: strict === false
? !link.startsWith('/') && !/[A-z]+:\/\//.test(link)
: !link.startsWith('/') &&
!link.startsWith('@') &&
!/[A-z]+:\/\//.test(link)

// if the link is relative path, and the `env.filePathRelative` exists
// add `@source` alias to the link
Expand Down
Loading