Skip to content

Commit

Permalink
feat($plugin-search): searchable paths with test RegExp (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsontenorio authored and ulivz committed Dec 12, 2018
1 parent 1ac172c commit d6bddf1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
21 changes: 20 additions & 1 deletion packages/@vuepress/plugin-search/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</template>

<script>
/* global SEARCH_MAX_SUGGESTIONS */
/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS */
export default {
data () {
return {
Expand Down Expand Up @@ -76,6 +76,12 @@ export default {
if (this.getPageLocalePath(p) !== localePath) {
continue
}
// filter out results that do not match searchable paths
if (!this.isSearchable(p)) {
continue
}
if (matches(p)) {
res.push(p)
} else if (p.headers) {
Expand Down Expand Up @@ -112,6 +118,19 @@ export default {
return '/'
},
isSearchable (page) {
let searchPaths = SEARCH_PATHS
// all paths searchables
if (searchPaths === null) { return true }
searchPaths = Array.isArray(searchPaths) ? searchPaths : new Array(searchPaths)
return searchPaths.filter(path => {
return page.path.match(path)
}).length > 0
},
onUp () {
if (this.showSuggestions) {
if (this.focusIndex > 0) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@vuepress/plugin-search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = (options) => ({
path.resolve(__dirname, 'SearchBox.vue')
},
define: {
SEARCH_MAX_SUGGESTIONS: options.searchMaxSuggestions || 5
SEARCH_MAX_SUGGESTIONS: options.searchMaxSuggestions || 5,
SEARCH_PATHS: options.test || null
}
})
3 changes: 2 additions & 1 deletion packages/@vuepress/theme-blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module.exports = {
'@vuepress/pagination',
'@vuepress/medium-zoom',
['@vuepress/search', {
searchMaxSuggestions: 10
searchMaxSuggestions: 10,
test: null
}]
]
}
32 changes: 31 additions & 1 deletion packages/docs/docs/plugin/official/plugin-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Note that this plugin has been included in **default theme**, the search box you
module.exports = {
plugins: [
['@vuepress/search', {
searchMaxSuggestions: 10
searchMaxSuggestions: 10
}]
]
}
Expand Down Expand Up @@ -65,6 +65,36 @@ export default {

Set the maximum number of results for search.

### test

- Type: `RegExp` | `Array<RegExp>`
- Default: `null`

Set up searchable paths with regular expressions. If no test expression is provided it will search on all paths. Considering you have this structure:

```bash
docs/
β”œβ”€β”€ .vuepress/
β”‚ └── ...
β”‚
β”œβ”€β”€ master/
β”‚ └── ...
β”‚
β”œβ”€β”€ 1.0/
β”‚ └── ...
β”‚
└── 2.0/
└── ...
```

You can set up searchable paths with `test` as:

- RegExp: `'/1\.0/'`
- Array of RegExp: `['/1\.0/', '/2\.0/']`


Otherwise, the default search will return duplicates, once you can have similar content between folders `/master/`, `/1.0/` and `/2.0/`.

## Tips

### Tweak the default colors.
Expand Down

0 comments on commit d6bddf1

Please sign in to comment.