From 3ac6b42e16dca5a44ed3fec2c0314f1dbbc2322b Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 16 Jul 2023 06:02:12 +0000
Subject: [PATCH] finish path refactoring, add sourcemap + better trace support

---
 quartz/components/scripts/search.inline.ts |   32 +++++++++++++++++++++-----------
 1 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts
index 054d352..c738fc9 100644
--- a/quartz/components/scripts/search.inline.ts
+++ b/quartz/components/scripts/search.inline.ts
@@ -1,15 +1,18 @@
 import { Document } from "flexsearch"
 import { ContentDetails } from "../../plugins/emitters/contentIndex"
-import { registerEscapeHandler, relative, removeAllChildren } from "./util"
+import { registerEscapeHandler, removeAllChildren } from "./util"
+import { CanonicalSlug, getClientSlug, resolveRelative } from "../../path"
 
 interface Item {
-  slug: string,
+  slug: CanonicalSlug,
   title: string,
   content: string,
 }
+
 let index: Document<Item> | undefined = undefined
 
 const contextWindowWords = 30
+const numSearchResults = 5
 function highlight(searchTerm: string, text: string, trim?: boolean) {
   // try to highlight longest tokens first
   const tokenizedTerms = searchTerm.split(/\s+/).filter(t => t !== "").sort((a, b) => b.length - a.length)
@@ -60,6 +63,7 @@
 
   const data = await fetchData
   const container = document.getElementById("search-container")
+  const sidebar = container?.closest(".sidebar") as HTMLElement
   const searchIcon = document.getElementById("search-icon")
   const searchBar = document.getElementById("search-bar") as HTMLInputElement | null
   const results = document.getElementById("results-container")
@@ -69,12 +73,18 @@
     if (searchBar) {
       searchBar.value = "" // clear the input when we dismiss the search
     }
+    if (sidebar) {
+      sidebar.style.zIndex = "unset"
+    }
     if (results) {
       removeAllChildren(results)
     }
   }
 
   function showSearch() {
+    if (sidebar) {
+      sidebar.style.zIndex = "1"
+    }
     container?.classList.add("active")
     searchBar?.focus()
   }
@@ -92,7 +102,7 @@
     }
   }
 
-  const formatForDisplay = (term: string, slug: string) => ({
+  const formatForDisplay = (term: string, slug: CanonicalSlug) => ({
     slug,
     title: highlight(term, data[slug].title ?? ""),
     content: highlight(term, data[slug].content ?? "", true),
@@ -104,8 +114,8 @@
     button.id = slug
     button.innerHTML = `<h3>${title}</h3><p>${content}</p>`
     button.addEventListener('click', () => {
-      const targ = relative(currentSlug, slug)
-      window.spaNavigate(new URL(targ))
+      const targ = resolveRelative(currentSlug, slug)
+      window.spaNavigate(new URL(targ, getClientSlug(window)))
     })
     return button
   }
@@ -127,14 +137,14 @@
 
   function onType(e: HTMLElementEventMap["input"]) {
     const term = (e.target as HTMLInputElement).value
-    const searchResults = index?.search(term, 5) ?? []
-    const getByField = (field: string): string[] => {
+    const searchResults = index?.search(term, numSearchResults) ?? []
+    const getByField = (field: string): CanonicalSlug[] => {
       const results = searchResults.filter((x) => x.field === field)
-      return results.length === 0 ? [] : [...results[0].result] as string[]
+      return results.length === 0 ? [] : [...results[0].result] as CanonicalSlug[]
     }
 
     // order titles ahead of content
-    const allIds: Set<string> = new Set([...getByField("title"), ...getByField("content")])
+    const allIds: Set<CanonicalSlug> = new Set([...getByField("title"), ...getByField("content")])
     const finalResults = [...allIds].map(id => formatForDisplay(term, id))
     displayResults(finalResults)
   }
@@ -145,7 +155,7 @@
   searchIcon?.addEventListener("click", showSearch)
   searchBar?.removeEventListener("input", onType)
   searchBar?.addEventListener("input", onType)
-  
+
   // setup index if it hasn't been already
   if (!index) {
     index = new Document({
@@ -170,7 +180,7 @@
 
     for (const [slug, fileData] of Object.entries<ContentDetails>(data)) {
       await index.addAsync(slug, {
-        slug,
+        slug: slug as CanonicalSlug,
         title: fileData.title,
         content: fileData.content
       })

--
Gitblit v1.10.0