-
Notifications
You must be signed in to change notification settings - Fork 49
feat(site): search #165
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
Merged
Merged
feat(site): search #165
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,99 @@ | ||
| import type { AstroIntegration } from 'astro'; | ||
| import { spawn } from 'node:child_process'; | ||
| import { existsSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import sirv from 'sirv'; | ||
|
|
||
| export interface PagefindOptions { | ||
| /** | ||
| * The path to the built site to index. | ||
| * Defaults to the Astro output directory. | ||
| */ | ||
| site?: string; | ||
| } | ||
|
|
||
| export default function pagefind(options: PagefindOptions = {}): AstroIntegration { | ||
| return { | ||
| name: 'pagefind', | ||
| hooks: { | ||
| 'astro:server:setup': ({ server, logger }) => { | ||
| // Serve Pagefind index from previous build during development | ||
| const rootDir = server.config.root; | ||
| const indexDir = join(rootDir, 'dist', 'client'); | ||
| const pagefindDir = join(indexDir, 'pagefind'); | ||
|
|
||
| // Warn if index doesn't exist yet | ||
| if (!existsSync(pagefindDir)) { | ||
| logger.warn( | ||
| 'Pagefind index not found. Run `pnpm build` first to generate ' | ||
| + 'the search index for development mode.', | ||
| ); | ||
| } else { | ||
| logger.debug(`Serving Pagefind index from ${indexDir}`); | ||
| } | ||
|
|
||
| // Create sirv middleware to serve static files | ||
| // approach adapted from https://github.com/shishkin/astro-pagefind | ||
| const serve = sirv(indexDir, { | ||
| dev: true, // No caching in dev mode | ||
| etag: true, // Enable cache validation | ||
| }); | ||
|
|
||
| // Mount middleware for /pagefind/* routes only | ||
| server.middlewares.use((req, res, next) => { | ||
| if (req.url?.startsWith('/pagefind/')) { | ||
| serve(req, res, next); | ||
| } else { | ||
| next(); | ||
| } | ||
| }); | ||
| }, | ||
|
|
||
| 'astro:build:done': async ({ dir, logger }) => { | ||
| // Determine the site directory to index | ||
| // The dir parameter already points to the correct static output directory | ||
| const siteDir = options.site || fileURLToPath(dir); | ||
|
|
||
| // Map Astro logger levels to Pagefind CLI flags | ||
| const logLevel = logger.options.level; | ||
| const logFlags: string[] = []; | ||
|
|
||
| if (logLevel === 'silent' || logLevel === 'error') { | ||
| logFlags.push('--silent'); | ||
| } else if (logLevel === 'warn') { | ||
| logFlags.push('--quiet'); | ||
| } else if (logLevel === 'debug') { | ||
| logFlags.push('--verbose'); | ||
| } | ||
| // 'info' level uses no flag (default) | ||
|
|
||
| logger.info('Running Pagefind indexer...'); | ||
|
|
||
| return new Promise<void>((resolve, reject) => { | ||
| const pagefindProcess = spawn( | ||
| 'npx', | ||
| ['-y', 'pagefind', ...logFlags, '--site', siteDir], | ||
| { | ||
| stdio: 'inherit', | ||
| shell: true, | ||
| }, | ||
| ); | ||
|
|
||
| pagefindProcess.on('close', (code) => { | ||
| if (code === 0) { | ||
| logger.info('Pagefind indexing complete'); | ||
| resolve(); | ||
| } else { | ||
| reject(new Error(`Pagefind process exited with code ${code}`)); | ||
| } | ||
| }); | ||
|
|
||
| pagefindProcess.on('error', (error) => { | ||
| reject(new Error(`Failed to start Pagefind: ${error.message}`)); | ||
| }); | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } |
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
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,111 @@ | ||
| --- | ||
| /** | ||
| * We wrap Search.tsx in an .astro entry point for | ||
| * 1. easy CSS support, since we can't use Tailwind on pagefind-ui | ||
| * 2. a synchronous script to set the meta key (ctrl or cmd) without FOUC or hydration errors | ||
| */ | ||
| import SearchClient from './Search'; | ||
| import { join } from 'node:path/posix'; | ||
| import SearchIcon from './searchIcon.svg'; | ||
| import searchIconString from './searchIcon.svg?raw'; | ||
| import clsx from 'clsx'; | ||
|
|
||
| const baseUrl = import.meta.env.BASE_URL || '/'; | ||
| const bundlePath = join(baseUrl, 'pagefind/'); | ||
|
|
||
| interface Props { | ||
| class?: string; | ||
| dark: boolean; | ||
| } | ||
| const { class: className, dark } = Astro.props; | ||
| --- | ||
|
|
||
| <SearchClient | ||
| client:load | ||
| className={clsx( | ||
| 'inline-flex items-center gap-2 cursor-pointer min-h-6 px-2 sm:border intent:border-current rounded-full', | ||
| dark ? 'border-dark-80' : 'border-light-40 dark:border-dark-80', | ||
| className, | ||
| )} | ||
| baseUrl={baseUrl} | ||
| bundlePath={bundlePath} | ||
| searchId="pagefind-ui" | ||
| searchStyle={{ | ||
| '--search-icon-url': `url('data:image/svg+xml;utf8,${encodeURIComponent(searchIconString)}')`, | ||
| } as React.CSSProperties} | ||
| > | ||
| <SearchIcon class="w-4 h-4 sm:w-3 sm:h-3" /> | ||
| <kbd data-platform-key="default" class="font-sans text-sm hidden sm:inline">Ctrl K</kbd> | ||
| <kbd data-platform-key="mac" class="font-sans text-sm hidden sm:inline">⌘K</kbd> | ||
| </SearchClient> | ||
|
|
||
| { | ||
| /** | ||
| * Adapted from https://github.com/withastro/starlight/blob/8a72a19e2cfec235941b4e1401b69b44e6695068/packages/starlight/components/Search.astro#L55C1-L75C1 | ||
| * This is intentionally inlined to avoid briefly showing an invalid shortcut. | ||
| * Purposely using the deprecated `navigator.platform` property to detect Apple devices, as the | ||
| * user agent is spoofed by some browsers when opening the devtools. | ||
| */ | ||
| } | ||
| <script is:inline> | ||
| (() => { | ||
| const defaultPlatformKey = document.querySelector('kbd[data-platform-key="default"]'); | ||
| const macPlatformKey = document.querySelector('kbd[data-platform-key="mac"]'); | ||
| if (/Mac|iPhone|iPod|iPad/i.test(navigator.platform)) { | ||
| if (defaultPlatformKey) defaultPlatformKey.style.display = 'none'; | ||
| } else { | ||
| if (macPlatformKey) macPlatformKey.style.display = 'none'; | ||
| } | ||
| })(); | ||
| </script> | ||
|
|
||
| <style is:global> | ||
| @import url('@pagefind/default-ui/css/ui.css'); | ||
|
|
||
| #pagefind-ui { | ||
| --pagefind-ui-scale: 1; | ||
| --pagefind-ui-primary: var(--color-yellow); | ||
| --pagefind-ui-text: var(--color-dark-100); | ||
| --pagefind-ui-background: var(--color-light-100); | ||
| --pagefind-ui-border: var(--color-light-40); | ||
| --pagefind-ui-tag: green; | ||
| --pagefind-ui-border-width: 1px; | ||
| --pagefind-ui-border-radius: var(--radius-lg); | ||
| --pagefind-ui-font: inherit; | ||
| } | ||
|
|
||
| .dark #pagefind-ui { | ||
| --pagefind-ui-text: var(--color-light-80); | ||
| --pagefind-ui-background: var(--color-dark-100); | ||
| --pagefind-ui-border: var(--color-dark-80); | ||
| } | ||
|
|
||
| #pagefind-ui .pagefind-ui__form::before { | ||
| -webkit-mask-image: var(--search-icon-url); | ||
| mask-image: var(--search-icon-url); | ||
| } | ||
| #pagefind-ui .pagefind-ui__search-input { | ||
| font-size: var(--text-lg); | ||
| line-height: var(--text-lg--line-height); | ||
| letter-spacing: var(--text-lg--letter-spacing); | ||
| font-weight: var(--text-lg--font-weight); | ||
| } | ||
| #pagefind-ui .pagefind-ui__filter-block { | ||
| border-bottom: solid 1px var(--pagefind-ui-border); | ||
| } | ||
| #pagefind-ui .pagefind-ui__message { | ||
| font-weight: var(--font-weight-normal); | ||
| } | ||
| #pagefind-ui .pagefind-ui__result-title, | ||
| #pagefind-ui .pagefind-ui__filter-name { | ||
| font-weight: var(--font-weight-semibold); | ||
| } | ||
| #pagefind-ui .pagefind-ui__result-nested .pagefind-ui__result-link::before { | ||
| font-weight: var(--font-weight-normal); | ||
| font-size: 0.9em; | ||
| } | ||
| #pagefind-ui mark { | ||
| background-color: var(--color-yellow); | ||
| color: var(--color-dark-100); | ||
| } | ||
| </style> | ||
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.