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,
assuming the source documents were also valid markdown. */
function rewriteWdiffMarkdown(source) {
//initialize a stack for the lexed input
//make it a lodash container, just for kicks
var tokens = _([]);
@ -196,6 +195,7 @@ function rewriteWdiffMarkdown(source) {
// * now emit the output string
var output = "";
var newline = true;
var newlineIndex = -1;
// prefixes are matched as follows:
// ^ - start of line
@ -206,7 +206,8 @@ function rewriteWdiffMarkdown(source) {
// |([ \t]+[0-9]+\.) - numeric lists
// )?
// [ \t]+ - trailing whitespace
var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]+/
//var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]+/
var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]*/
//var PREFIX = /^#*/
@ -214,16 +215,24 @@ function rewriteWdiffMarkdown(source) {
//newlines are undecorated
if (item.string == '\n') {
output += '\n';
//flag the new line
newline = true;
//and record the offset in the output string
newlineIndex = output.length;
return
}
var prestring = "";
var poststring = item.string;
//wrap del strings with tags
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
//and output them outside the del/ins tags
if (newline) {
//ins strings have to be handled a little differently:
//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
else if (item.state == SINS && newline) {
var prestring, poststring;
var match = item.string.match(PREFIX);
if (match == null)
prestring ="";
@ -231,20 +240,27 @@ function rewriteWdiffMarkdown(source) {
prestring = match[0];
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
if (item.state == SDEL)
output += prestring+'<del>' + poststring + '</del>';
else if (item.state ==SINS)
output += prestring+'<ins>' + poststring + '</ins>';
else if (item.state == SINS) {
output += '<ins>' + item.string + '</ins>';
}
//and just output other strings
else
output += prestring+poststring;
else {
output += item.string;
//this resets the newline state
newline = false;
});
return output;
newlineIndex = -1;
}
});
return output;
}