try and fix diff treatment of spaces
This commit is contained in:
parent
755143c0c3
commit
9c10e06c15
45
src/common/util/EditorsDiff.js
Normal file
45
src/common/util/EditorsDiff.js
Normal file
@ -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)
|
@ -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)/)
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user