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
|
||||
|
@ -38,9 +38,9 @@ describe('dubdiff', () => {
|
||||
})
|
||||
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(
|
||||
|
Loading…
Reference in New Issue
Block a user