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

Add openInNewTab option to nav and sidebar #236

Merged
merged 3 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions src/components/HeaderNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@
v-for="(childItem, index) in item.children"
:key="index"
>
<uni-link :to="childItem.link">{{ childItem.title }}</uni-link>
<uni-link
:to="childItem.link"
:openInNewTab="childItem.openInNewTab"
>{{ childItem.title }}</uni-link
>
</li>
</ul>
</div>

<uni-link v-if="!item.children" :to="item.link">{{
item.title
}}</uni-link>
<uni-link
v-if="!item.children"
:to="item.link"
:openInNewTab="item.openInNewTab"
>{{ item.title }}</uni-link
>
</div>
</div>
</template>
Expand Down
35 changes: 17 additions & 18 deletions src/components/SidebarItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,14 @@
</div>
<template v-if="!item.collapsable || open">
<template v-for="(link, index) of children">
<a
v-if="isExternalLink(link.link)"
:key="index"
:href="link.link"
<uni-link
class="ItemLink"
target="_blank"
>
{{ link.title }}
<external-link-icon />
</a>
<router-link
v-else
:key="index"
:to="link.link"
:prefetch-files="getPrefetchFiles(link.link)"
class="ItemLink"
:class="{active: $route.path === link.link}"
:openInNewTab="link.openInNewTab"
:prefetchFiles="getPrefetchFiles(link.link)"
>{{ link.title }}</uni-link
>
{{ link.title }}
</router-link>
<div
class="LinkToc"
v-if="
Expand All @@ -54,8 +42,7 @@
v-for="heading in $store.state.page.headings"
:key="heading.slug"
v-html="heading.text"
>
</router-link>
></router-link>
</div>
</template>
</template>
Expand All @@ -64,8 +51,12 @@

<script>
import {isExternalLink, getFileUrl, getFilenameByPath} from '../utils'
import UniLink from './UniLink.vue'

export default {
components: {
UniLink
},
props: {
item: {
type: Object,
Expand All @@ -89,6 +80,7 @@ export default {
},
methods: {
isExternalLink,

getPrefetchFiles(path) {
const {sourcePath, routes} = this.$store.getters.config
if (routes && routes[path]) {
Expand All @@ -98,6 +90,13 @@ export default {
const filename = getFilenameByPath(path)
const fileUrl = getFileUrl(sourcePath, filename)
return fileUrl ? [fileUrl] : []
},

getLinkTarget(link) {
if (!isExternalLink(link) || link.openInNewTab === false) {
return '_self'
}
return '_blank'
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/components/UniLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export default {

render(h, {data, children}) {
const attrs = {...data.attrs}
const {to} = attrs
const {to, openInNewTab} = attrs
delete attrs.openInNewTab
if (isExternalLink(to)) {
delete attrs.to
delete attrs.prefetchFiles
return h(
'a',
{
Expand All @@ -17,10 +19,15 @@ export default {
attrs: {
...attrs,
href: to,
target: '_blank'
target: openInNewTab === false ? '_self' : '_blank'
}
},
[...children, h('external-link-icon', {class: 'external-link-icon'})]
[
...children,
openInNewTab === false
? null
: h('external-link-icon', {class: 'external-link-icon'})
]
)
}
return h('router-link', data, children)
Expand Down
9 changes: 9 additions & 0 deletions website/docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ An array of navigation items to display at navbar.
interface NavItem {
title: string
link?: string
// Whether to open the link in a new tab
// Only works for external links
// Defaults to `true`
openInNewTab?: boolean
// Or use `children` to display dropdown menu
children?: Array<NavItemLink>
}

interface NavItemLink {
title: string
link: string
openInNewTab?: boolean
}
```

Expand All @@ -66,6 +71,10 @@ interface SidebarItem {
interface SidebarItemLink {
title: string
link: string
// Whether to open the link in a new tab
// Only works for external links
// Defaults to `true`
openInNewTab?: boolean
/* Whether to show TOC, true by default */
toc?: boolean
}
Expand Down
9 changes: 9 additions & 0 deletions website/docs/zh/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ Customize the logo in header.
interface NavItem {
title: string
link?: string
// Whether to open the link in a new tab
// Only works for external links
// Defaults to `true`
openInNewTab?: boolean
// Use `children` instead of `link` to display dropdown
children?: Array<NavItemLink>
}

interface NavItemLink {
title: string
link: string
openInNewTab?: boolean
}
```

Expand All @@ -66,6 +71,10 @@ interface SidebarItem {
interface ItemLink {
title: string
link: string
// Whether to open the link in a new tab
// Only works for external links
// Defaults to `true`
openInNewTab?: boolean
/* 是否显示 TOC,默认为 true */
toc: boolean
}
Expand Down