Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonzq committed Apr 30, 2020
2 parents 63d04c7 + 79df5c6 commit bbea71f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion assets/js/theme.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ enableEmoji = true
placeholder = ""
# max number of results length
maxResultLength = 10
# snippet length of the result
snippetLength = 30
# HTML tag name of the highlight part in results
highlightTag = "em"
[languages.en.params.search.algolia]
Expand Down Expand Up @@ -315,6 +317,8 @@ enableEmoji = true
placeholder = ""
# 最大结果数目
maxResultLength = 10
# 结果内容片段长度
snippetLength = 50
# 搜索结果中高亮部分的 HTML 标签
highlightTag = "em"
[languages.zh-cn.params.search.algolia]
Expand Down Expand Up @@ -505,6 +509,8 @@ enableEmoji = true
placeholder = ""
# max number of results length
maxResultLength = 10
# snippet length of the result
snippetLength = 30
# HTML tag name of the highlight part in results
highlightTag = "em"
[languages.fr.params.search.algolia]
Expand Down
4 changes: 4 additions & 0 deletions exampleSite/content/posts/theme-documentation-basics.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Please open the code block below to view the complete sample configuration :(far
placeholder = ""
# {{< version 0.2.1 >}} max number of results length
maxResultLength = 10
# {{< version 0.2.3 >}} snippet length of the result
snippetLength = 30
# {{< version 0.2.1 >}} HTML tag name of the highlight part in results
highlightTag = "em"
[params.search.algolia]
Expand Down Expand Up @@ -979,6 +981,8 @@ Here is the search configuration in your [site configuration](#site-configuratio
placeholder = ""
# {{< version 0.2.1 >}} max number of results length
maxResultLength = 10
# {{< version 0.2.3 >}} snippet length of the result
snippetLength = 30
# {{< version 0.2.1 >}} HTML tag name of the highlight part in results
highlightTag = "em"
[params.search.algolia]
Expand Down
4 changes: 4 additions & 0 deletions exampleSite/content/posts/theme-documentation-basics.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ Please open the code block below to view the complete sample configuration :(far
placeholder = ""
# {{< version 0.2.1 >}} max number of results length
maxResultLength = 10
# {{< version 0.2.3 >}} snippet length of the result
snippetLength = 30
# {{< version 0.2.1 >}} HTML tag name of the highlight part in results
highlightTag = "em"
[params.search.algolia]
Expand Down Expand Up @@ -984,6 +986,8 @@ Here is the search configuration in your [site configuration](#site-configuratio
placeholder = ""
# {{< version 0.2.1 >}} max number of results length
maxResultLength = 10
# {{< version 0.2.3 >}} snippet length of the result
snippetLength = 30
# {{< version 0.2.1 >}} HTML tag name of the highlight part in results
highlightTag = "em"
[params.search.algolia]
Expand Down
12 changes: 8 additions & 4 deletions exampleSite/content/posts/theme-documentation-basics.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,11 @@ hugo
contentLength = 4000
# 搜索框的占位提示语
placeholder = ""
# 最大结果数目
# {{< version 0.2.1 >}} 最大结果数目
maxResultLength = 10
# 搜索结果中高亮部分的 HTML 标签
# {{< version 0.2.3 >}} 结果内容片段长度
snippetLength = 50
# {{< version 0.2.1 >}} 搜索结果中高亮部分的 HTML 标签
highlightTag = "em"
[params.search.algolia]
index = ""
Expand Down Expand Up @@ -982,9 +984,11 @@ defaultContentLanguage = "zh-cn"
contentLength = 4000
# 搜索框的占位提示语
placeholder = ""
# 最大结果数目
# {{< version 0.2.1 >}} 最大结果数目
maxResultLength = 10
# 搜索结果中高亮部分的 HTML 标签
# {{< version 0.2.3 >}} 结果内容片段长度
snippetLength = 50
# {{< version 0.2.1 >}} 搜索结果中高亮部分的 HTML 标签
highlightTag = "em"
[params.search.algolia]
index = ""
Expand Down
18 changes: 9 additions & 9 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Theme {
initSearch() {
const searchConfig = this.config.search;
if (!searchConfig.maxResultLength) searchConfig.maxResultLength = 10;
if (!searchConfig.snippetLength) searchConfig.snippetLength = 50;
if (!searchConfig.highlightTag) searchConfig.highlightTag = 'em';
const isMobile = this.util.isMobile();
if (!searchConfig || isMobile && this._searchMobileOnce || !isMobile && this._searchDesktopOnce) return;
Expand Down Expand Up @@ -154,7 +155,6 @@ class Theme {
else $searchClear.style.display = 'inline';
}, false);

const CONTEXT_LENGTH = 200;
const initAutosearch = () => {
const autosearch = autocomplete(`#search-input-${classSuffix}`, {
hint: false,
Expand Down Expand Up @@ -188,12 +188,12 @@ class Theme {
if (matchPosition < position || position === 0) position = matchPosition;
}
});
position -= CONTEXT_LENGTH / 5;
position -= searchConfig.snippetLength / 5;
if (position > 0) {
position += context.substr(position, 25).lastIndexOf(' ') + 1;
context = '...' + context.substr(position, CONTEXT_LENGTH);
position += context.substr(position, 20).lastIndexOf(' ') + 1;
context = '...' + context.substr(position, searchConfig.snippetLength);
} else {
context = context.substr(0, CONTEXT_LENGTH);
context = context.substr(0, searchConfig.snippetLength);
}
Object.keys(metadata).forEach(key => {
title = title.replace(new RegExp(`(${key})`, 'gi'), `<${searchConfig.highlightTag}>$1</${searchConfig.highlightTag}>`);
Expand Down Expand Up @@ -238,24 +238,24 @@ class Theme {
this._algoliaIndex
.search(query, {
offset: 0,
length: searchConfig.maxResultLength * 5,
length: searchConfig.maxResultLength * 8,
attributesToHighlight: ['title'],
attributesToSnippet: ['content:30'],
attributesToSnippet: [`content:${searchConfig.snippetLength}`],
highlightPreTag: `<${searchConfig.highlightTag}>`,
highlightPostTag: `</${searchConfig.highlightTag}>`,
})
.then(({ hits }) => {
const results = {};
hits.forEach(({ uri, date, _highlightResult: { title }, _snippetResult: { content } }) => {
if (results[uri]) return;
if (results[uri] && results[uri].context.length > content.value) return;
results[uri] = {
uri: uri,
title: title.value,
date: date,
context: content.value,
};
});
finish(Object.values(results));
finish(Object.values(results).slice(0, searchConfig.maxResultLength));
})
.catch(err => {
console.error(err);
Expand Down

0 comments on commit bbea71f

Please sign in to comment.