From 76c092dcf20959bc52fcb13b28cee50cd4217e40 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 20 Jul 2023 04:59:48 +0000
Subject: [PATCH] add custom.scss
---
quartz/components/scripts/search.inline.ts | 23 +++++++++++++----------
1 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts
index e257c0a..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)
@@ -99,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),
@@ -111,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
}
@@ -134,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)
}
@@ -177,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