From 3cbf4fe9d131bc4f0d4c8af45ec9c6b083aec99f Mon Sep 17 00:00:00 2001 From: David Baum Date: Sat, 5 Mar 2022 16:43:04 +0100 Subject: [PATCH] add option to exclude paths from search --- README.md | 1 + index.js | 1 + src/SearchBox.vue | 10 ++++++++-- src/utils.js | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 32904dc..525ab0f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ module.exports = { */ maxSuggestions: 10, // how many search suggestions to show on the menu, the default is 10. searchPaths: ['path1', 'path2'], // an array of paths to search in, keep it null to search all docs. + searchExcludePaths: ['pathToExclude1', 'pathToExclude1'], // an array of paths to exclude. searchHotkeys: ['s'], // Hot keys to activate the search input, the default is "s" but you can add more. searchResultLength: 60, // the length of the suggestion result text by characters, the default is 60 characters. splitHighlightedWords: ' ', // regex or string to split highlighted words by, keep it null to use flexsearch.split diff --git a/index.js b/index.js index c183b97..bed3632 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ module.exports = (options) => ({ SEARCH_OPTIONS: options.search_options || defaultSearchOptions, SEARCH_MAX_SUGGESTIONS: options.maxSuggestions || 10, SEARCH_PATHS: options.searchPaths || null, + SEARCH_EXCLUDE_PATHS: options.searchExcludePaths || null, SEARCH_HOTKEYS: options.searchHotkeys || "s", SEARCH_RESULT_LENGTH: Number(options.searchResultLength) || DEFAULT_SEARCH_RESULT_LENGTH, diff --git a/src/SearchBox.vue b/src/SearchBox.vue index 5ef68a8..74fac04 100644 --- a/src/SearchBox.vue +++ b/src/SearchBox.vue @@ -49,10 +49,12 @@ import VuepressSearchBox from "@vuepress/plugin-search/SearchBox.vue"; import Flexsearch from "flexsearch"; import { highlightText } from "./utils"; +import { isSearchable } from "./utils"; /* global SEARCH_MAX_SUGGESTIONS SEARCH_PATHS +SEARCH_EXCLUDE_PATHS SEARCH_HOTKEYS SEARCH_OPTIONS SEARCH_RESULT_LENGTH @@ -74,7 +76,7 @@ export default { return } - const result = this.index + const unfilteredResult = this.index .search(query, SEARCH_MAX_SUGGESTIONS) .map((page) => { return { @@ -84,7 +86,11 @@ export default { }; }); - return result; + if(SEARCH_EXCLUDE_PATHS === null) { + return unfilteredResult; + } else { + return unfilteredResult.filter(page => isSearchable(page, SEARCH_EXCLUDE_PATHS)); + } }, splitBy() { diff --git a/src/utils.js b/src/utils.js index 2bdd5f1..de54f89 100644 --- a/src/utils.js +++ b/src/utils.js @@ -43,3 +43,20 @@ module.exports.highlightText = (fullText, highlightTarget, splitBy) => { return result; }; +/** + * Returns whether the given page is searchable, i.e. it's path is not excluded. + * + * @param page + * @param excludePaths Array of excluded paths + * @returns {boolean} + */ +module.exports.isSearchable = (page, excludePaths) => { + if(excludePaths === null) { + return true; + } + return excludePaths.filter(path => { + return page.path.match(path) + }).length === 0 +} + +