feat(cli): docsearch v3

This commit is contained in:
chenjiahan 2020-12-01 17:25:46 +08:00
parent 63b354e0f1
commit b33737ef77
6 changed files with 211 additions and 105 deletions

View File

@ -43,6 +43,8 @@
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.12.7",
"@babel/preset-typescript": "^7.12.7",
"@docsearch/css": "3.0.0-alpha.31",
"@docsearch/js": "3.0.0-alpha.31",
"@nuxt/friendly-errors-webpack-plugin": "^2.5.0",
"@types/jest": "^26.0.15",
"@types/webpack-dev-server": "^3.11.1",

View File

@ -7,12 +7,6 @@
<span>{{ config.title }}</span>
</a>
<search-input
v-if="searchConfig"
:lang="lang"
:search-config="searchConfig"
/>
<ul class="van-doc-header__top-nav">
<li
v-for="(item, index) in config.links"
@ -56,6 +50,12 @@
<li v-if="langLabel && langLink" class="van-doc-header__top-nav-item">
<a class="van-doc-header__cube" :href="langLink">{{ langLabel }}</a>
</li>
<search-input
v-if="searchConfig"
:lang="lang"
:search-config="searchConfig"
/>
</ul>
</div>
</div>

View File

@ -1,8 +1,11 @@
<template>
<input class="van-doc-search" :placeholder="placeholder" />
<div id="docsearch" />
</template>
<script>
import '@docsearch/css';
import docsearch from '@docsearch/js';
export default {
name: 'van-doc-search',
@ -11,41 +14,28 @@ export default {
searchConfig: Object,
},
computed: {
placeholder() {
return this.searchConfig.placeholder || 'Search...';
},
},
watch: {
lang(lang) {
if (this.docsearchInstance) {
this.docsearchInstance.algoliaOptions.facetFilters = [`lang:${lang}`];
}
lang() {
this.initDocsearch();
},
},
mounted() {
if (this.searchConfig) {
this.docsearchInstance = window.docsearch({
...this.searchConfig,
transformData: (hits) => {
hits.forEach((hit) => {
if (hit.anchor) {
hit.url = hit.url + '#' + hit.anchor;
hit.anchor = null;
}
});
if (this.searchConfig.transformData) {
this.searchConfig.transformData(hits);
}
},
inputSelector: '.van-doc-search',
algoliaOptions: {
facetFilters: [`lang:${this.lang}`],
},
});
}
this.initDocsearch();
},
methods: {
initDocsearch() {
if (this.searchConfig) {
docsearch({
...this.searchConfig,
container: '#docsearch',
searchParameters: {
facetFilters: [`lang:${this.lang}`],
},
});
}
},
},
};
</script>
@ -53,60 +43,8 @@ export default {
<style lang="less">
@import '../../common/style/var';
.van-doc-search {
width: 400px;
height: @van-doc-header-top-height;
margin-left: 140px;
color: #fff;
font-size: 14px;
background-color: transparent;
border: none;
&:focus {
outline: none;
}
&::placeholder {
color: #fff;
opacity: 0.7;
}
}
.ds-dropdown-menu {
line-height: 1.8;
}
.algolia-autocomplete {
.algolia-docsearch-suggestion--highlight {
color: @van-doc-blue;
background-color: transparent;
}
.algolia-docsearch-suggestion--title {
font-weight: 600;
}
.algolia-docsearch-suggestion--text {
.algolia-docsearch-suggestion--highlight {
box-shadow: inset 0 -1px 0 0 @van-doc-blue;
}
}
.algolia-docsearch-suggestion--category-header {
border-bottom-color: #eee;
}
.ds-dropdown-menu [class^='ds-dataset-'] {
border: none;
}
.ds-dropdown-menu {
top: 80% !important;
box-shadow: 0 4px 12px #ebedf0;
&::before {
display: none;
}
}
#docsearch {
display: inline-block;
vertical-align: middle;
}
</style>

View File

@ -19,10 +19,6 @@
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link
href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"
rel="stylesheet"
/>
<% if (htmlWebpackPlugin.options.baiduAnalytics) { %>
<script>
var _hmt = _hmt || [];
@ -38,6 +34,5 @@
</head>
<body ontouchstart>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
</body>
</html>

View File

@ -41,17 +41,17 @@ module.exports = {
apiKey: '90067aecdaa2c85220e2783cd305caac',
indexName: 'vant',
placeholder: '搜索文档...',
transformData(hits) {
if (location.hostname === 'vant-contrib.gitee.io') {
hits.forEach((hit) => {
if (hit.url) {
hit.url = hit.url.replace(
'youzan.github.io',
'vant-contrib.gitee.io'
);
transformItems(items) {
if (location.hostname !== 'youzan.github.io') {
items.forEach((item) => {
if (item.url) {
item.url =
item.url &&
item.url.replace('youzan.github.io', location.hostname);
}
});
}
return items;
},
},
nav: [

171
yarn.lock
View File

@ -2,6 +2,129 @@
# yarn lockfile v1
"@algolia/autocomplete-core@^1.0.0-alpha.35":
version "1.0.0-alpha.35"
resolved "https://registry.npm.taobao.org/@algolia/autocomplete-core/download/@algolia/autocomplete-core-1.0.0-alpha.35.tgz#a54949edf7b402da6fd6cf08943438bfb802aa50"
integrity sha1-pUlJ7fe0Atpv1s8IlDQ4v7gCqlA=
dependencies:
"@algolia/autocomplete-shared" "1.0.0-alpha.35"
"@algolia/autocomplete-preset-algolia@^1.0.0-alpha.35":
version "1.0.0-alpha.35"
resolved "https://registry.npm.taobao.org/@algolia/autocomplete-preset-algolia/download/@algolia/autocomplete-preset-algolia-1.0.0-alpha.35.tgz#b1218a689caa66b607a532c2e363973309764158"
integrity sha1-sSGKaJyqZrYHpTLC42OXMwl2QVg=
dependencies:
"@algolia/autocomplete-shared" "1.0.0-alpha.35"
"@algolia/autocomplete-shared@1.0.0-alpha.35":
version "1.0.0-alpha.35"
resolved "https://registry.npm.taobao.org/@algolia/autocomplete-shared/download/@algolia/autocomplete-shared-1.0.0-alpha.35.tgz#d6819cb1420a389ac9894199bda2c7abd276fd3d"
integrity sha1-1oGcsUIKOJrJiUGZvaLHq9J2/T0=
"@algolia/cache-browser-local-storage@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/cache-browser-local-storage/download/@algolia/cache-browser-local-storage-4.8.2.tgz#e94c50d360c53fc48d107484de2012f3a0bbed9a"
integrity sha1-6UxQ02DFP8SNEHSE3iAS86C77Zo=
dependencies:
"@algolia/cache-common" "4.8.2"
"@algolia/cache-common@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/cache-common/download/@algolia/cache-common-4.8.2.tgz#e992e29ffeec75e2bc77eef5280efca71ac27d56"
integrity sha1-6ZLin/7sdeK8d+71KA78pxrCfVY=
"@algolia/cache-in-memory@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/cache-in-memory/download/@algolia/cache-in-memory-4.8.2.tgz#12d66469d5ff7142d092272e01e02abfb6a1315d"
integrity sha1-EtZkadX/cULQkicuAeAqv7ahMV0=
dependencies:
"@algolia/cache-common" "4.8.2"
"@algolia/client-account@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/client-account/download/@algolia/client-account-4.8.2.tgz#ad4066085d871fb5ea10dcf6a40a423480d3bbdc"
integrity sha1-rUBmCF2HH7XqENz2pApCNIDTu9w=
dependencies:
"@algolia/client-common" "4.8.2"
"@algolia/client-search" "4.8.2"
"@algolia/transporter" "4.8.2"
"@algolia/client-analytics@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/client-analytics/download/@algolia/client-analytics-4.8.2.tgz#29592bc9a2d39a50c17ede959bdc14812edd0fba"
integrity sha1-KVkryaLTmlDBft6Vm9wUgS7dD7o=
dependencies:
"@algolia/client-common" "4.8.2"
"@algolia/client-search" "4.8.2"
"@algolia/requester-common" "4.8.2"
"@algolia/transporter" "4.8.2"
"@algolia/client-common@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/client-common/download/@algolia/client-common-4.8.2.tgz#989ca2a396938db833578a65c63bce1b1785bde9"
integrity sha1-mJyio5aTjbgzV4plxjvOGxeFvek=
dependencies:
"@algolia/requester-common" "4.8.2"
"@algolia/transporter" "4.8.2"
"@algolia/client-recommendation@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/client-recommendation/download/@algolia/client-recommendation-4.8.2.tgz#f8483adca6ce829414d8e8c5b58420b22d160d49"
integrity sha1-+Eg63KbOgpQU2OjFtYQgsi0WDUk=
dependencies:
"@algolia/client-common" "4.8.2"
"@algolia/requester-common" "4.8.2"
"@algolia/transporter" "4.8.2"
"@algolia/client-search@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/client-search/download/@algolia/client-search-4.8.2.tgz#714d1604bfdf5b8ba33247ee571c6f3c2abc6c31"
integrity sha1-cU0WBL/fW4ujMkfuVxxvPCq8bDE=
dependencies:
"@algolia/client-common" "4.8.2"
"@algolia/requester-common" "4.8.2"
"@algolia/transporter" "4.8.2"
"@algolia/logger-common@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/logger-common/download/@algolia/logger-common-4.8.2.tgz#7cecc05b2725f3d68babdc26aed24f2fb60bc4bf"
integrity sha1-fOzAWycl89aLq9wmrtJPL7YLxL8=
"@algolia/logger-console@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/logger-console/download/@algolia/logger-console-4.8.2.tgz#65aca402330f20a23551e63014dc3ff5586ce39b"
integrity sha1-ZaykAjMPIKI1UeYwFNw/9Vhs45s=
dependencies:
"@algolia/logger-common" "4.8.2"
"@algolia/requester-browser-xhr@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/requester-browser-xhr/download/@algolia/requester-browser-xhr-4.8.2.tgz#4f3396aa842e30a8c76916640086a96135b245a9"
integrity sha1-TzOWqoQuMKjHaRZkAIapYTWyRak=
dependencies:
"@algolia/requester-common" "4.8.2"
"@algolia/requester-common@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/requester-common/download/@algolia/requester-common-4.8.2.tgz#42bb83c4d90f9a19ad4728524a763264723cbed4"
integrity sha1-QruDxNkPmhmtRyhSSnYyZHI8vtQ=
"@algolia/requester-node-http@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/requester-node-http/download/@algolia/requester-node-http-4.8.2.tgz#29e7a8404193dcf686b16909f1fce9f96f0ba150"
integrity sha1-KeeoQEGT3PaGsWkJ8fzp+W8LoVA=
dependencies:
"@algolia/requester-common" "4.8.2"
"@algolia/transporter@4.8.2":
version "4.8.2"
resolved "https://registry.npm.taobao.org/@algolia/transporter/download/@algolia/transporter-4.8.2.tgz#1c401e209d54a20296249158c64ad263891ee057"
integrity sha1-HEAeIJ1UogKWJJFYxkrSY4ke4Fc=
dependencies:
"@algolia/cache-common" "4.8.2"
"@algolia/logger-common" "4.8.2"
"@algolia/requester-common" "4.8.2"
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4", "@babel/code-frame@^7.8.3":
version "7.10.4"
resolved "https://registry.npm.taobao.org/@babel/code-frame/download/@babel/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
@ -899,6 +1022,29 @@
exec-sh "^0.3.2"
minimist "^1.2.0"
"@docsearch/css@3.0.0-alpha.31":
version "3.0.0-alpha.31"
resolved "https://registry.npm.taobao.org/@docsearch/css/download/@docsearch/css-3.0.0-alpha.31.tgz#5fb8595201b3f120e5bc4ae4d536068b1a5ca695"
integrity sha1-X7hZUgGz8SDlvErk1TYGixpcppU=
"@docsearch/js@3.0.0-alpha.31":
version "3.0.0-alpha.31"
resolved "https://registry.npm.taobao.org/@docsearch/js/download/@docsearch/js-3.0.0-alpha.31.tgz#a180cb0b790682f41ac2a8e352e84bdd5a6a2807"
integrity sha1-oYDLC3kGgvQawqjjUuhL3VpqKAc=
dependencies:
"@docsearch/react" "3.0.0-alpha.31"
preact "^10.0.0"
"@docsearch/react@3.0.0-alpha.31":
version "3.0.0-alpha.31"
resolved "https://registry.npm.taobao.org/@docsearch/react/download/@docsearch/react-3.0.0-alpha.31.tgz#3389cf4c7695e3a87a14b86e462ae08a8e08b84e"
integrity sha1-M4nPTHaV46h6FLhuRirgio4IuE4=
dependencies:
"@algolia/autocomplete-core" "^1.0.0-alpha.35"
"@algolia/autocomplete-preset-algolia" "^1.0.0-alpha.35"
"@docsearch/css" "3.0.0-alpha.31"
algoliasearch "^4.0.0"
"@eslint/eslintrc@^0.2.1":
version "0.2.1"
resolved "https://registry.npm.taobao.org/@eslint/eslintrc/download/@eslint/eslintrc-0.2.1.tgz#f72069c330461a06684d119384435e12a5d76e3c"
@ -2195,6 +2341,26 @@ ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.2, ajv@^6.12.4, ajv@^6.12.5, ajv
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"
algoliasearch@^4.0.0:
version "4.8.2"
resolved "https://registry.npm.taobao.org/algoliasearch/download/algoliasearch-4.8.2.tgz#499d187afe2169bc66c921cf7d5271c3fe0b3598"
integrity sha1-SZ0Yev4habxmySHPfVJxw/4LNZg=
dependencies:
"@algolia/cache-browser-local-storage" "4.8.2"
"@algolia/cache-common" "4.8.2"
"@algolia/cache-in-memory" "4.8.2"
"@algolia/client-account" "4.8.2"
"@algolia/client-analytics" "4.8.2"
"@algolia/client-common" "4.8.2"
"@algolia/client-recommendation" "4.8.2"
"@algolia/client-search" "4.8.2"
"@algolia/logger-common" "4.8.2"
"@algolia/logger-console" "4.8.2"
"@algolia/requester-browser-xhr" "4.8.2"
"@algolia/requester-common" "4.8.2"
"@algolia/requester-node-http" "4.8.2"
"@algolia/transporter" "4.8.2"
ansi-align@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb"
@ -8181,6 +8347,11 @@ postcss@^7.0.0, postcss@^7.0.14, postcss@^7.0.16, postcss@^7.0.2, postcss@^7.0.2
source-map "^0.6.1"
supports-color "^6.1.0"
preact@^10.0.0:
version "10.5.7"
resolved "https://registry.npm.taobao.org/preact/download/preact-10.5.7.tgz#f1d84725539e18f7ccbea937cf3db5895661dbd3"
integrity sha1-8dhHJVOeGPfMvqk3zz21iVZh29M=
prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"