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
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
13 changes: 5 additions & 8 deletions packages/@vuepress/plugin-search/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
</template>

<script>
import matchQuery from './match-query'

/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS, SEARCH_HOTKEYS */
export default {
name: 'SearchBox',
Expand Down Expand Up @@ -76,11 +78,6 @@ 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 res = []
for (let i = 0; i < pages.length; i++) {
if (res.length >= max) break
Expand All @@ -95,13 +92,13 @@ export default {
continue
}

if (matches(p)) {
if (matchQuery(query, p)) {
res.push(p)
} else if (p.headers) {
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 && matchQuery(query, p, h.title)) {
res.push(Object.assign({}, p, {
path: p.path + '#' + h.slug,
header: h
Expand Down Expand Up @@ -227,7 +224,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
42 changes: 42 additions & 0 deletions packages/@vuepress/plugin-search/__tests__/matchQuery.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import matchQuery from '../match-query'

describe('matchQuery', () => {
const page = {
title: 'HoMe PaGe',
frontmatter: {
tags: ['vuepress', 'is', 'jUst AwEsOme']
}
}

test('should match when query includes part of the page title', () => {
const query = 'hom'

const match = matchQuery(query, page)

expect(match).toBe(true)
})

test('should match when query includes the full page title', () => {
const query = 'home page'

const match = matchQuery(query, page)

expect(match).toBe(true)
})

test('should match when query includes a tag', () => {
const query = 'vuepress'

const match = matchQuery(query, page)

expect(match).toBe(true)
})

test('should match when query includes part of tag', () => {
const query = 'just aw'

const match = matchQuery(query, page)

expect(match).toBe(true)
})
})
42 changes: 42 additions & 0 deletions packages/@vuepress/plugin-search/match-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import get from 'lodash/get'

export default (query, page, additionalStr = null) => {
let domain = get(page, 'title', '')

if (get(page, 'frontmatter.tags')) {
domain += ` ${page.frontmatter.tags.join(' ')}`
}

if (additionalStr) {
domain += ` ${additionalStr}`
}

return matchTest(query, domain)
}

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

const words = query
.split(/\s+/g)
.map(str => str.trim())
.filter(str => !!str)
const hasTrailingSpace = query.endsWith(' ')
const searchRegex = new RegExp(
words
.map((word, index) => {
if (words.length === index + 1 && !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)
}

7 changes: 7 additions & 0 deletions packages/docs/docs/guide/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ lang = "en-US"

Title of current page.

### tags

- Type: `array`
- Default: `undefined`

You can use tags to improve [built-in search](/theme/default-theme-config.html#built-in-search).

### lang

- Type: `string`
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