NGU-Web/src/lib/contents.ts
Aidan Dona 31afa889d6 Page features: directory and blog post tabs, image converter, version names
- Featured event badge: above the event's card from lg up, left out on phones
- Version names from the tag (The Region Cards Update) in the site and admin footers
- Tools -> Image converter (JPG, PNG, WebP in the browser) and Download as in Media
- People directory sections take several tags, a tab each (migration 078)
- Tab blocks split a blog post into tabs; contents follow the open tab (migration 079)
- The side nav sun fades out as the page scrolls into the footer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 19:37:07 -05:00

79 lines
3.2 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
CONTENTS
The headings in a list of content blocks, with the anchor id each
one gets: ContentBlocks puts the ids on the headings, and
PostContents lists them. Both call headingAnchors on the same
blocks, so the links always match.
An id is the heading's text as a slug ("Getting there" →
"getting-there"); a repeat gets -2, -3.
═══════════════════════════════════════════════════════════════ */
import type { ContentBlock } from './useContent.ts'
export type ContentsEntry = { id: string; text: string; level: 1 | 2 }
const slug = (text: string) =>
text
.toLowerCase()
.normalize('NFKD')
.replace(/[^\w\s-]/g, '')
.trim()
.replace(/[\s_]+/g, '-')
.replace(/-+/g, '-')
.slice(0, 60) || 'section'
/* One entry per heading or subheading block, in order, and the id
for each block index that is one. prefix keeps a blog post's tabs
apart (splitTabs): "Overview" in two tabs is two ids. */
export function headingAnchors(blocks?: ContentBlock[] | null, prefix = ''): {
entries: ContentsEntry[]
idAt: Map<number, string>
} {
const entries: ContentsEntry[] = []
const idAt = new Map<number, string>()
const seen = new Map<string, number>()
;(blocks ?? []).forEach((block, index) => {
if (block.type !== 'heading' && block.type !== 'subheading') return
const text = (block.text ?? '').trim()
if (!text) return
const base = slug(text)
const n = (seen.get(base) ?? 0) + 1
seen.set(base, n)
const id = `${prefix}${n === 1 ? base : `${base}-${n}`}`
idAt.set(index, id)
entries.push({ id, text, level: block.type === 'heading' ? 1 : 2 })
})
return { entries, idAt }
}
/* ── A blog post's tabs (079) ──────────────────────────────────
A 'tab' block starts a tab, titled by its text, holding the
blocks after it up to the next; the blocks before the first are
the intro above the tabs. Each tab's heading ids start with its
own, so the same heading in two tabs is two ids; the intro's
keep their plain ids. */
export type PostTab = { id: string; title: string; prefix: string; blocks: ContentBlock[] }
export function splitTabs(blocks?: ContentBlock[] | null): { intro: ContentBlock[]; tabs: PostTab[] } {
const intro: ContentBlock[] = []
const tabs: PostTab[] = []
const seen = new Map<string, number>()
for (const block of blocks ?? []) {
if (block.type === 'tab') {
const title = (block.text ?? '').trim() || `Part ${tabs.length + 1}`
const base = `tab-${slug(title)}`
const n = (seen.get(base) ?? 0) + 1
seen.set(base, n)
const id = n === 1 ? base : `${base}-${n}`
tabs.push({ id, title, prefix: `${id}-`, blocks: [] })
} else if (tabs.length) {
tabs[tabs.length - 1].blocks.push(block)
} else {
intro.push(block)
}
}
return { intro, tabs }
}