Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): fix query parsing and absolute link fallback in linksPlugin (close #1536) #1537

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/markdown/src/plugins/linksPlugin/linksPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isLinkExternal, normalizeRoutePath } from '@vuepress/shared'
import { inferRoutePath, isLinkExternal } from '@vuepress/shared'
import type { PluginWithOptions } from 'markdown-it'
import type Token from 'markdown-it/lib/token.mjs'
import type { MarkdownEnv } from '../../types.js'
Expand Down Expand Up @@ -67,7 +67,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (

// if `href` attr exists, `token.attrs` is not `null`
const hrefAttr = token.attrs![hrefIndex]
const hrefLink = hrefAttr[1]
const hrefLink: string = hrefAttr[1]

// get `base` and `filePathRelative` from `env`
const { base = '/', filePathRelative = null } = env
Expand All @@ -83,7 +83,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (

// check if a link is an internal link
const internalLinkMatch = hrefLink.match(
/^((?:.*)(?:\/|\.md|\.html))(#.*)?$/,
/^([^#?]*?(?:\/|\.md|\.html))([#?].*)?$/,
)

if (!internalLinkMatch) {
Expand All @@ -97,7 +97,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (

// notice that the path and hash are encoded by markdown-it
const rawPath = internalLinkMatch[1]
const rawHash = internalLinkMatch[2] || ''
const rawHashAndQueries = internalLinkMatch[2] || ''

// resolve relative and absolute path
const { relativePath, absolutePath } = resolvePaths(
Expand All @@ -114,16 +114,19 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
// normalize markdown file path to route path
// we are removing the `base` from absolute path because it should not be
// passed to `<RouteLink>` or `<RouterLink>`
const normalizedPath = normalizeRoutePath(
absolutePath.replace(new RegExp(`^${base}`), '/'),
const normalizedPath = inferRoutePath(
absolutePath
? absolutePath.replace(new RegExp(`^${base}`), '/')
: relativePath,
)
// replace the original href link with the normalized path
hrefAttr[1] = `${normalizedPath}${rawHash}`
hrefAttr[1] = `${normalizedPath}${rawHashAndQueries}`
// set `hasOpenInternalLink` to modify the ending tag
hasOpenInternalLink = true
} else {
const normalizedPath = normalizeRoutePath(absolutePath)
hrefAttr[1] = `${normalizedPath}${rawHash}`
const normalizedPath = inferRoutePath(absolutePath ?? relativePath)
// replace the original href link with the normalized path
hrefAttr[1] = `${normalizedPath}${rawHashAndQueries}`
}

// extract internal links for file / page existence check
Expand All @@ -139,7 +142,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
return self.renderToken(tokens, idx, options)
}

md.renderer.rules.link_close = (tokens, idx, options, env, self) => {
md.renderer.rules.link_close = (tokens, idx, options, _env, self) => {
// convert ending tag of internal link
if (hasOpenInternalLink) {
hasOpenInternalLink = false
Expand Down
6 changes: 3 additions & 3 deletions packages/markdown/src/plugins/linksPlugin/resolvePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export const resolvePaths = (
base: string,
filePathRelative: string | null,
): {
absolutePath: string
absolutePath: string | null
relativePath: string
} => {
let absolutePath: string
let absolutePath: string | null
let relativePath: string

// if raw path is absolute
Expand Down Expand Up @@ -48,7 +48,7 @@ export const resolvePaths = (
// remove leading './'
relativePath = rawPath.replace(/^(?:\.\/)?(.*)$/, '$1')
// just take relative link as absolute link
absolutePath = relativePath
absolutePath = null
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type MarkdownHeader = PageHeader
export interface MarkdownLink {
raw: string
relative: string
absolute: string
absolute: string | null
}

/**
Expand Down