From e0a7f2f2ff26e1023e0bc98d3d117aa32dede9cb Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Mon, 28 Nov 2022 23:07:50 +0800 Subject: [PATCH] [opt] search json --- _config.yml | 7 +- layout/_partial/scripts/index.ejs | 2 +- scripts/generators/search.js | 103 ++++++++++++++++++++++++++++++ source/js/main.js | 2 +- source/js/search/local-search.js | 14 +--- 5 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 scripts/generators/search.js diff --git a/_config.yml b/_config.yml index ad5edfc..1696c27 100755 --- a/_config.yml +++ b/_config.yml @@ -62,8 +62,11 @@ article: search: - service: local_search # hexo, todo... - local_search: # npm i hexo-generator-search + service: local_search # local_search, todo... + local_search: + field: all # post, page, all + path: /search.json + content: true ######## Comments ######## diff --git a/layout/_partial/scripts/index.ejs b/layout/_partial/scripts/index.ejs index 7d848c3..4958ea5 100644 --- a/layout/_partial/scripts/index.ejs +++ b/layout/_partial/scripts/index.ejs @@ -111,7 +111,7 @@ stellar.search = {}; stellar.search.service = '<%- theme.search.service %>'; if (stellar.search.service == 'local_search') { - let service_obj = Object.assign({}, <%- JSON.stringify(config.search) %>); + let service_obj = Object.assign({}, <%- JSON.stringify(theme.search.local_search) %>); stellar.search[stellar.search.service] = service_obj; } } diff --git a/scripts/generators/search.js b/scripts/generators/search.js new file mode 100644 index 0000000..12e682e --- /dev/null +++ b/scripts/generators/search.js @@ -0,0 +1,103 @@ +/** + * https://github.com/wzpan/hexo-generator-search + */ + +hexo.extend.generator.register('search_json_generator', function (locals) { + if (this.theme.config.search.service != 'local_search') { + return {} + } + var config = this.config + const { local_search } = this.theme.config.search + + var searchfield = local_search.field + var content = local_search.content + + var posts, pages + + if (searchfield.trim() != '') { + searchfield = searchfield.trim() + if (searchfield == 'post'){ + posts = locals.posts.sort('-date') + } else if (searchfield == 'page') { + pages = locals.pages + } else { + posts = locals.posts.sort('-date') + pages = locals.pages + } + } else { + posts = locals.posts.sort('-date') + } + + var res = new Array() + var index = 0 + + if (posts) { + posts.each(function(post) { + if (post.indexing != undefined && !post.indexing) return + var temp_post = new Object() + if (post.title) { + temp_post.title = post.title + } + if (post.path) { + temp_post.url = config.root + post.path + } + if (content != false && post._content) { + temp_post.content = post._content + } + if (post.tags && post.tags.length > 0) { + var tags = [] + post.tags.forEach(function (tag) { + tags.push(tag.name) + }) + temp_post.tags = tags + } + if (post.categories && post.categories.length > 0) { + var categories = [] + post.categories.forEach(function (cate) { + categories.push(cate.name) + }) + temp_post.categories = categories + } + res[index] = temp_post + index += 1 + }) + } + if (pages) { + pages.each(function(page) { + if (page.indexing != undefined && !page.indexing) return + var temp_page = new Object() + if (page.title) { + temp_page.title = page.title + } + if (page.path) { + temp_page.url = config.root + page.path + } + if (content != false && page._content) { + temp_page.content = page._content + } + if (page.tags && page.tags.length > 0) { + var tags = new Array() + var tag_index = 0 + page.tags.each(function (tag) { + tags[tag_index] = tag.name + }) + temp_page.tags = tags + } + if (page.categories && page.categories.length > 0) { + temp_page.categories = [] + (page.categories.each || page.categories.forEach)(function (item) { + temp_page.categories.push(item) + }) + } + res[index] = temp_page + index += 1 + }) + } + + var json = JSON.stringify(res) + + return { + path: local_search.path, + data: json + } +}) \ No newline at end of file diff --git a/source/js/main.js b/source/js/main.js index 07fe333..dc91fa7 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -309,7 +309,7 @@ if (stellar.search.service) { var $inputArea = $("input#search-input"); var $resultArea = document.querySelector("div#search-result"); $inputArea.focus(function() { - var path = stellar.search[stellar.search.service]?.path || '/search.xml'; + var path = stellar.search[stellar.search.service]?.path || '/search.json'; if (!path.startsWith('/')) { path = '/' + path; } diff --git a/source/js/search/local-search.js b/source/js/search/local-search.js index 0a32acd..89c38da 100644 --- a/source/js/search/local-search.js +++ b/source/js/search/local-search.js @@ -51,17 +51,9 @@ var searchFunc = function(path, filter, searchId, contentId) { $.ajax({ url: path, - dataType: "xml", - success: function(xmlResponse) { - // get the contents from search data - var datas = $("entry", xmlResponse).map(function() { - return { - title: $("title", this).text(), - content: $("content", this).text(), - url: $("link", this).attr("href") - }; - }).get(); - + dataType: "json", + success: function(jsonResponse) { + var datas = jsonResponse; var $input = document.getElementById(searchId); if (!$input) { return; } var $resultContent = document.getElementById(contentId);