Something went wrong. Try again.
my own indieAuth provider! indiko.dunkirk.sh/docs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import { readFileSync, statSync } from "node:fs";import { join } from "node:path";import hljs from "highlight.js/lib/core";import css from "highlight.js/lib/languages/css";import http from "highlight.js/lib/languages/http";import json from "highlight.js/lib/languages/json";import xml from "highlight.js/lib/languages/xml";import { Marked, type RendererObject, type Tokens } from "marked";import { parseFrontmatter } from "./frontmatter";
hljs.registerLanguage("json", json);hljs.registerLanguage("http", http);hljs.registerLanguage("css", css);hljs.registerLanguage("xml", xml);hljs.registerLanguage("html", xml);
const MD_PATH = join(import.meta.dir, "../content/docs.md");
interface Frontmatter { title: string; subtitle: string;}
function docsFrontmatter(raw: string): { fm: Frontmatter; body: string } { const { fm, body } = parseFrontmatter(raw); return { fm: { title: fm.title ?? "documentation", subtitle: fm.subtitle ?? "", }, body, };}
function slugify(text: string): string { return text .toLowerCase() .replace(/<[^>]+>/g, "") .replace(/[^\w\s-]/g, "") .trim() .replace(/\s+/g, "-");}
// Custom hljs grammar for the stylized request examples (not wire-format// http). hljs composes the tokens correctly, so no fragile regex stacking.// keyword=method, string=url, number=param-name, attribute=header-name.hljs.registerLanguage("oauthreq", () => ({ name: "oauthreq", contains: [ { className: "keyword", begin: "^[A-Z]{2,7}(?=\\s)" }, // GET / POST { className: "attribute", begin: "^[A-Z][A-Za-z-]+(?=:)" }, // Header-Name: { className: "string", begin: "https?://[^\\s?&]+" }, // url (stops at query) { className: "number", begin: "(?<=[?&])[A-Za-z_][A-Za-z0-9_]*(?==)" }, // param name ],}));
function highlight(code: string, lang: string): string { if (lang === "http") { return hljs.highlight(code, { language: "oauthreq" }).value; } if (lang && hljs.getLanguage(lang)) { return hljs.highlight(code, { language: lang }).value; } return hljs.highlightAuto(code).value;}
interface TocEntry { level: number; id: string; text: string;}
export function renderDocs(origin: string): { title: string; subtitle: string; html: string; toc: TocEntry[];} { const raw = readFileSync(MD_PATH, "utf8"); const { fm, body } = docsFrontmatter(raw);
// Substitute server-known values so examples show the real origin const source = body.replaceAll("{{origin}}", origin);
const toc: TocEntry[] = []; const usedIds = new Map<string, number>();
const renderer: RendererObject = { heading({ tokens, depth }: Tokens.Heading): string { const text = this.parser.parseInline(tokens); const plain = text.replace(/<[^>]+>/g, ""); // Dedupe slugs so repeated headings (e.g. "key features") get unique anchors const base = slugify(plain); const seen = usedIds.get(base) ?? 0; usedIds.set(base, seen + 1); const id = seen === 0 ? base : `${base}-${seen}`; if (depth === 2 || depth === 3) { toc.push({ level: depth, id, text: plain }); } return `<h${depth} id="${id}">${text}</h${depth}>\n`; },
code({ text, lang }: Tokens.Code): string { const language = lang ?? ""; const highlighted = highlight(text, language); const langClass = language ? ` language-${language}` : ""; return `<pre><code class="hljs${langClass}">${highlighted}</code></pre>\n`; },
// > blockquotes become styled info-boxes (matches current docs look) blockquote({ tokens }: Tokens.Blockquote): string { const inner = this.parser.parse(tokens); return `<div class="info-box">${inner}</div>\n`; },
codespan({ text }: Tokens.Codespan): string { // marked passes codespan text through unescaped, so a backticked // `<link>` would parse as a real element. Escape it back to text. const escaped = text .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">"); return `<code>${escaped}</code>`; },
table({ header, rows }: Tokens.Table): string { const head = header .map((cell) => `<th>${this.parser.parseInline(cell.tokens)}</th>`) .join(""); const bodyRows = rows .map( (row) => `<tr>${row .map((cell) => `<td>${this.parser.parseInline(cell.tokens)}</td>`) .join("")}</tr>`, ) .join(""); return `<table><thead><tr>${head}</tr></thead><tbody>${bodyRows}</tbody></table>\n`; }, };
const marked = new Marked({ renderer }); let html = marked.parse(source, { async: false }) as string;
// Bespoke injection points the md leaves as placeholders. The button // snippet and demo button are client-rendered (they're code that documents // code, and the demo is interactive), so we drop markers the client fills. html = html.replace( /<p>:::demo-button<\/p>/, `<div class="demo-button-wrapper"><a href="#" id="demoButton" class="indiko-demo-button">Sign in with Indiko</a></div>`, ); html = html.replace( /<p>:::button-code<\/p>/, `<pre><code id="buttonCode"></code></pre>\n<button type="button" id="copyButtonCode" class="btn-brutal copy-btn">copy button code</button>`, );
return { title: fm.title, subtitle: fm.subtitle, html, toc };}
export function getRawMarkdown(): string { return readFileSync(MD_PATH, "utf8");}
export function docsLastModified(): Date { return statSync(MD_PATH).mtime;}