Skip to content

Commit

Permalink
feat(markdown): add isExternal option for linksPlugin (close #1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Oct 10, 2024
1 parent e693cdc commit 41214eb
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/markdown/src/plugins/linksPlugin/linksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -24,6 +17,20 @@ export interface LinksPluginOptions {
* ```
*/
externalAttrs?: Record<string, string>

/**
* 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
}

/**
Expand All @@ -38,6 +45,8 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
): 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 = {
Expand Down Expand Up @@ -73,7 +82,7 @@ export const linksPlugin: PluginWithOptions<LinksPluginOptions> = (
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)
Expand Down

0 comments on commit 41214eb

Please sign in to comment.