fix wdiff markdown parse error

This commit is contained in:
Adam Brown 2015-04-18 03:12:00 +00:00
parent 24167d5b45
commit 73f0b5f4b3
2 changed files with 48 additions and 38 deletions

View File

@ -1,6 +0,0 @@
{
"created": 1429314628678,
"a": "a",
"b": "n",
"_id": "b51422fbc0480e544784b4c2b119f666ea8c1e2b"
}

View File

@ -77,7 +77,6 @@ module.exports = function(a, b, asMarkdown, callback) {
/* Rewrites the given wdiff output to correctly render as markdown, /* Rewrites the given wdiff output to correctly render as markdown,
assuming the source documents were also valid markdown. */ assuming the source documents were also valid markdown. */
function rewriteWdiffMarkdown(source) { function rewriteWdiffMarkdown(source) {
//initialize a stack for the lexed input //initialize a stack for the lexed input
//make it a lodash container, just for kicks //make it a lodash container, just for kicks
var tokens = _([]); var tokens = _([]);
@ -196,6 +195,7 @@ function rewriteWdiffMarkdown(source) {
// * now emit the output string // * now emit the output string
var output = ""; var output = "";
var newline = true; var newline = true;
var newlineIndex = -1;
// prefixes are matched as follows: // prefixes are matched as follows:
// ^ - start of line // ^ - start of line
@ -206,24 +206,33 @@ function rewriteWdiffMarkdown(source) {
// |([ \t]+[0-9]+\.) - numeric lists // |([ \t]+[0-9]+\.) - numeric lists
// )? // )?
// [ \t]+ - trailing whitespace // [ \t]+ - trailing whitespace
var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]+/ //var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]+/
//var PREFIX = /^#*/ var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]*/
//var PREFIX = /^#*/
transform.forEach(function(item) { transform.forEach(function(item) {
//newlines are undecorated //newlines are undecorated
if (item.string == '\n') { if (item.string == '\n') {
output += '\n'; output += '\n';
//flag the new line
newline = true; newline = true;
//and record the offset in the output string
newlineIndex = output.length;
return return
} }
var prestring = ""; //wrap del strings with tags
var poststring = item.string; if (item.state == SDEL) {
output += '<del>' + item.string + '</del>';
//del doesn't reset the newline state
}
//if this is a newline, we need to peel off any markdown formatting prefixes //ins strings have to be handled a little differently:
//and output them outside the del/ins tags //if this is an ins just after a newline, or after a del after a newline, we need to peel off any markdown formatting prefixes and insert them at the beginning of the line outside the del/ins tags
if (newline) { else if (item.state == SINS && newline) {
var prestring, poststring;
var match = item.string.match(PREFIX); var match = item.string.match(PREFIX);
if (match == null) if (match == null)
prestring =""; prestring ="";
@ -231,20 +240,27 @@ function rewriteWdiffMarkdown(source) {
prestring = match[0]; prestring = match[0];
poststring = item.string.substring(prestring.length); poststring = item.string.substring(prestring.length);
output = output.substring(0, newlineIndex) + prestring + output.substring(newlineIndex);
output += '<ins>' + poststring + '</ins>';
newline = false;
newlineIndex = -1;
} }
//wrap ins and del strings with tags else if (item.state == SINS) {
if (item.state == SDEL) output += '<ins>' + item.string + '</ins>';
output += prestring+'<del>' + poststring + '</del>'; }
else if (item.state ==SINS)
output += prestring+'<ins>' + poststring + '</ins>';
//and just output other strings //and just output other strings
else else {
output += prestring+poststring; output += item.string;
//this resets the newline state
newline = false;
newlineIndex = -1;
}
newline = false;
}); });
return output;
return output;
} }