From 3de4c8245982668fcfc569658134711401557ea0 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 11 Feb 2022 19:54:18 +0800 Subject: [PATCH] feat: search --- docs/.vuepress/config.js | 4 +- docs/.vuepress/theme/components/Navbar.vue | 7 +- .../theme/components/SearchBox/index.vue | 330 ++++++++++++++++++ .../theme/components/SearchBox/match-query.js | 51 +++ .../theme/components/SearchBox/search.svg | 1 + docs/.vuepress/theme/mixin/navProvider.js | 2 +- docs/.vuepress/theme/styles/navbar.styl | 6 + docs/worktile/CLI.md | 2 +- 8 files changed, 398 insertions(+), 5 deletions(-) create mode 100644 docs/.vuepress/theme/components/SearchBox/index.vue create mode 100644 docs/.vuepress/theme/components/SearchBox/match-query.js create mode 100644 docs/.vuepress/theme/components/SearchBox/search.svg diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 49571b907..3fb637535 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -31,11 +31,11 @@ const config = { editLinks: true, editLinkText: '帮助我们改善此页面!', // smoothScroll: true, - algolia: { + /* algolia: { apiKey: 'ca67b01d14df58783e2f7dc45c79736e', indexName: 'en-uniapp-dcloud', appId: 'TZ0EGQ9J1Y' - } + } */ }, markdown: { slugify(str) { diff --git a/docs/.vuepress/theme/components/Navbar.vue b/docs/.vuepress/theme/components/Navbar.vue index cbf06ea0f..bb9ea5370 100644 --- a/docs/.vuepress/theme/components/Navbar.vue +++ b/docs/.vuepress/theme/components/Navbar.vue @@ -56,6 +56,7 @@ 'max-width': linksWrapMaxWidth + 'px' } : {}" > + 回到旧版 import AlgoliaSearchBox from '@AlgoliaSearchBox' -import SearchBox from '@SearchBox' +import SearchBox from './SearchBox' import SidebarButton from '@theme/components/SidebarButton.vue' import NavLinks from '@theme/components/NavLinks.vue' import MainNavbarLink from './MainNavbarLink.vue'; @@ -186,6 +187,10 @@ export default { toggleMobilePanel () { this.showMobilePanel = !this.showMobilePanel forbidScroll(this.showMobilePanel) + }, + switchVersion () { + document.cookie = '__new_version=;expires=-1' + location.reload() } }, diff --git a/docs/.vuepress/theme/components/SearchBox/index.vue b/docs/.vuepress/theme/components/SearchBox/index.vue new file mode 100644 index 000000000..a43486113 --- /dev/null +++ b/docs/.vuepress/theme/components/SearchBox/index.vue @@ -0,0 +1,330 @@ + + + + + + + + {{ s.title || 'Document' }} + {{ s.header.title }} + + + + + + + + + + diff --git a/docs/.vuepress/theme/components/SearchBox/match-query.js b/docs/.vuepress/theme/components/SearchBox/match-query.js new file mode 100644 index 000000000..c1c41f3ba --- /dev/null +++ b/docs/.vuepress/theme/components/SearchBox/match-query.js @@ -0,0 +1,51 @@ + +import get from 'lodash/get' + +export default (query, page, additionalStr = null) => { + let domain = get(page, 'title', '') + + if (get(page, 'frontmatter.tags')) { + domain += ` ${page.frontmatter.tags.join(' ')}` + } + + if (additionalStr) { + domain += ` ${additionalStr}` + } + + return matchTest(query, domain) +} + +const matchTest = (query, domain) => { + const escapeRegExp = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + + // eslint-disable-next-line no-control-regex + const nonASCIIRegExp = new RegExp('[^\x00-\x7F]') + + const words = query + .split(/\s+/g) + .map(str => str.trim()) + .filter(str => !!str) + + if (!nonASCIIRegExp.test(query)) { + // if the query only has ASCII chars, treat as English + const hasTrailingSpace = query.endsWith(' ') + const searchRegex = new RegExp( + words + .map((word, index) => { + if (words.length === index + 1 && !hasTrailingSpace) { + // The last word - ok with the word being "startswith"-like + return `(?=.*\\b${escapeRegExp(word)})` + } else { + // Not the last word - expect the whole word exactly + return `(?=.*\\b${escapeRegExp(word)}\\b)` + } + }) + .join('') + '.+', + 'gi' + ) + return searchRegex.test(domain) + } else { + // if the query has non-ASCII chars, treat as other languages + return words.some(word => domain.toLowerCase().indexOf(word) > -1) + } +} diff --git a/docs/.vuepress/theme/components/SearchBox/search.svg b/docs/.vuepress/theme/components/SearchBox/search.svg new file mode 100644 index 000000000..03d83913e --- /dev/null +++ b/docs/.vuepress/theme/components/SearchBox/search.svg @@ -0,0 +1 @@ + diff --git a/docs/.vuepress/theme/mixin/navProvider.js b/docs/.vuepress/theme/mixin/navProvider.js index 24db56990..54cebe6ae 100644 --- a/docs/.vuepress/theme/mixin/navProvider.js +++ b/docs/.vuepress/theme/mixin/navProvider.js @@ -49,7 +49,7 @@ export default { watch: { $route(after) { let navbarIndex = this.customNavBarKeys.indexOf((after.fullPath.match(/\/(\w+)+\/*/) || [])[1]) - navbarIndex === -1 && after.fullPath === '/' && (navbarIndex = 0) + navbarIndex === -1 && (navbarIndex = 0) this.navConfig.userNavIndex !== navbarIndex && navbarIndex !== -1 && (this.navConfig.userNavIndex = navbarIndex) } } diff --git a/docs/.vuepress/theme/styles/navbar.styl b/docs/.vuepress/theme/styles/navbar.styl index 6606f06a0..4c14ad772 100644 --- a/docs/.vuepress/theme/styles/navbar.styl +++ b/docs/.vuepress/theme/styles/navbar.styl @@ -70,6 +70,12 @@ $navbar-logo-height = $navbar-main-navbar-height - 2rem bottom 0 opacity 0 transform scaleX(0) + .switch-version + padding-right 20px + text-decoration underline + color inherit + &:hover + color $accentColor @media (max-width: $MQMobile) $navbar-a-color = #222; diff --git a/docs/worktile/CLI.md b/docs/worktile/CLI.md index a579291ca..02944106c 100644 --- a/docs/worktile/CLI.md +++ b/docs/worktile/CLI.md @@ -329,7 +329,7 @@ yarn build:mp-360 yarn dev:mp-360 # 监听文件变化且启用压缩 -yarn cross-env NODE_ENV=production UNI_PLATFORM=mp-360 vue-cli-service uni-build --watch +yarn build:mp-360 --watch ``` 可以自定义更多条件编译平台,比如钉钉小程序,参考[package.json 文档](https://uniapp.dcloud.io/collocation/package)。 -- GitLab