Skip to content

Commit d6bddf1

Browse files
robsontenorioulivz
authored andcommitted
feat($plugin-search): searchable paths with test RegExp (#1032)
1 parent 1ac172c commit d6bddf1

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

β€Žpackages/@vuepress/plugin-search/SearchBox.vue

+20-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</template>
3737

3838
<script>
39-
/* global SEARCH_MAX_SUGGESTIONS */
39+
/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS */
4040
export default {
4141
data () {
4242
return {
@@ -76,6 +76,12 @@ export default {
7676
if (this.getPageLocalePath(p) !== localePath) {
7777
continue
7878
}
79+
80+
// filter out results that do not match searchable paths
81+
if (!this.isSearchable(p)) {
82+
continue
83+
}
84+
7985
if (matches(p)) {
8086
res.push(p)
8187
} else if (p.headers) {
@@ -112,6 +118,19 @@ export default {
112118
return '/'
113119
},
114120
121+
isSearchable (page) {
122+
let searchPaths = SEARCH_PATHS
123+
124+
// all paths searchables
125+
if (searchPaths === null) { return true }
126+
127+
searchPaths = Array.isArray(searchPaths) ? searchPaths : new Array(searchPaths)
128+
129+
return searchPaths.filter(path => {
130+
return page.path.match(path)
131+
}).length > 0
132+
},
133+
115134
onUp () {
116135
if (this.showSuggestions) {
117136
if (this.focusIndex > 0) {

β€Žpackages/@vuepress/plugin-search/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = (options) => ({
66
path.resolve(__dirname, 'SearchBox.vue')
77
},
88
define: {
9-
SEARCH_MAX_SUGGESTIONS: options.searchMaxSuggestions || 5
9+
SEARCH_MAX_SUGGESTIONS: options.searchMaxSuggestions || 5,
10+
SEARCH_PATHS: options.test || null
1011
}
1112
})

β€Žpackages/@vuepress/theme-blog/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44
'@vuepress/pagination',
55
'@vuepress/medium-zoom',
66
['@vuepress/search', {
7-
searchMaxSuggestions: 10
7+
searchMaxSuggestions: 10,
8+
test: null
89
}]
910
]
1011
}

β€Žpackages/docs/docs/plugin/official/plugin-search.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Note that this plugin has been included in **default theme**, the search box you
2727
module.exports = {
2828
plugins: [
2929
['@vuepress/search', {
30-
searchMaxSuggestions: 10
30+
searchMaxSuggestions: 10
3131
}]
3232
]
3333
}
@@ -65,6 +65,36 @@ export default {
6565

6666
Set the maximum number of results for search.
6767

68+
### test
69+
70+
- Type: `RegExp` | `Array<RegExp>`
71+
- Default: `null`
72+
73+
Set up searchable paths with regular expressions. If no test expression is provided it will search on all paths. Considering you have this structure:
74+
75+
```bash
76+
docs/
77+
β”œβ”€β”€ .vuepress/
78+
β”‚ └── ...
79+
β”‚
80+
β”œβ”€β”€ master/
81+
β”‚ └── ...
82+
β”‚
83+
β”œβ”€β”€ 1.0/
84+
β”‚ └── ...
85+
β”‚
86+
└── 2.0/
87+
└── ...
88+
```
89+
90+
You can set up searchable paths with `test` as:
91+
92+
- RegExp: `'/1\.0/'`
93+
- Array of RegExp: `['/1\.0/', '/2\.0/']`
94+
95+
96+
Otherwise, the default search will return duplicates, once you can have similar content between folders `/master/`, `/1.0/` and `/2.0/`.
97+
6898
## Tips
6999

70100
### Tweak the default colors.

0 commit comments

Comments
Β (0)