返回 slidev
link.ts
根目录 / packages / slidev / node / syntax / link.ts
1 import type MarkdownExit from 'markdown-exit'
2
3 const RE_DIGITS_ONLY = /^\d+$/
4
5 export default function MarkdownItLink(md: MarkdownExit) {
6 const defaultRender = md.renderer.rules.link_open
7 ?? ((tokens, idx, options, _env, self) => self.renderToken(tokens, idx, options))
8
9 md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
10 const token = tokens[idx]
11 const hrefIndex = token.attrIndex('href')
12 const attr = token.attrs?.[hrefIndex]
13 const href = attr?.[1] ?? ''
14 if ('./#'.includes(href[0]) || RE_DIGITS_ONLY.test(href)) {
15 token.tag = 'Link'
16 attr![0] = 'to'
17
18 for (let i = idx + 1; i < tokens.length; i++) {
19 if (tokens[i].type === 'link_close') {
20 tokens[i].tag = 'Link'
21 break
22 }
23 }
24 }
25 else if (token.attrGet('target') == null) {
26 token.attrPush(['target', '_blank'])
27 }
28 return defaultRender(tokens, idx, options, env, self)
29 }
30 }
31
31 lines TYPESCRIPT