|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { Node } from '@tiptap/pm/model' |
| 7 | +import type { ComparisonEdit } from './markdownComparisonTypes.ts' |
| 8 | + |
| 9 | +import { increasingSubsequence } from './comparisonAlignment.ts' |
| 10 | + |
| 11 | +export interface ComparisonHeading { |
| 12 | + from: number |
| 13 | + text: string |
| 14 | +} |
| 15 | + |
| 16 | +export interface ComparisonSection { |
| 17 | + id: string |
| 18 | + title: string |
| 19 | + edits: readonly ComparisonEdit[] |
| 20 | +} |
| 21 | +type Heading = ComparisonHeading |
| 22 | + |
| 23 | +export function headingLocations(doc: Node): readonly Heading[] { |
| 24 | + const headings: Heading[] = [] |
| 25 | + doc.forEach((node, from) => { |
| 26 | + const text = node.textContent.trim() |
| 27 | + if (node.type.name === 'heading' && text) { |
| 28 | + headings.push({ from, text }) |
| 29 | + } |
| 30 | + }) |
| 31 | + return headings |
| 32 | +} |
| 33 | + |
| 34 | +function nearestHeadingIndex(headings: readonly Heading[], position: number) { |
| 35 | + let lower = 0 |
| 36 | + let upper = headings.length |
| 37 | + while (lower < upper) { |
| 38 | + const middle = Math.floor((lower + upper) / 2) |
| 39 | + if (headings[middle]!.from <= position) { |
| 40 | + lower = middle + 1 |
| 41 | + } else { |
| 42 | + upper = middle |
| 43 | + } |
| 44 | + } |
| 45 | + return lower - 1 |
| 46 | +} |
| 47 | +export function nearestHeading(headings: readonly Heading[], position: number) { |
| 48 | + return headings[nearestHeadingIndex(headings, position)]?.text ?? '' |
| 49 | +} |
| 50 | + |
| 51 | +interface HeadingIndex { |
| 52 | + headings: readonly Heading[] |
| 53 | + keys: readonly string[] |
| 54 | +} |
| 55 | + |
| 56 | +function indexByUniqueTitle(headings: readonly Heading[]) { |
| 57 | + const indexes = new Map<string, number>() |
| 58 | + const repeated = new Set<string>() |
| 59 | + headings.forEach(({ text }, index) => { |
| 60 | + if (indexes.has(text)) { |
| 61 | + repeated.add(text) |
| 62 | + } else { |
| 63 | + indexes.set(text, index) |
| 64 | + } |
| 65 | + }) |
| 66 | + for (const text of repeated) { |
| 67 | + indexes.delete(text) |
| 68 | + } |
| 69 | + return indexes |
| 70 | +} |
| 71 | + |
| 72 | +function headingAnchors(before: readonly Heading[], after: readonly Heading[]) { |
| 73 | + const beforeIndexes = indexByUniqueTitle(before) |
| 74 | + const afterIndexes = indexByUniqueTitle(after) |
| 75 | + const pairs: Array<readonly [number, number]> = [] |
| 76 | + after.forEach(({ text }, afterIndex) => { |
| 77 | + const beforeIndex = beforeIndexes.get(text) |
| 78 | + if (beforeIndex !== undefined && afterIndexes.get(text) === afterIndex) { |
| 79 | + pairs.push([beforeIndex, afterIndex]) |
| 80 | + } |
| 81 | + }) |
| 82 | + return increasingSubsequence(pairs.map(([index]) => index)).indices.map((index) => pairs[index]!) |
| 83 | +} |
| 84 | + |
| 85 | +function correlateHeadings(before: readonly Heading[], after: readonly Heading[]) { |
| 86 | + const beforeKeys: string[] = [] |
| 87 | + const afterKeys: string[] = [] |
| 88 | + let next = 0 |
| 89 | + let row = 0 |
| 90 | + let column = 0 |
| 91 | + |
| 92 | + function pairGap(rowEnd: number, columnEnd: number) { |
| 93 | + const rowCount = rowEnd - row |
| 94 | + const columnCount = columnEnd - column |
| 95 | + if (rowCount !== columnCount || rowCount > 1) { |
| 96 | + while (row < rowEnd) { |
| 97 | + beforeKeys[row++] = `#${next++}` |
| 98 | + } |
| 99 | + while (column < columnEnd) { |
| 100 | + afterKeys[column++] = `#${next++}` |
| 101 | + } |
| 102 | + return |
| 103 | + } |
| 104 | + while (row < rowEnd) { |
| 105 | + const key = `#${next++}` |
| 106 | + beforeKeys[row++] = key |
| 107 | + afterKeys[column++] = key |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for (const [anchorRow, anchorColumn] of headingAnchors(before, after)) { |
| 112 | + pairGap(anchorRow, anchorColumn) |
| 113 | + const key = `#${next++}` |
| 114 | + beforeKeys[row++] = key |
| 115 | + afterKeys[column++] = key |
| 116 | + } |
| 117 | + pairGap(before.length, after.length) |
| 118 | + return { before: beforeKeys, after: afterKeys } |
| 119 | +} |
| 120 | + |
| 121 | +function resolveSection(edit: ComparisonEdit, before: HeadingIndex, after: HeadingIndex) { |
| 122 | + const descriptor = edit.primary |
| 123 | + const deleted = descriptor.operation === 'delete' |
| 124 | + const side = deleted ? before : after |
| 125 | + const position = deleted |
| 126 | + ? descriptor.context.before?.from ?? descriptor.before.from |
| 127 | + : descriptor.context.after?.from ?? descriptor.after.from |
| 128 | + return side.keys[nearestHeadingIndex(side.headings, position)] ?? '' |
| 129 | +} |
| 130 | + |
| 131 | +export function buildComparisonSections(edits: readonly ComparisonEdit[], beforeDocument: Node, afterDocument: Node): readonly ComparisonSection[] { |
| 132 | + const beforeHeadings = headingLocations(beforeDocument) |
| 133 | + const afterHeadings = headingLocations(afterDocument) |
| 134 | + const correlation = correlateHeadings(beforeHeadings, afterHeadings) |
| 135 | + const before: HeadingIndex = { headings: beforeHeadings, keys: correlation.before } |
| 136 | + const after: HeadingIndex = { headings: afterHeadings, keys: correlation.after } |
| 137 | + const titleByKey = new Map<string, string>() |
| 138 | + beforeHeadings.forEach((heading, index) => titleByKey.set(correlation.before[index]!, heading.text)) |
| 139 | + afterHeadings.forEach((heading, index) => titleByKey.set(correlation.after[index]!, heading.text)) |
| 140 | + |
| 141 | + const sections: Array<{ id: string, key: string, title: string, edits: ComparisonEdit[] }> = [] |
| 142 | + for (const edit of edits) { |
| 143 | + const key = resolveSection(edit, before, after) |
| 144 | + const title = titleByKey.get(key) ?? '' |
| 145 | + const current = sections.at(-1) |
| 146 | + if (current?.key === key) { |
| 147 | + current.edits.push(edit) |
| 148 | + } else { |
| 149 | + sections.push({ id: edit.id, key, title, edits: [edit] }) |
| 150 | + } |
| 151 | + } |
| 152 | + return sections.map(({ id, title, edits: sectionEdits }) => ({ |
| 153 | + id, |
| 154 | + title, |
| 155 | + edits: sectionEdits, |
| 156 | + })) |
| 157 | +} |
0 commit comments