try and fix diff treatment of spaces

This commit is contained in:
Adam Brown 2016-12-09 18:41:21 -05:00
parent 755143c0c3
commit 9c10e06c15
3 changed files with 64 additions and 16 deletions

View 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)

View File

@ -1,4 +1,5 @@
import * as JsDiff from 'diff' import * as JsDiff from 'diff'
import EditorsDiff from './EditorsDiff'
//!!! this deal with adding and removing spaces could be done more elegantly by //!!! 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 //the current mechanism for adding and removing spaces is fragile and broken
export function plaintextDiff(original, final) { export function plaintextDiff(original, final) {
let arrOriginal = plaintextSplit(original) //let arrOriginal = plaintextSplit(original)
let arrFinal = plaintextSplit(final) //let arrFinal = plaintextSplit(final)
let diff = JsDiff.diffArrays(arrOriginal, arrFinal) let diff = EditorsDiff.diff(original, final)
diff = plaintextRestoreSpaces(diff) //diff = plaintextRestoreSpaces(diff)
return diff return diff
} }
export function markdownDiff(original, final) { export function markdownDiff(original, final) {
let arrOriginal = plaintextSplit(original) // let arrOriginal = plaintextSplit(original)
let arrFinal = plaintextSplit(final) // let arrFinal = plaintextSplit(final)
let diff = JsDiff.diffArrays(arrOriginal, arrFinal) // let diff = JsDiff.diffArrays(arrOriginal, arrFinal)
diff = plaintextRestoreSpaces(diff) // diff = plaintextRestoreSpaces(diff)
let diff = EditorsDiff.diff(original, final)
diff = rewriteMarkdownDiff(diff) diff = rewriteMarkdownDiff(diff)
return diff return diff

View File

@ -38,9 +38,9 @@ describe('dubdiff', () => {
}) })
it('diffs with word insertion', ()=>{ it('diffs with word insertion', ()=>{
expect(diff( expect(diff(
'Gonna delete word.', 'Gonna add word.',
'Gonna delete a word.' 'Gonna add a word.'
)).to.equal('Gonna delete {+a+} word.') )).to.equal('Gonna add {+a +}word.')
}) })
it('diffs accross newline without weird spaces', () => { it('diffs accross newline without weird spaces', () => {
expect(diff( expect(diff(