- 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]>
119 lines
5.5 KiB
Markdown
119 lines
5.5 KiB
Markdown
# Rich text (Lexical) — RenderRichText
|
|
|
|
Renderowanie pól `richText` (edytor Lexical) na froncie. Plugin dostarcza
|
|
`RenderRichText` — cienki wrapper nad OFICJALNYM konwerterem Payload
|
|
(`@payloadcms/richtext-lexical/react`). To RenderBlocks rich textu: plugin
|
|
dostarcza silnik, projekt dostarcza wygląd.
|
|
|
|
> **NIGDY nie pisz własnego renderera Lexical** (switch po `node.type`, flagi
|
|
> bitowe `node.format & 1`). To powiela to, co Payload już robi, pomija typy
|
|
> węzłów, rozjeżdża się przy aktualizacjach Lexical i kusi do
|
|
> `dangerouslySetInnerHTML` (XSS) oraz hardkodu locale (`.pl`). Użyj
|
|
> RenderRichText.
|
|
|
|
## Podstawowe użycie (RSC)
|
|
|
|
```tsx
|
|
import { RenderRichText } from '@intecion/ipal-kit/rsc'
|
|
|
|
<RenderRichText data={page.content} className="prose prose-slate max-w-none" />
|
|
```
|
|
|
|
`data` to pole richText z dokumentu — JUŻ zlokalizowane (Payload zwraca wartość
|
|
bieżącego locale). Zero obsługi kluczy per język, zero hardkodu `.pl`.
|
|
|
|
## Własne style per węzeł (converters)
|
|
|
|
Styl zostaje w projekcie (Tailwind), parsowanie w pluginie. Nadpisujesz JSX
|
|
konkretnych węzłów przez `converters`:
|
|
|
|
```tsx
|
|
import { RenderRichText } from '@intecion/ipal-kit/rsc'
|
|
import type { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'
|
|
|
|
const converters: JSXConvertersFunction = ({ defaultConverters }) => ({
|
|
...defaultConverters,
|
|
heading: ({ node, nodesToJSX }) => {
|
|
const children = nodesToJSX({ nodes: node.children })
|
|
const cls: Record<string, string> = {
|
|
h2: 'font-display text-2xl font-semibold mt-10 mb-4',
|
|
h3: 'font-display text-xl font-semibold mt-7 mb-3',
|
|
}
|
|
const Tag = node.tag as 'h1' | 'h2' | 'h3' | 'h4'
|
|
return <Tag className={cls[node.tag] ?? ''}>{children}</Tag>
|
|
},
|
|
paragraph: ({ node, nodesToJSX }) => (
|
|
<p className="text-base leading-relaxed mb-5">{nodesToJSX({ nodes: node.children })}</p>
|
|
),
|
|
})
|
|
|
|
<RenderRichText data={page.content} converters={converters} />
|
|
```
|
|
|
|
Spread `defaultConverters` — nadpisujesz tylko te węzły, które chcesz. Reszta
|
|
(listy, tabele, obrazy, linki) działa z oficjalnej obsługi Payload.
|
|
|
|
## Spis treści i kotwice nagłówków (1.11.1)
|
|
|
|
Długie dokumenty (polityka prywatności, regulamin) — spis treści z linkami do sekcji.
|
|
Id nagłówków w spisie i w treści liczy ta sama funkcja, więc się nie rozjadą.
|
|
|
|
```tsx
|
|
// config bloku
|
|
import { buildTableOfContentsField } from '@intecion/ipal-kit'
|
|
fields: [buildTableOfContentsField(), { name: 'content', type: 'richText' }] // grupa tableOfContents: show, label
|
|
|
|
// komponent bloku (RSC)
|
|
import { extractHeadings, RenderRichText } from '@intecion/ipal-kit/rsc'
|
|
import { TableOfContents } from '@intecion/ipal-kit/client'
|
|
|
|
const items = block.tableOfContents?.show ? extractHeadings(block.content) : [] // [{ id, text, level }]
|
|
{items.length ? <TableOfContents items={items} label={block.tableOfContents?.label} classNames={tocClasses} /> : null}
|
|
<RenderRichText data={block.content} converters={converters} headingIds={{ className: 'scroll-mt-24' }} />
|
|
```
|
|
|
|
- `extractHeadings(content, { levels: ['h2'] })` — nagłówki w kolejności z tekstu; id = `toSlug(tekst)`,
|
|
powtórzenia `-2`, `-3`, puste nagłówki pomijane. Także z roota: `import { extractHeadings, headingText } from '@intecion/ipal-kit'`.
|
|
- `RenderRichText headingIds` — `true` (h2) albo `{ levels, className }`. Twój konwerter `heading` dalej
|
|
renderuje nagłówek; plugin dokłada `id` (i `className`, np. `scroll-mt-*` pod przyklejonym nagłówkiem).
|
|
- `<TableOfContents>` — bez stylów: `<nav aria-label={label}>` + `<ol>` linków. Serwer renderuje całą listę
|
|
(działa bez JS); w przeglądarce link czytanej sekcji dostaje `aria-current="location"` i `classNames.linkActive`.
|
|
`classNames`: `nav`, `label`, `list`, `item`, `link`, `linkActive`; `offset` (domyślnie 0.3 wysokości ekranu);
|
|
`data-ipal="toc"`, `data-level` na pozycjach.
|
|
- `buildTableOfContentsField()` — przełącznik „Show table of contents” + lokalizowana etykieta (typ `TableOfContentsSettings`).
|
|
Nowe pole w bloku = migracja (`pnpm payload migrate:create`).
|
|
|
|
## lexicalToMarkdown — richText → Markdown (dla AI)
|
|
|
|
Do czytelności dla agentów AI (Markdown per strona, llms-full) potrzebny jest
|
|
tekst, nie JSX. `lexicalToMarkdown` konwertuje pole richText na czysty Markdown:
|
|
|
|
```ts
|
|
import { lexicalToMarkdown } from '@intecion/ipal-kit'
|
|
const md = lexicalToMarkdown(page.content) // '## Nagłówek\n\nAkapit **bold**…'
|
|
```
|
|
|
|
Obsługuje nagłówki, akapity, bold/kursywa/kod/strikethrough, linki, listy
|
|
(zagnieżdżone), cytaty, `---`, obrazy (upload). To tekstowy brat `RenderRichText`
|
|
— ten sam wejściowy kształt, inne wyjście. **Samodzielny walker**, NIE zależy od
|
|
konkretnego eksportu MD z `@payloadcms/richtext-lexical` (te zmieniają się między
|
|
wersjami), więc bump Payloada go nie zepsuje. Nieznany węzeł degraduje się do
|
|
swojej treści, nie rzuca.
|
|
|
|
Dla całej strony (bloki, nie pojedyncze pole) użyj `blocksToMarkdown` /
|
|
`createMarkdownRenderer` — patrz [content.md](./content.md).
|
|
|
|
## Zależność
|
|
|
|
Oficjalny konwerter jest częścią `@payloadcms/richtext-lexical`, które projekt
|
|
Payload już ma (to edytor). Zero dodatkowych zależności.
|
|
|
|
## Czego to unika (vs ręczny renderer)
|
|
|
|
- **XSS** — brak `dangerouslySetInnerHTML`
|
|
- **Hardkod locale** — brak `val.alt.pl` (dane już zlokalizowane)
|
|
- **Brakujące węzły** — oficjalny konwerter obsługuje wszystkie (zagnieżdżone
|
|
listy, upload, relacje, horizontal rule…)
|
|
- **Rozjazd przy aktualizacji** — Payload utrzymuje konwerter, nie Ty
|
|
- **`any`** — typowane (SerializedEditorState)
|