-
Notifications
You must be signed in to change notification settings - Fork 541
Add Pagefind docs search to left sidebar #2074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ComputelessComputer
wants to merge
5
commits into
main
from
devin/1764682496-add-pagefind-docs-search
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d5cde9f
Add Pagefind docs search to left sidebar
devin-ai-integration[bot] 72d1ba9
Fix Pagefind loading to use window global instead of ES import
devin-ai-integration[bot] d1655a0
Fix Pagefind ES module loading with inline module script
devin-ai-integration[bot] c040339
Refactor docs search to use cmdk command palette with Cmd+K hotkey
devin-ai-integration[bot] f707ca7
Add DOMPurify for HTML sanitization and pagefind to gitignore
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,6 @@ internal | |
|
|
||
| # Local Netlify folder | ||
| .netlify | ||
|
|
||
| # Pagefind generated files | ||
| **/pagefind/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import DOMPurify from "dompurify"; | ||
| import { Search } from "lucide-react"; | ||
| import { useCallback, useEffect, useRef, useState } from "react"; | ||
| import type { | ||
| Pagefind, | ||
| PagefindSearchFragment, | ||
| } from "vite-plugin-pagefind/types"; | ||
|
|
||
| import { | ||
| CommandDialog, | ||
| CommandEmpty, | ||
| CommandGroup, | ||
| CommandInput, | ||
| CommandItem, | ||
| CommandList, | ||
| } from "@hypr/ui/components/ui/command"; | ||
| import { cn } from "@hypr/utils"; | ||
|
|
||
| export function DocsSearch() { | ||
| const [open, setOpen] = useState(false); | ||
| const [query, setQuery] = useState(""); | ||
| const [results, setResults] = useState<PagefindSearchFragment[]>([]); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const pagefindRef = useRef<Pagefind | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (typeof window === "undefined") return; | ||
| let cancelled = false; | ||
|
|
||
| (async () => { | ||
| try { | ||
| const pagefind = (await import( | ||
| "/pagefind/pagefind.js" | ||
| )) as unknown as Pagefind; | ||
| if (!cancelled) pagefindRef.current = pagefind; | ||
| } catch { | ||
| // Pagefind not available in dev mode | ||
| } | ||
| })(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (typeof window === "undefined") return; | ||
|
|
||
| const handler = (event: KeyboardEvent) => { | ||
| const isCmdK = | ||
| (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k"; | ||
|
|
||
| const target = event.target as HTMLElement | null; | ||
| if ( | ||
| isCmdK && | ||
| target && | ||
| !["INPUT", "TEXTAREA"].includes(target.tagName) && | ||
| !target.isContentEditable | ||
| ) { | ||
| event.preventDefault(); | ||
| setOpen((prev) => !prev); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener("keydown", handler); | ||
| return () => window.removeEventListener("keydown", handler); | ||
| }, []); | ||
|
|
||
| const handleSearch = useCallback(async (value: string) => { | ||
| setQuery(value); | ||
| if (!value.trim() || !pagefindRef.current) { | ||
| setResults([]); | ||
| return; | ||
| } | ||
|
|
||
| setIsLoading(true); | ||
| try { | ||
| const res = await pagefindRef.current.search(value); | ||
| if (!res?.results) { | ||
| setResults([]); | ||
| return; | ||
| } | ||
| const data = await Promise.all( | ||
| res.results.slice(0, 10).map((r) => r.data()), | ||
| ); | ||
| setResults(data); | ||
| } catch { | ||
| setResults([]); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }, []); | ||
|
|
||
| const handleSelect = useCallback((url: string) => { | ||
| setOpen(false); | ||
| setQuery(""); | ||
| setResults([]); | ||
| window.location.assign(url); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <> | ||
| <button | ||
| type="button" | ||
| onClick={() => setOpen(true)} | ||
| className={cn([ | ||
| "w-full flex items-center justify-between", | ||
| "px-3 py-2 text-sm", | ||
| "bg-neutral-50 border border-neutral-200 rounded-sm", | ||
| "text-neutral-500 hover:bg-neutral-100", | ||
| "transition-colors cursor-pointer", | ||
| ])} | ||
| > | ||
| <span className="flex items-center gap-2"> | ||
| <Search size={16} className="text-neutral-400" /> | ||
| <span>Search docs...</span> | ||
| </span> | ||
| <span className="text-[11px] rounded border border-neutral-300 px-1.5 py-0.5 text-neutral-400"> | ||
| <span className="font-sans">⌘</span>K | ||
| </span> | ||
| </button> | ||
|
|
||
| <CommandDialog open={open} onOpenChange={setOpen}> | ||
| <CommandInput | ||
| placeholder="Search docs..." | ||
| value={query} | ||
| onValueChange={handleSearch} | ||
| /> | ||
| <CommandList> | ||
| {isLoading && ( | ||
| <div className="py-6 text-center text-sm text-muted-foreground"> | ||
| Searching... | ||
| </div> | ||
| )} | ||
| {!isLoading && query && results.length === 0 && ( | ||
| <CommandEmpty>No results found.</CommandEmpty> | ||
| )} | ||
| {!isLoading && results.length > 0 && ( | ||
| <CommandGroup heading="Results"> | ||
| {results.map((result) => ( | ||
| <CommandItem | ||
| key={result.url} | ||
| value={`${result.meta.title} ${result.url}`} | ||
| onSelect={() => handleSelect(result.url)} | ||
| className="flex flex-col items-start gap-1 py-3" | ||
| > | ||
| <div className="text-sm font-medium">{result.meta.title}</div> | ||
| <div | ||
| className="text-xs text-muted-foreground line-clamp-2" | ||
| dangerouslySetInnerHTML={{ | ||
| __html: DOMPurify.sanitize(result.excerpt), | ||
| }} | ||
| /> | ||
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| )} | ||
| </CommandList> | ||
| </CommandDialog> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.