- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
81 lines
3.4 KiB
TypeScript
81 lines
3.4 KiB
TypeScript
import type { Field, GroupField } from 'payload'
|
|
|
|
import { toSlug } from '../slug/buildSlugField.js'
|
|
|
|
export type HeadingLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
|
|
|
|
/** One table-of-contents entry: the heading's anchor id, its plain text and level. */
|
|
export type HeadingItem = { id: string; level: HeadingLevel; text: string }
|
|
|
|
export type ExtractHeadingsOptions = {
|
|
/** Heading levels that get an anchor and a TOC entry. Default ['h2'] (sections of a document). */
|
|
levels?: readonly HeadingLevel[]
|
|
}
|
|
|
|
type LexicalNode = { children?: LexicalNode[]; tag?: string; text?: string; type?: string }
|
|
|
|
/** Plain text of a Lexical node (a heading with bold parts, links…). */
|
|
export function headingText(node: unknown): string {
|
|
const n = node as LexicalNode
|
|
if (typeof n?.text === 'string') {return n.text}
|
|
return (n?.children ?? []).map(headingText).join('')
|
|
}
|
|
|
|
/**
|
|
* Walks the document in reading order and gives every heading of the chosen
|
|
* levels an id: slug of its text (toSlug), repeats get "-2", "-3"; an empty
|
|
* heading gets "section". Keyed by the node object, so RenderRichText puts
|
|
* exactly these ids on exactly these headings.
|
|
*/
|
|
export function headingIds(data: unknown, { levels = ['h2'] }: ExtractHeadingsOptions = {}): Map<object, HeadingItem> {
|
|
const wanted = new Set<string>(levels)
|
|
const seen = new Map<string, number>()
|
|
const out = new Map<object, HeadingItem>()
|
|
const walk = (node: LexicalNode) => {
|
|
if (node.type === 'heading' && node.tag && wanted.has(node.tag)) {
|
|
const text = headingText(node).replace(/\s+/g, ' ').trim()
|
|
const base = toSlug(text) || 'section'
|
|
const count = (seen.get(base) ?? 0) + 1
|
|
seen.set(base, count)
|
|
out.set(node, { id: count === 1 ? base : `${base}-${count}`, level: node.tag as HeadingLevel, text })
|
|
return
|
|
}
|
|
for (const child of node.children ?? []) {walk(child)}
|
|
}
|
|
const root = (data as { root?: LexicalNode } | null | undefined)?.root
|
|
if (root) {walk(root)}
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* Table-of-contents entries of a rich-text field — the same ids RenderRichText
|
|
* gives the headings with `headingIds`. Empty headings are left out.
|
|
*
|
|
* const items = extractHeadings(block.content) // h2 only
|
|
* <TableOfContents items={items} label={block.tableOfContents?.label} />
|
|
* <RenderRichText data={block.content} headingIds />
|
|
*/
|
|
export function extractHeadings(data: unknown, options: ExtractHeadingsOptions = {}): HeadingItem[] {
|
|
return [...headingIds(data, options).values()].filter((h) => h.text.length > 0)
|
|
}
|
|
|
|
/**
|
|
* Panel switch for a table of contents on a text section: `show` + the label
|
|
* above the list ("Contents"), localized. Named type `TableOfContentsSettings`.
|
|
*
|
|
* fields: [buildTableOfContentsField(), { name: 'content', type: 'richText' }]
|
|
*/
|
|
export function buildTableOfContentsField({ name = 'tableOfContents' }: { name?: string } = {}): GroupField {
|
|
const fields: Field[] = [
|
|
{ name: 'show', type: 'checkbox', admin: { description: 'List of the section headings (H2) with links to them — for long documents.' }, label: 'Show table of contents' },
|
|
{
|
|
name: 'label',
|
|
type: 'text',
|
|
admin: { condition: (_: unknown, s: { show?: boolean } | undefined) => Boolean(s?.show), description: 'Heading above the list, e.g. "Contents". Empty → none.' },
|
|
label: 'Label',
|
|
localized: true,
|
|
},
|
|
]
|
|
return { name, type: 'group', fields, interfaceName: 'TableOfContentsSettings', label: 'Table of contents' }
|
|
}
|