2019-04-15 10:35:40 +08:00
|
|
|
<template>
|
|
|
|
<nav
|
|
|
|
class="nav-links"
|
|
|
|
v-if="userLinks.length || repoLink"
|
|
|
|
>
|
|
|
|
<!-- user links -->
|
|
|
|
<div
|
|
|
|
class="nav-item"
|
|
|
|
v-for="item in userLinks"
|
|
|
|
:key="item.link">
|
|
|
|
<DropdownLink
|
|
|
|
v-if="item.type === 'links'"
|
|
|
|
:item="item"/>
|
|
|
|
<NavLink
|
|
|
|
v-else
|
|
|
|
:item="item"/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- repo link -->
|
|
|
|
<a
|
|
|
|
v-if="repoLink"
|
|
|
|
:href="repoLink"
|
|
|
|
class="repo-link"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer">
|
2019-11-27 20:03:10 +08:00
|
|
|
<i :class="`iconfont reco-${repoLabel.toLowerCase()}`"></i>
|
2019-04-15 10:35:40 +08:00
|
|
|
{{ repoLabel }}
|
|
|
|
<OutboundLink/>
|
|
|
|
</a>
|
|
|
|
</nav>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2019-12-06 00:11:16 +08:00
|
|
|
import DropdownLink from '@theme/components/DropdownLink'
|
2019-12-05 14:59:03 +08:00
|
|
|
import { resolveNavLinkItem } from '@theme/helpers/utils'
|
2019-12-06 00:11:16 +08:00
|
|
|
import NavLink from '@theme/components/NavLink'
|
2019-04-15 10:35:40 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
components: { NavLink, DropdownLink },
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
userNav () {
|
2019-05-08 15:13:18 +08:00
|
|
|
return this.$themeLocaleConfig.nav || this.$themeConfig.nav || []
|
2019-04-15 10:35:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
nav () {
|
2019-09-20 13:54:13 +08:00
|
|
|
const { $site: { locales }, userNav } = this
|
2019-04-15 10:35:40 +08:00
|
|
|
if (locales && Object.keys(locales).length > 1) {
|
|
|
|
const currentLink = this.$page.path
|
|
|
|
const routes = this.$router.options.routes
|
2019-05-08 15:13:18 +08:00
|
|
|
const themeLocales = this.$themeConfig.locales || {}
|
2019-04-15 10:35:40 +08:00
|
|
|
const languageDropdown = {
|
|
|
|
text: this.$themeLocaleConfig.selectText || 'Languages',
|
|
|
|
items: Object.keys(locales).map(path => {
|
|
|
|
const locale = locales[path]
|
|
|
|
const text = themeLocales[path] && themeLocales[path].label || locale.lang
|
|
|
|
let link
|
|
|
|
// Stay on the current page
|
|
|
|
if (locale.lang === this.$lang) {
|
|
|
|
link = currentLink
|
|
|
|
} else {
|
|
|
|
// Try to stay on the same page
|
|
|
|
link = currentLink.replace(this.$localeConfig.path, path)
|
|
|
|
// fallback to homepage
|
|
|
|
if (!routes.some(route => route.path === link)) {
|
|
|
|
link = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { text, link }
|
|
|
|
})
|
|
|
|
}
|
2019-09-20 13:54:13 +08:00
|
|
|
return [...userNav, languageDropdown]
|
2019-04-15 10:35:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// blogConfig 的处理,根绝配置自动添加分类和标签
|
2019-09-20 13:54:13 +08:00
|
|
|
const blogConfig = this.$themeConfig.blogConfig || {}
|
|
|
|
const isHasCategory = userNav.some(item => {
|
|
|
|
if (blogConfig.category) {
|
|
|
|
return item.text === (blogConfig.category.text || '分类')
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const isHasTag = userNav.some(item => {
|
|
|
|
if (blogConfig.tag) {
|
|
|
|
return item.text === (blogConfig.tag.text || '标签')
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
2019-04-15 10:35:40 +08:00
|
|
|
|
2019-09-20 13:54:13 +08:00
|
|
|
if (!isHasCategory && Object.hasOwnProperty.call(blogConfig, 'category')) {
|
2019-04-15 10:35:40 +08:00
|
|
|
const category = blogConfig.category
|
|
|
|
const $categories = this.$categories
|
2019-09-20 13:54:13 +08:00
|
|
|
userNav.splice(parseInt(category.location || 2) - 1, 0, {
|
2019-04-15 10:35:40 +08:00
|
|
|
items: $categories.list.map(item => {
|
|
|
|
item.link = item.path
|
|
|
|
item.text = item.name
|
|
|
|
return item
|
|
|
|
}),
|
|
|
|
text: category.text || '分类',
|
2019-09-20 13:54:13 +08:00
|
|
|
type: 'links',
|
|
|
|
icon: 'reco-category'
|
2019-04-15 10:35:40 +08:00
|
|
|
})
|
|
|
|
}
|
2019-09-20 13:54:13 +08:00
|
|
|
if (!isHasTag && Object.hasOwnProperty.call(blogConfig, 'tag')) {
|
2019-04-15 10:35:40 +08:00
|
|
|
const tag = blogConfig.tag
|
2019-09-20 13:54:13 +08:00
|
|
|
userNav.splice(parseInt(tag.location || 3) - 1, 0, {
|
2019-04-15 10:35:40 +08:00
|
|
|
link: '/tag/',
|
|
|
|
text: tag.text || '标签',
|
2019-09-20 13:54:13 +08:00
|
|
|
type: 'links',
|
|
|
|
icon: 'reco-tag'
|
2019-04-15 10:35:40 +08:00
|
|
|
})
|
|
|
|
}
|
2019-09-20 13:54:13 +08:00
|
|
|
|
|
|
|
return userNav
|
2019-04-15 10:35:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
userLinks () {
|
|
|
|
return (this.nav || []).map(link => {
|
|
|
|
return Object.assign(resolveNavLinkItem(link), {
|
|
|
|
items: (link.items || []).map(resolveNavLinkItem)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
repoLink () {
|
2019-05-08 15:13:18 +08:00
|
|
|
const { repo } = this.$themeConfig
|
2019-04-15 10:35:40 +08:00
|
|
|
if (repo) {
|
|
|
|
return /^https?:/.test(repo)
|
|
|
|
? repo
|
|
|
|
: `https://github.com/${repo}`
|
|
|
|
}
|
2019-09-20 13:54:13 +08:00
|
|
|
return ''
|
2019-04-15 10:35:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
repoLabel () {
|
|
|
|
if (!this.repoLink) return
|
2019-05-08 15:13:18 +08:00
|
|
|
if (this.$themeConfig.repoLabel) {
|
|
|
|
return this.$themeConfig.repoLabel
|
2019-04-15 10:35:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const repoHost = this.repoLink.match(/^https?:\/\/[^/]+/)[0]
|
|
|
|
const platforms = ['GitHub', 'GitLab', 'Bitbucket']
|
|
|
|
for (let i = 0; i < platforms.length; i++) {
|
|
|
|
const platform = platforms[i]
|
|
|
|
if (new RegExp(platform, 'i').test(repoHost)) {
|
|
|
|
return platform
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Source'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="stylus">
|
|
|
|
.nav-links
|
|
|
|
display inline-block
|
|
|
|
a
|
|
|
|
line-height 1.4rem
|
2019-11-27 20:03:10 +08:00
|
|
|
color var(--text-color)
|
2019-04-15 10:35:40 +08:00
|
|
|
&:hover, &.router-link-active
|
|
|
|
color $accentColor
|
|
|
|
.iconfont
|
|
|
|
color $accentColor
|
|
|
|
.nav-item
|
|
|
|
position relative
|
|
|
|
display inline-block
|
|
|
|
margin-left 1.5rem
|
|
|
|
line-height 2rem
|
|
|
|
&:first-child
|
|
|
|
margin-left 0
|
|
|
|
.repo-link
|
|
|
|
margin-left 1.5rem
|
|
|
|
|
|
|
|
@media (max-width: $MQMobile)
|
|
|
|
.nav-links
|
|
|
|
.nav-item, .repo-link
|
|
|
|
margin-left 0
|
|
|
|
|
|
|
|
@media (min-width: $MQMobile)
|
|
|
|
.nav-item > a:not(.external)
|
|
|
|
&:hover, &.router-link-active
|
|
|
|
margin-bottom -2px
|
|
|
|
border-bottom 2px solid lighten($accentColor, 8%)
|
|
|
|
</style>
|