2022-09-23 10:03:04 +08:00
|
|
|
import type MarkdownIt from 'markdown-it'
|
|
|
|
import type Renderer from 'markdown-it/lib/renderer'
|
|
|
|
|
|
|
|
export default (md: MarkdownIt): void => {
|
|
|
|
const renderToken: Renderer.RenderRule = (tokens, idx, options, env, self) =>
|
|
|
|
self.renderToken(tokens, idx, options)
|
|
|
|
const defaultLinkOpenRenderer = md.renderer.rules.link_open || renderToken
|
|
|
|
const defaultLinkCloseRenderer = md.renderer.rules.link_close || renderToken
|
|
|
|
let isExternalLink = false
|
|
|
|
|
|
|
|
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
|
|
|
|
const token = tokens[idx]
|
|
|
|
const href = token.attrGet('href')
|
|
|
|
|
2022-09-24 08:45:10 +08:00
|
|
|
if (href) {
|
|
|
|
token.attrJoin('class', 'vp-link')
|
|
|
|
if (/^((ht|f)tps?):\/\/?/.test(href)) {
|
|
|
|
isExternalLink = true
|
|
|
|
}
|
2022-09-23 10:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return defaultLinkOpenRenderer(tokens, idx, options, env, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
md.renderer.rules.link_close = (tokens, idx, options, env, self) => {
|
|
|
|
if (isExternalLink) {
|
|
|
|
isExternalLink = false
|
|
|
|
return `<i-ri-external-link-line class="link-icon" />${self.renderToken(
|
|
|
|
tokens,
|
|
|
|
idx,
|
|
|
|
options
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultLinkCloseRenderer(tokens, idx, options, env, self)
|
|
|
|
}
|
|
|
|
}
|