Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 6fe1fc0

Browse files
authored
feat: add openInNewTab option to nav and sidebar (#236)
* add openInNewTab option to nav and sidebar * update docs * tweaks
1 parent 02e7111 commit 6fe1fc0

File tree

5 files changed

+56
-25
lines changed

5 files changed

+56
-25
lines changed

src/components/HeaderNav.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
v-for="(childItem, index) in item.children"
1212
:key="index"
1313
>
14-
<uni-link :to="childItem.link">{{ childItem.title }}</uni-link>
14+
<uni-link
15+
:to="childItem.link"
16+
:openInNewTab="childItem.openInNewTab"
17+
>{{ childItem.title }}</uni-link
18+
>
1519
</li>
1620
</ul>
1721
</div>
1822

19-
<uni-link v-if="!item.children" :to="item.link">{{
20-
item.title
21-
}}</uni-link>
23+
<uni-link
24+
v-if="!item.children"
25+
:to="item.link"
26+
:openInNewTab="item.openInNewTab"
27+
>{{ item.title }}</uni-link
28+
>
2229
</div>
2330
</div>
2431
</template>

src/components/SidebarItem.vue

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,14 @@
1616
</div>
1717
<template v-if="!item.collapsable || open">
1818
<template v-for="(link, index) of children">
19-
<a
20-
v-if="isExternalLink(link.link)"
21-
:key="index"
22-
:href="link.link"
19+
<uni-link
2320
class="ItemLink"
24-
target="_blank"
25-
>
26-
{{ link.title }}
27-
<external-link-icon />
28-
</a>
29-
<router-link
30-
v-else
3121
:key="index"
3222
:to="link.link"
33-
:prefetch-files="getPrefetchFiles(link.link)"
34-
class="ItemLink"
35-
:class="{active: $route.path === link.link}"
23+
:openInNewTab="link.openInNewTab"
24+
:prefetchFiles="getPrefetchFiles(link.link)"
25+
>{{ link.title }}</uni-link
3626
>
37-
{{ link.title }}
38-
</router-link>
3927
<div
4028
class="LinkToc"
4129
v-if="
@@ -54,8 +42,7 @@
5442
v-for="heading in $store.state.page.headings"
5543
:key="heading.slug"
5644
v-html="heading.text"
57-
>
58-
</router-link>
45+
></router-link>
5946
</div>
6047
</template>
6148
</template>
@@ -64,8 +51,12 @@
6451

6552
<script>
6653
import {isExternalLink, getFileUrl, getFilenameByPath} from '../utils'
54+
import UniLink from './UniLink.vue'
6755
6856
export default {
57+
components: {
58+
UniLink
59+
},
6960
props: {
7061
item: {
7162
type: Object,
@@ -89,6 +80,7 @@ export default {
8980
},
9081
methods: {
9182
isExternalLink,
83+
9284
getPrefetchFiles(path) {
9385
const {sourcePath, routes} = this.$store.getters.config
9486
if (routes && routes[path]) {
@@ -98,6 +90,13 @@ export default {
9890
const filename = getFilenameByPath(path)
9991
const fileUrl = getFileUrl(sourcePath, filename)
10092
return fileUrl ? [fileUrl] : []
93+
},
94+
95+
getLinkTarget(link) {
96+
if (!isExternalLink(link) || link.openInNewTab === false) {
97+
return '_self'
98+
}
99+
return '_blank'
101100
}
102101
}
103102
}

src/components/UniLink.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ export default {
66
77
render(h, {data, children}) {
88
const attrs = {...data.attrs}
9-
const {to} = attrs
9+
const {to, openInNewTab} = attrs
10+
delete attrs.openInNewTab
1011
if (isExternalLink(to)) {
1112
delete attrs.to
13+
delete attrs.prefetchFiles
1214
return h(
1315
'a',
1416
{
@@ -17,10 +19,15 @@ export default {
1719
attrs: {
1820
...attrs,
1921
href: to,
20-
target: '_blank'
22+
target: openInNewTab === false ? '_self' : '_blank'
2123
}
2224
},
23-
[...children, h('external-link-icon', {class: 'external-link-icon'})]
25+
[
26+
...children,
27+
openInNewTab === false
28+
? null
29+
: h('external-link-icon', {class: 'external-link-icon'})
30+
]
2431
)
2532
}
2633
return h('router-link', data, children)

website/docs/options.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ An array of navigation items to display at navbar.
4040
interface NavItem {
4141
title: string
4242
link?: string
43+
// Whether to open the link in a new tab
44+
// Only works for external links
45+
// Defaults to `true`
46+
openInNewTab?: boolean
4347
// Or use `children` to display dropdown menu
4448
children?: Array<NavItemLink>
4549
}
4650

4751
interface NavItemLink {
4852
title: string
4953
link: string
54+
openInNewTab?: boolean
5055
}
5156
```
5257

@@ -66,6 +71,10 @@ interface SidebarItem {
6671
interface SidebarItemLink {
6772
title: string
6873
link: string
74+
// Whether to open the link in a new tab
75+
// Only works for external links
76+
// Defaults to `true`
77+
openInNewTab?: boolean
6978
/* Whether to show TOC, true by default */
7079
toc?: boolean
7180
}

website/docs/zh/options.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ Customize the logo in header.
4141
interface NavItem {
4242
title: string
4343
link?: string
44+
// Whether to open the link in a new tab
45+
// Only works for external links
46+
// Defaults to `true`
47+
openInNewTab?: boolean
4448
// Use `children` instead of `link` to display dropdown
4549
children?: Array<NavItemLink>
4650
}
4751

4852
interface NavItemLink {
4953
title: string
5054
link: string
55+
openInNewTab?: boolean
5156
}
5257
```
5358

@@ -66,6 +71,10 @@ interface SidebarItem {
6671
interface ItemLink {
6772
title: string
6873
link: string
74+
// Whether to open the link in a new tab
75+
// Only works for external links
76+
// Defaults to `true`
77+
openInNewTab?: boolean
6978
/* 是否显示 TOC,默认为 true */
7079
toc: boolean
7180
}

0 commit comments

Comments
 (0)