Skip to content

Commit 770ba72

Browse files
billyyyyy3320kefranabg
authored andcommittedOct 16, 2019
feat($theme-default): Support configuring target and rel for nav links (close #1353) (#1734)
1 parent bc4032d commit 770ba72

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed
 

‎packages/@vuepress/theme-default/components/NavLink.vue

+36-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
class="nav-link"
44
:to="link"
55
@focusout.native="focusoutAction"
6-
v-if="!isExternal(link)"
6+
v-if="isInternal"
77
:exact="exact"
88
>{{ item.text }}</router-link>
99
<a
1010
v-else
1111
:href="link"
1212
@focusout="focusoutAction"
1313
class="nav-link external"
14-
:target="isMailto(link) || isTel(link) ? null : '_blank'"
15-
:rel="isMailto(link) || isTel(link) ? null : 'noopener noreferrer'"
14+
:target="target"
15+
:rel="rel"
1616
>
1717
{{ item.text }}
18-
<OutboundLink/>
18+
<OutboundLink v-if="isBlankTarget"/>
1919
</a>
2020
</template>
2121

@@ -39,13 +39,42 @@ export default {
3939
return Object.keys(this.$site.locales).some(rootLink => rootLink === this.link)
4040
}
4141
return this.link === '/'
42+
},
43+
44+
isNonHttpURI () {
45+
return isMailto(this.link) || isTel(this.link)
46+
},
47+
48+
isBlankTarget () {
49+
return this.target === '_blank'
50+
},
51+
52+
isInternal () {
53+
return !isExternal(this.link) && !this.isBlankTarget
54+
},
55+
56+
target () {
57+
if (this.isNonHttpURI) {
58+
return null
59+
}
60+
if (this.item.target) {
61+
return this.item.target
62+
}
63+
return isExternal(this.link) ? '_blank' : ''
64+
},
65+
66+
rel () {
67+
if (this.isNonHttpURI) {
68+
return null
69+
}
70+
if (this.item.rel) {
71+
return this.item.rel
72+
}
73+
return this.isBlankTarget ? 'noopener noreferrer' : ''
4274
}
4375
},
4476
4577
methods: {
46-
isExternal,
47-
isMailto,
48-
isTel,
4978
focusoutAction () {
5079
this.$emit('focusout')
5180
}

‎packages/docs/docs/theme/default-theme-config.md

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ module.exports = {
5454
}
5555
```
5656

57+
Outbound links automatically get `target="_blank" rel="noopener noreferrer"`. You can offer `target` and `rel` to customize the attributes:
58+
59+
``` js
60+
// .vuepress/config.js
61+
module.exports = {
62+
themeConfig: {
63+
nav: [
64+
{ text: 'External', link: 'https://google.com', target:'_self', rel:'' },
65+
{ text: 'Guide', link: '/guide/', target:'_blank' }
66+
]
67+
}
68+
}
69+
```
70+
5771
These links can also be dropdown menus if you provide an array of `items` instead of a `link`:
5872

5973
```js

‎packages/docs/docs/zh/theme/default-theme-config.md

+15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ module.exports = {
4848
]
4949
}
5050
}
51+
52+
53+
```
54+
外部链接 `<a>` 标签的特性将默认包含`target="_blank" rel="noopener noreferrer"`,你可以提供 `target``rel`,它们将被作为特性被增加到 `<a>` 标签上:
55+
56+
``` js
57+
// .vuepress/config.js
58+
module.exports = {
59+
themeConfig: {
60+
nav: [
61+
{ text: 'External', link: 'https://google.com', target:'_self', rel:'' },
62+
{ text: 'Guide', link: '/guide/', target:'_blank' }
63+
]
64+
}
65+
}
5166
```
5267

5368
当你提供了一个 `items` 数组而不是一个单一的 `link` 时,它将显示为一个 `下拉列表`

0 commit comments

Comments
 (0)
Please sign in to comment.