From 3602715194ec27dfd55529c43c3dfc9e907309d2 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Thu, 17 Mar 2022 10:56:04 +0800 Subject: [PATCH] wip: searchPgae --- .../components/DcloudSearchPage/index.vue | 242 ++++++++++++++++++ .../DcloudSearchPage/searchClient.js | 96 +++++++ docs/.vuepress/theme/components/Navbar.vue | 28 +- .../.vuepress/theme/components/NavbarLogo.vue | 24 ++ docs/.vuepress/theme/layouts/Layout.vue | 9 +- docs/.vuepress/theme/util/index.js | 2 + docs/.vuepress/theme/util/searchUtils.js | 39 +++ 7 files changed, 415 insertions(+), 25 deletions(-) create mode 100644 docs/.vuepress/theme/components/DcloudSearchPage/index.vue create mode 100644 docs/.vuepress/theme/components/DcloudSearchPage/searchClient.js create mode 100644 docs/.vuepress/theme/components/NavbarLogo.vue create mode 100644 docs/.vuepress/theme/util/searchUtils.js diff --git a/docs/.vuepress/theme/components/DcloudSearchPage/index.vue b/docs/.vuepress/theme/components/DcloudSearchPage/index.vue new file mode 100644 index 000000000..1d7788806 --- /dev/null +++ b/docs/.vuepress/theme/components/DcloudSearchPage/index.vue @@ -0,0 +1,242 @@ + + + + + diff --git a/docs/.vuepress/theme/components/DcloudSearchPage/searchClient.js b/docs/.vuepress/theme/components/DcloudSearchPage/searchClient.js new file mode 100644 index 000000000..7c117c868 --- /dev/null +++ b/docs/.vuepress/theme/components/DcloudSearchPage/searchClient.js @@ -0,0 +1,96 @@ +import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser'; +import { removeHighlightTags, groupBy } from '../../util' + +let searchClient +function createSearchClient(appId, apiKey) { + if (searchClient) return searchClient + searchClient = algoliasearch(appId, apiKey); + searchClient.addAlgoliaAgent('dcloudsearch', '1.0.0'); + + return searchClient +} + +export function search({ query, indexName, appId, apiKey, searchParameters = {}, snippetLength = 0, transformItems = () => { }, ...args }) { + return createSearchClient(appId, apiKey) + .search([ + { + query, + indexName, + params: { + attributesToRetrieve: [ + 'hierarchy.lvl0', + 'hierarchy.lvl1', + 'hierarchy.lvl2', + 'hierarchy.lvl3', + 'hierarchy.lvl4', + 'hierarchy.lvl5', + 'hierarchy.lvl6', + 'content', + 'type', + 'url', + ], + attributesToSnippet: [ + `hierarchy.lvl1:${snippetLength}`, + `hierarchy.lvl2:${snippetLength}`, + `hierarchy.lvl3:${snippetLength}`, + `hierarchy.lvl4:${snippetLength}`, + `hierarchy.lvl5:${snippetLength}`, + `hierarchy.lvl6:${snippetLength}`, + `content:${snippetLength}`, + ], + snippetEllipsisText: '…', + highlightPreTag: '', + highlightPostTag: '', + hitsPerPage: 20, + ...args, + ...searchParameters, + }, + }, + ]) + .catch((error) => { + throw error; + }) + .then(({ results }) => { + const { hits, nbHits } = results[0]; + const sources = groupBy(hits, (hit) => removeHighlightTags(hit)); + + return Object.values(sources).map( + (items, index) => { + return { + sourceId: `hits${index}`, + onSelect({ item, event }) { + // saveRecentSearch(item); + + // if (!event.shiftKey && !event.ctrlKey && !event.metaKey) { + // onClose(); + // } + }, + getItemUrl({ item }) { + return item.url; + }, + getItems() { + return Object.values( + groupBy(items, (item) => item.hierarchy.lvl1) + ) + .map(transformItems) + .map((groupedHits) => + groupedHits.map((item) => { + return { + ...item, + __docsearch_parent: + item.type !== 'lvl1' && + groupedHits.find( + (siblingItem) => + siblingItem.type === 'lvl1' && + siblingItem.hierarchy.lvl1 === + item.hierarchy.lvl1 + ), + }; + }) + ).flat(); + }, + }; + } + ); + }); +} \ No newline at end of file diff --git a/docs/.vuepress/theme/components/Navbar.vue b/docs/.vuepress/theme/components/Navbar.vue index 985bb46f3..1d1228992 100644 --- a/docs/.vuepress/theme/components/Navbar.vue +++ b/docs/.vuepress/theme/components/Navbar.vue @@ -3,29 +3,7 @@