diff --git a/astro.config.mjs b/astro.config.mjs index be6a9e2..1b89fc3 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -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'})] }) diff --git a/lib/astro-pagefind/components/Search.astro b/lib/astro-pagefind/components/Search.astro new file mode 100644 index 0000000..ea5f46d --- /dev/null +++ b/lib/astro-pagefind/components/Search.astro @@ -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; +} + +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}/`; +--- + +
+
+ diff --git a/lib/astro-pagefind/components/Search.ts b/lib/astro-pagefind/components/Search.ts new file mode 100644 index 0000000..8dc8ff7 --- /dev/null +++ b/lib/astro-pagefind/components/Search.ts @@ -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"; diff --git a/lib/astro-pagefind/pagefind.ts b/lib/astro-pagefind/pagefind.ts new file mode 100644 index 0000000..028fdfc --- /dev/null +++ b/lib/astro-pagefind/pagefind.ts @@ -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], + }); + }, + }, + }; +} diff --git a/package-lock.json b/package-lock.json index fa1e341..9d392f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,16 +10,20 @@ "license": "ISC", "dependencies": { "@astrojs/tailwind": "^3.1.3", + "@pagefind/default-ui": "^1.0.4", "astro": "^2.10.15", + "pagefind": "^1.0.4", + "sirv": "^2.0.3", "tailwindcss": "^3.3.5" }, "devDependencies": { "@tailwindcss/typography": "^0.5.10", - "astro-pagefind": "^1.3.0", + "astro-pagefind": "1800joe/astro-pagefind#main", "concurrently": "^8.2.2", "eslint": "^8.53.0", "eslint-config-standard": "^17.1.0", - "prettier": "^2.8.8" + "prettier": "^2.8.8", + "typescript": "^5.2.2" }, "engines": { "node": ">=20", @@ -1068,71 +1072,65 @@ } }, "node_modules/@pagefind/darwin-arm64": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/darwin-arm64/-/darwin-arm64-1.0.3.tgz", - "integrity": "sha512-vsHDtvao3W4iFCxVc4S0BVhpj3E2MAoIVM7RmuQfGp1Ng22nGLRaMP6FguLO8TMabRJdvp4SVr227hL4WGKOHA==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/darwin-arm64/-/darwin-arm64-1.0.4.tgz", + "integrity": "sha512-2OcthvceX2xhm5XbgOmW+lT45oLuHqCmvFeFtxh1gsuP5cO8vcD8ZH8Laj4pXQFCcK6eAdSShx+Ztx/LsQWZFQ==", "cpu": [ "arm64" ], - "dev": true, "optional": true, "os": [ "darwin" ] }, "node_modules/@pagefind/darwin-x64": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/darwin-x64/-/darwin-x64-1.0.3.tgz", - "integrity": "sha512-NhEXHHYmB/hT6lx5rCcmnVTxH+uIkMAd43bzEqMwHQosqTZEIQfwihmV39H+m8yo7jFvz3zRbJNzhAh7G4PiwA==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/darwin-x64/-/darwin-x64-1.0.4.tgz", + "integrity": "sha512-xkdvp0D9Ld/ZKsjo/y1bgfhTEU72ITimd2PMMQtts7jf6JPIOJbsiErCvm37m/qMFuPGEq/8d+fZ4pydOj08HQ==", "cpu": [ "x64" ], - "dev": true, "optional": true, "os": [ "darwin" ] }, "node_modules/@pagefind/default-ui": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/default-ui/-/default-ui-1.0.3.tgz", - "integrity": "sha512-WieFJXvezyvjZh49I8j7a7Kz3LsXYY2Uep3IWvG5NG05mmiurURXjXc+KyrpIp/iAycSnjrC1TDJ8CdES/ee3A==", - "dev": true + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/default-ui/-/default-ui-1.0.4.tgz", + "integrity": "sha512-edkcaPSKq67C49Vehjo+LQCpT615v4d7JRhfGzFPccePvdklaL+VXrfghN/uIfsdoG+HoLI1PcYy2iFcB9CTkw==" }, "node_modules/@pagefind/linux-arm64": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/linux-arm64/-/linux-arm64-1.0.3.tgz", - "integrity": "sha512-RGsMt4AmGT8WxCSeP09arU7Za6Vf/We4TWHVSbY7vDMuwWql9Ngoib/q1cP9dIAIMdkXh9ePG/S3mGnJYsdzuQ==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/linux-arm64/-/linux-arm64-1.0.4.tgz", + "integrity": "sha512-jGBrcCzIrMnNxLKVtogaQyajVfTAXM59KlBEwg6vTn8NW4fQ6nuFbbhlG4dTIsaamjEM5e8ZBEAKZfTB/qd9xw==", "cpu": [ "arm64" ], - "dev": true, "optional": true, "os": [ "linux" ] }, "node_modules/@pagefind/linux-x64": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/linux-x64/-/linux-x64-1.0.3.tgz", - "integrity": "sha512-o+VCKaqImL42scSH1n5gUfppYSNyu3BuGTvtKKgWHmycbL+A3fkFH+ZOFbaLeN7LVTvJqJIOYbk4j2yaq9784Q==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/linux-x64/-/linux-x64-1.0.4.tgz", + "integrity": "sha512-LIn/QcvcEtLEBqKe5vpSbSC2O3fvqbRCWOTIklslqSORisCsvzsWbP6j+LYxE9q0oWIfkdMoWV1vrE/oCKRxHg==", "cpu": [ "x64" ], - "dev": true, "optional": true, "os": [ "linux" ] }, "node_modules/@pagefind/windows-x64": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@pagefind/windows-x64/-/windows-x64-1.0.3.tgz", - "integrity": "sha512-S+Yq4FyvXJm4F+iN/wRiLvEEF8Xs9lTKGtQGaRHXJslQyl65dytDDPIULXJXIadrDbnMrnTt4C2YHmEUIyUIHg==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@pagefind/windows-x64/-/windows-x64-1.0.4.tgz", + "integrity": "sha512-QlBCVeZfj9fc9sbUgdOz76ZDbeK4xZihOBAFqGuRJeChfM8pnVeH9iqSnXgO3+m9oITugTf7PicyRUFAG76xeQ==", "cpu": [ "x64" ], - "dev": true, "optional": true, "os": [ "win32" @@ -1160,8 +1158,7 @@ "node_modules/@polka/url": { "version": "1.0.0-next.23", "resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.23.tgz", - "integrity": "sha512-C16M+IYz0rgRhWZdCmK+h58JMv8vijAA61gmz2rspCSwKwzBebpdcsiUmwrtJRdphuY30i6BSLEOP8ppbNLyLg==", - "dev": true + "integrity": "sha512-C16M+IYz0rgRhWZdCmK+h58JMv8vijAA61gmz2rspCSwKwzBebpdcsiUmwrtJRdphuY30i6BSLEOP8ppbNLyLg==" }, "node_modules/@proload/core": { "version": "0.3.3", @@ -1660,18 +1657,13 @@ } }, "node_modules/astro-pagefind": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/astro-pagefind/-/astro-pagefind-1.3.0.tgz", - "integrity": "sha512-7VqoJnaW301KY+mjf1cGJTfKNI+zjdYxDXtBIE6aD77NKNPrOjxIXllt9Fo3wU84z1sNmaI+LwzK6S0Zm8t9RQ==", + "name": "root", + "resolved": "git+ssh://git@github.com/1800joe/astro-pagefind.git#c6ef8b544e474981dcfb1cb001109d6f136bb12e", "dev": true, - "dependencies": { - "@pagefind/default-ui": "^1.0.3", - "pagefind": "^1.0.3", - "sirv": "^2.0.3" - }, - "peerDependencies": { - "astro": "^2.0.4 || ^3.0.0" - } + "workspaces": [ + "packages/example", + "packages/astro-pagefind" + ] }, "node_modules/autoprefixer": { "version": "10.4.16", @@ -5608,7 +5600,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/mrmime/-/mrmime-1.0.1.tgz", "integrity": "sha512-hzzEagAgDyoU1Q6yg5uI+AorQgdvMCur3FcKf7NhMKWsaYg+RnbTyHRa/9IlLF9rf455MOCtcqqrQQ83pPP7Uw==", - "dev": true, "engines": { "node": ">=10" } @@ -5972,19 +5963,18 @@ } }, "node_modules/pagefind": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/pagefind/-/pagefind-1.0.3.tgz", - "integrity": "sha512-ws7kmMxW6OuxzsOjj3YAx6TYq/54MiE3wfyBM3J5CInbZyBBvM2Z8c8IYvnMkBcb5v2EoB9DewXEekOEiDRu5g==", - "dev": true, + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pagefind/-/pagefind-1.0.4.tgz", + "integrity": "sha512-oRIizYe+zSI2Jw4zcMU0ebDZm27751hRFiSOBLwc1OIYMrsZKk+3m8p9EVaOmc6zZdtqwwdilNUNxXvBeHcP9w==", "bin": { "pagefind": "lib/runner/bin.cjs" }, "optionalDependencies": { - "@pagefind/darwin-arm64": "1.0.3", - "@pagefind/darwin-x64": "1.0.3", - "@pagefind/linux-arm64": "1.0.3", - "@pagefind/linux-x64": "1.0.3", - "@pagefind/windows-x64": "1.0.3" + "@pagefind/darwin-arm64": "1.0.4", + "@pagefind/darwin-x64": "1.0.4", + "@pagefind/linux-arm64": "1.0.4", + "@pagefind/linux-x64": "1.0.4", + "@pagefind/windows-x64": "1.0.4" } }, "node_modules/parent-module": { @@ -7109,7 +7099,6 @@ "version": "2.0.3", "resolved": "https://registry.npmjs.org/sirv/-/sirv-2.0.3.tgz", "integrity": "sha512-O9jm9BsID1P+0HOi81VpXPoDxYP374pkOLzACAoyUQ/3OUVndNpsz6wMnY2z+yOxzbllCKZrM+9QrWsv4THnyA==", - "dev": true, "dependencies": { "@polka/url": "^1.0.0-next.20", "mrmime": "^1.0.0", @@ -7507,7 +7496,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.1.tgz", "integrity": "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==", - "dev": true, "engines": { "node": ">=6" } diff --git a/package.json b/package.json index d14b26f..b189d7b 100644 --- a/package.json +++ b/package.json @@ -42,16 +42,20 @@ }, "dependencies": { "@astrojs/tailwind": "^3.1.3", + "@pagefind/default-ui": "^1.0.4", "astro": "^2.10.15", + "pagefind": "^1.0.4", + "sirv": "^2.0.3", "tailwindcss": "^3.3.5" }, "devDependencies": { "@tailwindcss/typography": "^0.5.10", - "astro-pagefind": "^1.3.0", + "astro-pagefind": "1800joe/astro-pagefind#main", "concurrently": "^8.2.2", "eslint": "^8.53.0", "eslint-config-standard": "^17.1.0", - "prettier": "^2.8.8" + "prettier": "^2.8.8", + "typescript": "^5.2.2" }, "eslintConfig": { "root": true, diff --git a/pagefind.yml b/pagefind.yml index 4a6af63..c17a629 100644 --- a/pagefind.yml +++ b/pagefind.yml @@ -1,2 +1,2 @@ -site: dist +site: distoooooo output_subdir: assets/docs/pagefind diff --git a/src/components/Search.astro b/src/components/Search.astro index 7359cb3..2c45c17 100644 --- a/src/components/Search.astro +++ b/src/components/Search.astro @@ -1,5 +1,5 @@ --- -import Search from "astro-pagefind/components/Search"; +import Search from "../../lib/astro-pagefind/components/Search.astro"; const {id} = Astro.props ---