-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
temporarily inlining external package until it has support for pagefi…
…nd subdir config OR we publish our own fork.
- Loading branch information
1 parent
2eb4786
commit c6145e3
Showing
8 changed files
with
186 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { defineConfig } from 'astro/config' | ||
import pagefind from "astro-pagefind"; | ||
import { defineConfig } from 'astro/config'; | ||
import pagefind from "./lib/astro-pagefind/pagefind.ts" | ||
import tailwind from '@astrojs/tailwind' | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
build: { | ||
inlineStylesheets: 'always', | ||
assets: 'assets/docs/_astro' | ||
}, | ||
integrations: [tailwind(), pagefind()] | ||
build: { | ||
inlineStylesheets: 'always', | ||
assets: 'assets/docs/_astro' | ||
}, | ||
|
||
integrations: [tailwind(), pagefind({site: 'dist', outputSubdir: 'assets/docs/pagefind'})] | ||
}) |
This file contains 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,55 @@ | ||
--- | ||
// Temporarily inlined from: https://github.com/shishkin/astro-pagefind | ||
// Need functionality from PR: https://github.com/shishkin/astro-pagefind/pull/58 | ||
import "@pagefind/default-ui/css/ui.css"; | ||
export interface Props { | ||
readonly id?: string; | ||
readonly className?: string; | ||
readonly query?: string; | ||
readonly uiOptions?: Record<string, any>; | ||
} | ||
const { id, className, query, uiOptions = {} } = Astro.props as Props; | ||
const { PAGEFIND_OUTPUT_SUBDIR = 'pagefind', PAGEFIND_SITE} = process.env; | ||
const bundlePath = `${import.meta.env.BASE_URL}${PAGEFIND_OUTPUT_SUBDIR}/`; | ||
--- | ||
|
||
<div | ||
id={id} | ||
class:list={[className]} | ||
data-pagefind-ui | ||
data-bundle-path={bundlePath} | ||
data-query={query} | ||
data-ui-options={JSON.stringify(uiOptions)} | ||
> | ||
</div> | ||
<script> | ||
import { PagefindUI } from "@pagefind/default-ui"; | ||
window.addEventListener("DOMContentLoaded", () => { | ||
const allSelector = "[data-pagefind-ui]"; | ||
for (const el of document.querySelectorAll(allSelector)) { | ||
const elSelector = [ | ||
...(el.id ? [`#${el.id}`] : []), | ||
...[...el.classList.values()].map((c) => `.${c}`), | ||
allSelector, | ||
].join(""); | ||
const bundlePath = el.getAttribute("data-bundle-path"); | ||
const opts = JSON.parse(el.getAttribute("data-ui-options") ?? "{}"); | ||
new PagefindUI({ | ||
...opts, | ||
element: elSelector, | ||
bundlePath, | ||
}); | ||
const query = el.getAttribute("data-query"); | ||
if (query) { | ||
const input = el.querySelector<HTMLInputElement>(`input[type="text"]`); | ||
if (input) { | ||
input.value = query; | ||
input.dispatchEvent(new Event("input", { bubbles: true })); | ||
} | ||
} | ||
} | ||
}); | ||
</script> |
This file contains 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,5 @@ | ||
// Temporarily inlined from: https://github.com/shishkin/astro-pagefind | ||
// Need functionality from PR: https://github.com/shishkin/astro-pagefind/pull/58 | ||
// TODO: figure out how to avoid this: | ||
// @ts-expect-error | ||
export { default } from "./Search.astro"; |
This file contains 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,71 @@ | ||
// Temporarily inlined from: https://github.com/shishkin/astro-pagefind | ||
// Need functionality from PR: https://github.com/shishkin/astro-pagefind/pull/58 | ||
|
||
import type { AstroIntegration } from "astro"; | ||
import { fileURLToPath } from "node:url"; | ||
import { execSync } from "child_process"; | ||
import sirv from "sirv"; | ||
|
||
export default function pagefind(pagefindConfig: { site?: string; outputSubdir?: string } = {}): AstroIntegration { | ||
const {site, outputSubdir = 'pagefind'} = pagefindConfig | ||
|
||
process.env.PAGEFIND_OUTPUT_SUBDIR = outputSubdir | ||
process.env.PAGEFIND_SITE = site | ||
|
||
let outDir: string; | ||
return { | ||
name: "pagefind", | ||
hooks: { | ||
"astro:config:setup": ({ config }) => { | ||
if (config.output === "server") { | ||
console.warn( | ||
"Output type `server` does not produce static *.html pages in its output and thus will not work with astro-pagefind integration.", | ||
); | ||
return; | ||
} | ||
|
||
if (config.adapter?.name.startsWith("@astrojs/vercel")) { | ||
outDir = fileURLToPath(new URL(".vercel/output/static/", config.root)); | ||
} else if (config.adapter?.name === "@astrojs/cloudflare") { | ||
outDir = fileURLToPath(new URL(config.base?.replace(/^\//, ""), config.outDir)); | ||
} else if (config.adapter?.name === "@astrojs/node" && config.output === "hybrid") { | ||
outDir = fileURLToPath(config.build.client!); | ||
} else { | ||
outDir = fileURLToPath(config.outDir); | ||
} | ||
}, | ||
"astro:server:setup": ({ server }) => { | ||
if (!outDir) { | ||
console.warn( | ||
"astro-pagefind could reliably determine the output directory. Search assets will not be served.", | ||
); | ||
return; | ||
} | ||
|
||
const serve = sirv(outDir, { | ||
dev: true, | ||
etag: true, | ||
}); | ||
server.middlewares.use((req, res, next) => { | ||
if (req.url?.startsWith(`/${outputSubdir}/`)) { | ||
serve(req, res, next); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
}, | ||
"astro:build:done": () => { | ||
if (!outDir) { | ||
console.warn( | ||
"astro-pagefind could reliably determine the output directory. Search index will not be built.", | ||
); | ||
return; | ||
} | ||
const cmd = `npx pagefind --site "${outDir}" --output-subdir "${outputSubdir}"`; | ||
execSync(cmd, { | ||
stdio: [process.stdin, process.stdout, process.stderr], | ||
}); | ||
}, | ||
}, | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
site: dist | ||
site: distoooooo | ||
output_subdir: assets/docs/pagefind |
This file contains 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