include home page in search
This commit is contained in:
		| @@ -4,6 +4,7 @@ import { registerEscapeHandler, removeAllChildren } from "./util" | |||||||
| import { CanonicalSlug, getClientSlug, resolveRelative } from "../../path" | import { CanonicalSlug, getClientSlug, resolveRelative } from "../../path" | ||||||
|  |  | ||||||
| interface Item { | interface Item { | ||||||
|  |   id: number | ||||||
|   slug: CanonicalSlug |   slug: CanonicalSlug | ||||||
|   title: string |   title: string | ||||||
|   content: string |   content: string | ||||||
| @@ -72,6 +73,7 @@ document.addEventListener("nav", async (e: unknown) => { | |||||||
|   const searchIcon = document.getElementById("search-icon") |   const searchIcon = document.getElementById("search-icon") | ||||||
|   const searchBar = document.getElementById("search-bar") as HTMLInputElement | null |   const searchBar = document.getElementById("search-bar") as HTMLInputElement | null | ||||||
|   const results = document.getElementById("results-container") |   const results = document.getElementById("results-container") | ||||||
|  |   const idDataMap = Object.keys(data) as CanonicalSlug[] | ||||||
|  |  | ||||||
|   function hideSearch() { |   function hideSearch() { | ||||||
|     container?.classList.remove("active") |     container?.classList.remove("active") | ||||||
| @@ -107,11 +109,15 @@ document.addEventListener("nav", async (e: unknown) => { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   const formatForDisplay = (term: string, slug: CanonicalSlug) => ({ |   const formatForDisplay = (term: string, id: number) => { | ||||||
|  |     const slug = idDataMap[id] | ||||||
|  |     return { | ||||||
|  |       id, | ||||||
|       slug, |       slug, | ||||||
|       title: highlight(term, data[slug].title ?? ""), |       title: highlight(term, data[slug].title ?? ""), | ||||||
|       content: highlight(term, data[slug].content ?? "", true), |       content: highlight(term, data[slug].content ?? "", true), | ||||||
|   }) |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   const resultToHTML = ({ slug, title, content }: Item) => { |   const resultToHTML = ({ slug, title, content }: Item) => { | ||||||
|     const button = document.createElement("button") |     const button = document.createElement("button") | ||||||
| @@ -142,13 +148,14 @@ document.addEventListener("nav", async (e: unknown) => { | |||||||
|   async function onType(e: HTMLElementEventMap["input"]) { |   async function onType(e: HTMLElementEventMap["input"]) { | ||||||
|     const term = (e.target as HTMLInputElement).value |     const term = (e.target as HTMLInputElement).value | ||||||
|     const searchResults = (await index?.searchAsync(term, numSearchResults)) ?? [] |     const searchResults = (await index?.searchAsync(term, numSearchResults)) ?? [] | ||||||
|     const getByField = (field: string): CanonicalSlug[] => { |     console.log(searchResults) | ||||||
|  |     const getByField = (field: string): number[] => { | ||||||
|       const results = searchResults.filter((x) => x.field === field) |       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 |     // 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)) |     const finalResults = [...allIds].map((id) => formatForDisplay(term, id)) | ||||||
|     displayResults(finalResults) |     displayResults(finalResults) | ||||||
|   } |   } | ||||||
| @@ -168,11 +175,11 @@ document.addEventListener("nav", async (e: unknown) => { | |||||||
|       optimize: true, |       optimize: true, | ||||||
|       encode: encoder, |       encode: encoder, | ||||||
|       document: { |       document: { | ||||||
|         id: "slug", |         id: "id", | ||||||
|         index: [ |         index: [ | ||||||
|           { |           { | ||||||
|             field: "title", |             field: "title", | ||||||
|             tokenize: "forward", |             tokenize: "reverse", | ||||||
|           }, |           }, | ||||||
|           { |           { | ||||||
|             field: "content", |             field: "content", | ||||||
| @@ -182,12 +189,15 @@ document.addEventListener("nav", async (e: unknown) => { | |||||||
|       }, |       }, | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
|  |     let id = 0 | ||||||
|     for (const [slug, fileData] of Object.entries<ContentDetails>(data)) { |     for (const [slug, fileData] of Object.entries<ContentDetails>(data)) { | ||||||
|       await index.addAsync(slug, { |       await index.addAsync(id, { | ||||||
|  |         id, | ||||||
|         slug: slug as CanonicalSlug, |         slug: slug as CanonicalSlug, | ||||||
|         title: fileData.title, |         title: fileData.title, | ||||||
|         content: fileData.content, |         content: fileData.content, | ||||||
|       }) |       }) | ||||||
|  |       id++ | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user