map.ts

2.7 kB · typescript · 57 lines

1import { readFileSync } from "node:fs";2import { basename, join, relative } from "node:path";3import { config, rawPath } from "../kit/git/git.ts";4import type { Row, Section, Site } from "../kit/ssg/build.ts";5import { front, summary, title } from "../kit/ssg/md.ts";67/* TYPES */89type Wiki = { name: string; lead: string; file: string; href: string };1011type Note = { title: string; lead: string; file: string; href: string };1213type Claim = { title: string; md: string; file: string; href: string };1415type Paper = { name: string; lead: string; file: string; href: string };1617type Lane = { name: string; blurb: string; md: string; href: string };1819type Post = { slug: string; name: string; lead: string };2021export type Lists = { wiki: Wiki[]; notes: Note[]; claims: Claim[]; papers: Paper[]; lanes: Lane[]; posts: Post[]; math: string[]; pages: string[] };2223/* CLAIMS */2425const CLAIM = /^- (\d{4}-\d{2}-\d{2}) \[(?:Proved|Verified|Conjecture|Refuted)\] /gm;2627function claimed(md: string) {28  const dates = [...md.matchAll(CLAIM)].map((m) => m[1]!).sort();29  return dates.length ? `${dates.length} claim${dates.length > 1 ? "s" : ""}, newest ${dates.at(-1)}` : "no claim yet";30}3132/* SECTIONS */3334export function sections(site: Site, lists: Lists): Section[] {35  const git = config(site);36  const served = new Set(site.routes.flatMap((route) => (route.urls ?? []).map((one) => one.route)));37  const row = (name: string, note: string, file: string, page: string): Row => {38    const raw = git ? `/${rawPath(relative(git.root, file))}` : "";39    return { name, note, href: served.has(raw) ? raw : page };40  };41  const read = (file: string, page: string, name?: string) => {42    const { data, body } = front(readFileSync(file, "utf8"));43    return row(name || data.title || title(body) || basename(file, ".md"), data.lead || summary(body), file, page);44  };45  return [46    { name: "Wiki, in prerequisite order", rows: lists.wiki.map((e) => row(e.name, e.lead, e.file, e.href)) },47    { name: "Research notes", rows: lists.notes.map((n) => row(n.title, n.lead, join(site.input("research").path, n.file), n.href)) },48    { name: "Claims", rows: lists.claims.map((c) => row(c.title, claimed(c.md), c.file, c.href)) },49    {50      name: "Papers",51      rows: [...lists.papers.map((p) => row(p.name, p.lead, p.file, p.href)), ...lists.lanes.map((p) => ({ name: p.name, note: p.blurb || summary(p.md), href: p.href }))],52    },53    { name: "Blog", rows: lists.posts.map((p) => row(p.name, p.lead, join(site.input("blog").path, p.slug, "index.md"), `/blog/${p.slug}/`)) },54    { name: "Math", rows: lists.math.map((file) => read(file, "/math/", "Math")) },55    { name: "About", rows: lists.pages.map((file) => read(file, `/${basename(file, ".md")}/`)) },56  ];57}