From da4a1d04e1af7344f2e8abaf1d55f19604937008 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Thu, 12 Sep 2024 17:25:43 +0100 Subject: [PATCH 1/2] feat: pass-through myst search json --- themes/article/app/routes/$slug[.json].tsx | 8 +++++++- themes/article/app/utils/loaders.server.ts | 7 +++++++ themes/book/app/routes/($project)_.$slug[.json].tsx | 8 +++++++- themes/book/app/utils/loaders.server.ts | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/themes/article/app/routes/$slug[.json].tsx b/themes/article/app/routes/$slug[.json].tsx index 9d9ae8bee..5d06aeff6 100644 --- a/themes/article/app/routes/$slug[.json].tsx +++ b/themes/article/app/routes/$slug[.json].tsx @@ -1,6 +1,6 @@ import type { LoaderFunction } from '@remix-run/node'; import { json } from '@remix-run/node'; -import { getMystXrefJson, getPage } from '~/utils/loaders.server'; +import { getMystXrefJson, getMystSearchJson, getPage } from '~/utils/loaders.server'; function api404(message = 'No API route found at this URL') { return json( @@ -20,6 +20,12 @@ export const loader: LoaderFunction = async ({ request, params }) => { if (!xref) return new Response('myst.xref.json not found', { status: 404 }); return json(xref); } + // Handle /myst.search.json as slug + else if (slug === 'myst.search') { + const xref = await getMystSearchJson(); + if (!xref) return new Response('myst.search.json not found', { status: 404 }); + return json(xref); + } const data = await getPage(request, { slug }).catch(() => null); if (!data) return api404('No page found at this URL.'); return json(data, { diff --git a/themes/article/app/utils/loaders.server.ts b/themes/article/app/utils/loaders.server.ts index bf9ab3063..fca278055 100644 --- a/themes/article/app/utils/loaders.server.ts +++ b/themes/article/app/utils/loaders.server.ts @@ -89,6 +89,13 @@ export async function getMystXrefJson(): Promise | null> { return xrefs; } +export async function getMystSearchJson(): Promise | null> { + const url = updateLink('/myst.search.json'); + const response = await fetch(url).catch(() => null); + if (!response || response.status === 404) return null; + return await response.json(); +} + export async function getFavicon(): Promise<{ contentType: string | null; buffer: Buffer } | null> { const config = await getConfig(); const url = updateLink(config.options?.favicon) || 'https://mystmd.org/favicon.ico'; diff --git a/themes/book/app/routes/($project)_.$slug[.json].tsx b/themes/book/app/routes/($project)_.$slug[.json].tsx index 62bb25d36..ae9d8c5c0 100644 --- a/themes/book/app/routes/($project)_.$slug[.json].tsx +++ b/themes/book/app/routes/($project)_.$slug[.json].tsx @@ -1,7 +1,7 @@ import { isFlatSite } from '@myst-theme/common'; import type { LoaderFunction } from '@remix-run/node'; import { json } from '@remix-run/node'; -import { getConfig, getMystXrefJson, getPage } from '~/utils/loaders.server'; +import { getConfig, getMystXrefJson, getMystSearchJson, getPage } from '~/utils/loaders.server'; function api404(message = 'No API route found at this URL') { return json( @@ -21,6 +21,12 @@ export const loader: LoaderFunction = async ({ request, params }) => { if (!xref) return new Response('myst.xref.json not found', { status: 404 }); return json(xref); } + // Handle /myst.search.json as slug + else if (slug === 'myst.search') { + const xref = await getMystSearchJson(); + if (!xref) return new Response('myst.search.json not found', { status: 404 }); + return json(xref); + } const config = await getConfig(); const flat = isFlatSite(config); const data = await getPage(request, { diff --git a/themes/book/app/utils/loaders.server.ts b/themes/book/app/utils/loaders.server.ts index 502827d87..a8b4052d3 100644 --- a/themes/book/app/utils/loaders.server.ts +++ b/themes/book/app/utils/loaders.server.ts @@ -89,6 +89,13 @@ export async function getMystXrefJson(): Promise | null> { return xrefs; } +export async function getMystSearchJson(): Promise | null> { + const url = updateLink('/myst.search.json'); + const response = await fetch(url).catch(() => null); + if (!response || response.status === 404) return null; + return await response.json(); +} + export async function getFavicon(): Promise<{ contentType: string | null; buffer: Buffer } | null> { const config = await getConfig(); const url = updateLink(config.options?.favicon) || 'https://mystmd.org/favicon.ico'; From 680059f50a94cbbb49375cc0d222de8d424c3bda Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Thu, 12 Sep 2024 18:09:32 +0100 Subject: [PATCH 2/2] chore: add changeset --- .changeset/curly-dryers-occur.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/curly-dryers-occur.md diff --git a/.changeset/curly-dryers-occur.md b/.changeset/curly-dryers-occur.md new file mode 100644 index 000000000..bf5688ec2 --- /dev/null +++ b/.changeset/curly-dryers-occur.md @@ -0,0 +1,6 @@ +--- +'@myst-theme/article': minor +'@myst-theme/book': minor +--- + +Add `myst.search.json` routing