( element: Plate.MdxBlockElement | Plate.MdxInlineElement, parentField: RichTextField, flatten: boolean, imageCallback: (url: string) => string )
| 42 | directiveType: string; |
| 43 | }; |
| 44 | export function stringifyProps( |
| 45 | element: Plate.MdxBlockElement | Plate.MdxInlineElement, |
| 46 | parentField: RichTextField, |
| 47 | flatten: boolean, |
| 48 | imageCallback: (url: string) => string |
| 49 | ): { |
| 50 | attributes: MdxJsxAttribute[]; |
| 51 | children: Md.BlockContent[] | Md.PhrasingContent[]; |
| 52 | useDirective: boolean; |
| 53 | directiveType: string; |
| 54 | } { |
| 55 | const attributes: MdxJsxAttribute[] = []; |
| 56 | const children: Md.Content[] = []; |
| 57 | let template: RichTextTemplate | undefined; |
| 58 | let useDirective = false; |
| 59 | let directiveType = 'leaf'; |
| 60 | template = parentField.templates?.find((template) => { |
| 61 | if (typeof template === 'string') { |
| 62 | throw new Error('Global templates not supported'); |
| 63 | } |
| 64 | return template.name === element.name; |
| 65 | }); |
| 66 | if (!template) { |
| 67 | template = parentField.templates?.find((template) => { |
| 68 | const templateName = template?.match?.name; |
| 69 | return templateName === element.name; |
| 70 | }); |
| 71 | } |
| 72 | if (!template || typeof template === 'string') { |
| 73 | throw new Error(`Unable to find template for JSX element ${element.name}`); |
| 74 | } |
| 75 | if (template.fields.find((f) => f.name === 'children')) { |
| 76 | directiveType = 'block'; |
| 77 | } |
| 78 | useDirective = !!template.match; |
| 79 | Object.entries(element.props).forEach(([name, value]) => { |
| 80 | if (typeof template === 'string') { |
| 81 | throw new Error(`Unable to find template for JSX element ${name}`); |
| 82 | } |
| 83 | const field = template?.fields?.find((field) => field.name === name); |
| 84 | if (!field) { |
| 85 | if (name === 'children') { |
| 86 | return; |
| 87 | } |
| 88 | return; |
| 89 | // throw new Error(`No field definition found for property ${name}`) |
| 90 | } |
| 91 | switch (field.type) { |
| 92 | case 'reference': |
| 93 | if (field.list) { |
| 94 | if (Array.isArray(value)) { |
| 95 | attributes.push({ |
| 96 | type: 'mdxJsxAttribute', |
| 97 | name, |
| 98 | value: { |
| 99 | type: 'mdxJsxAttributeValueExpression', |
| 100 | value: `[${value.map((item) => `"${item}"`).join(', ')}]`, |
| 101 | }, |
no test coverage detected