Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 4c6269e

Browse files
committed
DEV: preserve line-breaks from raw text
1 parent 30daa64 commit 4c6269e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

assets/javascripts/discourse/lib/diff-streamer.gjs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,49 @@ export default class DiffStreamer {
108108
const oldWords = oldText.trim().split(/\s+/);
109109
const newWords = newText.trim().split(/\s+/);
110110

111+
// Track where the line breaks are in the original oldText
112+
const lineBreakMap = (() => {
113+
const lines = oldText.trim().split("\n");
114+
const map = new Set();
115+
let wordIndex = 0;
116+
117+
for (const line of lines) {
118+
const wordsInLine = line.trim().split(/\s+/);
119+
wordIndex += wordsInLine.length;
120+
map.add(wordIndex - 1); // Mark the last word in each line
121+
}
122+
123+
return map;
124+
})();
125+
111126
const diff = [];
112127
let i = 0;
113128

114-
while (i < oldWords.length) {
129+
while (i < oldWords.length || i < newWords.length) {
115130
const oldWord = oldWords[i];
116131
const newWord = newWords[i];
117132

118133
let wordHTML = "";
119-
let originalWordHTML = `<span class="ghost">${oldWord}</span>`;
120134

121135
if (newWord === undefined) {
122-
wordHTML = originalWordHTML;
136+
wordHTML = `<span class="ghost">${oldWord}</span>`;
123137
} else if (oldWord === newWord) {
124138
wordHTML = `<span class="same-word">${newWord}</span>`;
125139
} else if (oldWord !== newWord) {
126-
wordHTML = `<del>${oldWord}</del> <ins>${newWord}</ins>`;
140+
wordHTML = `<del>${oldWord ?? ""}</del> <ins>${newWord ?? ""}</ins>`;
127141
}
128142
129143
if (i === newWords.length - 1 && opts.markLastWord) {
130144
wordHTML = `<mark class="highlight">${wordHTML}</mark>`;
131145
}
132146
133147
diff.push(wordHTML);
148+
149+
// Add a line break after this word if it ended a line in the original text
150+
if (lineBreakMap.has(i)) {
151+
diff.push("<br>");
152+
}
153+
134154
i++;
135155
}
136156

0 commit comments

Comments
 (0)