Give blog posts an optional table of contents

Blog posts -> Show contents (migration 043, off by default): with two
or more headings, the post's headings sit in a sticky column beside
it with the one being read lit (an IntersectionObserver, not a scroll
listener), and fold out at the top on phones. Content-block headings
now carry anchor ids, so a link can point at one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 09:21:48 -05:00
parent f59ab314e0
commit 92e7938b64
10 changed files with 264 additions and 44 deletions

View file

@ -10,20 +10,22 @@
section is where posts are listed, so "back" goes there.
═══════════════════════════════════════════════════════════════ */
import { Link, useParams } from 'react-router-dom'
import { Link, useParams } from "react-router-dom"
import PageShell, { type ShellSection } from '../components/PageShell.tsx'
import PageState from '../components/PageState.tsx'
import ContentBlocks from '../components/ContentBlocks.tsx'
import PeopleTiles from '../components/PeopleTiles.tsx'
import { mediaUrl } from '../lib/media.ts'
import { postDate, useBlogPost } from '../lib/useBlog.ts'
import { accentText, BRAND } from '../lib/color.ts'
import PageShell, { type ShellSection } from "../components/PageShell.tsx"
import PageState from "../components/PageState.tsx"
import ContentBlocks from "../components/ContentBlocks.tsx"
import PeopleTiles from "../components/PeopleTiles.tsx"
import { mediaUrl } from "../lib/media.ts"
import { postDate, useBlogPost } from "../lib/useBlog.ts"
import { accentText, BRAND } from "../lib/color.ts"
import PostContents from "../components/PostContents.tsx"
import { headingAnchors } from "../lib/contents.ts"
const BODY = 'var(--color-muted)'
const BODY = "var(--color-muted)"
/* Where "back" goes: the section on the Community page. */
const BACK = { to: '/community#blog', label: 'Community Blog' }
const BACK = { to: "/community#blog", label: "Community Blog" }
export default function BlogPost() {
const { id } = useParams()
@ -43,12 +45,16 @@ export default function BlogPost() {
)
}
// Two headings or more make a list worth showing.
const contents = post.show_contents ? headingAnchors(post.blocks).entries : []
const withContents = contents.length >= 2
const date = postDate(post.published_on)
const cover = mediaUrl(post.cover_image)
const byline = [
...post.authors.map((author) => author.name),
...(post.guest ? [post.guest.name] : []),
].join(', ')
].join(", ")
const authors = [
...post.authors.map((author) => ({
@ -58,50 +64,89 @@ export default function BlogPost() {
pronouns: author.pronouns,
photo: mediaUrl(author.photo),
})),
...(post.guest ? [{ name: post.guest.name, title: 'Guest author', photo: mediaUrl(post.guest.photo) }] : []),
...(post.guest
? [
{
name: post.guest.name,
title: "Guest author",
photo: mediaUrl(post.guest.photo),
},
]
: []),
]
const sections: ShellSection[] = [
{
id: 'post',
title: 'Post',
id: "post",
title: "Post",
accent: BRAND,
background: 'var(--color-surface)',
background: "var(--color-surface)",
content: (
<article className="mx-auto max-w-3xl space-y-8 px-6">
<div className="flex flex-wrap items-center gap-x-4 gap-y-2 text-sm" style={{ color: BODY }}>
{date && <time dateTime={post.published_on ?? undefined}>{date}</time>}
{byline && <span>By {byline}</span>}
{post.tags.map((tag) => (
<span
key={tag}
className="rounded-full px-3 py-0.5 text-xs font-semibold"
style={{ background: 'var(--color-tint)', color: accentText(BRAND) }}
<div
className={
withContents
? "mx-auto grid max-w-6xl gap-8 px-6 lg:grid-cols-[14rem_minmax(0,1fr)] lg:gap-12"
: "contents"
}
>
{withContents && <PostContents entries={contents} />}
<article
className={
withContents
? "min-w-0 max-w-3xl space-y-8"
: "mx-auto max-w-3xl space-y-8 px-6"
}
>
<div
className="flex flex-wrap items-center gap-x-4 gap-y-2 text-sm"
style={{ color: BODY }}
>
{date && (
<time dateTime={post.published_on ?? undefined}>{date}</time>
)}
{byline && <span>By {byline}</span>}
{post.tags.map((tag) => (
<span
key={tag}
className="rounded-full px-3 py-0.5 text-xs font-semibold"
style={{
background: "var(--color-tint)",
color: accentText(BRAND),
}}
>
{tag}
</span>
))}
<Link
to={BACK.to}
className="ml-auto hover:underline"
style={{ color: accentText(BRAND) }}
>
{tag}
</span>
))}
<Link to={BACK.to} className="ml-auto hover:underline" style={{ color: accentText(BRAND) }}>
{BACK.label}
</Link>
</div>
{BACK.label}
</Link>
</div>
{cover && (
<img src={cover} alt={post.cover_alt ?? ""} className="aspect-video w-full rounded-2xl object-cover" />
)}
{cover && (
<img
src={cover}
alt={post.cover_alt ?? ""}
className="aspect-video w-full rounded-2xl object-cover"
/>
)}
<ContentBlocks blocks={post.blocks} accent={BRAND} />
</article>
<ContentBlocks blocks={post.blocks} accent={BRAND} />
</article>
</div>
),
},
]
if (authors.length > 0) {
sections.push({
id: 'authors',
title: authors.length === 1 ? 'About the author' : 'About the authors',
id: "authors",
title: authors.length === 1 ? "About the author" : "About the authors",
accent: BRAND,
background: 'var(--color-tint)',
background: "var(--color-tint)",
content: (
<div className="mx-auto max-w-6xl px-6">
<PeopleTiles size="md" accent={BRAND} people={authors} />
@ -110,5 +155,11 @@ export default function BlogPost() {
})
}
return <PageShell title={post.title} intro={post.excerpt ?? undefined} sections={sections} />
return (
<PageShell
title={post.title}
intro={post.excerpt ?? undefined}
sections={sections}
/>
)
}