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 1 commit
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
25 changes: 24 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,23 @@ export default {
return '/'
},

isSearchable (page) {
const searchPaths = SEARCH_PATHS

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

let searchable = false

searchPaths.forEach((path) => {
if (page.path.indexOf(path) !== -1) {
searchable = true
return
}
})
return searchable
},

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.searchPaths || []
}
})
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,
searchPaths: []
}]
]
}
24 changes: 23 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,8 @@ Note that this plugin has been included in **default theme**, the search box you
module.exports = {
plugins: [
['@vuepress/search', {
searchMaxSuggestions: 10
searchMaxSuggestions: 10,
searchPaths: []
}]
]
}
Expand Down Expand Up @@ -65,6 +66,27 @@ export default {

Set the maximum number of results for search.

### searchPaths

- Type: `array`
- Default: []

Set the array of searchable paths. Default value (a empty array) will search on all paths. Considering you have this structure:

```bash
docs/
├── .vuepress/
│ └── ...
├── 1.0/
│ └── ...
└── 2.0/
└── ...
```

You can restrict the searching setting with `searchPaths` as `['/1.0/']`. Otherwise, the default search will return duplications, once you can have similar content between folders `/1.0/` and `/2.0/`.

## Tips

### Tweak the default colors.
Expand Down