-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($plugin-search): improve the native search algorithm (#1557)
- Loading branch information
Showing
5 changed files
with
107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/@vuepress/plugin-search/__tests__/matchQuery.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters