Skip to content

Commit c690612

Browse files
authored
Migrate diff-match-patch to a modern fork (#9511)
* Migrate to diff-match-patch-es & update api * Update acknowledgements * Update change notes * Fix editcost attribute not working * Make library compatible with ES2017
1 parent c4c6093 commit c690612

File tree

7 files changed

+917
-1740
lines changed

7 files changed

+917
-1740
lines changed

core/acknowledgements.tid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ TiddlyWiki incorporates code from these fine OpenSource projects:
55
* [[The Stanford Javascript Crypto Library|http://bitwiseshiftleft.github.io/sjcl/]]
66
* [[The Jasmine JavaScript Test Framework|https://jasmine.github.io/]]
77
* [[modern-normalize by Sindre Sorhus|https://github.com/sindresorhus/modern-normalize]]
8+
* [[diff-match-patch-es by antfu|https://github.com/antfu/diff-match-patch-es]]

core/modules/filters/strings.js

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -71,107 +71,103 @@ exports.join = makeStringReducingOperator(
7171
},null
7272
);
7373

74-
var dmp = require("$:/core/modules/utils/diff-match-patch/diff_match_patch.js");
74+
const dmp = require("$:/core/modules/utils/diff-match-patch/diff_match_patch.js");
7575

7676
exports.levenshtein = makeStringBinaryOperator(
7777
function(a,b) {
78-
var dmpObject = new dmp.diff_match_patch(),
79-
diffs = dmpObject.diff_main(a,b);
80-
return [dmpObject.diff_levenshtein(diffs) + ""];
78+
const diffs = dmp.diffMain(a,b);
79+
return [dmp.diffLevenshtein(diffs).toString()];
8180
}
8281
);
8382

8483
// these two functions are adapted from https://github.com/google/diff-match-patch/wiki/Line-or-Word-Diffs
8584
function diffLineWordMode(text1,text2,mode) {
86-
var dmpObject = new dmp.diff_match_patch();
8785
var a = diffPartsToChars(text1,text2,mode);
8886
var lineText1 = a.chars1;
8987
var lineText2 = a.chars2;
9088
var lineArray = a.lineArray;
91-
var diffs = dmpObject.diff_main(lineText1,lineText2,false);
92-
dmpObject.diff_charsToLines_(diffs,lineArray);
89+
var diffs = dmp.diffMain(lineText1,lineText2,false);
90+
dmp.diffCharsToLines(diffs,lineArray);
9391
return diffs;
9492
}
9593

9694
function diffPartsToChars(text1,text2,mode) {
9795
var lineArray = [];
9896
var lineHash = {};
99-
lineArray[0] = '';
97+
lineArray[0] = "";
10098

101-
function diff_linesToPartsMunge_(text,mode) {
102-
var chars = '';
103-
var lineStart = 0;
104-
var lineEnd = -1;
105-
var lineArrayLength = lineArray.length,
106-
regexpResult;
107-
var searchRegexp = /\W+/g;
108-
while(lineEnd < text.length - 1) {
109-
if(mode === "words") {
110-
regexpResult = searchRegexp.exec(text);
111-
lineEnd = searchRegexp.lastIndex;
112-
if(regexpResult === null) {
113-
lineEnd = text.length;
114-
}
115-
lineEnd = --lineEnd;
116-
} else {
117-
lineEnd = text.indexOf('\n', lineStart);
118-
if(lineEnd == -1) {
119-
lineEnd = text.length - 1;
120-
}
121-
}
122-
var line = text.substring(lineStart, lineEnd + 1);
99+
function diff_linesToPartsMunge_(text,mode) {
100+
var chars = "";
101+
var lineStart = 0;
102+
var lineEnd = -1;
103+
var lineArrayLength = lineArray.length,
104+
regexpResult;
105+
var searchRegexp = /\W+/g;
106+
while(lineEnd < text.length - 1) {
107+
if(mode === "words") {
108+
regexpResult = searchRegexp.exec(text);
109+
lineEnd = searchRegexp.lastIndex;
110+
if(regexpResult === null) {
111+
lineEnd = text.length;
112+
}
113+
lineEnd = --lineEnd;
114+
} else {
115+
lineEnd = text.indexOf("\n", lineStart);
116+
if(lineEnd == -1) {
117+
lineEnd = text.length - 1;
118+
}
119+
}
120+
var line = text.substring(lineStart, lineEnd + 1);
123121

124-
if(lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) : (lineHash[line] !== undefined)) {
122+
if(lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) : (lineHash[line] !== undefined)) {
125123
chars += String.fromCharCode(lineHash[line]);
126-
} else {
127-
if(lineArrayLength == maxLines) {
128-
line = text.substring(lineStart);
129-
lineEnd = text.length;
130-
}
131-
chars += String.fromCharCode(lineArrayLength);
132-
lineHash[line] = lineArrayLength;
133-
lineArray[lineArrayLength++] = line;
134-
}
135-
lineStart = lineEnd + 1;
136-
}
137-
return chars;
138-
}
139-
var maxLines = 40000;
140-
var chars1 = diff_linesToPartsMunge_(text1,mode);
141-
maxLines = 65535;
142-
var chars2 = diff_linesToPartsMunge_(text2,mode);
143-
return {chars1: chars1, chars2: chars2, lineArray: lineArray};
124+
} else {
125+
if(lineArrayLength == maxLines) {
126+
line = text.substring(lineStart);
127+
lineEnd = text.length;
128+
}
129+
chars += String.fromCharCode(lineArrayLength);
130+
lineHash[line] = lineArrayLength;
131+
lineArray[lineArrayLength++] = line;
132+
}
133+
lineStart = lineEnd + 1;
134+
}
135+
return chars;
136+
}
137+
var maxLines = 40000;
138+
var chars1 = diff_linesToPartsMunge_(text1,mode);
139+
maxLines = 65535;
140+
var chars2 = diff_linesToPartsMunge_(text2,mode);
141+
return {chars1: chars1, chars2: chars2, lineArray: lineArray};
144142
};
145143

146144
exports.makepatches = function(source,operator,options) {
147-
var dmpObject = new dmp.diff_match_patch(),
148-
suffix = operator.suffix || "",
145+
var suffix = operator.suffix || "",
149146
result = [];
150147

151-
source(function(tiddler,title) {
152-
var diffs, patches;
153-
if(suffix === "lines" || suffix === "words") {
154-
diffs = diffLineWordMode(title,operator.operand,suffix);
155-
patches = dmpObject.patch_make(title,diffs);
156-
} else {
157-
patches = dmpObject.patch_make(title,operator.operand);
158-
}
159-
Array.prototype.push.apply(result,[dmpObject.patch_toText(patches)]);
160-
});
148+
source(function(tiddler,title) {
149+
let diffs, patches;
150+
if(suffix === "lines" || suffix === "words") {
151+
diffs = diffLineWordMode(title,operator.operand,suffix);
152+
patches = dmp.patchMake(title,diffs);
153+
} else {
154+
patches = dmp.patchMake(title,operator.operand);
155+
}
156+
Array.prototype.push.apply(result,[dmp.patchToText(patches)]);
157+
});
161158

162159
return result;
163160
};
164161

165162
exports.applypatches = makeStringBinaryOperator(
166163
function(a,b) {
167-
var dmpObject = new dmp.diff_match_patch(),
168-
patches;
164+
let patches;
169165
try {
170-
patches = dmpObject.patch_fromText(b);
166+
patches = dmp.patchFromText(b);
171167
} catch(e) {
172168
}
173169
if(patches) {
174-
return [dmpObject.patch_apply(patches,a)[0]];
170+
return [dmp.patchApply(patches,a)[0]];
175171
} else {
176172
return [a];
177173
}

0 commit comments

Comments
 (0)