import { createElement as h } from 'react' import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, test } from 'vitest' import { buildTableOfContentsField, extractHeadings } from '../src/modules/richtext/headings.js' import { RenderRichText } from '../src/modules/richtext/RenderRichText.js' import { TableOfContents } from '../src/modules/richtext/TableOfContents.js' const text = (t: string, format = 0) => ({ type: 'text', detail: 0, format, mode: 'normal', style: '', text: t, version: 1 }) const heading = (tag: string, ...children: object[]) => ({ type: 'heading', children, direction: 'ltr', format: '', indent: 0, tag, version: 1 }) const paragraph = (t: string) => ({ type: 'paragraph', children: [text(t)], direction: 'ltr', format: '', indent: 0, textFormat: 0, version: 1 }) const doc = { root: { type: 'root', children: [ heading('h2', text('Administrator '), text('danych', 1)), paragraph('Treść'), heading('h3', text('Szczegóły')), heading('h2', text('Pliki cookies')), heading('h2', text('Pliki cookies')), heading('h2'), ], direction: 'ltr', format: '', indent: 0, version: 1, }, } as never describe('table of contents', () => { test('extractHeadings: h2 in order, slug ids, repeats numbered, empty skipped', () => { expect(extractHeadings(doc)).toEqual([ { id: 'administrator-danych', level: 'h2', text: 'Administrator danych' }, { id: 'pliki-cookies', level: 'h2', text: 'Pliki cookies' }, { id: 'pliki-cookies-2', level: 'h2', text: 'Pliki cookies' }, ]) expect(extractHeadings(doc, { levels: ['h2', 'h3'] }).map((h) => h.id)).toContain('szczegoly') expect(extractHeadings(null)).toEqual([]) }) test('RenderRichText headingIds: the same ids on the headings, project converter kept', () => { const html = renderToStaticMarkup( h(RenderRichText, { converters: ({ defaultConverters }) => ({ ...defaultConverters, heading: ({ node, nodesToJSX }) => h(node.tag, { className: 'title' }, ...nodesToJSX({ nodes: node.children })), }), data: doc, headingIds: { className: 'scroll-mt-24' }, }), ) expect(html).toContain('