@grove-dev/astro 0.4.1 → 0.5.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ ---
2
+ /**
3
+ * TableOfContents.astro
4
+ *
5
+ * Collapsible on-page nav for a record's Markdown body.
6
+ *
7
+ * Behavior:
8
+ * - Mobile (<lg): rendered as a closed `<details>` dropdown.
9
+ * - Desktop (lg+): forced open via `lg:!open` so the nav stays
10
+ * visible while the user scrolls.
11
+ * - Heading IDs come from the markdown renderer; the TOC entries
12
+ * are pre-computed by `extractToc` and passed in.
13
+ *
14
+ * The scroll-spy + smooth-scroll logic lives in an inline script
15
+ * that finds every `data-toc-link` on the page and highlights the
16
+ * link whose target is closest to the top of the viewport (rAF-
17
+ * throttled). Active styling comes from the `data-toc-active`
18
+ * attribute toggled in the script + CSS rules in `styles.css`.
19
+ */
20
+ import type { TocEntry } from "@grove-dev/core";
21
+
22
+ interface Props {
23
+ /** Heading entries from `extractToc`. */
24
+ items: TocEntry[];
25
+ /** Optional accessible label override. */
26
+ label?: string;
27
+ }
28
+
29
+ const { items, label = "On this page" } = Astro.props;
30
+ const hasItems = items.length > 1;
31
+ ---
32
+
33
+ {hasItems && (
34
+ <details class="grove-toc mb-8 rounded-[var(--radius-lg)] border border-ink-200 bg-background open:bg-background dark:border-ink-800 lg:!open">
35
+ <summary class="cursor-pointer list-none px-4 py-3 text-2xs font-semibold uppercase tracking-wider text-ink-500 hover:text-ink-900 dark:text-ink-400 dark:hover:text-ink-100 lg:cursor-default lg:pointer-events-none">
36
+ <span class="inline-flex items-center gap-1.5">
37
+ <svg viewBox="0 0 16 16" width="11" height="11" aria-hidden="true" fill="currentColor" class="grove-toc-chevron transition-transform lg:hidden">
38
+ <path d="M6 4l4 4-4 4V4z" />
39
+ </svg>
40
+ {label}
41
+ <span class="ml-1 rounded-full bg-ink-100 px-1.5 py-0.5 text-[10px] font-medium text-ink-500 dark:bg-ink-800 dark:text-ink-300">
42
+ {items.length}
43
+ </span>
44
+ </span>
45
+ </summary>
46
+ <ol class="grove-toc-list mt-1 list-none border-t border-ink-200 p-2 text-sm dark:border-ink-800 lg:border-t-0 lg:p-0">
47
+ {items.map((entry) => (
48
+ <li class="grove-toc-item border-b border-ink-100 last:border-b-0 dark:border-ink-800 lg:border-b-0 lg:py-0.5">
49
+ <a
50
+ href={`#${entry.id}`}
51
+ class="grove-toc-link block truncate px-3 py-2 text-ink-500 no-underline transition-colors hover:text-ink-900 dark:text-ink-400 dark:hover:text-ink-100 lg:px-2 lg:py-1"
52
+ data-toc-link={entry.id}
53
+ >
54
+ {entry.text}
55
+ </a>
56
+ </li>
57
+ ))}
58
+ </ol>
59
+ </details>
60
+ )}
61
+
62
+ {hasItems && (
63
+ <script is:inline define:vars={{ tocIds: items.map((i) => i.id) }}>
64
+ (function () {
65
+ // Smooth scroll + history-replace on TOC link clicks.
66
+ function onClick(e) {
67
+ var link = e.target.closest("a[data-toc-link]");
68
+ if (!link) return;
69
+ e.preventDefault();
70
+ var id = link.getAttribute("data-toc-link");
71
+ var target = document.getElementById(id);
72
+ if (!target) return;
73
+ target.scrollIntoView({ behavior: "smooth", block: "start" });
74
+ history.replaceState(null, "", "#" + id);
75
+ }
76
+ document.addEventListener("click", onClick);
77
+
78
+ // Scroll-spy: rAF-throttled, toggles `data-toc-active` on the
79
+ // link whose heading is closest to the top of the viewport.
80
+ // CSS handles the active styling — no dynamic Tailwind classes.
81
+ var headings = tocIds
82
+ .map(function (id) { return document.getElementById(id); })
83
+ .filter(function (el) { return !!el; });
84
+ if (!headings.length) return;
85
+ var links = new Map();
86
+ document.querySelectorAll("a[data-toc-link]").forEach(function (a) {
87
+ links.set(a.getAttribute("data-toc-link"), a);
88
+ });
89
+ var active = null;
90
+ var ticking = false;
91
+ function update() {
92
+ ticking = false;
93
+ var top = window.scrollY + 140;
94
+ var current = headings[0].id;
95
+ for (var i = 0; i < headings.length; i++) {
96
+ if (headings[i].offsetTop <= top) current = headings[i].id;
97
+ else break;
98
+ }
99
+ if (current === active) return;
100
+ active = current;
101
+ links.forEach(function (a, id) {
102
+ if (id === current) a.setAttribute("data-toc-active", "");
103
+ else a.removeAttribute("data-toc-active");
104
+ });
105
+ }
106
+ function onScroll() {
107
+ if (ticking) return;
108
+ ticking = true;
109
+ requestAnimationFrame(update);
110
+ }
111
+ window.addEventListener("scroll", onScroll, { passive: true });
112
+ update();
113
+ })();
114
+ </script>
115
+ )}
@@ -38,7 +38,13 @@ import { fileURLToPath } from "node:url";
38
38
  import { dirname, resolve } from "node:path";
