import { Heading, Paragraph, Text } from '@toegang-voor-iedereen/document-spec-types';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
// ...
export const TextDto = z.object({
type: z.literal('text'),
text: z.string(),
marks: z.array(
z.union([BoldMarkDto, ItalicMarkDto, IdentifierMarkDto])
)
}).transform(({ text, marks }): Text => {
return {
id: marks.find(mark => mark.type === 'identifier')?.attrs.identifier ?? uuidv4();
type: 'https://spec.nldoc.nl/Resource/Text',
text,
styling: marksToStyling(marks),
descriptors: []
}
})
export const HeadingDto = z.object({
type: z.literal('heading'),
attrs: z.object({
identifier: z.string(),
level: z.number(),
}),
content: z.array(
Te
)
}).transform(({ attrs, content }): Heading => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Heading',
level: attrs.level,
descriptors: [],
children: content
}
});
export const ParagraphDto = z.object({
type: z.literal('paragraph'),
attrs: z.object({
identifier: z.string(),
}),
content: z.array(TextDto)
}).transform(({ attrs, content }): Paragraph => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Paragraph',
descriptors: [],
children: content
}
});
export const DocumentDto = z.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string().nullable(),
}),
content: z.array(
z.union([HeadingDto, ParagraphDto])
)
}).transform(({ attrs, content }): Document => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Document',
descriptors: [],
children: content
}
}