feat: publicize doc implemetation

This commit is contained in:
Justineo
2020-08-13 11:47:56 +08:00
parent 55b9b044f2
commit 1e5fcff6ad
372 changed files with 50636 additions and 0 deletions

99
common/i18n.js Normal file
View File

@@ -0,0 +1,99 @@
import { compact, find } from 'lodash'
import nav from '../assets/data/nav.json'
export const LOCALES = [
{
code: 'zh-Hans',
label: '简体中文'
},
{
code: 'en-US',
label: 'English (US)'
}
]
export const DEFAULT_LOCALE = LOCALES[0].code
export const LOCALE_CODES = LOCALES.map(l => l.code)
export const RE_LOCALE = new RegExp(`^\\/(${LOCALE_CODES.join('|')})(?=\\/|$)`)
export function getLocale (path) {
if (path != null) {
let [, locale] = path.match(RE_LOCALE) || []
if (locale) {
return locale
}
}
return DEFAULT_LOCALE
}
export function getCanonicalPath (path) {
if (path === '') {
return '/'
}
let locale = getLocale(path)
path = path.replace(new RegExp(`^\\/${locale}(?=\\/|$)`), '')
return path.charAt(0) === '/' ? path : `/${path}`
}
export function getLocalePath (path, locale) {
if (locale === DEFAULT_LOCALE) {
return path
}
return `/${locale}${path}`.replace(/\/$/, '')
}
export const htmlAttrs = {
head () {
return {
htmlAttrs: {
lang: getLocale(this.$route.path)
}
}
}
}
export default {
computed: {
canonicalPath () {
if (this.$route.path === '') {
return '/'
}
let path = this.$route.path.replace(new RegExp(`^\\/${this.locale}(?=\\/|$)`), '')
return path.charAt(0) === '/' ? path : `/${path}`
},
locale () {
if (this.$route) {
let [, locale] = this.$route.path.match(RE_LOCALE) || []
if (locale) {
return locale
}
}
return DEFAULT_LOCALE
},
locales () {
return LOCALES
}
},
methods: {
getLocalePath (path, locale) {
let loc = locale || this.locale
if (loc === DEFAULT_LOCALE) {
return path
}
return `/${loc}${path}`.replace(/\/$/, '')
},
isPathDisabled (path, locale) {
let segments = compact(path.split('/'))
let navItem = segments.reduce((node, seg) => {
if (!node) {
return null
}
return find(node.children, ({ slug }) => slug === seg)
}, { children: nav[locale] || [] })
if (!navItem) {
return null
}
return navItem.disabled
}
}
}

2070
common/region.js Normal file

File diff suppressed because it is too large Load Diff

38
common/util.js Normal file
View File

@@ -0,0 +1,38 @@
export function walk (list, base, callback) {
if (!Array.isArray(list)) {
return
}
let stopped = list.some(item => {
let newBase = `${base}/${item.slug}`
if (typeof callback === 'function') {
if (callback(item, newBase) === false) {
return true
}
}
if (item.children) {
if (walk(item.children, newBase, callback) === false) {
return true
}
}
return false
})
return !stopped
}
export function getLink ({ slug, link, children }) {
if (link === false) {
let actual = null
walk(children, slug, (item, base) => {
// leaf
if (!item.children || !item.children.length) {
actual = base
return false
}
})
if (actual !== null) {
return `/${actual}`
}
}
return `/${slug}`
}