|
1 | 1 | # vuepress-plugin-fulltext-search
|
2 | 2 |
|
3 |
| -Add full-text search capabilities to your [VuePress](https://vuepress.vuejs.org/) website using the [Flexsearch](https://github.com/nextapps-de/flexsearch) library. |
| 3 | +Add full-text search capabilities to your [VuePress](https://vuepress.vuejs.org/) website using the |
| 4 | +[Flexsearch](https://github.com/nextapps-de/flexsearch) library. |
4 | 5 |
|
5 | 6 | Many thanks to [Ahmad Mostafa](https://ahmadmostafa.com/2019/12/09/build-better-search-in-vuepress-site/) for the idea.
|
6 | 7 |
|
@@ -29,79 +30,86 @@ And that is it! Just compile your app and see for yourself.
|
29 | 30 | Webpack alias `@SearchBox` will be replaced with plugin's implementation, so it should work automatically with any
|
30 | 31 | VuePress theme.
|
31 | 32 |
|
32 |
| -### Functions |
33 |
| - |
34 |
| -You can define functions to hook into various seach features. The functions that can be implemented are as follows: |
| 33 | +### Search parameters |
35 | 34 |
|
36 |
| -```ts |
37 |
| -/** |
38 |
| - * Augment, adjust, or manipulate the suggestions shown to users. |
39 |
| - */ |
40 |
| -async function processSuggestions(suggestions: Suggestion[], queryString: string, queryTerms: string[]): Suggestion[] |
| 35 | +The `query` URL search parameter can be provided to automatically populate and focus the search box. This is useful for |
| 36 | +adding your VuePress website as a custom search engine in browsers. For example: |
41 | 37 |
|
42 |
| -/** |
43 |
| - * Callback function to call when a user clicks on a suggestion. |
44 |
| - */ |
45 |
| -async function onGoToSuggestion(index: number, suggestion: Suggestion, queryString: string, queryTerms: string[]) |
| 38 | +```none |
| 39 | +https://your-website.com?query=hello+world |
46 | 40 | ```
|
47 | 41 |
|
48 |
| -Functions are provided to the plugin like so: |
| 42 | +### Excluding pages from search |
| 43 | + |
| 44 | +You can exclude pages from search suggestions by adding `search: false` to a page's frontmatter: |
49 | 45 |
|
50 |
| -```js |
51 |
| -// docs/.vuepress/config.js |
52 |
| -const fs = require('fs'); |
53 |
| -const { path } = require('@vuepress/shared-utils'); |
| 46 | +```none |
| 47 | +--- |
| 48 | +search: false |
| 49 | +--- |
54 | 50 |
|
55 |
| -module.exports = { |
56 |
| - plugins: [ |
57 |
| - ['fulltext-search', { |
58 |
| - // provide the contents of a JavaScript file |
59 |
| - functions: fs.readFileSync(path.resolve(__dirname, 'fulltextSearchFunctions.js')), |
60 |
| - }], |
61 |
| - ] |
62 |
| -} |
| 51 | +<!-- page content --> |
63 | 52 | ```
|
64 | 53 |
|
65 |
| -For example, in `fulltextSearchFunctions.js`, you might have: |
| 54 | +### Hooks |
66 | 55 |
|
67 |
| -```js |
68 |
| -// docs/.vuepress/fulltextSearchFunctions.js |
69 |
| -export async function processSuggestions(suggestions, queryString, queryTerms) { |
70 |
| - if (queryString) { |
71 |
| - // add a suggestion to start a search in an external service |
72 |
| - suggestions.push({ |
73 |
| - path: 'https://sourcegraph.com/search?patternType=literal&q=', |
74 |
| - slug: queryString, |
75 |
| - parentPageTitle: 'Sourcegraph', |
76 |
| - title: 'Search code', |
77 |
| - contentStr: 'Search for "' + queryString + '" on Sourcegraph', |
78 |
| - external: true, |
79 |
| - }); |
80 |
| - } |
81 |
| - return suggestions; |
82 |
| -} |
| 56 | +You can define several hooks to customize behaviour of various search features. For example: |
83 | 57 |
|
84 |
| -export async function onGoToSuggestion() { |
85 |
| - // create an analytics event |
| 58 | +```js |
| 59 | +// /docs/.vuepress/searchHooks.js |
| 60 | +export default { |
| 61 | + async processSuggestions(suggestions, queryString, queryTerms) { |
| 62 | + if (queryString) { |
| 63 | + // add a suggestion to start a search in an external service |
| 64 | + suggestions.push({ |
| 65 | + path: 'https://sourcegraph.com/search?patternType=literal&q=', |
| 66 | + slug: queryString, |
| 67 | + parentPageTitle: 'Sourcegraph', |
| 68 | + title: 'Search code', |
| 69 | + contentStr: 'Search for "' + queryString + '" on Sourcegraph', |
| 70 | + external: true, |
| 71 | + }) |
| 72 | + } |
| 73 | + return suggestions |
| 74 | + }, |
| 75 | + |
| 76 | + async onGoToSuggestion(index, suggestion, queryString, queryTerms) { |
| 77 | + // e.g. create an analytics event |
| 78 | + |
| 79 | + // return true if you want to prevent default navigation |
| 80 | + return true |
| 81 | + }, |
86 | 82 | }
|
87 |
| -``` |
88 | 83 |
|
89 |
| -### Search parameters |
90 |
| - |
91 |
| -The `query` URL search parameter can be provided to automatically populate and focus the search box. This is useful for adding your VuePress website as a custom search engine in browsers. For example: |
| 84 | +// /docs/.vuepress/config.js |
| 85 | +// Important: Because of the way Vuepress build works, you cannot use regular import/require, |
| 86 | +// code must be provided as plaintext. Hence syntax below is required with fs.readFileSync |
| 87 | +const fs = require('fs') |
| 88 | +const { path } = require('@vuepress/shared-utils') |
92 | 89 |
|
93 |
| -```none |
94 |
| -https://your-website.com?query=hello+world |
| 90 | +module.exports = { |
| 91 | + plugins: [ |
| 92 | + [ |
| 93 | + 'fulltext-search', |
| 94 | + { |
| 95 | + // provide the contents of a JavaScript file |
| 96 | + hooks: fs.readFileSync(path.resolve(__dirname, './searchHooks.js')), |
| 97 | + }, |
| 98 | + ], |
| 99 | + ], |
| 100 | +} |
95 | 101 | ```
|
96 | 102 |
|
97 |
| -### Excluding pages from search |
98 |
| - |
99 |
| -You can exclude pages from search suggestions by adding `search: false` to a page's fontmatter: |
| 103 | +Supported hooks are: |
100 | 104 |
|
101 |
| -```none |
102 |
| ---- |
103 |
| -search: false |
104 |
| ---- |
| 105 | +```ts |
| 106 | +/** |
| 107 | + * Augment, adjust, or manipulate the suggestions shown to users. |
| 108 | + */ |
| 109 | +async function processSuggestions(suggestions: Suggestion[], queryString: string, queryTerms: string[]): Suggestion[] |
105 | 110 |
|
106 |
| -<!-- page content --> |
| 111 | +/** |
| 112 | + * Callback function to call a suggestion. |
| 113 | + */ |
| 114 | +async function onGoToSuggestion(index: number, suggestion: Suggestion, queryString: string, queryTerms: string[]): Boolean? |
107 | 115 | ```
|
0 commit comments