Skip to content

Commit

Permalink
feat(search): 添加 DocSearch 搜索支持
Browse files Browse the repository at this point in the history
- 在 _config.yml 中增加 DocSearch 相关配置选项
- 在 search/index.pug 中添加 DocSearch 搜索的条件包含
- 在 plugins.yml 中添加 DocSearch 所需的 CSS 和 JS 文件
- 在 merge_config.js 中添加 DocSearch 配置项的默认值
  • Loading branch information
everfu committed Jan 18, 2025
1 parent 73d4539 commit 7aae06d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
14 changes: 12 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -869,17 +869,27 @@ giscus:
# Search
search:
enable: false
# type: 使用的搜索引擎 / search engine
type: local # local / algolia
type: # local / algolia / docsearch
tags:
# - Solitude
# - Hexo
# Algolia
algolia:
# hits:
# per_page: 6

# Local search
local:
preload: false
CDN: # url: search.xml

# DocSearch
# https://docsearch.algolia.com/
docsearch:
appId:
apiKey:
indexName:
option:
# --------------------------- end ---------------------------

# --------------------------- start ---------------------------
Expand Down
26 changes: 26 additions & 0 deletions layout/includes/widgets/third-party/search/docsearch.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- const { placeholder, docsearch: { appId, apiKey, indexName, option } } = theme.search

.docsearch-wrap
#docsearch(style="display:none")
script(src=url_for(theme.cdn.docsearch_js))
script.
(() => {
docsearch(Object.assign({
appId: '!{appId}',
apiKey: '!{apiKey}',
indexName: '!{indexName}',
container: '#docsearch',
placeholder: '!{ placeholder || _p("search.input_placeholder")}',
}, !{JSON.stringify(option)}))

const handleClick = () => {
document.querySelector('.DocSearch-Button').click()
}

const searchClickFn = () => {
utils.addEventListenerPjax(document.querySelector('#search-button > .search'), 'click', handleClick)
}

searchClickFn()
window.addEventListener('pjax:complete', searchClickFn)
})()
5 changes: 4 additions & 1 deletion layout/includes/widgets/third-party/search/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ if theme.search.enable
script(src=url_for(theme.cdn.algolia_js))
when 'local'
include local-search
script(src=url_for(theme.cdn.local_search))
script(src=url_for(theme.cdn.local_search))
when 'docsearch'
include docsearch
link(rel="stylesheet" href=url_for(theme.cdn.docsearch_css))
10 changes: 10 additions & 0 deletions plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ instantsearch:
name: instantsearch.js
file: dist/instantsearch.production.min.js
version: 4.75.6
docsearch_css:
name: '@docsearch/css'
other_name: docsearch-css
file: dist/style.css
version: 3.8.2
docsearch_js:
name: '@docsearch/js'
other_name: docsearch-js
file: dist/umd/index.js
version: 3.8.2
pjax:
name: pjax
file: pjax.min.js
Expand Down
6 changes: 6 additions & 0 deletions scripts/event/merge_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ hexo.extend.filter.register('before_generate', () => {
local: {
preload: false,
CDN: null,
},
docsearch: {
appId: null,
apiKey: null,
indexName: null,
option: null
}
},
rightside: {
Expand Down

0 comments on commit 7aae06d

Please sign in to comment.