WizardsBowl 1 month ago committed by GitHub
commit 19e25ebee1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -27,7 +27,7 @@ import mditCjkFriendly from 'markdown-it-cjk-friendly'
import { full as emojiPlugin } from 'markdown-it-emoji'
import type { BuiltinLanguage, BuiltinTheme, Highlighter } from 'shiki'
import type { Logger } from 'vite'
import type { Awaitable } from '../shared'
import type { Awaitable, ExternalLinkAttrValue } from '../shared'
import { containerPlugin, type ContainerOptions } from './plugins/containers'
import { gitHubAlertsPlugin } from './plugins/githubAlerts'
import { highlight as createHighlighter } from './plugins/highlight'
@ -63,7 +63,7 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions {
* Disable cache (experimental)
*/
cache?: boolean
externalLinks?: Record<string, string>
externalLinks?: Record<string, string | ExternalLinkAttrValue>
/* ==================== Syntax Highlighting ==================== */
@ -286,7 +286,11 @@ export async function createMarkdownRenderer(
imagePlugin(md, options.image)
linkPlugin(
md,
{ target: '_blank', rel: 'noreferrer', ...options.externalLinks },
{
target: '_blank',
rel: { value: 'noreferrer', join: true },
...options.externalLinks
},
base,
slugify
)

@ -8,14 +8,15 @@ import {
EXTERNAL_URL_RE,
isExternal,
treatAsHtml,
type MarkdownEnv
type MarkdownEnv,
type ExternalLinkAttrValue
} from '../../shared'
const indexRE = /(^|.*\/)index.md(#?.*)$/i
export const linkPlugin = (
md: MarkdownItAsync,
externalAttrs: Record<string, string>,
externalAttrs: Record<string, string | ExternalLinkAttrValue>,
base: string,
slugify: (str: string) => string
) => {
@ -39,7 +40,15 @@ export const linkPlugin = (
hrefAttr[1] = url
if (isExternal(url)) {
Object.entries(externalAttrs).forEach(([key, val]) => {
token.attrSet(key, val)
if (typeof val === 'string') {
token.attrSet(key, val)
} else {
if (val.join) {
token.attrJoin(key, val.value)
} else {
token.attrSet(key, val.value)
}
}
})
// catch localhost links as dead link
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {

@ -19,7 +19,8 @@ export type {
SSGContext,
AdditionalConfig,
AdditionalConfigDict,
AdditionalConfigLoader
AdditionalConfigLoader,
ExternalLinkAttrValue
} from '../../types/shared'
export const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i

5
types/shared.d.ts vendored

@ -232,3 +232,8 @@ export interface MarkdownEnv {
realPath?: string
localeIndex?: string
}
export interface ExternalLinkAttrValue {
value: string
join?: boolean
}

Loading…
Cancel
Save