From 27cd6fb391d3f3040925d310600604f507edb387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=97=A5=E8=AF=AD?= Date: Fri, 5 Apr 2024 15:41:39 +0800 Subject: [PATCH] [feat] algolia_search (#423) --- _config.yml | 6 +- layout/_plugins/search/algolia_search.ejs | 13 +++++ source/js/search/algolia-search.js | 69 +++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 layout/_plugins/search/algolia_search.ejs create mode 100644 source/js/search/algolia-search.js diff --git a/_config.yml b/_config.yml index fcde48f..58a1768 100755 --- a/_config.yml +++ b/_config.yml @@ -145,11 +145,15 @@ article: max_count: 5 search: - service: local_search # local_search, todo... + service: local_search # local_search, algolia_search, todo... local_search: # 在 front-matter 中设置 indexing:false 来避免被搜索索引 field: all # post, page, all path: /search.json # 搜索文件存放位置 content: true # 是否搜索内容 + algolia_search: # Docsearch https://docsearch.algolia.com/apply/ 申请 + appId: + apiKey: + indexName: ######## Comments ######## diff --git a/layout/_plugins/search/algolia_search.ejs b/layout/_plugins/search/algolia_search.ejs new file mode 100644 index 0000000..91e7b2f --- /dev/null +++ b/layout/_plugins/search/algolia_search.ejs @@ -0,0 +1,13 @@ + + \ No newline at end of file diff --git a/source/js/search/algolia-search.js b/source/js/search/algolia-search.js new file mode 100644 index 0000000..573a960 --- /dev/null +++ b/source/js/search/algolia-search.js @@ -0,0 +1,69 @@ +utils.jq(() => { + var $inputArea = $("input#search-input"); + if ($inputArea.length === 0) { + return; + } + + var $resultArea = $("#search-result"); + var $searchWrapper = $("#search-wrapper"); + var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey); + var index = client.initIndex(window.searchConfig.indexName); + + function displayResults(hits) { + var $resultList = $("