Skip to content
Draft
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
19 changes: 5 additions & 14 deletions packages/site/src/endpoints/list-txt.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
/**
* /list.txt — plaintext listing of every public item across the
* collection map. Same visibility filter as every other surface.
* content tree. Same visibility filter as every other surface.
*/
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";
import path from "node:path";
import config from "virtual:shellsite/config";
import { byTitle, entryId, publicEntries } from "../public.js";
import { buildListTxtLines, projectSiteTree } from "../site-tree.js";

export const GET: APIRoute = async () => {
const lines: string[] = [config.identity.name, ""];
for (const spec of config.console?.collections ?? []) {
const items = byTitle(
publicEntries(await getCollection(spec.collection)),
);
lines.push(
spec.dir,
...items.map((item) => `- ${(item.data as { title?: string } | undefined)?.title ?? entryId(item)}`),
"",
);
}
const tree = projectSiteTree(path.resolve("src", "content"));
const lines = buildListTxtLines(tree, config.identity.name);
return new Response(lines.join("\n"), {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
Expand Down
26 changes: 9 additions & 17 deletions packages/site/src/endpoints/llms-txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@
* Public items only; same visibility filter as every surface.
*/
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";
import path from "node:path";
import config from "virtual:shellsite/config";
import { byTitle, entryId, publicEntries } from "../public.js";
import { buildLlmsTxtLines, projectSiteTree } from "../site-tree.js";

export const GET: APIRoute = async () => {
const base = import.meta.env.BASE_URL.replace(/\/$/, "");
const lines: string[] = [`# ${config.identity.name}`, ""];
if (config.identity.tagline) lines.push(`> ${config.identity.tagline}`, "");
for (const spec of config.console?.collections ?? []) {
const items = byTitle(publicEntries(await getCollection(spec.collection)));
if (!items.length) continue;
lines.push(`## ${spec.dir}`, "");
for (const item of items) {
const data = (item.data ?? {}) as Record<string, unknown>;
const title = String(data.title ?? entryId(item));
const url = spec.urlPrefix ? `${base}${spec.urlPrefix}/${entryId(item)}` : `${base}/${spec.dir}/${entryId(item)}`;
const desc = data.summary ? `: ${String(data.summary)}` : "";
lines.push(`- [${title}](${url})${desc}`);
}
lines.push("");
}
const tree = projectSiteTree(path.resolve("src", "content"));
const lines = buildLlmsTxtLines(
tree,
config.identity.name,
config.identity.tagline,
base,
);
return new Response(lines.join("\n"), {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
Expand Down
49 changes: 18 additions & 31 deletions packages/site/src/endpoints/section-txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,30 @@
* as every surface. Parallels the marquee's /projects.txt.
*/
import type { APIRoute, GetStaticPaths } from "astro";
import { getCollection } from "astro:content";
import path from "node:path";
import config from "virtual:shellsite/config";
import type { ConsoleCollectionSpec } from "../config.js";
import { byTitle, entryId, publicEntries } from "../public.js";
import { buildSectionTxt, projectSiteTree } from "../site-tree.js";

export const getStaticPaths: GetStaticPaths = async () =>
(config.console?.collections ?? []).map((spec: ConsoleCollectionSpec) => ({
params: { section: spec.dir },
export const getStaticPaths: GetStaticPaths = async () => {
const tree = projectSiteTree(path.resolve("src", "content"));
return tree.sections.map((section) => ({
params: { section: section.dir },
}));
};

export const GET: APIRoute = async ({ params, site }) => {
const spec = (config.console?.collections ?? []).find((s: ConsoleCollectionSpec) => s.dir === params.section);
if (!spec) return new Response("not found\n", { status: 404 });
const origin = site ?? new URL("http://localhost");
const base = import.meta.env.BASE_URL.replace(/\/$/, "");

const items = byTitle(publicEntries(await getCollection(spec.collection as never)));
const records = items.map((item) => {
const data = (item.data ?? {}) as Record<string, unknown>;
const id = entryId(item);
const page = spec.urlPrefix ? `${base}${spec.urlPrefix}/${id}` : `${base}/${spec.dir}/${id}`;
const lines = [
`${"=".repeat(72)}`,
String(data.title ?? id),
`page: ${new URL(page, origin).href}`,
];
const meta = [data.kind, data.org, data.dates, data.venue, data.role, data.year, data.date]
.filter((v) => v != null && v !== "")
.join(" · ");
if (meta) lines.push(meta);
if (data.summary) lines.push(String(data.summary));
const body = (item as { body?: string }).body;
if (body) lines.push("", body);
return lines.join("\n");
});
return new Response(
`# ${spec.dir} — ${config.identity.name}\n\n${records.join("\n\n")}\n`,
{ headers: { "Content-Type": "text/plain; charset=utf-8" } },
const tree = projectSiteTree(path.resolve("src", "content"));
const body = buildSectionTxt(
tree,
String(params.section),
config.identity.name,
origin.href,
base,
);
if (!body) return new Response("not found\n", { status: 404 });
return new Response(body, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
};
27 changes: 27 additions & 0 deletions packages/site/src/plaintext-endpoints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from "node:assert/strict";
import path from "node:path";
import { test } from "node:test";
import {
buildListTxtLines,
buildLlmsTxtLines,
buildSectionTxt,
projectSiteTree,
} from "./site-tree.js";

const fixture = path.join(import.meta.dirname, "..", "..", "..", "apps", "demo", "src", "content");

test("plaintext endpoints project demo content tree", () => {
const tree = projectSiteTree(fixture);
const list = buildListTxtLines(tree, "Shellside").join("\n");
assert.match(list, /console-engine/);
assert.match(list, /^packages$/m);

const llms = buildLlmsTxtLines(tree, "Shellside", "tagline", "/shellside").join("\n");
assert.match(llms, /console-engine/);
assert.match(llms, /\[console-engine\]/);

const section = buildSectionTxt(tree, "packages", "Shellside", "https://example.test/", "/shellside");
assert.ok(section);
assert.match(section!, /console-engine/);
assert.match(section!, /page: https:\/\/example\.test\/shellside\/packages\/console-engine/);
});
105 changes: 105 additions & 0 deletions packages/site/src/site-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fs from "node:fs";
import path from "node:path";
import yaml from "js-yaml";
import { isPublicItem } from "@shellside/content-model";
import { parseFrontmatter } from "./frontmatter.js";

export type TreeEntry = {
Expand Down Expand Up @@ -168,3 +169,107 @@ export function projectSiteTree(contentDir: string): SiteTree {
tree.entries.sort((a, b) => (a.title ?? a.slug).localeCompare(b.title ?? b.slug));
return tree;
}

export function isPublicTreeEntry(entry: TreeEntry): boolean {
return isPublicItem({ data: entry.data });
}

export function publicTreeEntries(entries: readonly TreeEntry[]): TreeEntry[] {
return entries.filter(isPublicTreeEntry);
}

export function byTreeTitle(entries: readonly TreeEntry[]): TreeEntry[] {
return [...entries].sort((a, b) =>
String(a.title ?? a.slug).localeCompare(String(b.title ?? b.slug), "en"),
);
}

function treeEntryLabel(entry: TreeEntry): string {
return String(entry.title ?? entry.slug.split("/").pop() ?? entry.slug);
}

function treeEntryPageUrl(base: string, entry: TreeEntry, origin?: string): string {
const page = entry.data.url ? String(entry.data.url) : `${base}/${entry.slug}`;
return origin ? new URL(page, origin).href : page;
}

/** Lines for `/list.txt` from a projected content tree. */
export function buildListTxtLines(tree: SiteTree, siteName: string): string[] {
const lines: string[] = [siteName, ""];
const roots = byTreeTitle(publicTreeEntries(tree.entries));
if (roots.length) {
lines.push(...roots.map((e) => `- ${treeEntryLabel(e)}`), "");
}
for (const section of tree.sections) {
const items = byTreeTitle(publicTreeEntries(section.entries));
if (!items.length) continue;
lines.push(section.dir, ...items.map((e) => `- ${treeEntryLabel(e)}`), "");
}
return lines;
}

/** Lines for `/llms.txt` from a projected content tree. */
export function buildLlmsTxtLines(
tree: SiteTree,
siteName: string,
tagline: string | undefined,
base: string,
): string[] {
const lines: string[] = [`# ${siteName}`, ""];
if (tagline) lines.push(`> ${tagline}`, "");
const roots = byTreeTitle(publicTreeEntries(tree.entries));
if (roots.length) {
lines.push("## index", "");
for (const entry of roots) {
const title = treeEntryLabel(entry);
const url = treeEntryPageUrl(base, entry);
const desc = entry.data.summary ? `: ${String(entry.data.summary)}` : "";
lines.push(`- [${title}](${url})${desc}`);
}
lines.push("");
}
for (const section of tree.sections) {
const items = byTreeTitle(publicTreeEntries(section.entries));
if (!items.length) continue;
lines.push(`## ${section.dir}`, "");
for (const entry of items) {
const title = treeEntryLabel(entry);
const url = treeEntryPageUrl(base, entry);
const desc = entry.data.summary ? `: ${String(entry.data.summary)}` : "";
lines.push(`- [${title}](${url})${desc}`);
}
lines.push("");
}
return lines;
}

function formatSectionRecord(entry: TreeEntry, page: string): string {
const data = entry.data;
const title = treeEntryLabel(entry);
const lines = [`${"=".repeat(72)}`, title, `page: ${page}`];
const meta = [data.kind, data.org, data.dates, data.venue, data.role, data.year, data.date]
.filter((v) => v != null && v !== "")
.join(" · ");
if (meta) lines.push(meta);
if (data.summary) lines.push(String(data.summary));
if (entry.body) lines.push("", entry.body);
return lines.join("\n");
}

/** Body for `/<section>.txt`; `null` when the section is unknown. */
export function buildSectionTxt(
tree: SiteTree,
sectionDir: string,
siteName: string,
origin: string,
base: string,
): string | null {
const section = tree.sections.find((s) => s.dir === sectionDir);
if (!section) return null;
const items = byTreeTitle(publicTreeEntries(section.entries));
const records = items.map((entry) => {
const page = treeEntryPageUrl(base, entry, origin);
return formatSectionRecord(entry, page);
});
return `# ${section.dir} — ${siteName}\n\n${records.join("\n\n")}\n`;
}
Loading