Skip to content

Commit a61cb0b

Browse files
author
Leonid Buneev
committed
Change naming for functionality added in leo-buneev#10
1 parent e186625 commit a61cb0b

File tree

7 files changed

+1653
-1256
lines changed

7 files changed

+1653
-1256
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## Unreleased
6+
7+
## 2.1.0 (2020-10-22)
8+
9+
### Added
10+
11+
- Support for Hiragana and Katakana in Japanese. (@shout-star in #20)
12+
- Support for hooks functions (@bobheadxi in #10)
13+
- Ability to set search query from URL parameter (@bobheadxi in #10)
14+
- Ability to exclude page from search (@bobheadxi in #10)

README.md

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# vuepress-plugin-fulltext-search
22

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.
45

56
Many thanks to [Ahmad Mostafa](https://ahmadmostafa.com/2019/12/09/build-better-search-in-vuepress-site/) for the idea.
67

@@ -29,79 +30,86 @@ And that is it! Just compile your app and see for yourself.
2930
Webpack alias `@SearchBox` will be replaced with plugin's implementation, so it should work automatically with any
3031
VuePress theme.
3132

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
3534

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:
4137

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
4640
```
4741

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:
4945

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+
---
5450
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 -->
6352
```
6453

65-
For example, in `fulltextSearchFunctions.js`, you might have:
54+
### Hooks
6655

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:
8357

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+
},
8682
}
87-
```
8883

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')
9289

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+
}
95101
```
96102

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:
100104

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[]
105110

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?
107115
```

components/SearchBox.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import flexsearchSvc from '../services/flexsearchSvc'
4444
4545
// see https://vuepress.vuejs.org/plugin/option-api.html#clientdynamicmodules
46-
import * as functions from '@dynamic/functions'
46+
import hooks from '@dynamic/hooks'
4747
4848
/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS, SEARCH_HOTKEYS */
4949
export default {
@@ -119,8 +119,8 @@ export default {
119119
)
120120
121121
// augment suggestions with user-provided function
122-
if (functions && functions.processSuggestions) {
123-
this.suggestions = await functions.processSuggestions(suggestions, this.query, this.queryTerms)
122+
if (hooks.processSuggestions) {
123+
this.suggestions = await hooks.processSuggestions(suggestions, this.query, this.queryTerms)
124124
} else {
125125
this.suggestions = suggestions
126126
}
@@ -174,8 +174,9 @@ export default {
174174
if (!this.showSuggestions) {
175175
return
176176
}
177-
if (functions && functions.onGoToSuggestion) {
178-
functions.onGoToSuggestion(i, this.suggestions[i], this.query, this.queryTerms)
177+
if (hooks.onGoToSuggestion) {
178+
const result = hooks.onGoToSuggestion(i, this.suggestions[i], this.query, this.queryTerms)
179+
if (result === true) return
179180
}
180181
if (this.suggestions[i].external) {
181182
window.open(this.suggestions[i].path + this.suggestions[i].slug, '_blank')
@@ -206,7 +207,7 @@ export default {
206207
return null
207208
}
208209
return new URLSearchParams(window.location.search)
209-
}
210+
},
210211
},
211212
}
212213
</script>

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ module.exports = options => ({
3434
},
3535
clientDynamicModules() {
3636
return {
37-
name: 'functions.js',
38-
content: options.functions || 'export default null',
37+
name: 'hooks.js',
38+
content: options.hooks || 'export default {}',
3939
}
40-
}
40+
},
4141
})
4242

4343
function getCharsets(text) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"vuepress": "^1.4.0"
1717
},
1818
"devDependencies": {
19+
"cross-env": "^7.0.2",
1920
"eslint-plugin-tyrecheck": "^2.10.3"
2021
}
2122
}

services/flexsearchSvc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const cjkRegex = /[\u3131-\u314e|\u314f-\u3163|\uac00-\ud7a3]|[\u4E00-\u9FCC\u34
1313

1414
export default {
1515
buildIndex(allPages) {
16-
const pages = allPages.filter((p) => !p.frontmatter || (p.frontmatter.search !== false))
16+
const pages = allPages.filter(p => !p.frontmatter || p.frontmatter.search !== false)
1717
const indexSettings = {
1818
tokenize: 'forward',
1919
async: true,

0 commit comments

Comments
 (0)