diff --git a/_config.yml b/_config.yml index 4afe2c69..654ad3f4 100644 --- a/_config.yml +++ b/_config.yml @@ -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 --------------------------- diff --git a/layout/includes/widgets/third-party/search/docsearch.pug b/layout/includes/widgets/third-party/search/docsearch.pug new file mode 100644 index 00000000..7e35fe7e --- /dev/null +++ b/layout/includes/widgets/third-party/search/docsearch.pug @@ -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) + })() \ No newline at end of file diff --git a/layout/includes/widgets/third-party/search/index.pug b/layout/includes/widgets/third-party/search/index.pug index f6fa9e17..98306331 100644 --- a/layout/includes/widgets/third-party/search/index.pug +++ b/layout/includes/widgets/third-party/search/index.pug @@ -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)) \ No newline at end of file + script(src=url_for(theme.cdn.local_search)) + when 'docsearch' + include docsearch + link(rel="stylesheet" href=url_for(theme.cdn.docsearch_css)) \ No newline at end of file diff --git a/plugins.yml b/plugins.yml index 13813057..2aae17d8 100644 --- a/plugins.yml +++ b/plugins.yml @@ -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 diff --git a/scripts/event/merge_config.js b/scripts/event/merge_config.js index a98ed9e5..6f1077fc 100644 --- a/scripts/event/merge_config.js +++ b/scripts/event/merge_config.js @@ -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: {