Skip to content

Commit af96f28

Browse files
committed
fix: wrong OutboundLink insertion position (close: #496)
1 parent 11b1830 commit af96f28

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/markdown/link.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
module.exports = (md, externalAttrs) => {
66
let hasOpenRouterLink = false
7+
let hasOpenExternalLink = false
78

89
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
910
const token = tokens[idx]
@@ -17,11 +18,8 @@ module.exports = (md, externalAttrs) => {
1718
Object.entries(externalAttrs).forEach(([key, val]) => {
1819
token.attrSet(key, val)
1920
})
20-
2121
if (/_blank/i.test(externalAttrs['target'])) {
22-
// add OutBoundLink to content if it opens in _blank
23-
tokens[idx + 1].type = 'html_block'
24-
tokens[idx + 1].content += '<OutboundLink/>'
22+
hasOpenExternalLink = true
2523
}
2624
} else if (isSourceLink) {
2725
hasOpenRouterLink = true
@@ -59,6 +57,11 @@ module.exports = (md, externalAttrs) => {
5957
token.tag = 'router-link'
6058
hasOpenRouterLink = false
6159
}
60+
if (hasOpenExternalLink) {
61+
hasOpenExternalLink = false
62+
// add OutBoundLink to the beforeend of this link if it opens in _blank.
63+
return '<OutboundLink/>' + self.renderToken(tokens, idx, options)
64+
}
6265
return self.renderToken(tokens, idx, options)
6366
}
6467
}

0 commit comments

Comments
 (0)