Skip to content
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

🔍 Pass-through MyST search JSON from content-server (1 of 2) #467

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/curly-dryers-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@myst-theme/article': minor
'@myst-theme/book': minor
---

Add `myst.search.json` routing
8 changes: 7 additions & 1 deletion themes/article/app/routes/$slug[.json].tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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, {
Expand Down
7 changes: 7 additions & 0 deletions themes/article/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export async function getMystXrefJson(): Promise<Record<string, any> | null> {
return xrefs;
}

export async function getMystSearchJson(): Promise<Record<string, any> | 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';
Expand Down
8 changes: 7 additions & 1 deletion themes/book/app/routes/($project)_.$slug[.json].tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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, {
Expand Down
7 changes: 7 additions & 0 deletions themes/book/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export async function getMystXrefJson(): Promise<Record<string, any> | null> {
return xrefs;
}

export async function getMystSearchJson(): Promise<Record<string, any> | 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';
Expand Down
Loading