|
| 1 | +import { DOMParser, Fragment, Schema } from "prosemirror-model"; |
| 2 | + |
| 3 | +/** |
| 4 | + * This function is used to parse the content of a list item external HTML node. |
| 5 | + * |
| 6 | + * Due to a change in how prosemirror-model handles parsing elements, we have additional flexibility in how we can "fit" content into a list item. |
| 7 | + * |
| 8 | + * We've decided to take an approach that is similar to Notion. The core rules of the algorithm are: |
| 9 | + * |
| 10 | + * - If the first child of an `li` has ONLY text content, take the text content, and flatten it into the list item. Subsequent siblings are carried over as is, as children of the list item. |
| 11 | + * - e.g. `<li><h1>Hello</h1><p>World</p></li> -> <li>Hello<blockGroup><blockContainer><p>World</p></blockContainer></blockGroup></li>` |
| 12 | + * - Else, take the content and insert it as children instead. |
| 13 | + * - e.g. `<li><img src="url" /></li> -> <li><p></p><blockGroup><blockContainer><img src="url" /></blockContainer></blockGroup></li>` |
| 14 | + * |
| 15 | + * This ensures that a list item's content is always valid ProseMirror content. Smoothing over differences between how external HTML may be rendered, and how ProseMirror expects content to be structured. |
| 16 | + */ |
| 17 | +export function getListItemContent( |
| 18 | + /** |
| 19 | + * The `li` element to parse. |
| 20 | + */ |
| 21 | + node: Node, |
| 22 | + /** |
| 23 | + * The schema to use for parsing. |
| 24 | + */ |
| 25 | + schema: Schema, |
| 26 | + /** |
| 27 | + * The name of the list item node. |
| 28 | + */ |
| 29 | + name: string |
| 30 | +): Fragment { |
| 31 | + /** |
| 32 | + * To actually implement this algorithm, we need to leverage ProseMirror's "fitting" algorithm. |
| 33 | + * Where, if content is parsed which doesn't fit into the current node, it will be moved into the parent node. |
| 34 | + * |
| 35 | + * This allows us to parse multiple pieces of content from within the list item (even though it normally would not match the list item's schema) and "throw" the excess content into the list item's children. |
| 36 | + * |
| 37 | + * The expected return value is a `Fragment` which contains the list item's content as the first element, and the children wrapped in a blockGroup node. Like so: |
| 38 | + * ``` |
| 39 | + * Fragment<[Node<Text>, Node<BlockGroup<Node<BlockContainer<any>>>>]> |
| 40 | + * ``` |
| 41 | + */ |
| 42 | + const parser = DOMParser.fromSchema(schema); |
| 43 | + |
| 44 | + if (!(node instanceof HTMLElement)) { |
| 45 | + // TODO: This will be unnecessary in the future: https://github.com/ProseMirror/prosemirror-model/commit/166188d4f9db96eb86fb7de62e72049c86c9dd79 |
| 46 | + throw new Error("Node is not an HTMLElement"); |
| 47 | + } |
| 48 | + |
| 49 | + // Move the `li` element's content into a new `div` element |
| 50 | + // This is a hacky workaround to not re-trigger list item parsing, |
| 51 | + // when we are looking to understand what the list item's content actually is, in terms of the schema. |
| 52 | + const clonedNodeDiv = document.createElement("div"); |
| 53 | + // Mark the `div` element as a `blockGroup` to make the parsing easier. |
| 54 | + clonedNodeDiv.setAttribute("data-node-type", "blockGroup"); |
| 55 | + // Clone all children of the `li` element into the new `div` element |
| 56 | + for (const child of Array.from(node.childNodes)) { |
| 57 | + clonedNodeDiv.appendChild(child.cloneNode(true)); |
| 58 | + } |
| 59 | + |
| 60 | + // Parses children of the `li` element into a `blockGroup` with `blockContainer` node children |
| 61 | + // This is the structure of list item children, so parsing into this structure allows for |
| 62 | + // easy separation of list item content from child list item content. |
| 63 | + let blockGroupNode = parser.parse(clonedNodeDiv, { |
| 64 | + topNode: schema.nodes.blockGroup.create(), |
| 65 | + }); |
| 66 | + |
| 67 | + // There is an edge case where a list item's content may contain a `<input>` element. |
| 68 | + // Causing it to be recognized as a `checkListItem`. |
| 69 | + // We want to skip this, and just parse the list item's content as is. |
| 70 | + if (blockGroupNode.firstChild?.firstChild?.type.name === "checkListItem") { |
| 71 | + // We skip the first child, by cutting it out of the `blockGroup` node. |
| 72 | + // and continuing with the rest of the algorithm. |
| 73 | + blockGroupNode = blockGroupNode.copy( |
| 74 | + blockGroupNode.content.cut( |
| 75 | + blockGroupNode.firstChild.firstChild.nodeSize + 2 |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Structure above is `blockGroup<blockContainer<any>[]>` |
| 81 | + // We want to extract the first `blockContainer` node's content, and see if it is a text block. |
| 82 | + const listItemsFirstChild = blockGroupNode.firstChild?.firstChild; |
| 83 | + |
| 84 | + // If the first node is not a text block, then it's first child is not compatible with the list item node. |
| 85 | + if (!listItemsFirstChild?.isTextblock) { |
| 86 | + // So, we do not try inserting anything into the list item, and instead return anything we found as children for the list item. |
| 87 | + return Fragment.from(blockGroupNode); |
| 88 | + } |
| 89 | + |
| 90 | + // If it is a text block, then we know it only contains text content. |
| 91 | + // So, we extract it, and insert its content into the `listItemNode`. |
| 92 | + // The remaining nodes in the `blockGroup` stay in-place. |
| 93 | + const listItemNode = schema.nodes[name].create( |
| 94 | + {}, |
| 95 | + listItemsFirstChild.content |
| 96 | + ); |
| 97 | + |
| 98 | + // We have `blockGroup<listItemsFirstChild, ...blockContainer<any>[]>` |
| 99 | + // We want to extract out the rest of the nodes as `<...blockContainer<any>[]>` |
| 100 | + const remainingListItemChildren = blockGroupNode.content.cut( |
| 101 | + // +2 for the `blockGroup` node's start and end markers |
| 102 | + listItemsFirstChild.nodeSize + 2 |
| 103 | + ); |
| 104 | + const hasRemainingListItemChildren = remainingListItemChildren.size > 0; |
| 105 | + |
| 106 | + if (hasRemainingListItemChildren) { |
| 107 | + // Copy the remaining list item children back into the `blockGroup` node. |
| 108 | + // This will make it back into: `blockGroup<...blockContainer<any>[]>` |
| 109 | + const listItemsChildren = blockGroupNode.copy(remainingListItemChildren); |
| 110 | + |
| 111 | + // Return the `listItem` node's content, then add the parsed children after to be lifted out by ProseMirror "fitting" algorithm. |
| 112 | + return listItemNode.content.addToEnd(listItemsChildren); |
| 113 | + } |
| 114 | + |
| 115 | + // Otherwise, just return the `listItem` node's content. |
| 116 | + return listItemNode.content; |
| 117 | +} |
0 commit comments