Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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'
Expand Down Expand Up @@ -62,7 +62,7 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions {
* Disable cache (experimental)
*/
cache?: boolean
externalLinks?: Record<string, string>
externalLinks?: Record<string, string | ExternalLinkAttrValue>

/* ==================== Syntax Highlighting ==================== */

Expand Down Expand Up @@ -279,7 +279,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
)
Expand Down
15 changes: 12 additions & 3 deletions src/node/markdown/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) => {
Expand All @@ -38,7 +39,15 @@ export const linkPlugin = (
const url = hrefAttr[1]
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:')) {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export type {
SSGContext,
AdditionalConfig,
AdditionalConfigDict,
AdditionalConfigLoader
AdditionalConfigLoader,
ExternalLinkAttrValue
} from '../../types/shared'

export const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i
Expand Down
5 changes: 5 additions & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,8 @@ export interface MarkdownEnv {
realPath?: string
localeIndex?: string
}

export interface ExternalLinkAttrValue {
value: string
join?: boolean
}