2016-12-02 17:20:59 +01:00
|
|
|
import * as JsDiff from 'diff'
|
2016-12-10 00:41:21 +01:00
|
|
|
import EditorsDiff from './EditorsDiff'
|
2016-12-02 17:20:59 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
let plaintextDiffer = new EditorsDiff()
|
|
|
|
let markdownDiffer = new EditorsDiff(/([\s,.:]|[*\[\]\(\)])/)
|
2016-12-02 17:20:59 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//returns a comparison of the texts as plaintext
|
2016-12-02 17:20:59 +01:00
|
|
|
export function plaintextDiff(original, final) {
|
2016-12-14 07:30:09 +01:00
|
|
|
let diff = plaintextDiffer.diff(original, final)
|
2016-12-02 17:20:59 +01:00
|
|
|
return diff
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
2016-12-02 17:20:59 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//returns a comparison of the texts as markdown
|
2016-12-09 23:07:42 +01:00
|
|
|
export function markdownDiff(original, final) {
|
2016-12-14 07:30:09 +01:00
|
|
|
let diff = markdownDiffer.diff(original, final)
|
2016-12-09 23:07:42 +01:00
|
|
|
diff = rewriteMarkdownDiff(diff)
|
2016-12-02 17:20:59 +01:00
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
return diff
|
2016-12-02 17:20:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-10 00:01:59 +01:00
|
|
|
// returns a string version of the diff, with "{+ ... +}" and "[- ... -]"
|
|
|
|
// representing ins and del blocks
|
2016-12-14 07:30:09 +01:00
|
|
|
export function diffToString(diff, tags={added:{start:'{+', end:'+}'}, removed:{start:'[-', end:'-]'}, same:{start:'', end:''}}) {
|
|
|
|
|
2016-12-02 17:20:59 +01:00
|
|
|
return diff.map(({added, removed, value}) => {
|
2016-12-14 07:30:09 +01:00
|
|
|
|
|
|
|
let {start,end} = added ? tags.added : (removed ? tags.removed : tags.same)
|
|
|
|
|
2016-12-10 00:01:59 +01:00
|
|
|
let string = value
|
|
|
|
if (Array.isArray(value))
|
|
|
|
string = value.join('')
|
|
|
|
|
|
|
|
return start+string+end
|
2016-12-10 00:41:21 +01:00
|
|
|
}).join('')
|
2016-12-02 17:20:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
export function diffToHtml(diff) {
|
|
|
|
return diffToString(diff, {added:{start:'<ins>', end:'</ins>'}, removed:{start:'<del>', end:'</del>'}, same:{start:'', end:''}})
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Rewrites the given diff to correctly render as markdown, assuming the source
|
|
|
|
// documents were also valid markdown.
|
|
|
|
// In essence, moves the markdown formatting elements in or out of the inserted and deleted blocks, as appropriate
|
|
|
|
|
|
|
|
//rules:
|
|
|
|
// 1. if a multiline del block is followed by an ins block,
|
|
|
|
// the first line of the ins block should be inserted at the end of the first line of the del block
|
|
|
|
// so the markdown will apply to the ins text as it should
|
|
|
|
// 2. after a newline, if an ins or del block begins with a markdown line formatting prefix (eg. for a title or list)
|
|
|
|
// then that prefix should be moved out of the block
|
|
|
|
|
|
|
|
//not yet implemented rules:
|
|
|
|
// 3. if an ins or del block spans one half of a bold, italic or link string
|
|
|
|
// eg. **Hello <del>World** I</del><ins>Darling** she</ins> said
|
|
|
|
// the block should be broken up to move the formatting code outside
|
|
|
|
// OR the whole formatting string could be brought into the block
|
|
|
|
// eg. **Hello <del>World</del><ins>Darling</ins>** <ins>I</ins><del>she</del> said
|
|
|
|
function rewriteMarkdownDiff(diff) {
|
|
|
|
//apply transformation rules
|
|
|
|
let transformedDiff = diff
|
|
|
|
transformedDiff= applyTransformationRule1(transformedDiff)
|
2016-12-14 07:30:09 +01:00
|
|
|
transformedDiff= applyTransformationRule2(transformedDiff)
|
2016-12-09 23:07:42 +01:00
|
|
|
return transformedDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
//Transformation rule 1
|
|
|
|
// 1. if a multiline del block is followed by an ins block,
|
|
|
|
// the first line of the ins block should be inserted at the end of the first line of the del block
|
|
|
|
// so the markdown will apply to the ins text as it should
|
|
|
|
function applyTransformationRule1(diff) {
|
|
|
|
let transformedDiff = []
|
|
|
|
|
2016-12-10 00:01:59 +01:00
|
|
|
const B_ADDED='added', B_REMOVED='removed', B_SAME='same'
|
2016-12-09 23:07:42 +01:00
|
|
|
let previousBlockType = null
|
|
|
|
let currentBlockType = null
|
|
|
|
let previousBlockWasMultiline = false
|
|
|
|
let currentBlockIsMultiline = false
|
|
|
|
|
|
|
|
//iterate the input tokens to create the intermediate representation
|
|
|
|
diff.forEach((currentBlock) => {
|
2016-12-10 00:01:59 +01:00
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
previousBlockType = currentBlockType
|
|
|
|
previousBlockWasMultiline = currentBlockIsMultiline
|
2016-12-10 00:01:59 +01:00
|
|
|
currentBlockType = (currentBlock.added ? B_ADDED : (currentBlock.removed ? B_REMOVED : B_SAME))
|
2016-12-09 23:07:42 +01:00
|
|
|
currentBlockIsMultiline = isMultilineDiffBlock(currentBlock)
|
|
|
|
|
|
|
|
//transform rule 1 applys when:
|
|
|
|
// the previous block was a del and had multiple lines
|
|
|
|
// the current block is an ins
|
2016-12-10 00:01:59 +01:00
|
|
|
if (previousBlockType == B_REMOVED && currentBlockType == B_ADDED && previousBlockWasMultiline) {
|
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
//split the first line from the current block
|
|
|
|
let currentBlockSplit = splitMultilineDiffBlock(currentBlock)
|
|
|
|
|
|
|
|
//pop the previous diff entry
|
|
|
|
let previousBlock = transformedDiff.pop()
|
|
|
|
|
|
|
|
//split the first line from the previous block
|
2016-12-10 00:01:59 +01:00
|
|
|
let previousBlockSplit = splitMultilineDiffBlock(previousBlock)
|
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
|
|
|
|
//now add the blocks back, interleaving del and ins blocks
|
|
|
|
for (let i=0; i<Math.max(previousBlockSplit.length, currentBlockSplit.length); i++) {
|
|
|
|
if (i<previousBlockSplit.length)
|
|
|
|
transformedDiff.push(previousBlockSplit[i])
|
|
|
|
if (i<currentBlockSplit.length)
|
|
|
|
transformedDiff.push(currentBlockSplit[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//otherwise, we just add the current block to the transformed list
|
|
|
|
transformedDiff.push(currentBlock)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return transformedDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
// matches markdown prefixes that affect the formatting of the whole subsequent line
|
|
|
|
// ^ - start of line
|
|
|
|
// ([ \t]*\>)* - blockquotes (possibly nested)
|
|
|
|
// (
|
|
|
|
// ([ \t]*#*) - headers
|
|
|
|
// |([ \t]+[\*\+-]) - unordered lists
|
|
|
|
// |([ \t]+[0-9]+\.) - numeric lists
|
|
|
|
// )?
|
|
|
|
// [ \t]* - trailing whitespace
|
|
|
|
const MARKDOWN_PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]*/
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//matches strings that end with a newline followed by some whitespace
|
|
|
|
const NEWLINE_SUFFIX = /\n\s*$/
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
// transformation rule 2:
|
|
|
|
// after a newline, if an ins or del block begins with a markdown line formatting prefix (eg. for a title or list)
|
|
|
|
// then that prefix should be moved out of the block
|
|
|
|
// also, if an ins block begins with a formatting prefix and follows immediately after a del block that follows a newline,
|
|
|
|
// the prefix should be moved out of the block _and_ an extra newline character should be added to the beginning of it
|
|
|
|
function applyTransformationRule2(diff) {
|
|
|
|
let transformedDiff = []
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
let isNewline = true
|
|
|
|
let newlineString = '\n'
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//iterate the input tokens to create the intermediate representation
|
|
|
|
diff.forEach((currentBlock) => {
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
if (isNewline && (currentBlock.added || currentBlock.removed) ) {
|
|
|
|
let match = currentBlock.value.match(MARKDOWN_PREFIX)
|
|
|
|
if (match) {
|
|
|
|
let preBlock = {value:match[0]}
|
|
|
|
let postBlock = {added:currentBlock.added, removed:currentBlock.removed, value:currentBlock.value.substring(match[0].length)}
|
|
|
|
|
|
|
|
if (currentBlock.added) {
|
|
|
|
let newlineBlock = {value: newlineString}
|
|
|
|
transformedDiff.push(newlineBlock)
|
|
|
|
}
|
|
|
|
transformedDiff.push(preBlock)
|
|
|
|
transformedDiff.push(postBlock)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
transformedDiff.push(currentBlock)
|
|
|
|
}
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-12-14 07:30:09 +01:00
|
|
|
transformedDiff.push(currentBlock)
|
|
|
|
isNewline = NEWLINE_SUFFIX.test(currentBlock.value)
|
|
|
|
if (isNewline)
|
|
|
|
newlineString = currentBlock.value.match(NEWLINE_SUFFIX)[0]
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
2016-12-14 07:30:09 +01:00
|
|
|
})
|
2016-12-09 23:07:42 +01:00
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
return transformedDiff
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//returns true if the given diff block contains a newline element
|
|
|
|
function isMultilineDiffBlock({value}) {
|
2016-12-14 07:30:09 +01:00
|
|
|
return value.indexOf('\n') != -1
|
2016-12-09 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//returns an array of diff blocks that have the same added, removed fields as the given one
|
2016-12-14 07:30:09 +01:00
|
|
|
//but with the string split by newlines
|
2016-12-09 23:07:42 +01:00
|
|
|
//if the diff block has no newlines, an array containing only that diff will be returned
|
|
|
|
//if the diff block has newlines, the resulting array will have a series of blocks,
|
|
|
|
// each of which subsequent to the first block will begin with a newline
|
|
|
|
//if the diff block begins with a newline, the returned array will begin with an empty diff
|
|
|
|
function splitMultilineDiffBlock({added, removed, value}) {
|
|
|
|
//find the indices of the diff block that coorespond to newlines
|
2016-12-14 07:30:09 +01:00
|
|
|
const splits = indicesOf(value, c => (c=='\n') )
|
2016-12-10 00:01:59 +01:00
|
|
|
|
|
|
|
splits.push(value.length)
|
2016-12-09 23:07:42 +01:00
|
|
|
|
|
|
|
//create a range from each index
|
2016-12-14 07:30:09 +01:00
|
|
|
const ranges = splits.reduce(
|
2016-12-09 23:07:42 +01:00
|
|
|
//the accumulator is a structure with the last index and the list of ranges
|
|
|
|
//the ranges are a {start, end} structure
|
2016-12-10 00:01:59 +01:00
|
|
|
({last, ranges}, i) => {
|
|
|
|
ranges = ranges.concat([{start:last, end:i}])
|
|
|
|
return {last:i, ranges}
|
|
|
|
},
|
2016-12-09 23:07:42 +01:00
|
|
|
//start with the zero index and an empty array
|
|
|
|
{last: 0, ranges:[]}
|
|
|
|
).ranges
|
|
|
|
|
2016-12-10 00:01:59 +01:00
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
//map the ranges into blocks
|
|
|
|
const blocks = ranges.map(
|
|
|
|
//each block is the same as the given original block, but with the values split at newlines
|
2016-12-14 07:30:09 +01:00
|
|
|
({start, end}) => ({added, removed, value:value.substring(start, end)})
|
2016-12-09 23:07:42 +01:00
|
|
|
)
|
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//console.log({value, splits, ranges, blocks})
|
2016-12-10 00:01:59 +01:00
|
|
|
|
2016-12-09 23:07:42 +01:00
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2016-12-14 07:30:09 +01:00
|
|
|
//collect all the indices of the given string that satisfy the test function
|
|
|
|
const indicesOf = (string, test) => string.split('').reduce(
|
2016-12-09 23:07:42 +01:00
|
|
|
//add indexes that satisfy the test function to the array
|
|
|
|
(acc, x, i) => (test(x) ? acc.concat([i]) : acc ),
|
|
|
|
//start with the empty array
|
|
|
|
[]
|
|
|
|
)
|