Skip to content
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

@vuepress-seach / Searchable paths #1032

Merged
merged 3 commits into from
Dec 12, 2018
Merged
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
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
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code here can be simplified as:

isSearchable (page) {
  const searchPaths = SEARCH_PATHS
  // all paths searchables
  if (searchPaths.length === 0) { return true }

  return searchPaths.filter(path => {
    return page.path.indexOf(path) !== -1
  }).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