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 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';