| 24 | |
| 25 | export const HTMLDeserializer = { |
| 26 | transform(node: DOMNode, options: HTMLDeserializerOptions = {}): Descendant[] { |
| 27 | const { element, text, matchNewline } = options |
| 28 | if (isDOMText(node)) { |
| 29 | const content = node.textContent ?? '' |
| 30 | if ( |
| 31 | matchNewline && |
| 32 | /^\s{0,}(\r\n|\n)+\s{0,}$/.test(content) && |
| 33 | (typeof matchNewline === 'boolean' || matchNewline(content)) |
| 34 | ) { |
| 35 | return [] |
| 36 | } |
| 37 | const dataArray = content.split(/\r\n|\n/) |
| 38 | return dataArray.map(data => ({ ...text, text: data })) |
| 39 | } |
| 40 | |
| 41 | const children = [] |
| 42 | for (const child of node.childNodes) { |
| 43 | children.push(...this.transform(child, { text })) |
| 44 | } |
| 45 | |
| 46 | switch (node.nodeName) { |
| 47 | case 'P': |
| 48 | case 'DIV': |
| 49 | if (children.length === 0) children.push({ text: '' }) |
| 50 | return [{ ...element, type: 'paragraph', children }] |
| 51 | default: |
| 52 | return children |
| 53 | } |
| 54 | }, |
| 55 | |
| 56 | with<T = HTMLDeserializerOptions>(transform: HTMLDeserializerWithTransform<T>, options: T) { |
| 57 | const { transform: t } = this |