From 41214eb4060e6b5537cd1ee25e2803bf1f66a7e6 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Thu, 10 Oct 2024 09:42:29 +0800 Subject: [PATCH] feat(markdown): add isExternal option for linksPlugin (close #1611) --- .../src/plugins/linksPlugin/linksPlugin.ts | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/markdown/src/plugins/linksPlugin/linksPlugin.ts b/packages/markdown/src/plugins/linksPlugin/linksPlugin.ts index 6da9f97fdc..14c04eac46 100644 --- a/packages/markdown/src/plugins/linksPlugin/linksPlugin.ts +++ b/packages/markdown/src/plugins/linksPlugin/linksPlugin.ts @@ -5,13 +5,6 @@ import type { MarkdownEnv } from '../../types.js' import { resolvePaths } from './resolvePaths.js' export interface LinksPluginOptions { - /** - * Tag for internal links - * - * @default 'RouteLink' - */ - internalTag?: 'a' | 'RouteLink' | 'RouterLink' - /** * Additional attributes for external links * @@ -24,6 +17,20 @@ export interface LinksPluginOptions { * ``` */ externalAttrs?: Record + + /** + * Tag for internal links + * + * @default 'RouteLink' + */ + internalTag?: 'a' | 'RouteLink' | 'RouterLink' + + /** + * Method to check if a link is external + * + * @default import { isLinkExternal } from '@vuepress/shared' + */ + isExternal?: (href: string, env: MarkdownEnv) => boolean } /** @@ -38,6 +45,8 @@ export const linksPlugin: PluginWithOptions = ( ): void => { // tag of internal links const internalTag = options.internalTag || 'RouteLink' + const isExternal = + options.isExternal ?? ((href, env) => isLinkExternal(href, env.base)) // attrs that going to be added to external links const externalAttrs = { @@ -73,7 +82,7 @@ export const linksPlugin: PluginWithOptions = ( const { base = '/', filePathRelative = null } = env // check if a link is an external link - if (isLinkExternal(hrefLink, base)) { + if (isExternal(hrefLink, env)) { // set `externalAttrs` to current token Object.entries(externalAttrs).forEach(([key, val]) => { token.attrSet(key, val)