diff --git a/package.json b/package.json index 119abd6c..677679cc 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..3dde210e 100644 --- a/src/components/HeaderSearch/index.vue +++ b/src/components/HeaderSearch/index.vue @@ -23,6 +23,7 @@ import Fuse from 'fuse.js' import path from 'path' import i18n from '@/lang' +import pinyin from 'pinyin' export default { name: 'HeaderSearch', @@ -51,6 +52,10 @@ export default { this.searchPool = this.generateRoutes(this.routes) }, searchPool(list) { + // Support pinyin search + if (this.lang === 'zh') { + this.pinyin(list) + } this.initFuse(list) }, show(value) { @@ -65,6 +70,22 @@ export default { this.searchPool = this.generateRoutes(this.routes) }, methods: { + pinyin(list) { + 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 +116,9 @@ export default { keys: [{ name: 'title', weight: 0.7 + }, { + name: 'pinyinTitle', + weight: 0.3 }, { name: 'path', weight: 0.3