From 735af8db7b3b62c37f96b7a196ab2ded11f31fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 23 Feb 2024 14:24:05 +0100 Subject: [PATCH] restore the previously chosen result --- src/client/search.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/client/search.js b/src/client/search.js index 4a8b68efb..5820df1e3 100644 --- a/src/client/search.js +++ b/src/client/search.js @@ -38,20 +38,27 @@ input.addEventListener("input", () => { container.setAttribute("data-shortcut", ""); // prevent conflict with close button sidebar.classList.add("observablehq-search-results"); // hide pages while showing search results const results = index.search(currentValue, {boost: {title: 4, keywords: 4}, fuzzy: 0.15, prefix: true}); + const previous = sessionStorage.getItem("observablehq-search-chosen"); resultsContainer.innerHTML = results.length === 0 ? "
no results
" : `
${results.length.toLocaleString("en-US")} result${results.length === 1 ? "" : "s"}
    ${results - .map(renderResult) + .map(({id, score, title}, i) => { + const href = import.meta.resolve(`../${id}`); + return `
  1. ${escapeText(String(title ?? "—"))}
  2. `; + }) .join("")}
`; + if (previous !== null) { + resultsContainer.querySelector(`.${activeClass}`).scrollIntoView({block: "nearest"}); + sessionStorage.removeItem("observablehq-search-chosen"); + } }); -function renderResult({id, score, title}, i) { - return `
  • ${escapeText(String(title ?? "—"))}
  • `; -} - function escapeDoubleQuote(text) { return text.replace(/["&]/g, entity); }