-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from smori1983/database-mixin
Extract database codes into database-mixin.js
- Loading branch information
Showing
3 changed files
with
74 additions
and
87 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import data from '@dynamic/vuepress-plugin-flexsearch/data'; | ||
|
||
import Database from './database'; | ||
import excerpt from './excerpt'; | ||
import ngram from '../tokenizer/ngram'; | ||
|
||
/* global FLEX_SEARCH_MAX_SUGGESTIONS */ | ||
/* global FLEX_SEARCH_EXCERPT_AROUND_LENGTH */ | ||
/* global FLEX_SEARCH_EXCERPT_HEAD_TEXT */ | ||
/* global FLEX_SEARCH_EXCERPT_TAIL_TEXT */ | ||
/* global FLEX_SEARCH_NGRAM_SIZE */ | ||
export default { | ||
data () { | ||
return { | ||
database: new Database(), | ||
}; | ||
}, | ||
|
||
methods: { | ||
databaseInit() { | ||
for (const locale in data) { | ||
// Consider the case locale defined but no pages created. | ||
this.database.add(locale, data[locale] || {}); | ||
} | ||
}, | ||
|
||
/** | ||
* @param {string} queryRow | ||
* @return {{title: string, path: string, excerpt: string}[]} | ||
*/ | ||
databaseSearch(queryRow) { | ||
const query = queryRow.trim(); | ||
|
||
if (query.length === 0) { | ||
return []; | ||
} | ||
|
||
const localePath = this.$localePath; | ||
const queryForSearch = ngram.createForSearch(query, FLEX_SEARCH_NGRAM_SIZE); | ||
const limit = this.$site.themeConfig.searchMaxSuggestions || FLEX_SEARCH_MAX_SUGGESTIONS; | ||
|
||
const matchedKeys = this.database.search(localePath, queryForSearch, limit); | ||
|
||
return matchedKeys.map((key) => { | ||
const page = data[localePath][key]; | ||
|
||
return { | ||
title: page.title, | ||
path: page.path, | ||
excerpt: excerpt.create(page.dataForExcerpt, query, { | ||
aroundLength: FLEX_SEARCH_EXCERPT_AROUND_LENGTH, | ||
headText: FLEX_SEARCH_EXCERPT_HEAD_TEXT, | ||
tailText: FLEX_SEARCH_EXCERPT_TAIL_TEXT, | ||
}), | ||
}; | ||
}); | ||
}, | ||
}, | ||
}; |