| 95 | |
| 96 | export const MarkdownDeserializer = { |
| 97 | transform(node: Content | Root, options: MarkdownDeserializerOptions = {}): Descendant[] { |
| 98 | const { element, text } = options |
| 99 | const children = [] |
| 100 | switch (node.type) { |
| 101 | case 'root': |
| 102 | for (const child of node.children) { |
| 103 | children.push(...this.transform(child)) |
| 104 | } |
| 105 | return children |
| 106 | case 'paragraph': |
| 107 | for (const child of node.children) { |
| 108 | children.push(...this.transform(child, { text })) |
| 109 | } |
| 110 | return [ |
| 111 | { |
| 112 | ...element, |
| 113 | type: 'paragraph', |
| 114 | children: children.length === 0 ? [{ text: '' }] : children, |
| 115 | }, |
| 116 | ] |
| 117 | |
| 118 | case 'html': |
| 119 | if (this._consumedHTMLNodes.has(node)) { |
| 120 | if (this._consumedHTMLNodes.get(node) === true) this._consumedHTMLNodes.delete(node) |
| 121 | return [] |
| 122 | } |
| 123 | const nodes = HTMLDeserializer.transform( |
| 124 | new DOMParser().parseFromString(node.value, 'text/html').body, |
| 125 | options, |
| 126 | ) |
| 127 | return nodes.length === 0 ? [{ text: node.value }] : nodes |
| 128 | |
| 129 | case 'text': |
| 130 | return [Object.assign({}, this._text, text, { text: node.value })] |
| 131 | default: |
| 132 | if ('children' in node) { |
| 133 | for (const child of node.children) { |
| 134 | children.push(...this.transform(child, { text })) |
| 135 | } |
| 136 | } else if ('value' in node) { |
| 137 | children.push(Object.assign(text ?? {}, { text: node.value })) |
| 138 | } |
| 139 | return children |
| 140 | } |
| 141 | }, |
| 142 | |
| 143 | with<T = MarkdownDeserializerOptions>( |
| 144 | transform: MarkdownDeserializerWithTransform<T>, |