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

Improved the Native Search Algorithm #1

Merged
merged 3 commits into from
Apr 24, 2019
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
46 changes: 40 additions & 6 deletions packages/@vuepress/plugin-search/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,44 @@ export default {
const { pages } = this.$site
const max = SEARCH_MAX_SUGGESTIONS
const localePath = this.$localePath
const matches = 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 @@ -88,7 +122,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 @@ -198,7 +232,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
11 changes: 10 additions & 1 deletion packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,18 @@ 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 Search](#algolia-search).
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).
:::

```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