Skip to content

Commit 20e5bd8

Browse files
ycmjasonulivz
authored andcommitted
feat: support global markdown config for attributes of external links (#358)
1 parent 582723c commit 20e5bd8

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

docs/config/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ Provide config options to the used theme. The options will vary depending on the
134134

135135
Function for transforming header texts into slugs. This affects the ids/links generated for header anchors, table of contents and sidebar links.
136136

137+
### markdown.externalLinks
138+
139+
- Type: `Object`
140+
- Default: `{ target: '_blank', rel: 'noopener noreferrer' }`
141+
142+
The key and value pair will be added to `<a>` tags that points to an external link. The default option will open external links in a new window.
143+
137144
### markdown.anchor
138145

139146
- Type: `Object`

docs/guide/markdown.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ Given the following directory structure:
4848

4949
### External Links
5050

51-
Outbound links automatically gets `target="_blank"`:
51+
Outbound links automatically gets `target="_blank" rel="noopener noreferrer"`:
5252

5353
- [vuejs.org](https://vuejs.org)
5454
- [VuePress on GitHub](https://github.com/vuejs/vuepress)
5555

56+
You can customize the attributes added to external links by setting `config.markdown.externalLinks`. See more [here](/config/#markdown-externalLinks).
57+
5658
## Front Matter
5759

5860
[YAML front matter](https://jekyllrb.com/docs/frontmatter/) is supported out of the box:

lib/markdown/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module.exports = ({ markdown = {}} = {}) => {
2020
// custom plugins
2121
.use(component)
2222
.use(highlightLines)
23-
.use(convertRouterLink)
23+
.use(convertRouterLink, Object.assign({
24+
target: '_blank',
25+
rel: 'noopener noreferrer'
26+
}, markdown.externalLinks))
2427
.use(hoistScriptStyle)
2528
.use(containers)
2629

lib/markdown/link.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 1. adding target="_blank" to external links
33
// 2. converting internal links to <router-link>
44

5-
module.exports = md => {
5+
module.exports = (md, externalAttrs) => {
66
let hasOpenRouterLink = false
77

88
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
@@ -14,8 +14,9 @@ module.exports = md => {
1414
const isExternal = /^https?:/.test(href)
1515
const isSourceLink = /(\/|\.md|\.html)(#.*)?$/.test(href)
1616
if (isExternal) {
17-
addAttr(token, 'target', '_blank')
18-
addAttr(token, 'rel', 'noopener noreferrer')
17+
Object.entries(externalAttrs).forEach(([key, val]) => {
18+
addAttr(token, key, val)
19+
})
1920
} else if (isSourceLink) {
2021
hasOpenRouterLink = true
2122
tokens[idx] = toRouterLink(token, link)

0 commit comments

Comments
 (0)