From e374e3abd42c1719ad1fdc6bd9c641efb2ddf954 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 21 Nov 2022 23:36:27 -0800 Subject: [PATCH] fix: jump to search for operand --- assets/js/full-text-search.js | 2 +- assets/js/semantic-search.js | 4 ++-- assets/js/util.js | 26 ++++++++++++++------------ layouts/partials/head.html | 2 ++ 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/assets/js/full-text-search.js b/assets/js/full-text-search.js index a8f7f23..108db10 100644 --- a/assets/js/full-text-search.js +++ b/assets/js/full-text-search.js @@ -56,6 +56,6 @@ } const allIds = new Set([...getByField("title"), ...getByField("content")]) const finalResults = [...allIds].map(formatForDisplay) - displayResults(finalResults, true) + displayResults(term, finalResults, true) }) })() diff --git a/assets/js/semantic-search.js b/assets/js/semantic-search.js index bfe102e..fca2851 100644 --- a/assets/js/semantic-search.js +++ b/assets/js/semantic-search.js @@ -24,7 +24,7 @@ async function searchContents(query) { }, body: JSON.stringify({ query: query, - limit: 8, + limit: 10, }), }) if (result.ok) { @@ -48,7 +48,7 @@ registerHandlers( debounce((e) => { let term = e.target.value if (term !== "") { - searchContents(term).then((results) => displayResults(results)) + searchContents(term).then((results) => displayResults(term, results)) } }), ) diff --git a/assets/js/util.js b/assets/js/util.js index 06db3c1..d17594a 100644 --- a/assets/js/util.js +++ b/assets/js/util.js @@ -115,9 +115,11 @@ const resultToHTML = ({ url, title, content }) => { } const redir = (id, term) => { - // SPA navigation + const shouldTrim = PRODUCTION && SEARCH_ENABLED + const baseURLPrefix = shouldTrim ? "" : BASE_URL.replace(/\/$/g, "") + const urlString = `${baseURLPrefix}${id}#:~:text=${encodeURIComponent(term)}/` window.Million.navigate( - new URL(`${BASE_URL.replace(/\/$/g, "")}${id}#:~:text=${encodeURIComponent(term)}/`), + new URL(urlString), ".singlePage", ) closeSearch() @@ -179,7 +181,7 @@ const registerHandlers = (onInputFn) => { }) } -const displayResults = (finalResults, extractHighlight = false) => { +const displayResults = (term, finalResults, extractHighlight = false) => { const results = document.getElementById("results-container") if (finalResults.length === 0) { results.innerHTML = `