import { assert } from '../lib/assert'; import type { Text, Table, List, ListItem } from 'mdast'; import type { Node, Parent } from 'unist'; import { XMap } from '../lib/XMap'; import { XNode } from '../lib/XNode'; import { DiffType, IdBox, NodeType } from '../types/base'; import { isOpaque, isInlineOnlyChildren } from '../utils/node'; import { nodeClone, nodeCloneTree } from './clone'; import { diffCodeLines } from './code-lines'; import { nodeLcs } from './lcs'; import { diffInlineChildren } from './inline-diff'; import { diffSentenceSegments, hasSentenceBoundary } from './sentence-diff'; function collectChildren(node: Node): Node[] { const result: Node[] = []; let child = node.children?.[0]; while (child) { result.push(child); child = child._next; } return result; } function hasAnyDiff(node: Node): boolean { if (node.data?.diff) { return true; } if (node.data?.diffOldContent) { return true; } if (node.data?.diffProperties) { return true; } if (node.children) { for (const child of node.children) { if (hasAnyDiff(child)) { return true; } } } return false; } function detectPropertyChanges(oldNode: Node, newNode: Node): void { const props: Record = {}; if (oldNode.type === NodeType.List && newNode.type === NodeType.List) { const o = oldNode as unknown as List; const n = newNode as unknown as List; if (o.ordered !== n.ordered) { props.ordered = { old: o.ordered, new: n.ordered }; } } if (oldNode.type === NodeType.ListItem && newNode.type === NodeType.ListItem) { const o = oldNode as unknown as ListItem; const n = newNode as unknown as ListItem; if (o.checked !== n.checked) { props.checked = { old: o.checked, new: n.checked }; } } if (Object.keys(props).length > 0) { if (!newNode.data) { newNode.data = {}; } newNode.data.diffProperties = props; } } function markUnchangedIfClean(node: Node): void { if (!hasAnyDiff(node)) { if (!node.data) { node.data = {}; } node.data.diffUnchanged = true; } } /* * Merge the new tree "nNew" with the old "nOld" using a depth-first * algorithm. * The produced tree will show the new tree with deleted nodes from the * old and inserted ones. * It will also show moved nodes by delete/add pairs. * This uses "Phase 5" semantics, but implements the merge algorithm * without notes from the paper. */ export function nodeMerge(_nOld: Node, xOldMap: XMap, _nNew: Node, xNewMap: XMap, idBox: IdBox /* max id in new tree */) { let xOld: XNode | undefined, xNew: XNode | undefined; let nn: Node; /* * Invariant: the current nodes are matched. * Start by putting that node into the current output. */ assert(_nNew && _nOld); assert(_nNew._xNode); assert(_nOld._xNode); xNew = _nNew._xNode; xOld = _nOld._xNode; assert(xOld.match); assert(xNew.match); assert(xNew.match === xOld.node); const n = nodeClone(_nNew, idBox.value) as Parent; n.children = []; /* Now walk through the children on both sides. */ let nOld = _nOld.children?.[0]; let nNew = _nNew.children?.[0]; while (nNew) { /* * Begin by flushing out all of the nodes that have been * deleted from the old tree at this level. * According to the paper, deleted nodes have no match. * These will leave us with old nodes that are in the * new tree (not necessarily at this level, though). */ while (nOld) { xOld = nOld._xNode; assert(xOld); if (xOld.match || nOld.type === NodeType.Text) { break; } nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nOld = nOld._next; } /* * Now flush inserted nodes. * According to the paper, these have no match. * This leaves us with nodes that are matched somewhere * (not necessarily at this level) with the old. */ while (nNew) { xNew = nNew._xNode; assert(xNew); if (xNew.match || nNew.type === NodeType.Text) { break; } nn = nodeCloneTree(nNew, idBox); nn._parent = n; nn.data!.diff = DiffType.Ins; n.children.push(nn); nNew = nNew._next; } /* * If both nodes are text nodes, then we want to run the * LCS algorithm on them. * This is an extension of the BULD algorithm. */ assert(xOld && xNew); if ( nOld && nNew && nOld.type == NodeType.Text && !xOld.match && nNew.type == NodeType.Text && !xNew.match ) { nodeLcs(nOld as Text, nNew as Text, n, idBox); nOld = nOld._next; nNew = nNew._next; } while (nOld) { xOld = nOld._xNode; assert(xOld); if (xOld.match) { break; } nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nOld = nOld._next; } while (nNew) { xNew = nNew._xNode; assert(xNew); if (xNew.match) { break; } nn = nodeCloneTree(nNew, idBox); nn._parent = n; nn.data!.diff = DiffType.Ins; n.children.push(nn); nNew = nNew._next; } /* Nothing more to do at this level? */ if (!nNew) { break; } /* * Now we take the current new node and see if it's a * match with a node in the current level. * If it is, then we can flush out old nodes (moved, * which we call deleted and re-inserted) until we get * to the matching one. * Then we'll be in lock-step with the old tree. */ xNew = nNew._xNode; assert(xNew); assert(xNew.match); /* Scan ahead to find a matching old. */ let nnOld = nOld; while (nnOld) { xOld = nnOld._xNode; assert(xOld); if (xNew.node === xOld.match) { break; } nnOld = nnOld._next; } /* * We did not find a match. * This means that the new node has been moved from * somewhere else in the tree. */ if (!nnOld) { nn = nodeCloneTree(nNew, idBox); nn._parent = n; nn.data!.diff = DiffType.Ins; n.children.push(nn); nNew = nNew._next; continue; } /* Match found: flush old nodes til the match. */ while (nOld) { xOld = nOld._xNode; assert(xOld); if (xNew.node === xOld.match) { break; } nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nOld = nOld._next; } assert(nOld); /* * Now we're in lock-step. * Do the recursive step between the matched pair. * Then continue on to the next nodes. */ if (nNew.type === NodeType.Text && nOld.type === NodeType.Text) { if ((nOld as Text).value === (nNew as Text).value) { nn = nodeCloneTree(nNew, idBox); nn._parent = n; n.children.push(nn); } else { nodeLcs(nOld as Text, nNew as Text, n, idBox); } } else if (isOpaque(nNew)) { assert(isOpaque(nOld)); nn = nodeCloneTree(nNew, idBox); nn._parent = n; n.children.push(nn); } else if (nNew.type === NodeType.Table && nOld.type === NodeType.Table) { const oldAlign = JSON.stringify((nOld as Table).align); const newAlign = JSON.stringify((nNew as Table).align); const oldAlignLen = (nOld as Table).align?.length ?? 0; const newAlignLen = (nNew as Table).align?.length ?? 0; if (oldAlign !== newAlign && oldAlignLen === newAlignLen) { nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nn = nodeCloneTree(nNew, idBox); nn._parent = n; nn.data!.diff = DiffType.Ins; n.children.push(nn); } else { nn = nodeMerge(nOld, xOldMap, nNew, xNewMap, idBox); nn._parent = n; n.children.push(nn); } } else if (nNew.type === NodeType.Code && nOld.type === NodeType.Code) { const oldLang = (nOld as any).lang as string | undefined; const newLang = (nNew as any).lang as string | undefined; const oldValue = (nOld as any).value as string; const newValue = (nNew as any).value as string; const isMermaid = oldLang === 'mermaid' || newLang === 'mermaid'; if (oldLang !== newLang || (isMermaid && oldValue !== newValue)) { nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nn = nodeCloneTree(nNew, idBox); nn._parent = n; nn.data!.diff = DiffType.Ins; n.children.push(nn); } else { nn = nodeCloneTree(nNew, idBox); nn._parent = n; if (oldValue !== newValue && newLang !== 'mermaid') { if (!nn.data) { nn.data = {}; } nn.data.diffOldContent = oldValue; // 行级差异必须在 merge 阶段写入: // similarity 阶段写会被 nodeCloneTree(nNew) 覆盖,这里才是最终归属。 const lines = diffCodeLines(oldValue, newValue); if (lines.some((l) => l.type !== 'unchanged')) { nn.data.diffLines = lines; } } n.children.push(nn); } } else if (isInlineOnlyChildren(nOld) && isInlineOnlyChildren(nNew)) { const oldInline = collectChildren(nOld); const newInline = collectChildren(nNew); let result = diffInlineChildren(oldInline, newInline, idBox, n); if (result.shouldFallback && hasSentenceBoundary(oldInline)) { result = diffSentenceSegments(oldInline, newInline, idBox, n); } if (!result.shouldFallback) { nn = nodeClone(nNew, ++idBox.value) as Parent; nn._parent = n; nn.children = result.nodes; n.children.push(nn); } else { nn = nodeMerge(nOld, xOldMap, nNew, xNewMap, idBox); nn._parent = n; n.children.push(nn); } } else { assert(!isOpaque(nOld)); nn = nodeMerge(nOld, xOldMap, nNew, xNewMap, idBox); nn._parent = n; n.children.push(nn); } nOld = nOld._next; nNew = nNew._next; } /* Flush remaining old nodes. */ while (nOld) { nn = nodeCloneTree(nOld, idBox); nn._parent = n; nn.data!.diff = DiffType.Del; n.children.push(nn); nOld = nOld._next; } detectPropertyChanges(_nOld, n); markUnchangedIfClean(n); return n; }