Skip to content

feat: add option to exclude paths from search #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions src/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,7 +76,7 @@ export default {
return
}

const result = this.index
const unfilteredResult = this.index
.search(query, SEARCH_MAX_SUGGESTIONS)
.map((page) => {
return {
Expand All @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}