From ec9a544f0490049188dc1d2722e36f5fecc978fa Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Thu, 28 Nov 2024 11:23:35 +0800 Subject: [PATCH] :sparkles: Feat: add Google CSE support --- assets/js/theme.js | 50 ++++++++++++++++++++++++++---------- hugo.toml | 19 +++++++++----- layouts/page/search.html | 4 +-- layouts/partials/assets.html | 10 ++++++++ 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index fecdb263..fb33748b 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -159,8 +159,10 @@ class FixIt { document.body.classList.add('blur'); $header.classList.add('open'); $searchInput.focus(); + this.disableScrollEvent = true; }, false); $searchClear.addEventListener('click', () => { + this.disableScrollEvent = false; $searchClear.style.display = 'none'; this._searchDesktop && this._searchDesktop.autocomplete.setVal(''); }, false); @@ -289,25 +291,47 @@ class FixIt { finish([]); }); } else finish(search()); - } + } else if (searchConfig.type === 'cse') { + const cseConfig = this.config.cse; + if (cseConfig.engine === 'google' && cseConfig.cx) { + finish([{ + uri: `${cseConfig.resultsPage}#gsc.tab=0&gsc.q=${encodeURIComponent(query)}`, + title: cseConfig.searchIn, + date: '', + context: cseConfig.gotoResultsPage + }]); + } + } }, templates: { suggestion: ({ title, date, context }) => `
${title}${date}
${context}
`, empty: ({ query }) => `
${searchConfig.noResultsFound}: "${query}"
`, footer: ({}) => { - const { searchType, icon, href } = - searchConfig.type === 'algolia' - ? { - searchType: 'algolia', - icon: '', - href: 'https://www.algolia.com/' - } - : { - searchType: 'Fuse.js', - icon: '', - href: 'https://fusejs.io/' - } + let searchType, icon, href; + switch (searchConfig.type) { + case 'algolia': + searchType = 'algolia'; + icon = ''; + href = 'https://www.algolia.com/'; + break; + case 'fuse': + searchType = 'Fuse.js'; + icon = ''; + href = 'https://fusejs.io/'; + break; + case 'cse': + if (this.config.cse.engine === 'google') { + searchType = 'Google CSE'; + icon = ''; + href = 'https://programmablesearchengine.google.com/'; + } + break; + default: + searchType = ''; + icon = ''; + href = ''; + } return ``; } } diff --git a/hugo.toml b/hugo.toml index dab6f241..ef0b7e22 100644 --- a/hugo.toml +++ b/hugo.toml @@ -418,14 +418,19 @@ enableEmoji = true ignoreLocation = false useExtendedSearch = false ignoreFieldNorm = false - # FixIt 0.3.16 | NEW Custom Search Engine (CSE) - [params.search.cse] - # CSE engine: ["google", "bing"] - engine = "google" - # Google: https://programmablesearchengine.google.com/ - # Google Custom Search Engine Context + + # FixIt 0.3.16 | NEW Custom Search Engine (CSE) + [params.cse] + # Search Engine: ["google", "bing"] + engine = "" + # Search results page URL (layout: search) + resultsPage = "/search/" + # Google: https://programmablesearchengine.google.com/ + # Google Custom Search Engine Context + [params.cse.google] cx = "" - # Bing (Unsupported): https://www.customsearch.ai/ + # Bing (Unsupported): https://www.customsearch.ai/ + [params.cse.bing] # Header config [params.header] diff --git a/layouts/page/search.html b/layouts/page/search.html index 1eeb2ee0..148fc177 100644 --- a/layouts/page/search.html +++ b/layouts/page/search.html @@ -5,7 +5,7 @@ {{- define "content" -}} {{- $params := partial "function/params.html" -}} - {{- $cse := .Site.Params.search.cse -}} + {{- $cse := .Site.Params.cse -}}