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

feat: Improved the Native Search Algorithm #1557

Merged
merged 9 commits into from
Mar 8, 2020
Merged
46 changes: 39 additions & 7 deletions packages/@vuepress/plugin-search/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,43 @@ export default {
const { pages } = this.$site
const max = this.$site.themeConfig.searchMaxSuggestions || SEARCH_MAX_SUGGESTIONS
const localePath = this.$localePath
const matches = item => (
item
&& item.title
&& item.title.toLowerCase().indexOf(query) > -1
)

const matchTest = (q, domain) => {
const escapeRegExp = (s) => {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}

const words = q
.split(/\s+/g)
.map(s => s.trim())
.filter(s => !!s)
const hasTrailingSpace = q.endsWith(' ')
const searchRegex = new RegExp(
words
.map((word, i) => {
if (i + 1 === words.length && !hasTrailingSpace) {
// The last word - ok with the word being "startswith"-like
return `(?=.*\\b${escapeRegExp(word)})`
} else {
// Not the last word - expect the whole word exactly
return `(?=.*\\b${escapeRegExp(word)}\\b)`
}
})
.join('') + '.+',
'gi'
)
return searchRegex.test(domain)
}

const matches = (item, additionalStr = null) => {
let domain
domain = (item.title) ? item.title : ''
domain = (item.frontmatter && item.frontmatter.tags) ? domain + ' ' + item.frontmatter.tags.join(' ') : domain
if (additionalStr) domain = domain + ' ' + additionalStr

return matchTest(query, domain)
}

const res = []
for (let i = 0; i < pages.length; i++) {
if (res.length >= max) break
Expand All @@ -101,7 +133,7 @@ export default {
for (let j = 0; j < p.headers.length; j++) {
if (res.length >= max) break
const h = p.headers[j]
if (matches(h)) {
if (h.title && matches(p, h.title)) {
res.push(Object.assign({}, p, {
path: p.path + '#' + h.slug,
header: h
Expand Down Expand Up @@ -227,7 +259,7 @@ export default {
background #fff
width 20rem
position absolute
top 1.5rem
top 2 rem
border 1px solid darken($borderColor, 10%)
border-radius 6px
padding 0.4rem
Expand Down
13 changes: 11 additions & 2 deletions packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,19 @@ search: false
```

::: tip
Built-in Search only builds index from the title, `h2` and `h3` headers, if you need full text search, you can use [Algolia DocSearch](#algolia-docsearch).
Built-in Search only builds index from the title, `h2` and `h3` headers and any tags added with `YAML front matter` as shown below, if you need full text search, you can use [Algolia Search](#algolia-search).
:::

### Algolia DocSearch
```yaml
---
tags:
- configuration
- theme
- indexing
---
```

### Algolia Search

The `themeConfig.algolia` option allows you to use [Algolia DocSearch](https://community.algolia.com/docsearch/) to replace the simple built-in search. To enable it, you need to provide at least `apiKey` and `indexName`:

Expand Down