|
| 1 | +// @ts-check |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @typedef {{ type: string, author: string, date: string, internalId: string }} TrackedChangeEntry |
| 6 | + * @typedef {{ lastTrackedChange: TrackedChangeEntry | null }} WalkContext |
| 7 | + */ |
| 8 | + |
| 9 | +const TRACKED_CHANGE_NAMES = new Set(['w:ins', 'w:del']); |
| 10 | + |
| 11 | +/** |
| 12 | + * Non-content marker elements that can appear between the two halves of a Word |
| 13 | + * replacement without breaking the pairing. These are range/annotation markers |
| 14 | + * that carry no document content. |
| 15 | + * |
| 16 | + * Any element NOT in this set (e.g. w:r, w:hyperlink, w:sdt) is treated as |
| 17 | + * content and resets the pairing state so unrelated revisions in the same |
| 18 | + * paragraph are never falsely linked. |
| 19 | + */ |
| 20 | +const PAIRING_TRANSPARENT_NAMES = new Set([ |
| 21 | + 'w:commentRangeStart', |
| 22 | + 'w:commentRangeEnd', |
| 23 | + 'w:bookmarkStart', |
| 24 | + 'w:bookmarkEnd', |
| 25 | + 'w:proofErr', |
| 26 | + 'w:permStart', |
| 27 | + 'w:permEnd', |
| 28 | + 'w:moveFromRangeStart', |
| 29 | + 'w:moveFromRangeEnd', |
| 30 | + 'w:moveToRangeStart', |
| 31 | + 'w:moveToRangeEnd', |
| 32 | +]); |
| 33 | + |
| 34 | +/** |
| 35 | + * Two adjacent tracked changes form a Word replacement pair when they are |
| 36 | + * opposite types (delete vs insert) from the same author at the same timestamp. |
| 37 | + * |
| 38 | + * @param {TrackedChangeEntry} previous |
| 39 | + * @param {{ type: string, author: string, date: string }} current |
| 40 | + * @returns {boolean} |
| 41 | + */ |
| 42 | +function isReplacementPair(previous, current) { |
| 43 | + return previous.type !== current.type && previous.author === current.author && previous.date === current.date; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Assigns an internal UUID to a tracked change element. Adjacent replacement |
| 48 | + * halves (w:del + w:ins with matching author/date) share the same UUID. |
| 49 | + * |
| 50 | + * @param {object} element XML element (w:ins or w:del) |
| 51 | + * @param {Map<string, string>} idMap Accumulates Word ID → internal UUID |
| 52 | + * @param {WalkContext} context Mutable walk state for replacement pairing |
| 53 | + * @param {boolean} insideTrackedChange Whether this element is nested in another tracked change |
| 54 | + */ |
| 55 | +function assignInternalId(element, idMap, context, insideTrackedChange) { |
| 56 | + const wordId = String(element.attributes?.['w:id'] ?? ''); |
| 57 | + if (!wordId) return; |
| 58 | + |
| 59 | + // Nested tracked changes get their own UUID but are never paired. |
| 60 | + if (insideTrackedChange) { |
| 61 | + if (!idMap.has(wordId)) { |
| 62 | + idMap.set(wordId, uuidv4()); |
| 63 | + } |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const current = { |
| 68 | + type: element.name, |
| 69 | + author: element.attributes?.['w:author'] ?? '', |
| 70 | + date: element.attributes?.['w:date'] ?? '', |
| 71 | + }; |
| 72 | + |
| 73 | + if (context.lastTrackedChange && isReplacementPair(context.lastTrackedChange, current)) { |
| 74 | + // Second half of a replacement — share the first half's UUID, but only |
| 75 | + // if this w:id hasn't already been mapped. A reused id that was already |
| 76 | + // part of an earlier pair must keep its original mapping. |
| 77 | + if (!idMap.has(wordId)) { |
| 78 | + idMap.set(wordId, context.lastTrackedChange.internalId); |
| 79 | + } |
| 80 | + context.lastTrackedChange = null; |
| 81 | + } else { |
| 82 | + // Reuse an existing mapping when the same w:id appears more than once |
| 83 | + // (Word reuses tracked-change ids across the document). Minting a fresh |
| 84 | + // UUID here would overwrite the earlier entry and break any replacement |
| 85 | + // pair that was already recorded for this id. |
| 86 | + const internalId = idMap.get(wordId) ?? uuidv4(); |
| 87 | + idMap.set(wordId, internalId); |
| 88 | + context.lastTrackedChange = { ...current, internalId }; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Recursively walks XML elements, assigning internal UUIDs to every tracked |
| 94 | + * change and pairing adjacent replacements. |
| 95 | + * |
| 96 | + * @param {Array} elements |
| 97 | + * @param {Map<string, string>} idMap |
| 98 | + * @param {WalkContext} context |
| 99 | + * @param {boolean} [insideTrackedChange] |
| 100 | + */ |
| 101 | +function walkElements(elements, idMap, context, insideTrackedChange = false) { |
| 102 | + if (!Array.isArray(elements)) return; |
| 103 | + |
| 104 | + for (const element of elements) { |
| 105 | + if (TRACKED_CHANGE_NAMES.has(element.name)) { |
| 106 | + assignInternalId(element, idMap, context, insideTrackedChange); |
| 107 | + |
| 108 | + if (element.elements) { |
| 109 | + // Descend with an isolated context so content inside a tracked change |
| 110 | + // cannot clear the outer replacement candidate. |
| 111 | + walkElements(element.elements, idMap, { lastTrackedChange: null }, /* insideTrackedChange */ true); |
| 112 | + } |
| 113 | + } else { |
| 114 | + // Content-bearing elements break replacement pairing. Only non-content |
| 115 | + // markers (comment/bookmark/permission ranges) are transparent. |
| 116 | + if (!PAIRING_TRANSPARENT_NAMES.has(element.name)) { |
| 117 | + context.lastTrackedChange = null; |
| 118 | + } |
| 119 | + |
| 120 | + if (element.elements) { |
| 121 | + walkElements(element.elements, idMap, context, insideTrackedChange); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Builds a map from OOXML `w:id` values to stable internal UUIDs by scanning |
| 129 | + * `word/document.xml`. |
| 130 | + * |
| 131 | + * Word tracked replacements use separate `w:id` values for the delete and |
| 132 | + * insert halves. This function detects adjacent opposite-type changes with |
| 133 | + * matching author and date and maps both halves to the same internal UUID so |
| 134 | + * the editor can resolve them as a single logical change. |
| 135 | + * |
| 136 | + * Must run before comment import so all consumers — translators, comment |
| 137 | + * helpers, and the tracked-change resolver — see a fully populated map. |
| 138 | + * |
| 139 | + * @param {object} docx Parsed DOCX package |
| 140 | + * @returns {Map<string, string>} Word `w:id` → internal UUID |
| 141 | + */ |
| 142 | +export function buildTrackedChangeIdMap(docx) { |
| 143 | + const body = docx?.['word/document.xml']?.elements?.[0]; |
| 144 | + if (!body?.elements) return new Map(); |
| 145 | + |
| 146 | + const idMap = new Map(); |
| 147 | + walkElements(body.elements, idMap, { lastTrackedChange: null }); |
| 148 | + |
| 149 | + return idMap; |
| 150 | +} |
0 commit comments