From 0c199975f2d469ecdfd7efcf2ddd16ffa1dc492b Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 17 Aug 2023 07:55:28 +0000
Subject: [PATCH] various path fixes for links to extensions, fix relative paths in links

---
 quartz/components/scripts/search.inline.ts |   38 ++++++++++++++++++++++++--------------
 1 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts
index 40d0bc2..5c7dae0 100644
--- a/quartz/components/scripts/search.inline.ts
+++ b/quartz/components/scripts/search.inline.ts
@@ -1,9 +1,10 @@
 import { Document } from "flexsearch"
 import { ContentDetails } from "../../plugins/emitters/contentIndex"
 import { registerEscapeHandler, removeAllChildren } from "./util"
-import { CanonicalSlug, getClientSlug, resolveRelative } from "../../path"
+import { CanonicalSlug, getClientSlug, resolveRelative } from "../../util/path"
 
 interface Item {
+  id: number
   slug: CanonicalSlug
   title: string
   content: string
@@ -72,6 +73,7 @@
   const searchIcon = document.getElementById("search-icon")
   const searchBar = document.getElementById("search-bar") as HTMLInputElement | null
   const results = document.getElementById("results-container")
+  const idDataMap = Object.keys(data) as CanonicalSlug[]
 
   function hideSearch() {
     container?.classList.remove("active")
@@ -107,11 +109,15 @@
     }
   }
 
-  const formatForDisplay = (term: string, slug: CanonicalSlug) => ({
-    slug,
-    title: highlight(term, data[slug].title ?? ""),
-    content: highlight(term, data[slug].content ?? "", true),
-  })
+  const formatForDisplay = (term: string, id: number) => {
+    const slug = idDataMap[id]
+    return {
+      id,
+      slug,
+      title: highlight(term, data[slug].title ?? ""),
+      content: highlight(term, data[slug].content ?? "", true),
+    }
+  }
 
   const resultToHTML = ({ slug, title, content }: Item) => {
     const button = document.createElement("button")
@@ -139,16 +145,17 @@
     }
   }
 
-  function onType(e: HTMLElementEventMap["input"]) {
+  async function onType(e: HTMLElementEventMap["input"]) {
     const term = (e.target as HTMLInputElement).value
-    const searchResults = index?.search(term, numSearchResults) ?? []
-    const getByField = (field: string): CanonicalSlug[] => {
+    const searchResults = (await index?.searchAsync(term, numSearchResults)) ?? []
+    console.log(searchResults)
+    const getByField = (field: string): number[] => {
       const results = searchResults.filter((x) => x.field === field)
-      return results.length === 0 ? [] : ([...results[0].result] as CanonicalSlug[])
+      return results.length === 0 ? [] : ([...results[0].result] as number[])
     }
 
     // order titles ahead of content
-    const allIds: Set<CanonicalSlug> = new Set([...getByField("title"), ...getByField("content")])
+    const allIds: Set<number> = new Set([...getByField("title"), ...getByField("content")])
     const finalResults = [...allIds].map((id) => formatForDisplay(term, id))
     displayResults(finalResults)
   }
@@ -168,11 +175,11 @@
       optimize: true,
       encode: encoder,
       document: {
-        id: "slug",
+        id: "id",
         index: [
           {
             field: "title",
-            tokenize: "forward",
+            tokenize: "reverse",
           },
           {
             field: "content",
@@ -182,12 +189,15 @@
       },
     })
 
+    let id = 0
     for (const [slug, fileData] of Object.entries<ContentDetails>(data)) {
-      await index.addAsync(slug, {
+      await index.addAsync(id, {
+        id,
         slug: slug as CanonicalSlug,
         title: fileData.title,
         content: fileData.content,
       })
+      id++
     }
   }
 

--
Gitblit v1.10.0