diff --git a/package.json b/package.json index 80f183a7..ec3ae6fa 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "normalize.css": "7.0.0", "nprogress": "0.2.0", "path-to-regexp": "2.4.0", + "pinyin": "2.9.0", "screenfull": "4.2.0", "showdown": "1.9.0", "sortablejs": "1.8.4", diff --git a/src/components/HeaderSearch/index.vue b/src/components/HeaderSearch/index.vue index 90eea067..0543b894 100644 --- a/src/components/HeaderSearch/index.vue +++ b/src/components/HeaderSearch/index.vue @@ -41,6 +41,9 @@ export default { }, lang() { return this.$store.getters.language + }, + supportPinyinSearch() { + return this.$store.state.settings.supportPinyinSearch } }, watch: { @@ -51,6 +54,10 @@ export default { this.searchPool = this.generateRoutes(this.routes) }, searchPool(list) { + // Support pinyin search + if (this.lang === 'zh' && this.supportPinyinSearch) { + this.addPinyinField(list) + } this.initFuse(list) }, show(value) { @@ -65,6 +72,23 @@ export default { this.searchPool = this.generateRoutes(this.routes) }, methods: { + async addPinyinField(list) { + const { default: pinyin } = await import('pinyin') + if (Array.isArray(list)) { + list.forEach(element => { + const title = element.title + if (Array.isArray(title)) { + title.forEach(v => { + v = pinyin(v, { + style: pinyin.STYLE_NORMAL + }).join('') + element.pinyinTitle = v + }) + } + }) + return list + } + }, click() { this.show = !this.show if (this.show) { @@ -95,6 +119,9 @@ export default { keys: [{ name: 'title', weight: 0.7 + }, { + name: 'pinyinTitle', + weight: 0.3 }, { name: 'path', weight: 0.3 @@ -105,29 +132,23 @@ export default { // And generate the internationalized title generateRoutes(routes, basePath = '/', prefixTitle = []) { let res = [] - for (const router of routes) { // skip hidden router if (router.hidden) { continue } - const data = { path: path.resolve(basePath, router.path), title: [...prefixTitle] } - if (router.meta && router.meta.title) { // generate internationalized title const i18ntitle = i18n.t(`route.${router.meta.title}`) - data.title = [...data.title, i18ntitle] - if (router.redirect !== 'noRedirect') { // only push the routes with title // special case: need to exclude parent router without redirect res.push(data) } } - // recursive child routes if (router.children) { const tempRoutes = this.generateRoutes(router.children, data.path, data.title) @@ -152,13 +173,11 @@ export default {