39
39
  import { marked } from "marked";
40
40
  import sanitizeHtml from "sanitize-html";
41
+ import { createHighlighter } from "shiki";
41
42
  import { prettySlug } from "../lib/display.js";
43
+ import {
44
+ readContentFile,
45
+ stripFrontmatter,
46
+ uniqueSlug,
47
+ } from "@grove-dev/core";
42
48
  import type {
43
49
  ProjectRecord,
44
50
  ResourceRecord,
@@ -287,13 +293,299 @@ export const fullProjects: ProjectRecord[] = fullRecords.filter(
287
293
  // missing. No page module needs to import `node:fs` anymore.
288
294
 
289
295
  const here = dirname(fileURLToPath(import.meta.url));
290
- function resolveContentPath(contentPath: string): string {
291
- const candidates = [
292
- resolve(here, "..", "..", contentPath),
293
- resolve(here, "..", "..", "..", contentPath),
294
- resolve(process.cwd(), contentPath),
295
- ];
296
- return candidates.find((p) => existsSync(p)) ?? "";
296
+
297
+ // ── Markdown rendering ────────────────────────────────────────────────
298
+ //
299
+ // Record bodies and consumer-authored pages both go through the
300
+ // same pipeline:
301
+ //
302
+ // body → marked.parse(body) → sanitizeHtml(html, allowlist)
303
+ //
304
+ // The differences are:
305
+ // - record bodies use the wide `RECORD_BODY_ALLOWLIST` (tables,
306
+ // images, task-list inputs, details/summary, kbd/mark/sub/sup,
307
+ // …) so a curated `.md` sidecar can use the full surface.
308
+ // - page bodies use the narrower `PAGE_BODY_ALLOWLIST` so a
309
+ // page-author's `.md` stays in a conservative prose-only space.
310
+ //
311
+ // Both paths run at module load so `getContentHtml(slug)` and
312
+ // `getPageContentHtml(name)` return pre-sanitized HTML with no
313
+ // per-request work.
314
+
315
+ /** Wide allowlist used for `ProjectRecord.content` Markdown bodies. */
316
+ const RECORD_BODY_ALLOWLIST = [
317
+ // Headings — full depth.
318
+ "h1", "h2", "h3", "h4", "h5", "h6",
319
+ // Block-level content.
320
+ "p", "br", "hr", "div", "blockquote",
321
+ // Lists.
322
+ "ul", "ol", "li",
323
+ // Definition lists.
324
+ "dl", "dt", "dd",
325
+ // Tables (GFM).
326
+ "table", "thead", "tbody", "tfoot", "tr", "th", "td",
327
+ "caption", "colgroup", "col",
328
+ // Inline formatting.
329
+ "strong", "em", "b", "i", "u", "s", "del", "ins",
330
+ "mark", "small", "sub", "sup", "kbd", "abbr",
331
+ // `<span>` is only here for Shiki's per-token wrappers inside
332
+ // highlighted code blocks. Sanitize-html's default is to strip
333
+ // any tag not in this list, so without `span` here the syntax
334
+ // highlights silently disappear from fenced code.
335
+ "span",
336
+ // Links + images.
337
+ "a", "img",
338
+ // Code.
339
+ "code", "pre",
340
+ // Forms (task-list checkboxes only).
341
+ "input", "label",
342
+ // Collapsibles and semantic blocks.
343
+ "details", "summary",
344
+ "figure", "figcaption",
345
+ "time",
346
+ ];
347
+
348
+ /** Narrow allowlist used for `content/pages/<page>.md`. */
349
+ const PAGE_BODY_ALLOWLIST = [
350
+ "h1", "h2", "h3", "h4",
351
+ "p", "br", "hr",
352
+ "ul", "ol", "li",
353
+ "strong", "em", "b", "i", "u", "s", "del",
354
+ "a", "code", "pre", "blockquote", "img",
355
+ ];
356
+
357
+ /**
358
+ * Attributes the sanitizer keeps per tag. The shared `* → ["id", "class"]`
359
+ * rule preserves the heading anchors the renderer adds and the
360
+ * Shiki syntax-highlighting classes (`shiki`, `language-bash`,
361
+ * …) on `<pre>` and `<code>` blocks. Image `loading/decoding`
362
+ * get forced to lazy/async to keep page weight down; `<input>`
363
+ * becomes a hardened disabled checkbox for GFM task lists.
364
+ */
365
+ const COMMON_BODY_ATTRIBUTES = {
366
+ a: ["href", "title", "rel", "target"],
367
+ img: ["src", "alt", "title", "width", "height", "loading", "decoding"],
368
+ th: ["scope", "colspan", "rowspan", "align"],
369
+ td: ["colspan", "rowspan", "align"],
370
+ col: ["span", "align"],
371
+ input: ["type", "checked", "disabled"],
372
+ label: ["for"],
373
+ abbr: ["title"],
374
+ time: ["datetime"],
375
+ details: ["open"],
376
+ div: ["class"], // table-wrap div the renderer injects
377
+ span: ["style"], // Shiki emits inline `--shiki-light` / `--shiki-dark` CSS variables
378
+ pre: ["class", "style"], // Shiki also tags the outer `<pre>` with theme classes
379
+ code: ["class"],
380
+ "*": ["id"],
381
+ };
382
+
383
+ /** `<a>` tag normalization — open in a new tab, no opener. */
384
+ const ANCHOR_TRANSFORM = sanitizeHtml.simpleTransform(
385
+ "a",
386
+ { rel: "noopener noreferrer", target: "_blank" },
387
+ true,
388
+ );
389
+
390
+ /** Hardens `<img>` to lazy-load + async-decode. */
391
+ const IMG_TRANSFORM = (
392
+ tagName: string,
393
+ attribs: Record<string, string>,
394
+ ) => ({
395
+ tagName,
396
+ attribs: {
397
+ ...attribs,
398
+ loading: attribs.loading ?? "lazy",
399
+ decoding: attribs.decoding ?? "async",
400
+ },
401
+ });
402
+
403
+ /** Forces `<input type="checkbox">` to be disabled so task lists
404
+ * render read-only at runtime (no on-page state to persist). */
405
+ const INPUT_TRANSFORM = (
406
+ tagName: string,
407
+ attribs: Record<string, string>,
408
+ ) => ({
409
+ tagName,
410
+ attribs: {
411
+ ...attribs,
412
+ type: "checkbox",
413
+ disabled: "",
414
+ ...(attribs.checked !== undefined ? { checked: "" } : {}),
415
+ },
416
+ });
417
+
418
+ /**
419
+ * GitHub-style slug for a heading. Matches `core/extractToc` so the
420
+ * IDs the renderer emits line up with the IDs a TOC consumer reads
421
+ * out of the same body — keeping deep links in lockstep.
422
+ */
423
+ function headingSlug(text: string): string {
424
+ return text
425
+ .toLowerCase()
426
+ .replace(/[‘’]/g, "")
427
+ .replace(/[^a-z0-9\s-]/g, "")
428
+ .replace(/\s+/g, "-")
429
+ .replace(/-+/g, "-")
430
+ .replace(/^-|-+$/g, "");
431
+ }
432
+
433
+ /**
434
+ * Render a Markdown string to sanitized HTML. The renderer is
435
+ * configured once (heading IDs + table wrap) and reused for both
436
+ * record and page bodies.
437
+ *
438
+ * Why pass `body` rather than a slug: this is a pure transform; the
439
+ * caller owns where the body came from (file, in-memory cache, …).
440
+ * `getContentHtml(slug)` is the per-record wrapper that reads the
441
+ * file and calls this.
442
+ */
443
+ export function renderMarkdownToSafeHtml(
444
+ body: string,
445
+ options: { allowlist?: string[] } = {},
446
+ ): string {
447
+ const allowlist = options.allowlist ?? RECORD_BODY_ALLOWLIST;
448
+ // Configure marked. GFM is on by default in v18 but set explicitly
449
+ // so the intent is visible; `breaks: false` keeps single newlines
450
+ // from becoming `<br>` per CommonMark.
451
+ //
452
+ // We register a small extension that:
453
+ // 1. Adds a stable `id` to every h2–h6 so the TOC sidebar can
454
+ // deep-link into the rendered body.
455
+ // 2. Wraps GFM tables in `<div class="grove-prose-table-wrap">`
456
+ // so the column-width overflow is contained inside a rounded
457
+ // border instead of breaking the layout.
458
+ marked.use({
459
+ gfm: true,
460
+ breaks: false,
461
+ renderer: {
462
+ heading({ tokens, depth }) {
463
+ const inline = this.parser.parseInline(tokens);
464
+ const plain = tokens
465
+ .map((t: { text?: string; raw?: string }) => t.text ?? t.raw ?? "")
466
+ .join("");
467
+ const id = depth === 1 ? "" : ` id="${uniqueSlug(headingSlug(plain) || "section", headingIds)}"`;
468
+ return `<h${depth}${id}>${inline}</h${depth}>\n`;
469
+ },
470
+ table(token: {
471
+ header: Array<{ tokens: unknown[] }>;
472
+ rows: Array<Array<{ tokens: unknown[] }>>;
473
+ }) {
474
+ const head = token.header
475
+ .map((cell) => `<th>${this.parser.parseInline(cell.tokens)}</th>`)
476
+ .join("");
477
+ const body = token.rows
478
+ .map((row) =>
479
+ `<tr>${row
480
+ .map((cell) => `<td>${this.parser.parseInline(cell.tokens)}</td>`)
481
+ .join("")}</tr>`,
482
+ )
483
+ .join("");
484
+ return `<div class="grove-prose-table-wrap"><table><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table></div>\n`;
485
+ },
486
+ // Fenced code blocks: route through Shiki instead of marked's
487
+ // default `<pre><code class="language-…">…</code></pre>`. The
488
+ // returned HTML already wraps in `<pre>` and `<code>`; we
489
+ // hand it to the sanitizer unchanged (Shiki's output is
490
+ // safe by construction — no script / event handlers).
491
+ code({ text, lang }) {
492
+ return highlightCode(text, lang ?? "") + "\n";
493
+ },
494
+ },
495
+ });
496
+
497
+ const rawHtml = marked.parse(body, { async: false }) as string;
498
+ return sanitizeHtml(rawHtml, {
499
+ allowedTags: allowlist,
500
+ allowedAttributes: COMMON_BODY_ATTRIBUTES,
501
+ allowedSchemes: ["http", "https", "mailto", "tel"],
502
+ allowedSchemesByTag: {
503
+ a: ["http", "https", "mailto", "tel"],
504
+ img: ["http", "https", "data"],
505
+ },
506
+ allowedSchemesAppliedToAttributes: ["href", "src"],
507
+ transformTags: {
508
+ a: ANCHOR_TRANSFORM,
509
+ img: IMG_TRANSFORM,
510
+ input: INPUT_TRANSFORM,
511
+ },
512
+ disallowedTagsMode: "discard",
513
+ });
514
+ }
515
+
516
+ /** Per-render counter so heading IDs don't collide within a body. */
517
+ const headingIds = new Map<string, number>();
518
+
519
+ // ── Shiki syntax highlighter ─────────────────────────────────────────
520
+ //
521
+ // Build-time tokenisation for fenced code blocks. Shiki returns a
522
+ // string of HTML — `<pre class="shiki …"><code>…</code></pre>` —
523
+ // and we drop it straight into the marked output where the code
524
+ // block would otherwise be a flat `<pre><code class="language-…">`.
525
+ //
526
+ // Two themes (light + dark) are loaded; the `defaultColor: false`
527
+ // flag asks Shiki to emit CSS variables (`--shiki-light`,
528
+ // `--shiki-dark`) instead of inline `color:` declarations. The
529
+ // selectors in `styles.css` then swap which set of variables wins
530
+ // when the consumer toggles `.dark` on `<html>`. Net effect:
531
+ // the same HTML is themed correctly in both modes without us
532
+ // re-rendering at runtime.
533
+ //
534
+ // We use `github-dark-default` rather than `github-dark` for the
535
+ // dark side: `github-dark` paints comments at `#6A737D`, which
536
+ // fails WCAG AA contrast (3.72:1) against our `--color-ink-950`
537
+ // `#171717` code-block background and trips the Lighthouse
538
+ // accessibility audit on any record that contains a `# …` shell
539
+ // comment. `github-dark-default` shifts comments to `#8B949E`,
540
+ // which clears 4.5:1 against the same background (~6.3:1).
541
+ //
542
+ // `getSingletonHighlighter()` lazily loads the engine on first use
543
+ // and caches it for every subsequent call — Shiki's WASM/grammar
544
+ // load takes a few hundred ms, so we want to amortise across all
545
+ // records in a build. The list of supported languages is curated
546
+ // (not `ALL`) so the build doesn't pull Shiki's full grammar pack.
547
+ const SUPPORTED_LANGS = [
548
+ "bash", "sh", "shell", "console",
549
+ "python", "py",
550
+ "javascript", "js", "jsx", "typescript", "ts", "tsx",
551
+ "json", "jsonc", "yaml", "yml", "toml",
552
+ "markdown", "md", "mdx",
553
+ "html", "css", "scss", "sass",
554
+ "sql", "graphql",
555
+ "dockerfile", "diff",
556
+ "rust", "go", "java", "kotlin", "swift", "ruby", "php",
557
+ "c", "cpp", "csharp", "objective-c",
558
+ "xml", "ini", "properties",
559
+ ];
560
+ const highlighter = await createHighlighter({
561
+ themes: ["github-light", "github-dark-default"],
562
+ langs: SUPPORTED_LANGS,
563
+ });
564
+
565
+ /**
566
+ * Highlight a fenced code block with Shiki. Returns the Shiki HTML
567
+ * wrapped in our `<pre>`-equivalent class so the existing
568
+ * `.grove-prose pre` rules continue to apply (padding, border,
569
+ * scroll behaviour).
570
+ */
571
+ function highlightCode(text: string, lang: string): string {
572
+ const normalized = lang?.toLowerCase() ?? "";
573
+ const safeLang = highlighter.getLoadedLanguages().includes(normalized)
574
+ ? normalized
575
+ : "text";
576
+ const html = highlighter.codeToHtml(text, {
577
+ lang: safeLang,
578
+ themes: { light: "github-light", dark: "github-dark-default" },
579
+ defaultColor: false,
580
+ });
581
+ // Strip the outer `<pre>`'s inline `background-color` so it doesn't
582
+ // fight our package's prose background. The per-token `<span>`
583
+ // `style="--shiki-light: …; --shiki-dark: …"` declarations must
584
+ // survive — they're what makes dual-theme work.
585
+ return html.replace(
586
+ /(<pre[^>]*?)\s+style="[^"]*"/g,
587
+ "$1",
588
+ );
297
589
  }
298
590
 
299
591
  const contentHtmlBySlug = new Map<string, string>();
@@ -301,36 +593,15 @@ for (const r of fullRecords) {
301
593
  if (r.kind !== "project") continue;
302
594
  const projectRecord = r as ProjectRecord;
303
595
  if (!projectRecord.content) continue;
304
- const path = resolveContentPath(projectRecord.content);
305
- if (!path) continue;
596
+ const read = readContentFile(projectRecord.content);
597
+ if (!read) continue;
598
+ // Each render needs a fresh headingIds map so the per-body
599
+ // collision counter starts at zero. Otherwise the second record
600
+ // in the loop would inherit the first record's counters and
601
+ // duplicate-heading IDs would collide across records.
602
+ headingIds.clear();
306
603
  try {
307
- const text = readFileSync(path, "utf8");
308
- // `async: false` keeps the call synchronous so we can populate
309
- // the map at module-load time. (marked v18 defaults to
310
- // Promise-returning; v9-17 also support this flag.)
311
- const rawHtml = marked.parse(text, { async: false }) as string;
312
- const safeHtml = sanitizeHtml(rawHtml, {
313
- allowedTags: [
314
- "h1", "h2", "h3", "h4",
315
- "p", "br", "hr",
316
- "ul", "ol", "li",
317
- "strong", "em", "b", "i", "u", "s", "del",
318
- "a", "code", "pre", "blockquote",
319
- ],
320
- allowedAttributes: {
321
- a: ["href", "title", "rel", "target"],
322
- },
323
- allowedSchemes: ["http", "https", "mailto", "tel"],
324
- allowedSchemesByTag: { a: ["http", "https", "mailto", "tel"] },
325
- transformTags: {
326
- a: sanitizeHtml.simpleTransform("a", {
327
- rel: "noopener noreferrer",
328
- target: "_blank",
329
- }, true),
330
- },
331
- disallowedTagsMode: "discard",
332
- });
333
- contentHtmlBySlug.set(r.slug, safeHtml);
604
+ contentHtmlBySlug.set(r.slug, renderMarkdownToSafeHtml(read.body));
334
605
  } catch {
335
606
  // Missing / unreadable / parse-failed content: skip the record
336
607
  // rather than render broken HTML. The page treats `null` as
@@ -364,35 +635,10 @@ export function getPageContentHtml(page: string): string | null {
364
635
  if (!path) return null;
365
636
 
366
637
  try {
367
- const markdown = readFileSync(path, "utf8").replace(
368
- /^---\r?\n[\s\S]*?\r?\n---\r?\n?/,
369
- "",
370
- );
371
- const rawHtml = marked.parse(markdown, { async: false }) as string;
372
- return sanitizeHtml(rawHtml, {
373
- allowedTags: [
374
- "h1", "h2", "h3", "h4",
375
- "p", "br", "hr",
376
- "ul", "ol", "li",
377
- "strong", "em", "b", "i", "u", "s", "del",
378
- "a", "code", "pre", "blockquote", "img",
379
- ],
380
- allowedAttributes: {
381
- a: ["href", "title", "rel", "target"],
382
- img: ["src", "alt", "title", "width", "height", "loading"],
383
- },
384
- allowedSchemes: ["http", "https", "mailto", "tel"],
385
- allowedSchemesByTag: {
386
- a: ["http", "https", "mailto", "tel"],
387
- img: ["http", "https"],
388
- },
389
- transformTags: {
390
- a: sanitizeHtml.simpleTransform("a", {
391
- rel: "noopener noreferrer",
392
- target: "_blank",
393
- }, true),
394
- },
395
- disallowedTagsMode: "discard",
638
+ const markdown = stripFrontmatter(readFileSync(path, "utf8"));
639
+ headingIds.clear();
640
+ return renderMarkdownToSafeHtml(markdown, {
641
+ allowlist: PAGE_BODY_ALLOWLIST,
396
642
  });
397
643
  } catch {
398
644
  return null;