-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use
<Link>
component for internal links (#1553)
Co-authored-by: _Kerman <kermanx@qq.com>
- Loading branch information
1 parent
d83edd8
commit f6507c1
Showing
5 changed files
with
32 additions
and
25 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/slidev/node/syntax/markdown-it/markdown-it-link.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type MarkdownIt from 'markdown-it' | ||
|
||
export default function markdownItLink(md: MarkdownIt) { | ||
const defaultRender = md.renderer.rules.link_open | ||
?? ((tokens, idx, options, _env, self) => self.renderToken(tokens, idx, options)) | ||
|
||
md.renderer.rules.link_open = function (tokens, idx, options, env, self) { | ||
const token = tokens[idx] | ||
const hrefIndex = token.attrIndex('href') | ||
const attr = token.attrs?.[hrefIndex] | ||
const href = attr?.[1] ?? '' | ||
if ('./#'.includes(href[0]) || /^\d+$/.test(href)) { | ||
token.tag = 'Link' | ||
attr![0] = 'to' | ||
|
||
for (let i = idx + 1; i < tokens.length; i++) { | ||
if (tokens[i].type === 'link_close') { | ||
tokens[i].tag = 'Link' | ||
break | ||
} | ||
} | ||
} | ||
else if (token.attrGet('target') == null) { | ||
token.attrPush(['target', '_blank']) | ||
} | ||
return defaultRender(tokens, idx, options, env, self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.