Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat($theme-default): support configuring target and rel for nav links (close #1353) #1734

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions packages/@vuepress/theme-default/components/NavLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
class="nav-link"
:to="link"
@focusout.native="focusoutAction"
v-if="!isExternal(link)"
v-if="isInternal"
:exact="exact"
>{{ item.text }}</router-link>
<a
v-else
:href="link"
@focusout="focusoutAction"
class="nav-link external"
:target="isMailto(link) || isTel(link) ? null : '_blank'"
:rel="isMailto(link) || isTel(link) ? null : 'noopener noreferrer'"
:target="target"
:rel="rel"
>
{{ item.text }}
<OutboundLink/>
<OutboundLink v-if="isTargetBlank"/>
</a>
</template>

Expand All @@ -39,13 +39,28 @@ export default {
return Object.keys(this.$site.locales).some(rootLink => rootLink === this.link)
}
return this.link === '/'
},

target () {
return isMailto(this.link) || isTel(this.link) ? null : this.item.target
|| (isExternal(this.link) ? '_blank' : '')
},

isTargetBlank () {
return this.target === '_blank'
},

rel () {
return isMailto(this.link) || isTel(this.link) ? null : this.item.rel
|| (this.isTargetBlank ? 'noopener noreferrer' : '')
},

isInternal () {
return !isExternal(this.link) && !this.isTargetBlank
}
billyyyyy3320 marked this conversation as resolved.
Show resolved Hide resolved
},

methods: {
isExternal,
isMailto,
isTel,
focusoutAction () {
this.$emit('focusout')
}
Expand Down
14 changes: 14 additions & 0 deletions packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ module.exports = {
}
```

Outbound links automatically get `target="_blank" rel="noopener noreferrer"`. You can offer `target` and `rel` to customize the attributes:

``` js
// .vuepress/config.js
module.exports = {
themeConfig: {
nav: [
{ text: 'External', link: 'https://google.com', target:'_self', rel:'' },
{ text: 'Guide', link: '/guide/', target:'_blank' }
]
}
}
```

These links can also be dropdown menus if you provide an array of `items` instead of a `link`:

```js
Expand Down
15 changes: 15 additions & 0 deletions packages/docs/docs/zh/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ module.exports = {
]
}
}


```
外部链接 `<a>` 标签的特性将默认包含`target="_blank" rel="noopener noreferrer"`,你可以提供 `target` 与 `rel`,它们将被作为特性被增加到 `<a>` 标签上:

``` js
// .vuepress/config.js
module.exports = {
themeConfig: {
nav: [
{ text: 'External', link: 'https://google.com', target:'_self', rel:'' },
{ text: 'Guide', link: '/guide/', target:'_blank' }
]
}
}
```

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