Skip to content

Commit 41214eb

Browse files
committedOct 10, 2024·
feat(markdown): add isExternal option for linksPlugin (close #1611)
1 parent e693cdc commit 41214eb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed
 

‎packages/markdown/src/plugins/linksPlugin/linksPlugin.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import type { MarkdownEnv } from '../../types.js'
55
import { resolvePaths } from './resolvePaths.js'
66

77
export interface LinksPluginOptions {
8-
/**
9-
* Tag for internal links
10-
*
11-
* @default 'RouteLink'
12-
*/
13-
internalTag?: 'a' | 'RouteLink' | 'RouterLink'
14-
158
/**
169
* Additional attributes for external links
1710
*
@@ -24,6 +17,20 @@ export interface LinksPluginOptions {
2417
* ```
2518
*/
2619
externalAttrs?: Record<string, string>
20+
21+
/**
22+
* Tag for internal links
23+
*
24+
* @default 'RouteLink'
25+
*/
26+
internalTag?: 'a' | 'RouteLink' | 'RouterLink'
27+
28+
/**
29+
* Method to check if a link is external
30+
*
31+
* @default import { isLinkExternal } from '@vuepress/shared'
32+
*/
33+
isExternal?: (href: string, env: MarkdownEnv) => boolean
2734
}
2835

2936
/**
@@ -38,6 +45,8 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
3845
): void => {
3946
// tag of internal links
4047
const internalTag = options.internalTag || 'RouteLink'
48+
const isExternal =
49+
options.isExternal ?? ((href, env) => isLinkExternal(href, env.base))
4150

4251
// attrs that going to be added to external links
4352
const externalAttrs = {
@@ -73,7 +82,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
7382
const { base = '/', filePathRelative = null } = env
7483

7584
// check if a link is an external link
76-
if (isLinkExternal(hrefLink, base)) {
85+
if (isExternal(hrefLink, env)) {
7786
// set `externalAttrs` to current token
7887
Object.entries(externalAttrs).forEach(([key, val]) => {
7988
token.attrSet(key, val)

0 commit comments

Comments
 (0)
Please sign in to comment.