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:
parent
f59ab314e0
commit
92e7938b64
10 changed files with 264 additions and 44 deletions
|
|
@ -39,6 +39,7 @@ import { Anchor, Inline, Paragraphs } from './InlineText.tsx'
|
|||
import VideoPlayer from './VideoPlayer.tsx'
|
||||
import ScheduleBlock from './ScheduleBlock.tsx'
|
||||
import { accentText, BRAND } from '../lib/color.ts'
|
||||
import { headingAnchors } from '../lib/contents.ts'
|
||||
|
||||
const BODY = 'var(--color-muted)'
|
||||
|
||||
|
|
@ -52,11 +53,14 @@ export default function ContentBlocks({
|
|||
className?: string
|
||||
}) {
|
||||
if (!blocks?.length) return null
|
||||
// Headings get ids so a post's contents (PostContents) and a shared
|
||||
// link (#getting-there) can point at them.
|
||||
const { idAt } = headingAnchors(blocks)
|
||||
|
||||
return (
|
||||
<div className={`space-y-6 ${className}`}>
|
||||
{blocks.map((block, index) => (
|
||||
<Block key={block.id ?? index} block={block} accent={accent} />
|
||||
<Block key={block.id ?? index} block={block} accent={accent} anchor={idAt.get(index)} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
|
@ -71,20 +75,23 @@ function Caption({ text }: { text?: string | null }) {
|
|||
)
|
||||
}
|
||||
|
||||
function Block({ block, accent }: { block: ContentBlock; accent: string }) {
|
||||
// Clear of the fixed header when jumped to.
|
||||
const ANCHOR_MARGIN = { scrollMarginTop: 'calc(var(--header-h, 5rem) + 1rem)' }
|
||||
|
||||
function Block({ block, accent, anchor }: { block: ContentBlock; accent: string; anchor?: string }) {
|
||||
const text = block.text ?? ''
|
||||
|
||||
switch (block.type) {
|
||||
case 'heading':
|
||||
return (
|
||||
<h3 className="text-2xl font-bold" style={{ color: accentText(accent) }}>
|
||||
<h3 id={anchor} className="text-2xl font-bold" style={{ color: accentText(accent), ...ANCHOR_MARGIN }}>
|
||||
{text}
|
||||
</h3>
|
||||
)
|
||||
|
||||
case 'subheading':
|
||||
return (
|
||||
<h4 className="text-lg font-semibold" style={{ color: accentText(accent) }}>
|
||||
<h4 id={anchor} className="text-lg font-semibold" style={{ color: accentText(accent), ...ANCHOR_MARGIN }}>
|
||||
{text}
|
||||
</h4>
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue