diff --git a/src/common/util/EditorsDiff.js b/src/common/util/EditorsDiff.js new file mode 100644 index 0000000..9ec5d2b --- /dev/null +++ b/src/common/util/EditorsDiff.js @@ -0,0 +1,45 @@ + +import {Diff} from 'diff' + +const EditorsDiff = new Diff() + +EditorsDiff.equals = function(left, right) { + return ( + left.string == right.string + ) + +} +EditorsDiff.tokenize = function(value) { + let tokens = value.split(/([ ]+)|(\n)/) + let annotatedTokens = [] + tokens.forEach( token => { + if (isSpace(token)) { + if (annotatedTokens.length == 0) + annotatedTokens.push({string:'', whitespace:[]}) + + let last = annotatedTokens[annotatedTokens.length-1] + last.whitespace.push(token) + } + else { + annotatedTokens.push({string:token, whitespace:[]}) + } + }) + console.log(annotatedTokens) + return annotatedTokens +} +EditorsDiff.join = function (annotatedTokens) { + let tokens = [] + annotatedTokens.forEach(annotatedToken => { + tokens.push(annotatedToken.string) + annotatedToken.whitespace.forEach(item => { + tokens.push(item) + }) + }) + + console.log(tokens.join('')) + return tokens.join('') +} + +export default EditorsDiff + +const isSpace = str => /[ ]+/.test(str) \ No newline at end of file diff --git a/src/common/util/dubdiff.js b/src/common/util/dubdiff.js index f94e98e..806d1fc 100644 --- a/src/common/util/dubdiff.js +++ b/src/common/util/dubdiff.js @@ -1,4 +1,5 @@ import * as JsDiff from 'diff' +import EditorsDiff from './EditorsDiff' //!!! this deal with adding and removing spaces could be done more elegantly by @@ -9,21 +10,23 @@ import * as JsDiff from 'diff' //the current mechanism for adding and removing spaces is fragile and broken export function plaintextDiff(original, final) { - let arrOriginal = plaintextSplit(original) - let arrFinal = plaintextSplit(final) + //let arrOriginal = plaintextSplit(original) + //let arrFinal = plaintextSplit(final) - let diff = JsDiff.diffArrays(arrOriginal, arrFinal) - diff = plaintextRestoreSpaces(diff) + let diff = EditorsDiff.diff(original, final) + //diff = plaintextRestoreSpaces(diff) return diff } export function markdownDiff(original, final) { - let arrOriginal = plaintextSplit(original) - let arrFinal = plaintextSplit(final) +// let arrOriginal = plaintextSplit(original) +// let arrFinal = plaintextSplit(final) - let diff = JsDiff.diffArrays(arrOriginal, arrFinal) - diff = plaintextRestoreSpaces(diff) +// let diff = JsDiff.diffArrays(arrOriginal, arrFinal) +// diff = plaintextRestoreSpaces(diff) + + let diff = EditorsDiff.diff(original, final) diff = rewriteMarkdownDiff(diff) return diff @@ -40,7 +43,7 @@ export function diffToString(diff) { string = value.join('') return start+string+end - }).join(' ') + }).join('') } let plaintextSplit = text =>text.split(/[ ]|(\n)/) diff --git a/test/dubdiffPlaintext.js b/test/dubdiffPlaintext.js index d8d3333..8fdb1f3 100644 --- a/test/dubdiffPlaintext.js +++ b/test/dubdiffPlaintext.js @@ -20,33 +20,33 @@ describe('dubdiff', () => { expect(diff( 'This is a smlb sentence.', 'This is a simple sentence.' - )).to.equal('This is a [-smlb-] {+simple+} sentence.') + )).to.equal('This is a [-smlb -]{+simple +}sentence.') }) it('diffs consecutive words', ()=>{ expect(diff( 'This is a smlb sentnce with no errors.', 'This is a simple sentence with no errors.' - )).to.equal('This is a [-smlb sentnce-] {+simple sentence+} with no errors.') + )).to.equal('This is a [-smlb sentnce-]{+simple sentence+} with no errors.') }) it('diffs with word deletion', ()=>{ expect(diff( 'Gonna delete a word.', 'Gonna delete word.' - )).to.equal('Gonna delete [-a-] word.') + )).to.equal('Gonna delete [-a -]word.') }) it('diffs with word insertion', ()=>{ expect(diff( - 'Gonna delete word.', - 'Gonna delete a word.' - )).to.equal('Gonna delete {+a+} word.') + 'Gonna add word.', + 'Gonna add a word.' + )).to.equal('Gonna add {+a +}word.') }) it('diffs accross newline without weird spaces', () => { expect(diff( 'This is a flawed\ncomment', 'This is a corrected\nitem' - )).to.equal('This is a [-flawed-] {+corrected+}\n[-comment-] {+item+}') + )).to.equal('This is a [-flawed-]{+corrected+}\n[-comment-]{+item+}') }) it('doesn\'t add spaces after newline', () => { expect(diff(