Created
March 4, 2015 19:00
-
-
Save jesperronn/45c92790ea2176d2a6c1 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/watofa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style> | |
#before, #after {display: none;} | |
</style> | |
<style id="jsbin-css"> | |
pre { background-color: #fff} | |
ins { | |
background-color: #eaffea; | |
} | |
del { | |
color: #666; | |
background-color: #ffecec; | |
} | |
ins,del{ | |
text-decoration: none; | |
} | |
</style> | |
</head> | |
<body> | |
<p>Will compare versions of a long markdown document in different ways. Markdown doc here <a href="https://github.com/jgm/CommonMark/commits/master/README.md" target="blank">markdown doc</a></p> | |
<div id="here">-</div> | |
<pre id="result">-</pre> | |
<div id="before"> | |
CommonMark | |
========== | |
CommonMark is a rationalized version of Markdown syntax, | |
with a [spec][the spec] and BSD3-licensed reference | |
implementations in C and JavaScript. | |
[Try it now!](http://spec.commonmark.org/dingus.html) | |
The implementations | |
------------------- | |
The C implementation provides both a library and a standalone program | |
`cmark` that converts CommonMark to HTML. It is written in standard C99 | |
and has no library dependencies. The parser is very fast, on par with | |
[sundown](https://github.com/vmg/sundown). Some benchmarks (on | |
an ancient Thinkpad running Intel Core 2 Duo at 2GHz, measured using | |
`time` and parsing a ~500K book, the English version of [*Pro | |
Git*](https://github.com/progit/progit/tree/master/en) by | |
Scott Chacon and Ben Straub): | |
|Implementation | Time | Factor| | |
|---------------|-------|--------| | |
| Markdown.pl | 5.162s| 286.8| | |
| PHP Markdown | 1.021s| 56.7| | |
| commonmark.js | 0.292s| 16.2| | |
| peg-markdown | 0.279s| 15.5| | |
| marked | 0.239s| 13.3| | |
| discount | 0.090s| 5.0| | |
| **cmark** | 0.020s| 1.1| | |
| sundown | 0.018s| 1.0| | |
The JavaScript implementation is a single JavaScript file, with | |
no dependencies, that can be linked to in an HTML page. Here | |
is a simple usage example: | |
``` javascript | |
var reader = new commonmark.DocParser(); | |
var writer = new commonmark.HtmlRenderer(); | |
var parsed = reader.parse("Hello *world*"); | |
var result = writer.render(parsed); | |
``` | |
A node package is also available; it includes a command-line tool called | |
`commonmark`. | |
**A note on security:** | |
Neither implementation attempts to sanitize link attributes or | |
raw HTML. If you use these libraries in applications that accept | |
untrusted user input, you must run the output through an HTML | |
sanitizer to protect against | |
[XSS attacks](http://en.wikipedia.org/wiki/Cross-site_scripting). | |
Installing (C) | |
-------------- | |
Building the C program (`cmark`) and shared library (`libcmark`) | |
requires [cmake] and [re2c], which is used to generate `scanners.c` from | |
`scanners.re`. (Note that [re2c] is only a build dependency for | |
developers, since `scanners.c` can be provided in a released source | |
tarball.) | |
On \*nix systems, you can simply `make` and `make install`. This | |
calls [cmake] to create a `Makefile` in the `build` directory, | |
then uses that `Makefile` to create the executable and library. | |
Alternatively, you can use [cmake] manually. [cmake] knows how | |
to create build environments for many build systems. For | |
example, to create Xcode project files on OSX: | |
mkdir build | |
cd build | |
cmake -G Xcode .. # optionally: -DCMAKE_INSTALL_PREFIX=path | |
make # executable will be created as build/src/cmake | |
make install | |
To run tests: | |
make test | |
(Or `perl runtests.pl spec.txt build/src/cmark` or, in the cmake | |
build directory, `ctest -V`.) | |
To test the shared library via a python wrapper: | |
make testlib | |
To run a "fuzz test" against ten long randomly generated inputs: | |
make fuzztest | |
To run a test for memory leaks using valgrind: | |
make leakcheck | |
To make a release tarball: | |
make tarball | |
Installing (JavaScript) | |
----------------------- | |
The JavaScript library can be installed through `npm`: | |
npm install commonmark | |
To build the JavaScript library as a single standalone file: | |
browserify --standalone commonmark js/lib/index.js -o js/commonmark.js | |
Or fetch a pre-built copy from | |
<http://spec.commonmark.org/js/commonmark.js>`. | |
To run tests for the JavaScript library: | |
make testjs | |
or | |
node js/test.js | |
The spec | |
-------- | |
[The spec] contains over 500 embedded examples which serve as conformance | |
tests. To run the tests for `cmark`, do `make test`. To run them for | |
another Markdown program, say `myprog`, do `make test PROG=myprog`. To | |
run the tests for `commonmark.js`, do `make testjs`. | |
[The spec]: http://jgm.github.io/CommonMark/spec.html | |
The source of [the spec] is `spec.txt`. This is basically a Markdown | |
file, with code examples written in a shorthand form: | |
. | |
Markdown source | |
. | |
expected HTML output | |
. | |
To build an HTML version of the spec, do `make spec.html`. To build a | |
PDF version, do `make spec.pdf`. Both these commands require that | |
[pandoc] is installed, and creating a PDF requires a latex installation. | |
The spec is written from the point of view of the human writer, not | |
the computer reader. It is not an algorithm---an English translation of | |
a computer program---but a declarative description of what counts as a block | |
quote, a code block, and each of the other structural elements that can | |
make up a Markdown document. | |
Because John Gruber's [canonical syntax | |
description](http://daringfireball.net/projects/markdown/syntax) leaves | |
many aspects of the syntax undetermined, writing a precise spec requires | |
making a large number of decisions, many of them somewhat arbitrary. | |
In making them, I have appealed to existing conventions and | |
considerations of simplicity, readability, expressive power, and | |
consistency. I have tried to ensure that "normal" documents in the many | |
incompatible existing implementations of Markdown will render, as far as | |
possible, as their authors intended. And I have tried to make the rules | |
for different elements work together harmoniously. In places where | |
different decisions could have been made (for example, the rules | |
governing list indentation), I have explained the rationale for | |
my choices. In a few cases, I have departed slightly from the canonical | |
syntax description, in ways that I think further the goals of Markdown | |
as stated in that description. | |
For the most part, I have limited myself to the basic elements | |
described in Gruber's canonical syntax description, eschewing extensions | |
like footnotes and definition lists. It is important to get the core | |
right before considering such things. However, I have included a visible | |
syntax for line breaks and fenced code blocks. | |
Differences from original Markdown | |
---------------------------------- | |
There are only a few places where this spec says things that contradict | |
the canonical syntax description: | |
- It [allows all punctuation symbols to be | |
backslash-escaped](http://jgm.github.io/stmd/spec.html#backslash-escapes), | |
not just the symbols with special meanings in Markdown. I found | |
that it was just too hard to remember which symbols could be | |
escaped. | |
- It introduces an [alternative syntax for hard line | |
breaks](http://jgm.github.io/stmd/spec.html#hard-line-breaks), a | |
backslash at the end of the line, supplementing the | |
two-spaces-at-the-end-of-line rule. This is motivated by persistent | |
complaints about the “invisible” nature of the two-space rule. | |
- Link syntax has been made a bit more predictable (in a | |
backwards-compatible way). For example, `Markdown.pl` allows single | |
quotes around a title in inline links, but not in reference links. | |
This kind of difference is really hard for users to remember, so the | |
spec [allows single quotes in both | |
contexts](http://jgm.github.io/stmd/spec.html#links). | |
- The rule for HTML blocks differs, though in most real cases it | |
shouldn't make a difference. (See | |
[here](http://jgm.github.io/stmd/spec.html#html-blocks) for | |
details.) The spec's proposal makes it easy to include Markdown | |
inside HTML block-level tags, if you want to, but also allows you to | |
exclude this. It is also makes parsing much easier, avoiding | |
expensive backtracking. | |
- It does not collapse adjacent bird-track blocks into a single | |
blockquote: | |
> this is two | |
> blockquotes | |
> this is a single | |
> | |
> blockquote with two paragraphs | |
- Rules for content in lists differ in a few respects, though (as with | |
HTML blocks), most lists in existing documents should render as | |
intended. There is some discussion of the choice points and | |
differences [here](http://jgm.github.io/stmd/spec.html#motivation). | |
I think that the spec's proposal does better than any existing | |
implementation in rendering lists the way a human writer or reader | |
would intuitively understand them. (I could give numerous examples | |
of perfectly natural looking lists that nearly every existing | |
implementation flubs up.) | |
- The spec stipulates that two blank lines break out of all list | |
contexts. This is an attempt to deal with issues that often come up | |
when someone wants to have two adjacent lists, or a list followed by | |
an indented code block. | |
- Changing bullet characters, or changing from bullets to numbers or | |
vice versa, starts a new list. I think that is almost always going | |
to be the writer's intent. | |
- The number that begins an ordered list item may be followed by | |
either `.` or `)`. Changing the delimiter style starts a new | |
list. | |
- The start number of an ordered list is significant. | |
- [Fenced code blocks](http://jgm.github.io/stmd/spec.html#fenced-code-blocks) are supported, delimited by either | |
backticks (` ``` `) or tildes (` ~~~ `). | |
In all of this, I have been guided by eight years experience writing | |
Markdown implementations in several languages, including the first | |
Markdown parser not based on regular expression substitutions | |
([pandoc](http://github.com/jgm/pandoc)) and the first markdown parsers | |
based on PEG grammars | |
([peg-markdown](http://github.com/jgm/peg-markdown), | |
[lunamark](http://github.com/jgm/lunamark)). Maintaining these projects | |
and responding to years of user feedback have given me a good sense of | |
the complexities involved in parsing Markdown, and of the various design | |
decisions that can be made. I have also explored differences between | |
Markdown implementations extensively using [BabelMark | |
2](http://johnmacfarlane.net/babelmark2/). In the early phases of | |
working out the spec, I benefited greatly from collaboration with David | |
Greenspan, and from feedback from several industrial users of Markdown, | |
including Jeff Atwood, Vincent Marti, and Neil Williams. | |
Contributing | |
------------ | |
There is a [forum for discussing | |
CommonMark](http://talk.commonmark.org); you should use it instead of | |
github issues for questions and possibly open-ended discussions. | |
Use the [github issue tracker](http://github.com/jgm/stmd/issues) | |
only for simple, clear, actionable issues. | |
[cmake]: http://www.cmake.org/download/ | |
[pandoc]: http://johnmacfarlane.net/pandoc/ | |
[re2c]: http://re2c.org | |
</div> | |
<div id="after">CommonMark | |
========== | |
CommonMark is a rationalized version of Markdown syntax, | |
with a [spec][the spec] and BSD-licensed reference | |
implementations in C and JavaScript. | |
[Try it now!](http://try.commonmark.org/) | |
[the spec]: http://spec.commonmark.org/ | |
For more details, see <http://commonmark.org>. | |
This repository contains the spec itself, along with tools for | |
running tests against the spec, and for creating HTML and PDF versions | |
of the spec. | |
The reference implementations live in separate repositories: | |
- <https://github.com/jgm/cmark> (C) | |
- <https://github.com/jgm/commonmark.js> (JavaScript) | |
Running tests against the spec | |
------------------------------ | |
[The spec] contains over 500 embedded examples which serve as conformance | |
tests. To run the tests using an executable `$PROG`: | |
python3 test/spec_tests.py --program $PROG | |
If you want to extract the raw test data from the spec without | |
actually running the tests, you can do: | |
python3 test/spec_tests.py --dump-tests | |
and you'll get all the tests in JSON format. | |
The spec | |
-------- | |
The source of [the spec] is `spec.txt`. This is basically a Markdown | |
file, with code examples written in a shorthand form: | |
. | |
Markdown source | |
. | |
expected HTML output | |
. | |
To build an HTML version of the spec, do `make spec.html`. To build a | |
PDF version, do `make spec.pdf`. (Creating a PDF requires [pandoc] | |
and a LaTeX installation. Creating the HTML version requires only | |
`libcmark` and `python3`.) | |
The spec is written from the point of view of the human writer, not | |
the computer reader. It is not an algorithm---an English translation of | |
a computer program---but a declarative description of what counts as a block | |
quote, a code block, and each of the other structural elements that can | |
make up a Markdown document. | |
Because John Gruber's [canonical syntax | |
description](http://daringfireball.net/projects/markdown/syntax) leaves | |
many aspects of the syntax undetermined, writing a precise spec requires | |
making a large number of decisions, many of them somewhat arbitrary. | |
In making them, we have appealed to existing conventions and | |
considerations of simplicity, readability, expressive power, and | |
consistency. We have tried to ensure that "normal" documents in the many | |
incompatible existing implementations of Markdown will render, as far as | |
possible, as their authors intended. And we have tried to make the rules | |
for different elements work together harmoniously. In places where | |
different decisions could have been made (for example, the rules | |
governing list indentation), we have explained the rationale for | |
my choices. In a few cases, we have departed slightly from the canonical | |
syntax description, in ways that we think further the goals of Markdown | |
as stated in that description. | |
For the most part, we have limited ourselves to the basic elements | |
described in Gruber's canonical syntax description, eschewing extensions | |
like footnotes and definition lists. It is important to get the core | |
right before considering such things. However, we have included a visible | |
syntax for line breaks and fenced code blocks. | |
Differences from original Markdown | |
---------------------------------- | |
There are only a few places where this spec says things that contradict | |
the canonical syntax description: | |
- It allows all punctuation symbols to be backslash-escaped, | |
not just the symbols with special meanings in Markdown. We found | |
that it was just too hard to remember which symbols could be | |
escaped. | |
- It introduces an alternative syntax for hard line | |
breaks, a backslash at the end of the line, supplementing the | |
two-spaces-at-the-end-of-line rule. This is motivated by persistent | |
complaints about the “invisible” nature of the two-space rule. | |
- Link syntax has been made a bit more predictable (in a | |
backwards-compatible way). For example, `Markdown.pl` allows single | |
quotes around a title in inline links, but not in reference links. | |
This kind of difference is really hard for users to remember, so the | |
spec allows single quotes in both contexts. | |
- The rule for HTML blocks differs, though in most real cases it | |
shouldn't make a difference. (See the section on HTML Blocks | |
for details.) The spec's proposal makes it easy to include Markdown | |
inside HTML block-level tags, if you want to, but also allows you to | |
exclude this. It is also makes parsing much easier, avoiding | |
expensive backtracking. | |
- It does not collapse adjacent bird-track blocks into a single | |
blockquote: | |
> this is two | |
> blockquotes | |
> this is a single | |
> | |
> blockquote with two paragraphs | |
- Rules for content in lists differ in a few respects, though (as with | |
HTML blocks), most lists in existing documents should render as | |
intended. There is some discussion of the choice points and | |
differences in the subsection of List Items entitled Motivation. | |
We think that the spec's proposal does better than any existing | |
implementation in rendering lists the way a human writer or reader | |
would intuitively understand them. (We could give numerous examples | |
of perfectly natural looking lists that nearly every existing | |
implementation flubs up.) | |
- The spec stipulates that two blank lines break out of all list | |
contexts. This is an attempt to deal with issues that often come up | |
when someone wants to have two adjacent lists, or a list followed by | |
an indented code block. | |
- Changing bullet characters, or changing from bullets to numbers or | |
vice versa, starts a new list. We think that is almost always going | |
to be the writer's intent. | |
- The number that begins an ordered list item may be followed by | |
either `.` or `)`. Changing the delimiter style starts a new | |
list. | |
- The start number of an ordered list is significant. | |
- Fenced code blocks are supported, delimited by either | |
backticks (```` ``` ```` or tildes (` ~~~ `). | |
Contributing | |
------------ | |
There is a [forum for discussing | |
CommonMark](http://talk.commonmark.org); you should use it instead of | |
github issues for questions and possibly open-ended discussions. | |
Use the [github issue tracker](http://github.com/jgm/CommonMark/issues) | |
only for simple, clear, actionable issues. | |
Authors | |
------- | |
The spec was written by John MacFarlane, drawing on | |
- his experience writing and maintaining Markdown implementations in several | |
languages, including the first Markdown parser not based on regular | |
expression substitutions ([pandoc](http://github.com/jgm/pandoc)) and | |
the first markdown parsers based on PEG grammars | |
([peg-markdown](http://github.com/jgm/peg-markdown), | |
[lunamark](http://github.com/jgm/lunamark)) | |
- a detailed examination of the differences between existing Markdown | |
implementations using [BabelMark 2](http://johnmacfarlane.net/babelmark2/), | |
and | |
- extensive discussions with David Greenspan, Jeff Atwood, Vicent | |
Marti, Neil Williams, and Benjamin Dumke-von der Ehe. | |
Since the first announcement, many people have contributed ideas. | |
Kārlis Gaņģis was especially helpful in refining the rules for | |
emphasis, strong emphasis, links, and images.</div> | |
<div id="crazylong950"> | |
</div> | |
<script id="jsbin-javascript"> | |
/*purpose: Take long markdown texts and diff asynchronously. | |
I hope to update display asynchronously | |
*/ | |
$(function(){ | |
var out = $('#here'); | |
var result = $('#result'); | |
out.empty() | |
.append("started...") | |
.append( typeof(JsDiff)); | |
var before = $('#before').text(); | |
var after = $('#after').text(); | |
console.time("diffWords"); | |
var bigdiff = JsDiff.diffLines(before, after); | |
console.timeEnd("diffWords"); | |
//console.log(bigdiff); | |
console.time("formatDiff"); | |
var htmlDiff = formatDiff(bigdiff); | |
console.timeEnd("formatDiff"); | |
console.time("dom insert"); | |
result.append(htmlDiff); | |
console.timeEnd("dom insert"); | |
}); | |
function formatDiff(diff) { | |
return diff.map(function(section) { | |
if (section.added && section.value) { | |
return wrap('ins', section.value); | |
} else if (section.removed && section.value) { | |
return wrap('del', section.value); | |
} else { | |
return section.value; | |
} | |
}).join(''); | |
} | |
function wrap(template, value) { | |
var starts_with_indent_block = new RegExp("^(=|-|\s)+$"); | |
var starts_with_md_special = new RegExp("^((#|\\*|\-|>)+\s+)"); | |
return value.split('\n').map(function(line) { | |
if (line.match(starts_with_indent_block )) { | |
return line; | |
} | |
var splitted = line.split(starts_with_md_special); | |
if (splitted.length === 4) { | |
return splitted[1] + wwrraapp(template, splitted[3] ); | |
} else { | |
return wwrraapp(template, line ); | |
} | |
}).join('\n'); | |
} | |
function wwrraapp(tag, content) { | |
return ['<',tag,'>',content,'</',tag,'>'].join(''); | |
} | |
//NOPROTECT | |
// BELOW FIND | |
/* See LICENSE file for terms of use */ | |
/* | |
* Text diff implementation. | |
* | |
* This library supports the following APIS: | |
* JsDiff.diffChars: Character by character diff | |
* JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace | |
* JsDiff.diffLines: Line based diff | |
* | |
* JsDiff.diffCss: Diff targeted at CSS content | |
* | |
* These methods are based on the implementation proposed in | |
* "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). | |
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 | |
*/ | |
(function(global, undefined) { | |
var JsDiff = (function() { | |
/*jshint maxparams: 5*/ | |
/*istanbul ignore next*/ | |
function map(arr, mapper, that) { | |
if (Array.prototype.map) { | |
return Array.prototype.map.call(arr, mapper, that); | |
} | |
var other = new Array(arr.length); | |
for (var i = 0, n = arr.length; i < n; i++) { | |
other[i] = mapper.call(that, arr[i], i, arr); | |
} | |
return other; | |
} | |
function clonePath(path) { | |
return { newPos: path.newPos, components: path.components.slice(0) }; | |
} | |
function removeEmpty(array) { | |
var ret = []; | |
for (var i = 0; i < array.length; i++) { | |
if (array[i]) { | |
ret.push(array[i]); | |
} | |
} | |
return ret; | |
} | |
function escapeHTML(s) { | |
var n = s; | |
n = n.replace(/&/g, '&'); | |
n = n.replace(/</g, '<'); | |
n = n.replace(/>/g, '>'); | |
n = n.replace(/"/g, '"'); | |
return n; | |
} | |
function buildValues(components, newString, oldString, useLongestToken) { | |
var componentPos = 0, | |
componentLen = components.length, | |
newPos = 0, | |
oldPos = 0; | |
for (; componentPos < componentLen; componentPos++) { | |
var component = components[componentPos]; | |
if (!component.removed) { | |
if (!component.added && useLongestToken) { | |
var value = newString.slice(newPos, newPos + component.count); | |
value = map(value, function(value, i) { | |
var oldValue = oldString[oldPos + i]; | |
return oldValue.length > value.length ? oldValue : value; | |
}); | |
component.value = value.join(''); | |
} else { | |
component.value = newString.slice(newPos, newPos + component.count).join(''); | |
} | |
newPos += component.count; | |
// Common case | |
if (!component.added) { | |
oldPos += component.count; | |
} | |
} else { | |
component.value = oldString.slice(oldPos, oldPos + component.count).join(''); | |
oldPos += component.count; | |
} | |
} | |
return components; | |
} | |
var Diff = function(ignoreWhitespace) { | |
this.ignoreWhitespace = ignoreWhitespace; | |
}; | |
Diff.prototype = { | |
diff: function(oldString, newString, callback) { | |
var self = this; | |
function done(value) { | |
if (callback) { | |
setTimeout(function() { callback(undefined, value); }, 0); | |
return true; | |
} else { | |
return value; | |
} | |
} | |
// Handle the identity case (this is due to unrolling editLength == 0 | |
if (newString === oldString) { | |
return done([{ value: newString }]); | |
} | |
if (!newString) { | |
return done([{ value: oldString, removed: true }]); | |
} | |
if (!oldString) { | |
return done([{ value: newString, added: true }]); | |
} | |
newString = this.tokenize(newString); | |
oldString = this.tokenize(oldString); | |
var newLen = newString.length, oldLen = oldString.length; | |
var maxEditLength = newLen + oldLen; | |
var bestPath = [{ newPos: -1, components: [] }]; | |
// Seed editLength = 0, i.e. the content starts with the same values | |
var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); | |
if (bestPath[0].newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
// Identity per the equality and tokenizer | |
return done([{value: newString.join('')}]); | |
} | |
// Main worker method. checks all permutations of a given edit length for acceptance. | |
function execEditLength() { | |
for (var diagonalPath = -1*editLength; diagonalPath <= editLength; diagonalPath+=2) { | |
var basePath; | |
var addPath = bestPath[diagonalPath-1], | |
removePath = bestPath[diagonalPath+1]; | |
oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; | |
if (addPath) { | |
// No one else is going to attempt to use this value, clear it | |
bestPath[diagonalPath-1] = undefined; | |
} | |
var canAdd = addPath && addPath.newPos+1 < newLen; | |
var canRemove = removePath && 0 <= oldPos && oldPos < oldLen; | |
if (!canAdd && !canRemove) { | |
// If this path is a terminal then prune | |
bestPath[diagonalPath] = undefined; | |
continue; | |
} | |
// Select the diagonal that we want to branch from. We select the prior | |
// path whose position in the new string is the farthest from the origin | |
// and does not pass the bounds of the diff graph | |
if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) { | |
basePath = clonePath(removePath); | |
self.pushComponent(basePath.components, undefined, true); | |
} else { | |
basePath = addPath; // No need to clone, we've pulled it from the list | |
basePath.newPos++; | |
self.pushComponent(basePath.components, true, undefined); | |
} | |
var oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); | |
// If we have hit the end of both strings, then we are done | |
if (basePath.newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
return done(buildValues(basePath.components, newString, oldString, self.useLongestToken)); | |
} else { | |
// Otherwise track this path as a potential candidate and continue. | |
bestPath[diagonalPath] = basePath; | |
} | |
} | |
editLength++; | |
} | |
// Performs the length of edit iteration. Is a bit fugly as this has to support the | |
// sync and async mode which is never fun. Loops over execEditLength until a value | |
// is produced. | |
var editLength = 1; | |
if (callback) { | |
(function exec() { | |
setTimeout(function() { | |
// This should not happen, but we want to be safe. | |
/*istanbul ignore next */ | |
if (editLength > maxEditLength) { | |
return callback(); | |
} | |
if (!execEditLength()) { | |
exec(); | |
} | |
}, 0); | |
})(); | |
} else { | |
while(editLength <= maxEditLength) { | |
var ret = execEditLength(); | |
if (ret) { | |
return ret; | |
} | |
} | |
} | |
}, | |
pushComponent: function(components, added, removed) { | |
var last = components[components.length-1]; | |
if (last && last.added === added && last.removed === removed) { | |
// We need to clone here as the component clone operation is just | |
// as shallow array clone | |
components[components.length-1] = {count: last.count + 1, added: added, removed: removed }; | |
} else { | |
components.push({count: 1, added: added, removed: removed }); | |
} | |
}, | |
extractCommon: function(basePath, newString, oldString, diagonalPath) { | |
var newLen = newString.length, | |
oldLen = oldString.length, | |
newPos = basePath.newPos, | |
oldPos = newPos - diagonalPath, | |
commonCount = 0; | |
while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) { | |
newPos++; | |
oldPos++; | |
commonCount++; | |
} | |
if (commonCount) { | |
basePath.components.push({count: commonCount}); | |
} | |
basePath.newPos = newPos; | |
return oldPos; | |
}, | |
equals: function(left, right) { | |
var reWhitespace = /\S/; | |
return left === right || (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)); | |
}, | |
tokenize: function(value) { | |
return value.split(''); | |
} | |
}; | |
var CharDiff = new Diff(); | |
var WordDiff = new Diff(true); | |
var WordWithSpaceDiff = new Diff(); | |
WordDiff.tokenize = WordWithSpaceDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/(\s+|\b)/)); | |
}; | |
var CssDiff = new Diff(true); | |
CssDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/([{}:;,]|\s+)/)); | |
}; | |
var LineDiff = new Diff(); | |
var TrimmedLineDiff = new Diff(); | |
TrimmedLineDiff.ignoreTrim = true; | |
LineDiff.tokenize = TrimmedLineDiff.tokenize = function(value) { | |
var retLines = [], | |
lines = value.split(/^/m); | |
for(var i = 0; i < lines.length; i++) { | |
var line = lines[i], | |
lastLine = lines[i - 1], | |
lastLineLastChar = lastLine ? lastLine[lastLine.length - 1] : ''; | |
// Merge lines that may contain windows new lines | |
if (line === '\n' && (lastLineLastChar === '\r' || lastLineLastChar === '\n')) { | |
if (this.ignoreTrim || lastLineLastChar === '\n'){ | |
//to avoid merging to \n\n, remove \n and add \r\n. | |
retLines[retLines.length - 1] = retLines[retLines.length - 1].slice(0,-1) + '\r\n'; | |
} else { | |
retLines[retLines.length - 1] += '\n'; | |
} | |
} else if (line) { | |
if (this.ignoreTrim) { | |
line = line.trim(); | |
//add a newline unless this is the last line. | |
if (i < lines.length - 1) { | |
line += '\n'; | |
} | |
} | |
retLines.push(line); | |
} | |
} | |
return retLines; | |
}; | |
var SentenceDiff = new Diff(); | |
SentenceDiff.tokenize = function (value) { | |
return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/)); | |
}; | |
var JsonDiff = new Diff(); | |
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a | |
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: | |
JsonDiff.useLongestToken = true; | |
JsonDiff.tokenize = LineDiff.tokenize; | |
JsonDiff.equals = function(left, right) { | |
return LineDiff.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1')); | |
}; | |
var objectPrototypeToString = Object.prototype.toString; | |
// This function handles the presence of circular references by bailing out when encountering an | |
// object that is already on the "stack" of items being processed. | |
function canonicalize(obj, stack, replacementStack) { | |
stack = stack || []; | |
replacementStack = replacementStack || []; | |
var i; | |
for (var i = 0 ; i < stack.length ; i += 1) { | |
if (stack[i] === obj) { | |
return replacementStack[i]; | |
} | |
} | |
var canonicalizedObj; | |
if ('[object Array]' === objectPrototypeToString.call(obj)) { | |
stack.push(obj); | |
canonicalizedObj = new Array(obj.length); | |
replacementStack.push(canonicalizedObj); | |
for (i = 0 ; i < obj.length ; i += 1) { | |
canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else if (typeof obj === 'object' && obj !== null) { | |
stack.push(obj); | |
canonicalizedObj = {}; | |
replacementStack.push(canonicalizedObj); | |
var sortedKeys = []; | |
for (var key in obj) { | |
sortedKeys.push(key); | |
} | |
sortedKeys.sort(); | |
for (i = 0 ; i < sortedKeys.length ; i += 1) { | |
var key = sortedKeys[i]; | |
canonicalizedObj[key] = canonicalize(obj[key], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else { | |
canonicalizedObj = obj; | |
} | |
return canonicalizedObj; | |
} | |
return { | |
Diff: Diff, | |
diffChars: function(oldStr, newStr, callback) { return CharDiff.diff(oldStr, newStr, callback); }, | |
diffWords: function(oldStr, newStr, callback) { return WordDiff.diff(oldStr, newStr, callback); }, | |
diffWordsWithSpace: function(oldStr, newStr, callback) { return WordWithSpaceDiff.diff(oldStr, newStr, callback); }, | |
diffLines: function(oldStr, newStr, callback) { return LineDiff.diff(oldStr, newStr, callback); }, | |
diffTrimmedLines: function(oldStr, newStr, callback) { return TrimmedLineDiff.diff(oldStr, newStr, callback); }, | |
diffSentences: function(oldStr, newStr, callback) { return SentenceDiff.diff(oldStr, newStr, callback); }, | |
diffCss: function(oldStr, newStr, callback) { return CssDiff.diff(oldStr, newStr, callback); }, | |
diffJson: function(oldObj, newObj, callback) { | |
return JsonDiff.diff( | |
typeof oldObj === 'string' ? oldObj : JSON.stringify(canonicalize(oldObj), undefined, ' '), | |
typeof newObj === 'string' ? newObj : JSON.stringify(canonicalize(newObj), undefined, ' '), | |
callback | |
); | |
}, | |
createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) { | |
var ret = []; | |
ret.push('Index: ' + fileName); | |
ret.push('==================================================================='); | |
ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader)); | |
ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader)); | |
var diff = LineDiff.diff(oldStr, newStr); | |
if (!diff[diff.length-1].value) { | |
diff.pop(); // Remove trailing newline add | |
} | |
diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier | |
function contextLines(lines) { | |
return map(lines, function(entry) { return ' ' + entry; }); | |
} | |
function eofNL(curRange, i, current) { | |
var last = diff[diff.length-2], | |
isLast = i === diff.length-2, | |
isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed); | |
// Figure out if this is the last line for the given file and missing NL | |
if (!/\n$/.test(current.value) && (isLast || isLastOfType)) { | |
curRange.push('\\ No newline at end of file'); | |
} | |
} | |
var oldRangeStart = 0, newRangeStart = 0, curRange = [], | |
oldLine = 1, newLine = 1; | |
for (var i = 0; i < diff.length; i++) { | |
var current = diff[i], | |
lines = current.lines || current.value.replace(/\n$/, '').split('\n'); | |
current.lines = lines; | |
if (current.added || current.removed) { | |
if (!oldRangeStart) { | |
var prev = diff[i-1]; | |
oldRangeStart = oldLine; | |
newRangeStart = newLine; | |
if (prev) { | |
curRange = contextLines(prev.lines.slice(-4)); | |
oldRangeStart -= curRange.length; | |
newRangeStart -= curRange.length; | |
} | |
} | |
curRange.push.apply(curRange, map(lines, function(entry) { return (current.added?'+':'-') + entry; })); | |
eofNL(curRange, i, current); | |
if (current.added) { | |
newLine += lines.length; | |
} else { | |
oldLine += lines.length; | |
} | |
} else { | |
if (oldRangeStart) { | |
// Close out any changes that have been output (or join overlapping) | |
if (lines.length <= 8 && i < diff.length-2) { | |
// Overlapping | |
curRange.push.apply(curRange, contextLines(lines)); | |
} else { | |
// end the range and output | |
var contextSize = Math.min(lines.length, 4); | |
ret.push( | |
'@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize) | |
+ ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize) | |
+ ' @@'); | |
ret.push.apply(ret, curRange); | |
ret.push.apply(ret, contextLines(lines.slice(0, contextSize))); | |
if (lines.length <= 4) { | |
eofNL(ret, i, current); | |
} | |
oldRangeStart = 0; newRangeStart = 0; curRange = []; | |
} | |
} | |
oldLine += lines.length; | |
newLine += lines.length; | |
} | |
} | |
return ret.join('\n') + '\n'; | |
}, | |
applyPatch: function(oldStr, uniDiff) { | |
var diffstr = uniDiff.split('\n'); | |
var diff = []; | |
var remEOFNL = false, | |
addEOFNL = false; | |
for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) { | |
if (diffstr[i][0] === '@') { | |
var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/); | |
diff.unshift({ | |
start:meh[3], | |
oldlength:meh[2], | |
oldlines:[], | |
newlength:meh[4], | |
newlines:[] | |
}); | |
} else if (diffstr[i][0] === '+') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '-') { | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === ' ') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '\\') { | |
if (diffstr[i-1][0] === '+') { | |
remEOFNL = true; | |
} else if (diffstr[i-1][0] === '-') { | |
addEOFNL = true; | |
} | |
} | |
} | |
var str = oldStr.split('\n'); | |
for (var i = diff.length - 1; i >= 0; i--) { | |
var d = diff[i]; | |
for (var j = 0; j < d.oldlength; j++) { | |
if (str[d.start-1+j] !== d.oldlines[j]) { | |
return false; | |
} | |
} | |
Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines)); | |
} | |
if (remEOFNL) { | |
while (!str[str.length-1]) { | |
str.pop(); | |
} | |
} else if (addEOFNL) { | |
str.push(''); | |
} | |
return str.join('\n'); | |
}, | |
convertChangesToXML: function(changes){ | |
var ret = []; | |
for ( var i = 0; i < changes.length; i++) { | |
var change = changes[i]; | |
if (change.added) { | |
ret.push('<ins>'); | |
} else if (change.removed) { | |
ret.push('<del>'); | |
} | |
ret.push(escapeHTML(change.value)); | |
if (change.added) { | |
ret.push('</ins>'); | |
} else if (change.removed) { | |
ret.push('</del>'); | |
} | |
} | |
return ret.join(''); | |
}, | |
// See: http://code.google.com/p/google-diff-match-patch/wiki/API | |
convertChangesToDMP: function(changes){ | |
var ret = [], change; | |
for ( var i = 0; i < changes.length; i++) { | |
change = changes[i]; | |
ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]); | |
} | |
return ret; | |
}, | |
canonicalize: canonicalize | |
}; | |
})(); | |
/*istanbul ignore next */ | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = JsDiff; | |
} | |
else if (typeof define === 'function' && define.amd) { | |
/*global define */ | |
define([], function() { return JsDiff; }); | |
} | |
else if (typeof global.JsDiff === 'undefined') { | |
global.JsDiff = JsDiff; | |
} | |
})(this); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//code.jquery.com/jquery-2.1.1.min.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style> | |
#before, #after {display: none;} | |
</style> | |
</head> | |
<body> | |
<p>Will compare versions of a long markdown document in different ways. Markdown doc here <a href="https://github.com/jgm/CommonMark/commits/master/README.md" target="blank">markdown doc</a></p> | |
<div id="here">-</div> | |
<pre id="result">-</pre> | |
<div id="before"> | |
CommonMark | |
========== | |
CommonMark is a rationalized version of Markdown syntax, | |
with a [spec][the spec] and BSD3-licensed reference | |
implementations in C and JavaScript. | |
[Try it now!](http://spec.commonmark.org/dingus.html) | |
The implementations | |
------------------- | |
The C implementation provides both a library and a standalone program | |
`cmark` that converts CommonMark to HTML. It is written in standard C99 | |
and has no library dependencies. The parser is very fast, on par with | |
[sundown](https://github.com/vmg/sundown). Some benchmarks (on | |
an ancient Thinkpad running Intel Core 2 Duo at 2GHz, measured using | |
`time` and parsing a ~500K book, the English version of [*Pro | |
Git*](https://github.com/progit/progit/tree/master/en) by | |
Scott Chacon and Ben Straub): | |
|Implementation | Time | Factor| | |
|---------------|-------|--------| | |
| Markdown.pl | 5.162s| 286.8| | |
| PHP Markdown | 1.021s| 56.7| | |
| commonmark.js | 0.292s| 16.2| | |
| peg-markdown | 0.279s| 15.5| | |
| marked | 0.239s| 13.3| | |
| discount | 0.090s| 5.0| | |
| **cmark** | 0.020s| 1.1| | |
| sundown | 0.018s| 1.0| | |
The JavaScript implementation is a single JavaScript file, with | |
no dependencies, that can be linked to in an HTML page. Here | |
is a simple usage example: | |
``` javascript | |
var reader = new commonmark.DocParser(); | |
var writer = new commonmark.HtmlRenderer(); | |
var parsed = reader.parse("Hello *world*"); | |
var result = writer.render(parsed); | |
``` | |
A node package is also available; it includes a command-line tool called | |
`commonmark`. | |
**A note on security:** | |
Neither implementation attempts to sanitize link attributes or | |
raw HTML. If you use these libraries in applications that accept | |
untrusted user input, you must run the output through an HTML | |
sanitizer to protect against | |
[XSS attacks](http://en.wikipedia.org/wiki/Cross-site_scripting). | |
Installing (C) | |
-------------- | |
Building the C program (`cmark`) and shared library (`libcmark`) | |
requires [cmake] and [re2c], which is used to generate `scanners.c` from | |
`scanners.re`. (Note that [re2c] is only a build dependency for | |
developers, since `scanners.c` can be provided in a released source | |
tarball.) | |
On \*nix systems, you can simply `make` and `make install`. This | |
calls [cmake] to create a `Makefile` in the `build` directory, | |
then uses that `Makefile` to create the executable and library. | |
Alternatively, you can use [cmake] manually. [cmake] knows how | |
to create build environments for many build systems. For | |
example, to create Xcode project files on OSX: | |
mkdir build | |
cd build | |
cmake -G Xcode .. # optionally: -DCMAKE_INSTALL_PREFIX=path | |
make # executable will be created as build/src/cmake | |
make install | |
To run tests: | |
make test | |
(Or `perl runtests.pl spec.txt build/src/cmark` or, in the cmake | |
build directory, `ctest -V`.) | |
To test the shared library via a python wrapper: | |
make testlib | |
To run a "fuzz test" against ten long randomly generated inputs: | |
make fuzztest | |
To run a test for memory leaks using valgrind: | |
make leakcheck | |
To make a release tarball: | |
make tarball | |
Installing (JavaScript) | |
----------------------- | |
The JavaScript library can be installed through `npm`: | |
npm install commonmark | |
To build the JavaScript library as a single standalone file: | |
browserify --standalone commonmark js/lib/index.js -o js/commonmark.js | |
Or fetch a pre-built copy from | |
<http://spec.commonmark.org/js/commonmark.js>`. | |
To run tests for the JavaScript library: | |
make testjs | |
or | |
node js/test.js | |
The spec | |
-------- | |
[The spec] contains over 500 embedded examples which serve as conformance | |
tests. To run the tests for `cmark`, do `make test`. To run them for | |
another Markdown program, say `myprog`, do `make test PROG=myprog`. To | |
run the tests for `commonmark.js`, do `make testjs`. | |
[The spec]: http://jgm.github.io/CommonMark/spec.html | |
The source of [the spec] is `spec.txt`. This is basically a Markdown | |
file, with code examples written in a shorthand form: | |
. | |
Markdown source | |
. | |
expected HTML output | |
. | |
To build an HTML version of the spec, do `make spec.html`. To build a | |
PDF version, do `make spec.pdf`. Both these commands require that | |
[pandoc] is installed, and creating a PDF requires a latex installation. | |
The spec is written from the point of view of the human writer, not | |
the computer reader. It is not an algorithm---an English translation of | |
a computer program---but a declarative description of what counts as a block | |
quote, a code block, and each of the other structural elements that can | |
make up a Markdown document. | |
Because John Gruber's [canonical syntax | |
description](http://daringfireball.net/projects/markdown/syntax) leaves | |
many aspects of the syntax undetermined, writing a precise spec requires | |
making a large number of decisions, many of them somewhat arbitrary. | |
In making them, I have appealed to existing conventions and | |
considerations of simplicity, readability, expressive power, and | |
consistency. I have tried to ensure that "normal" documents in the many | |
incompatible existing implementations of Markdown will render, as far as | |
possible, as their authors intended. And I have tried to make the rules | |
for different elements work together harmoniously. In places where | |
different decisions could have been made (for example, the rules | |
governing list indentation), I have explained the rationale for | |
my choices. In a few cases, I have departed slightly from the canonical | |
syntax description, in ways that I think further the goals of Markdown | |
as stated in that description. | |
For the most part, I have limited myself to the basic elements | |
described in Gruber's canonical syntax description, eschewing extensions | |
like footnotes and definition lists. It is important to get the core | |
right before considering such things. However, I have included a visible | |
syntax for line breaks and fenced code blocks. | |
Differences from original Markdown | |
---------------------------------- | |
There are only a few places where this spec says things that contradict | |
the canonical syntax description: | |
- It [allows all punctuation symbols to be | |
backslash-escaped](http://jgm.github.io/stmd/spec.html#backslash-escapes), | |
not just the symbols with special meanings in Markdown. I found | |
that it was just too hard to remember which symbols could be | |
escaped. | |
- It introduces an [alternative syntax for hard line | |
breaks](http://jgm.github.io/stmd/spec.html#hard-line-breaks), a | |
backslash at the end of the line, supplementing the | |
two-spaces-at-the-end-of-line rule. This is motivated by persistent | |
complaints about the “invisible” nature of the two-space rule. | |
- Link syntax has been made a bit more predictable (in a | |
backwards-compatible way). For example, `Markdown.pl` allows single | |
quotes around a title in inline links, but not in reference links. | |
This kind of difference is really hard for users to remember, so the | |
spec [allows single quotes in both | |
contexts](http://jgm.github.io/stmd/spec.html#links). | |
- The rule for HTML blocks differs, though in most real cases it | |
shouldn't make a difference. (See | |
[here](http://jgm.github.io/stmd/spec.html#html-blocks) for | |
details.) The spec's proposal makes it easy to include Markdown | |
inside HTML block-level tags, if you want to, but also allows you to | |
exclude this. It is also makes parsing much easier, avoiding | |
expensive backtracking. | |
- It does not collapse adjacent bird-track blocks into a single | |
blockquote: | |
> this is two | |
> blockquotes | |
> this is a single | |
> | |
> blockquote with two paragraphs | |
- Rules for content in lists differ in a few respects, though (as with | |
HTML blocks), most lists in existing documents should render as | |
intended. There is some discussion of the choice points and | |
differences [here](http://jgm.github.io/stmd/spec.html#motivation). | |
I think that the spec's proposal does better than any existing | |
implementation in rendering lists the way a human writer or reader | |
would intuitively understand them. (I could give numerous examples | |
of perfectly natural looking lists that nearly every existing | |
implementation flubs up.) | |
- The spec stipulates that two blank lines break out of all list | |
contexts. This is an attempt to deal with issues that often come up | |
when someone wants to have two adjacent lists, or a list followed by | |
an indented code block. | |
- Changing bullet characters, or changing from bullets to numbers or | |
vice versa, starts a new list. I think that is almost always going | |
to be the writer's intent. | |
- The number that begins an ordered list item may be followed by | |
either `.` or `)`. Changing the delimiter style starts a new | |
list. | |
- The start number of an ordered list is significant. | |
- [Fenced code blocks](http://jgm.github.io/stmd/spec.html#fenced-code-blocks) are supported, delimited by either | |
backticks (` ``` `) or tildes (` ~~~ `). | |
In all of this, I have been guided by eight years experience writing | |
Markdown implementations in several languages, including the first | |
Markdown parser not based on regular expression substitutions | |
([pandoc](http://github.com/jgm/pandoc)) and the first markdown parsers | |
based on PEG grammars | |
([peg-markdown](http://github.com/jgm/peg-markdown), | |
[lunamark](http://github.com/jgm/lunamark)). Maintaining these projects | |
and responding to years of user feedback have given me a good sense of | |
the complexities involved in parsing Markdown, and of the various design | |
decisions that can be made. I have also explored differences between | |
Markdown implementations extensively using [BabelMark | |
2](http://johnmacfarlane.net/babelmark2/). In the early phases of | |
working out the spec, I benefited greatly from collaboration with David | |
Greenspan, and from feedback from several industrial users of Markdown, | |
including Jeff Atwood, Vincent Marti, and Neil Williams. | |
Contributing | |
------------ | |
There is a [forum for discussing | |
CommonMark](http://talk.commonmark.org); you should use it instead of | |
github issues for questions and possibly open-ended discussions. | |
Use the [github issue tracker](http://github.com/jgm/stmd/issues) | |
only for simple, clear, actionable issues. | |
[cmake]: http://www.cmake.org/download/ | |
[pandoc]: http://johnmacfarlane.net/pandoc/ | |
[re2c]: http://re2c.org | |
</div> | |
<div id="after">CommonMark | |
========== | |
CommonMark is a rationalized version of Markdown syntax, | |
with a [spec][the spec] and BSD-licensed reference | |
implementations in C and JavaScript. | |
[Try it now!](http://try.commonmark.org/) | |
[the spec]: http://spec.commonmark.org/ | |
For more details, see <http://commonmark.org>. | |
This repository contains the spec itself, along with tools for | |
running tests against the spec, and for creating HTML and PDF versions | |
of the spec. | |
The reference implementations live in separate repositories: | |
- <https://github.com/jgm/cmark> (C) | |
- <https://github.com/jgm/commonmark.js> (JavaScript) | |
Running tests against the spec | |
------------------------------ | |
[The spec] contains over 500 embedded examples which serve as conformance | |
tests. To run the tests using an executable `$PROG`: | |
python3 test/spec_tests.py --program $PROG | |
If you want to extract the raw test data from the spec without | |
actually running the tests, you can do: | |
python3 test/spec_tests.py --dump-tests | |
and you'll get all the tests in JSON format. | |
The spec | |
-------- | |
The source of [the spec] is `spec.txt`. This is basically a Markdown | |
file, with code examples written in a shorthand form: | |
. | |
Markdown source | |
. | |
expected HTML output | |
. | |
To build an HTML version of the spec, do `make spec.html`. To build a | |
PDF version, do `make spec.pdf`. (Creating a PDF requires [pandoc] | |
and a LaTeX installation. Creating the HTML version requires only | |
`libcmark` and `python3`.) | |
The spec is written from the point of view of the human writer, not | |
the computer reader. It is not an algorithm---an English translation of | |
a computer program---but a declarative description of what counts as a block | |
quote, a code block, and each of the other structural elements that can | |
make up a Markdown document. | |
Because John Gruber's [canonical syntax | |
description](http://daringfireball.net/projects/markdown/syntax) leaves | |
many aspects of the syntax undetermined, writing a precise spec requires | |
making a large number of decisions, many of them somewhat arbitrary. | |
In making them, we have appealed to existing conventions and | |
considerations of simplicity, readability, expressive power, and | |
consistency. We have tried to ensure that "normal" documents in the many | |
incompatible existing implementations of Markdown will render, as far as | |
possible, as their authors intended. And we have tried to make the rules | |
for different elements work together harmoniously. In places where | |
different decisions could have been made (for example, the rules | |
governing list indentation), we have explained the rationale for | |
my choices. In a few cases, we have departed slightly from the canonical | |
syntax description, in ways that we think further the goals of Markdown | |
as stated in that description. | |
For the most part, we have limited ourselves to the basic elements | |
described in Gruber's canonical syntax description, eschewing extensions | |
like footnotes and definition lists. It is important to get the core | |
right before considering such things. However, we have included a visible | |
syntax for line breaks and fenced code blocks. | |
Differences from original Markdown | |
---------------------------------- | |
There are only a few places where this spec says things that contradict | |
the canonical syntax description: | |
- It allows all punctuation symbols to be backslash-escaped, | |
not just the symbols with special meanings in Markdown. We found | |
that it was just too hard to remember which symbols could be | |
escaped. | |
- It introduces an alternative syntax for hard line | |
breaks, a backslash at the end of the line, supplementing the | |
two-spaces-at-the-end-of-line rule. This is motivated by persistent | |
complaints about the “invisible” nature of the two-space rule. | |
- Link syntax has been made a bit more predictable (in a | |
backwards-compatible way). For example, `Markdown.pl` allows single | |
quotes around a title in inline links, but not in reference links. | |
This kind of difference is really hard for users to remember, so the | |
spec allows single quotes in both contexts. | |
- The rule for HTML blocks differs, though in most real cases it | |
shouldn't make a difference. (See the section on HTML Blocks | |
for details.) The spec's proposal makes it easy to include Markdown | |
inside HTML block-level tags, if you want to, but also allows you to | |
exclude this. It is also makes parsing much easier, avoiding | |
expensive backtracking. | |
- It does not collapse adjacent bird-track blocks into a single | |
blockquote: | |
> this is two | |
> blockquotes | |
> this is a single | |
> | |
> blockquote with two paragraphs | |
- Rules for content in lists differ in a few respects, though (as with | |
HTML blocks), most lists in existing documents should render as | |
intended. There is some discussion of the choice points and | |
differences in the subsection of List Items entitled Motivation. | |
We think that the spec's proposal does better than any existing | |
implementation in rendering lists the way a human writer or reader | |
would intuitively understand them. (We could give numerous examples | |
of perfectly natural looking lists that nearly every existing | |
implementation flubs up.) | |
- The spec stipulates that two blank lines break out of all list | |
contexts. This is an attempt to deal with issues that often come up | |
when someone wants to have two adjacent lists, or a list followed by | |
an indented code block. | |
- Changing bullet characters, or changing from bullets to numbers or | |
vice versa, starts a new list. We think that is almost always going | |
to be the writer's intent. | |
- The number that begins an ordered list item may be followed by | |
either `.` or `)`. Changing the delimiter style starts a new | |
list. | |
- The start number of an ordered list is significant. | |
- Fenced code blocks are supported, delimited by either | |
backticks (```` ``` ```` or tildes (` ~~~ `). | |
Contributing | |
------------ | |
There is a [forum for discussing | |
CommonMark](http://talk.commonmark.org); you should use it instead of | |
github issues for questions and possibly open-ended discussions. | |
Use the [github issue tracker](http://github.com/jgm/CommonMark/issues) | |
only for simple, clear, actionable issues. | |
Authors | |
------- | |
The spec was written by John MacFarlane, drawing on | |
- his experience writing and maintaining Markdown implementations in several | |
languages, including the first Markdown parser not based on regular | |
expression substitutions ([pandoc](http://github.com/jgm/pandoc)) and | |
the first markdown parsers based on PEG grammars | |
([peg-markdown](http://github.com/jgm/peg-markdown), | |
[lunamark](http://github.com/jgm/lunamark)) | |
- a detailed examination of the differences between existing Markdown | |
implementations using [BabelMark 2](http://johnmacfarlane.net/babelmark2/), | |
and | |
- extensive discussions with David Greenspan, Jeff Atwood, Vicent | |
Marti, Neil Williams, and Benjamin Dumke-von der Ehe. | |
Since the first announcement, many people have contributed ideas. | |
Kārlis Gaņģis was especially helpful in refining the rules for | |
emphasis, strong emphasis, links, and images.</div> | |
<div id="crazylong950"> | |
</div> | |
</body> | |
</html></script> | |
<script id="jsbin-source-css" type="text/css">pre { background-color: #fff} | |
ins { | |
background-color: #eaffea; | |
} | |
del { | |
color: #666; | |
background-color: #ffecec; | |
} | |
ins,del{ | |
text-decoration: none; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/*purpose: Take long markdown texts and diff asynchronously. | |
I hope to update display asynchronously | |
*/ | |
$(function(){ | |
var out = $('#here'); | |
var result = $('#result'); | |
out.empty() | |
.append("started...") | |
.append( typeof(JsDiff)); | |
var before = $('#before').text(); | |
var after = $('#after').text(); | |
console.time("diffWords"); | |
var bigdiff = JsDiff.diffLines(before, after); | |
console.timeEnd("diffWords"); | |
//console.log(bigdiff); | |
console.time("formatDiff"); | |
var htmlDiff = formatDiff(bigdiff); | |
console.timeEnd("formatDiff"); | |
console.time("dom insert"); | |
result.append(htmlDiff); | |
console.timeEnd("dom insert"); | |
}); | |
function formatDiff(diff) { | |
return diff.map(function(section) { | |
if (section.added && section.value) { | |
return wrap('ins', section.value); | |
} else if (section.removed && section.value) { | |
return wrap('del', section.value); | |
} else { | |
return section.value; | |
} | |
}).join(''); | |
} | |
function wrap(template, value) { | |
var starts_with_indent_block = new RegExp("^(=|-|\s)+$"); | |
var starts_with_md_special = new RegExp("^((#|\\*|\-|>)+\s+)"); | |
return value.split('\n').map(function(line) { | |
if (line.match(starts_with_indent_block )) { | |
return line; | |
} | |
var splitted = line.split(starts_with_md_special); | |
if (splitted.length === 4) { | |
return splitted[1] + wwrraapp(template, splitted[3] ); | |
} else { | |
return wwrraapp(template, line ); | |
} | |
}).join('\n'); | |
} | |
function wwrraapp(tag, content) { | |
return ['<',tag,'>',content,'</',tag,'>'].join(''); | |
} | |
//NOPROTECT | |
// BELOW FIND | |
/* See LICENSE file for terms of use */ | |
/* | |
* Text diff implementation. | |
* | |
* This library supports the following APIS: | |
* JsDiff.diffChars: Character by character diff | |
* JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace | |
* JsDiff.diffLines: Line based diff | |
* | |
* JsDiff.diffCss: Diff targeted at CSS content | |
* | |
* These methods are based on the implementation proposed in | |
* "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). | |
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 | |
*/ | |
(function(global, undefined) { | |
var JsDiff = (function() { | |
/*jshint maxparams: 5*/ | |
/*istanbul ignore next*/ | |
function map(arr, mapper, that) { | |
if (Array.prototype.map) { | |
return Array.prototype.map.call(arr, mapper, that); | |
} | |
var other = new Array(arr.length); | |
for (var i = 0, n = arr.length; i < n; i++) { | |
other[i] = mapper.call(that, arr[i], i, arr); | |
} | |
return other; | |
} | |
function clonePath(path) { | |
return { newPos: path.newPos, components: path.components.slice(0) }; | |
} | |
function removeEmpty(array) { | |
var ret = []; | |
for (var i = 0; i < array.length; i++) { | |
if (array[i]) { | |
ret.push(array[i]); | |
} | |
} | |
return ret; | |
} | |
function escapeHTML(s) { | |
var n = s; | |
n = n.replace(/&/g, '&'); | |
n = n.replace(/</g, '<'); | |
n = n.replace(/>/g, '>'); | |
n = n.replace(/"/g, '"'); | |
return n; | |
} | |
function buildValues(components, newString, oldString, useLongestToken) { | |
var componentPos = 0, | |
componentLen = components.length, | |
newPos = 0, | |
oldPos = 0; | |
for (; componentPos < componentLen; componentPos++) { | |
var component = components[componentPos]; | |
if (!component.removed) { | |
if (!component.added && useLongestToken) { | |
var value = newString.slice(newPos, newPos + component.count); | |
value = map(value, function(value, i) { | |
var oldValue = oldString[oldPos + i]; | |
return oldValue.length > value.length ? oldValue : value; | |
}); | |
component.value = value.join(''); | |
} else { | |
component.value = newString.slice(newPos, newPos + component.count).join(''); | |
} | |
newPos += component.count; | |
// Common case | |
if (!component.added) { | |
oldPos += component.count; | |
} | |
} else { | |
component.value = oldString.slice(oldPos, oldPos + component.count).join(''); | |
oldPos += component.count; | |
} | |
} | |
return components; | |
} | |
var Diff = function(ignoreWhitespace) { | |
this.ignoreWhitespace = ignoreWhitespace; | |
}; | |
Diff.prototype = { | |
diff: function(oldString, newString, callback) { | |
var self = this; | |
function done(value) { | |
if (callback) { | |
setTimeout(function() { callback(undefined, value); }, 0); | |
return true; | |
} else { | |
return value; | |
} | |
} | |
// Handle the identity case (this is due to unrolling editLength == 0 | |
if (newString === oldString) { | |
return done([{ value: newString }]); | |
} | |
if (!newString) { | |
return done([{ value: oldString, removed: true }]); | |
} | |
if (!oldString) { | |
return done([{ value: newString, added: true }]); | |
} | |
newString = this.tokenize(newString); | |
oldString = this.tokenize(oldString); | |
var newLen = newString.length, oldLen = oldString.length; | |
var maxEditLength = newLen + oldLen; | |
var bestPath = [{ newPos: -1, components: [] }]; | |
// Seed editLength = 0, i.e. the content starts with the same values | |
var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); | |
if (bestPath[0].newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
// Identity per the equality and tokenizer | |
return done([{value: newString.join('')}]); | |
} | |
// Main worker method. checks all permutations of a given edit length for acceptance. | |
function execEditLength() { | |
for (var diagonalPath = -1*editLength; diagonalPath <= editLength; diagonalPath+=2) { | |
var basePath; | |
var addPath = bestPath[diagonalPath-1], | |
removePath = bestPath[diagonalPath+1]; | |
oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; | |
if (addPath) { | |
// No one else is going to attempt to use this value, clear it | |
bestPath[diagonalPath-1] = undefined; | |
} | |
var canAdd = addPath && addPath.newPos+1 < newLen; | |
var canRemove = removePath && 0 <= oldPos && oldPos < oldLen; | |
if (!canAdd && !canRemove) { | |
// If this path is a terminal then prune | |
bestPath[diagonalPath] = undefined; | |
continue; | |
} | |
// Select the diagonal that we want to branch from. We select the prior | |
// path whose position in the new string is the farthest from the origin | |
// and does not pass the bounds of the diff graph | |
if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) { | |
basePath = clonePath(removePath); | |
self.pushComponent(basePath.components, undefined, true); | |
} else { | |
basePath = addPath; // No need to clone, we've pulled it from the list | |
basePath.newPos++; | |
self.pushComponent(basePath.components, true, undefined); | |
} | |
var oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); | |
// If we have hit the end of both strings, then we are done | |
if (basePath.newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
return done(buildValues(basePath.components, newString, oldString, self.useLongestToken)); | |
} else { | |
// Otherwise track this path as a potential candidate and continue. | |
bestPath[diagonalPath] = basePath; | |
} | |
} | |
editLength++; | |
} | |
// Performs the length of edit iteration. Is a bit fugly as this has to support the | |
// sync and async mode which is never fun. Loops over execEditLength until a value | |
// is produced. | |
var editLength = 1; | |
if (callback) { | |
(function exec() { | |
setTimeout(function() { | |
// This should not happen, but we want to be safe. | |
/*istanbul ignore next */ | |
if (editLength > maxEditLength) { | |
return callback(); | |
} | |
if (!execEditLength()) { | |
exec(); | |
} | |
}, 0); | |
})(); | |
} else { | |
while(editLength <= maxEditLength) { | |
var ret = execEditLength(); | |
if (ret) { | |
return ret; | |
} | |
} | |
} | |
}, | |
pushComponent: function(components, added, removed) { | |
var last = components[components.length-1]; | |
if (last && last.added === added && last.removed === removed) { | |
// We need to clone here as the component clone operation is just | |
// as shallow array clone | |
components[components.length-1] = {count: last.count + 1, added: added, removed: removed }; | |
} else { | |
components.push({count: 1, added: added, removed: removed }); | |
} | |
}, | |
extractCommon: function(basePath, newString, oldString, diagonalPath) { | |
var newLen = newString.length, | |
oldLen = oldString.length, | |
newPos = basePath.newPos, | |
oldPos = newPos - diagonalPath, | |
commonCount = 0; | |
while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) { | |
newPos++; | |
oldPos++; | |
commonCount++; | |
} | |
if (commonCount) { | |
basePath.components.push({count: commonCount}); | |
} | |
basePath.newPos = newPos; | |
return oldPos; | |
}, | |
equals: function(left, right) { | |
var reWhitespace = /\S/; | |
return left === right || (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)); | |
}, | |
tokenize: function(value) { | |
return value.split(''); | |
} | |
}; | |
var CharDiff = new Diff(); | |
var WordDiff = new Diff(true); | |
var WordWithSpaceDiff = new Diff(); | |
WordDiff.tokenize = WordWithSpaceDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/(\s+|\b)/)); | |
}; | |
var CssDiff = new Diff(true); | |
CssDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/([{}:;,]|\s+)/)); | |
}; | |
var LineDiff = new Diff(); | |
var TrimmedLineDiff = new Diff(); | |
TrimmedLineDiff.ignoreTrim = true; | |
LineDiff.tokenize = TrimmedLineDiff.tokenize = function(value) { | |
var retLines = [], | |
lines = value.split(/^/m); | |
for(var i = 0; i < lines.length; i++) { | |
var line = lines[i], | |
lastLine = lines[i - 1], | |
lastLineLastChar = lastLine ? lastLine[lastLine.length - 1] : ''; | |
// Merge lines that may contain windows new lines | |
if (line === '\n' && (lastLineLastChar === '\r' || lastLineLastChar === '\n')) { | |
if (this.ignoreTrim || lastLineLastChar === '\n'){ | |
//to avoid merging to \n\n, remove \n and add \r\n. | |
retLines[retLines.length - 1] = retLines[retLines.length - 1].slice(0,-1) + '\r\n'; | |
} else { | |
retLines[retLines.length - 1] += '\n'; | |
} | |
} else if (line) { | |
if (this.ignoreTrim) { | |
line = line.trim(); | |
//add a newline unless this is the last line. | |
if (i < lines.length - 1) { | |
line += '\n'; | |
} | |
} | |
retLines.push(line); | |
} | |
} | |
return retLines; | |
}; | |
var SentenceDiff = new Diff(); | |
SentenceDiff.tokenize = function (value) { | |
return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/)); | |
}; | |
var JsonDiff = new Diff(); | |
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a | |
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: | |
JsonDiff.useLongestToken = true; | |
JsonDiff.tokenize = LineDiff.tokenize; | |
JsonDiff.equals = function(left, right) { | |
return LineDiff.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1')); | |
}; | |
var objectPrototypeToString = Object.prototype.toString; | |
// This function handles the presence of circular references by bailing out when encountering an | |
// object that is already on the "stack" of items being processed. | |
function canonicalize(obj, stack, replacementStack) { | |
stack = stack || []; | |
replacementStack = replacementStack || []; | |
var i; | |
for (var i = 0 ; i < stack.length ; i += 1) { | |
if (stack[i] === obj) { | |
return replacementStack[i]; | |
} | |
} | |
var canonicalizedObj; | |
if ('[object Array]' === objectPrototypeToString.call(obj)) { | |
stack.push(obj); | |
canonicalizedObj = new Array(obj.length); | |
replacementStack.push(canonicalizedObj); | |
for (i = 0 ; i < obj.length ; i += 1) { | |
canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else if (typeof obj === 'object' && obj !== null) { | |
stack.push(obj); | |
canonicalizedObj = {}; | |
replacementStack.push(canonicalizedObj); | |
var sortedKeys = []; | |
for (var key in obj) { | |
sortedKeys.push(key); | |
} | |
sortedKeys.sort(); | |
for (i = 0 ; i < sortedKeys.length ; i += 1) { | |
var key = sortedKeys[i]; | |
canonicalizedObj[key] = canonicalize(obj[key], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else { | |
canonicalizedObj = obj; | |
} | |
return canonicalizedObj; | |
} | |
return { | |
Diff: Diff, | |
diffChars: function(oldStr, newStr, callback) { return CharDiff.diff(oldStr, newStr, callback); }, | |
diffWords: function(oldStr, newStr, callback) { return WordDiff.diff(oldStr, newStr, callback); }, | |
diffWordsWithSpace: function(oldStr, newStr, callback) { return WordWithSpaceDiff.diff(oldStr, newStr, callback); }, | |
diffLines: function(oldStr, newStr, callback) { return LineDiff.diff(oldStr, newStr, callback); }, | |
diffTrimmedLines: function(oldStr, newStr, callback) { return TrimmedLineDiff.diff(oldStr, newStr, callback); }, | |
diffSentences: function(oldStr, newStr, callback) { return SentenceDiff.diff(oldStr, newStr, callback); }, | |
diffCss: function(oldStr, newStr, callback) { return CssDiff.diff(oldStr, newStr, callback); }, | |
diffJson: function(oldObj, newObj, callback) { | |
return JsonDiff.diff( | |
typeof oldObj === 'string' ? oldObj : JSON.stringify(canonicalize(oldObj), undefined, ' '), | |
typeof newObj === 'string' ? newObj : JSON.stringify(canonicalize(newObj), undefined, ' '), | |
callback | |
); | |
}, | |
createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) { | |
var ret = []; | |
ret.push('Index: ' + fileName); | |
ret.push('==================================================================='); | |
ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader)); | |
ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader)); | |
var diff = LineDiff.diff(oldStr, newStr); | |
if (!diff[diff.length-1].value) { | |
diff.pop(); // Remove trailing newline add | |
} | |
diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier | |
function contextLines(lines) { | |
return map(lines, function(entry) { return ' ' + entry; }); | |
} | |
function eofNL(curRange, i, current) { | |
var last = diff[diff.length-2], | |
isLast = i === diff.length-2, | |
isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed); | |
// Figure out if this is the last line for the given file and missing NL | |
if (!/\n$/.test(current.value) && (isLast || isLastOfType)) { | |
curRange.push('\\ No newline at end of file'); | |
} | |
} | |
var oldRangeStart = 0, newRangeStart = 0, curRange = [], | |
oldLine = 1, newLine = 1; | |
for (var i = 0; i < diff.length; i++) { | |
var current = diff[i], | |
lines = current.lines || current.value.replace(/\n$/, '').split('\n'); | |
current.lines = lines; | |
if (current.added || current.removed) { | |
if (!oldRangeStart) { | |
var prev = diff[i-1]; | |
oldRangeStart = oldLine; | |
newRangeStart = newLine; | |
if (prev) { | |
curRange = contextLines(prev.lines.slice(-4)); | |
oldRangeStart -= curRange.length; | |
newRangeStart -= curRange.length; | |
} | |
} | |
curRange.push.apply(curRange, map(lines, function(entry) { return (current.added?'+':'-') + entry; })); | |
eofNL(curRange, i, current); | |
if (current.added) { | |
newLine += lines.length; | |
} else { | |
oldLine += lines.length; | |
} | |
} else { | |
if (oldRangeStart) { | |
// Close out any changes that have been output (or join overlapping) | |
if (lines.length <= 8 && i < diff.length-2) { | |
// Overlapping | |
curRange.push.apply(curRange, contextLines(lines)); | |
} else { | |
// end the range and output | |
var contextSize = Math.min(lines.length, 4); | |
ret.push( | |
'@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize) | |
+ ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize) | |
+ ' @@'); | |
ret.push.apply(ret, curRange); | |
ret.push.apply(ret, contextLines(lines.slice(0, contextSize))); | |
if (lines.length <= 4) { | |
eofNL(ret, i, current); | |
} | |
oldRangeStart = 0; newRangeStart = 0; curRange = []; | |
} | |
} | |
oldLine += lines.length; | |
newLine += lines.length; | |
} | |
} | |
return ret.join('\n') + '\n'; | |
}, | |
applyPatch: function(oldStr, uniDiff) { | |
var diffstr = uniDiff.split('\n'); | |
var diff = []; | |
var remEOFNL = false, | |
addEOFNL = false; | |
for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) { | |
if (diffstr[i][0] === '@') { | |
var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/); | |
diff.unshift({ | |
start:meh[3], | |
oldlength:meh[2], | |
oldlines:[], | |
newlength:meh[4], | |
newlines:[] | |
}); | |
} else if (diffstr[i][0] === '+') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '-') { | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === ' ') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '\\') { | |
if (diffstr[i-1][0] === '+') { | |
remEOFNL = true; | |
} else if (diffstr[i-1][0] === '-') { | |
addEOFNL = true; | |
} | |
} | |
} | |
var str = oldStr.split('\n'); | |
for (var i = diff.length - 1; i >= 0; i--) { | |
var d = diff[i]; | |
for (var j = 0; j < d.oldlength; j++) { | |
if (str[d.start-1+j] !== d.oldlines[j]) { | |
return false; | |
} | |
} | |
Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines)); | |
} | |
if (remEOFNL) { | |
while (!str[str.length-1]) { | |
str.pop(); | |
} | |
} else if (addEOFNL) { | |
str.push(''); | |
} | |
return str.join('\n'); | |
}, | |
convertChangesToXML: function(changes){ | |
var ret = []; | |
for ( var i = 0; i < changes.length; i++) { | |
var change = changes[i]; | |
if (change.added) { | |
ret.push('<ins>'); | |
} else if (change.removed) { | |
ret.push('<del>'); | |
} | |
ret.push(escapeHTML(change.value)); | |
if (change.added) { | |
ret.push('</ins>'); | |
} else if (change.removed) { | |
ret.push('</del>'); | |
} | |
} | |
return ret.join(''); | |
}, | |
// See: http://code.google.com/p/google-diff-match-patch/wiki/API | |
convertChangesToDMP: function(changes){ | |
var ret = [], change; | |
for ( var i = 0; i < changes.length; i++) { | |
change = changes[i]; | |
ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]); | |
} | |
return ret; | |
}, | |
canonicalize: canonicalize | |
}; | |
})(); | |
/*istanbul ignore next */ | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = JsDiff; | |
} | |
else if (typeof define === 'function' && define.amd) { | |
/*global define */ | |
define([], function() { return JsDiff; }); | |
} | |
else if (typeof global.JsDiff === 'undefined') { | |
global.JsDiff = JsDiff; | |
} | |
})(this);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pre { background-color: #fff} | |
ins { | |
background-color: #eaffea; | |
} | |
del { | |
color: #666; | |
background-color: #ffecec; | |
} | |
ins,del{ | |
text-decoration: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*purpose: Take long markdown texts and diff asynchronously. | |
I hope to update display asynchronously | |
*/ | |
$(function(){ | |
var out = $('#here'); | |
var result = $('#result'); | |
out.empty() | |
.append("started...") | |
.append( typeof(JsDiff)); | |
var before = $('#before').text(); | |
var after = $('#after').text(); | |
console.time("diffWords"); | |
var bigdiff = JsDiff.diffLines(before, after); | |
console.timeEnd("diffWords"); | |
//console.log(bigdiff); | |
console.time("formatDiff"); | |
var htmlDiff = formatDiff(bigdiff); | |
console.timeEnd("formatDiff"); | |
console.time("dom insert"); | |
result.append(htmlDiff); | |
console.timeEnd("dom insert"); | |
}); | |
function formatDiff(diff) { | |
return diff.map(function(section) { | |
if (section.added && section.value) { | |
return wrap('ins', section.value); | |
} else if (section.removed && section.value) { | |
return wrap('del', section.value); | |
} else { | |
return section.value; | |
} | |
}).join(''); | |
} | |
function wrap(template, value) { | |
var starts_with_indent_block = new RegExp("^(=|-|\s)+$"); | |
var starts_with_md_special = new RegExp("^((#|\\*|\-|>)+\s+)"); | |
return value.split('\n').map(function(line) { | |
if (line.match(starts_with_indent_block )) { | |
return line; | |
} | |
var splitted = line.split(starts_with_md_special); | |
if (splitted.length === 4) { | |
return splitted[1] + wwrraapp(template, splitted[3] ); | |
} else { | |
return wwrraapp(template, line ); | |
} | |
}).join('\n'); | |
} | |
function wwrraapp(tag, content) { | |
return ['<',tag,'>',content,'</',tag,'>'].join(''); | |
} | |
//NOPROTECT | |
// BELOW FIND | |
/* See LICENSE file for terms of use */ | |
/* | |
* Text diff implementation. | |
* | |
* This library supports the following APIS: | |
* JsDiff.diffChars: Character by character diff | |
* JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace | |
* JsDiff.diffLines: Line based diff | |
* | |
* JsDiff.diffCss: Diff targeted at CSS content | |
* | |
* These methods are based on the implementation proposed in | |
* "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). | |
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 | |
*/ | |
(function(global, undefined) { | |
var JsDiff = (function() { | |
/*jshint maxparams: 5*/ | |
/*istanbul ignore next*/ | |
function map(arr, mapper, that) { | |
if (Array.prototype.map) { | |
return Array.prototype.map.call(arr, mapper, that); | |
} | |
var other = new Array(arr.length); | |
for (var i = 0, n = arr.length; i < n; i++) { | |
other[i] = mapper.call(that, arr[i], i, arr); | |
} | |
return other; | |
} | |
function clonePath(path) { | |
return { newPos: path.newPos, components: path.components.slice(0) }; | |
} | |
function removeEmpty(array) { | |
var ret = []; | |
for (var i = 0; i < array.length; i++) { | |
if (array[i]) { | |
ret.push(array[i]); | |
} | |
} | |
return ret; | |
} | |
function escapeHTML(s) { | |
var n = s; | |
n = n.replace(/&/g, '&'); | |
n = n.replace(/</g, '<'); | |
n = n.replace(/>/g, '>'); | |
n = n.replace(/"/g, '"'); | |
return n; | |
} | |
function buildValues(components, newString, oldString, useLongestToken) { | |
var componentPos = 0, | |
componentLen = components.length, | |
newPos = 0, | |
oldPos = 0; | |
for (; componentPos < componentLen; componentPos++) { | |
var component = components[componentPos]; | |
if (!component.removed) { | |
if (!component.added && useLongestToken) { | |
var value = newString.slice(newPos, newPos + component.count); | |
value = map(value, function(value, i) { | |
var oldValue = oldString[oldPos + i]; | |
return oldValue.length > value.length ? oldValue : value; | |
}); | |
component.value = value.join(''); | |
} else { | |
component.value = newString.slice(newPos, newPos + component.count).join(''); | |
} | |
newPos += component.count; | |
// Common case | |
if (!component.added) { | |
oldPos += component.count; | |
} | |
} else { | |
component.value = oldString.slice(oldPos, oldPos + component.count).join(''); | |
oldPos += component.count; | |
} | |
} | |
return components; | |
} | |
var Diff = function(ignoreWhitespace) { | |
this.ignoreWhitespace = ignoreWhitespace; | |
}; | |
Diff.prototype = { | |
diff: function(oldString, newString, callback) { | |
var self = this; | |
function done(value) { | |
if (callback) { | |
setTimeout(function() { callback(undefined, value); }, 0); | |
return true; | |
} else { | |
return value; | |
} | |
} | |
// Handle the identity case (this is due to unrolling editLength == 0 | |
if (newString === oldString) { | |
return done([{ value: newString }]); | |
} | |
if (!newString) { | |
return done([{ value: oldString, removed: true }]); | |
} | |
if (!oldString) { | |
return done([{ value: newString, added: true }]); | |
} | |
newString = this.tokenize(newString); | |
oldString = this.tokenize(oldString); | |
var newLen = newString.length, oldLen = oldString.length; | |
var maxEditLength = newLen + oldLen; | |
var bestPath = [{ newPos: -1, components: [] }]; | |
// Seed editLength = 0, i.e. the content starts with the same values | |
var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); | |
if (bestPath[0].newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
// Identity per the equality and tokenizer | |
return done([{value: newString.join('')}]); | |
} | |
// Main worker method. checks all permutations of a given edit length for acceptance. | |
function execEditLength() { | |
for (var diagonalPath = -1*editLength; diagonalPath <= editLength; diagonalPath+=2) { | |
var basePath; | |
var addPath = bestPath[diagonalPath-1], | |
removePath = bestPath[diagonalPath+1]; | |
oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; | |
if (addPath) { | |
// No one else is going to attempt to use this value, clear it | |
bestPath[diagonalPath-1] = undefined; | |
} | |
var canAdd = addPath && addPath.newPos+1 < newLen; | |
var canRemove = removePath && 0 <= oldPos && oldPos < oldLen; | |
if (!canAdd && !canRemove) { | |
// If this path is a terminal then prune | |
bestPath[diagonalPath] = undefined; | |
continue; | |
} | |
// Select the diagonal that we want to branch from. We select the prior | |
// path whose position in the new string is the farthest from the origin | |
// and does not pass the bounds of the diff graph | |
if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) { | |
basePath = clonePath(removePath); | |
self.pushComponent(basePath.components, undefined, true); | |
} else { | |
basePath = addPath; // No need to clone, we've pulled it from the list | |
basePath.newPos++; | |
self.pushComponent(basePath.components, true, undefined); | |
} | |
var oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); | |
// If we have hit the end of both strings, then we are done | |
if (basePath.newPos+1 >= newLen && oldPos+1 >= oldLen) { | |
return done(buildValues(basePath.components, newString, oldString, self.useLongestToken)); | |
} else { | |
// Otherwise track this path as a potential candidate and continue. | |
bestPath[diagonalPath] = basePath; | |
} | |
} | |
editLength++; | |
} | |
// Performs the length of edit iteration. Is a bit fugly as this has to support the | |
// sync and async mode which is never fun. Loops over execEditLength until a value | |
// is produced. | |
var editLength = 1; | |
if (callback) { | |
(function exec() { | |
setTimeout(function() { | |
// This should not happen, but we want to be safe. | |
/*istanbul ignore next */ | |
if (editLength > maxEditLength) { | |
return callback(); | |
} | |
if (!execEditLength()) { | |
exec(); | |
} | |
}, 0); | |
})(); | |
} else { | |
while(editLength <= maxEditLength) { | |
var ret = execEditLength(); | |
if (ret) { | |
return ret; | |
} | |
} | |
} | |
}, | |
pushComponent: function(components, added, removed) { | |
var last = components[components.length-1]; | |
if (last && last.added === added && last.removed === removed) { | |
// We need to clone here as the component clone operation is just | |
// as shallow array clone | |
components[components.length-1] = {count: last.count + 1, added: added, removed: removed }; | |
} else { | |
components.push({count: 1, added: added, removed: removed }); | |
} | |
}, | |
extractCommon: function(basePath, newString, oldString, diagonalPath) { | |
var newLen = newString.length, | |
oldLen = oldString.length, | |
newPos = basePath.newPos, | |
oldPos = newPos - diagonalPath, | |
commonCount = 0; | |
while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) { | |
newPos++; | |
oldPos++; | |
commonCount++; | |
} | |
if (commonCount) { | |
basePath.components.push({count: commonCount}); | |
} | |
basePath.newPos = newPos; | |
return oldPos; | |
}, | |
equals: function(left, right) { | |
var reWhitespace = /\S/; | |
return left === right || (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)); | |
}, | |
tokenize: function(value) { | |
return value.split(''); | |
} | |
}; | |
var CharDiff = new Diff(); | |
var WordDiff = new Diff(true); | |
var WordWithSpaceDiff = new Diff(); | |
WordDiff.tokenize = WordWithSpaceDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/(\s+|\b)/)); | |
}; | |
var CssDiff = new Diff(true); | |
CssDiff.tokenize = function(value) { | |
return removeEmpty(value.split(/([{}:;,]|\s+)/)); | |
}; | |
var LineDiff = new Diff(); | |
var TrimmedLineDiff = new Diff(); | |
TrimmedLineDiff.ignoreTrim = true; | |
LineDiff.tokenize = TrimmedLineDiff.tokenize = function(value) { | |
var retLines = [], | |
lines = value.split(/^/m); | |
for(var i = 0; i < lines.length; i++) { | |
var line = lines[i], | |
lastLine = lines[i - 1], | |
lastLineLastChar = lastLine ? lastLine[lastLine.length - 1] : ''; | |
// Merge lines that may contain windows new lines | |
if (line === '\n' && (lastLineLastChar === '\r' || lastLineLastChar === '\n')) { | |
if (this.ignoreTrim || lastLineLastChar === '\n'){ | |
//to avoid merging to \n\n, remove \n and add \r\n. | |
retLines[retLines.length - 1] = retLines[retLines.length - 1].slice(0,-1) + '\r\n'; | |
} else { | |
retLines[retLines.length - 1] += '\n'; | |
} | |
} else if (line) { | |
if (this.ignoreTrim) { | |
line = line.trim(); | |
//add a newline unless this is the last line. | |
if (i < lines.length - 1) { | |
line += '\n'; | |
} | |
} | |
retLines.push(line); | |
} | |
} | |
return retLines; | |
}; | |
var SentenceDiff = new Diff(); | |
SentenceDiff.tokenize = function (value) { | |
return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/)); | |
}; | |
var JsonDiff = new Diff(); | |
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a | |
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: | |
JsonDiff.useLongestToken = true; | |
JsonDiff.tokenize = LineDiff.tokenize; | |
JsonDiff.equals = function(left, right) { | |
return LineDiff.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1')); | |
}; | |
var objectPrototypeToString = Object.prototype.toString; | |
// This function handles the presence of circular references by bailing out when encountering an | |
// object that is already on the "stack" of items being processed. | |
function canonicalize(obj, stack, replacementStack) { | |
stack = stack || []; | |
replacementStack = replacementStack || []; | |
var i; | |
for (var i = 0 ; i < stack.length ; i += 1) { | |
if (stack[i] === obj) { | |
return replacementStack[i]; | |
} | |
} | |
var canonicalizedObj; | |
if ('[object Array]' === objectPrototypeToString.call(obj)) { | |
stack.push(obj); | |
canonicalizedObj = new Array(obj.length); | |
replacementStack.push(canonicalizedObj); | |
for (i = 0 ; i < obj.length ; i += 1) { | |
canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else if (typeof obj === 'object' && obj !== null) { | |
stack.push(obj); | |
canonicalizedObj = {}; | |
replacementStack.push(canonicalizedObj); | |
var sortedKeys = []; | |
for (var key in obj) { | |
sortedKeys.push(key); | |
} | |
sortedKeys.sort(); | |
for (i = 0 ; i < sortedKeys.length ; i += 1) { | |
var key = sortedKeys[i]; | |
canonicalizedObj[key] = canonicalize(obj[key], stack, replacementStack); | |
} | |
stack.pop(); | |
replacementStack.pop(); | |
} else { | |
canonicalizedObj = obj; | |
} | |
return canonicalizedObj; | |
} | |
return { | |
Diff: Diff, | |
diffChars: function(oldStr, newStr, callback) { return CharDiff.diff(oldStr, newStr, callback); }, | |
diffWords: function(oldStr, newStr, callback) { return WordDiff.diff(oldStr, newStr, callback); }, | |
diffWordsWithSpace: function(oldStr, newStr, callback) { return WordWithSpaceDiff.diff(oldStr, newStr, callback); }, | |
diffLines: function(oldStr, newStr, callback) { return LineDiff.diff(oldStr, newStr, callback); }, | |
diffTrimmedLines: function(oldStr, newStr, callback) { return TrimmedLineDiff.diff(oldStr, newStr, callback); }, | |
diffSentences: function(oldStr, newStr, callback) { return SentenceDiff.diff(oldStr, newStr, callback); }, | |
diffCss: function(oldStr, newStr, callback) { return CssDiff.diff(oldStr, newStr, callback); }, | |
diffJson: function(oldObj, newObj, callback) { | |
return JsonDiff.diff( | |
typeof oldObj === 'string' ? oldObj : JSON.stringify(canonicalize(oldObj), undefined, ' '), | |
typeof newObj === 'string' ? newObj : JSON.stringify(canonicalize(newObj), undefined, ' '), | |
callback | |
); | |
}, | |
createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) { | |
var ret = []; | |
ret.push('Index: ' + fileName); | |
ret.push('==================================================================='); | |
ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader)); | |
ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader)); | |
var diff = LineDiff.diff(oldStr, newStr); | |
if (!diff[diff.length-1].value) { | |
diff.pop(); // Remove trailing newline add | |
} | |
diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier | |
function contextLines(lines) { | |
return map(lines, function(entry) { return ' ' + entry; }); | |
} | |
function eofNL(curRange, i, current) { | |
var last = diff[diff.length-2], | |
isLast = i === diff.length-2, | |
isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed); | |
// Figure out if this is the last line for the given file and missing NL | |
if (!/\n$/.test(current.value) && (isLast || isLastOfType)) { | |
curRange.push('\\ No newline at end of file'); | |
} | |
} | |
var oldRangeStart = 0, newRangeStart = 0, curRange = [], | |
oldLine = 1, newLine = 1; | |
for (var i = 0; i < diff.length; i++) { | |
var current = diff[i], | |
lines = current.lines || current.value.replace(/\n$/, '').split('\n'); | |
current.lines = lines; | |
if (current.added || current.removed) { | |
if (!oldRangeStart) { | |
var prev = diff[i-1]; | |
oldRangeStart = oldLine; | |
newRangeStart = newLine; | |
if (prev) { | |
curRange = contextLines(prev.lines.slice(-4)); | |
oldRangeStart -= curRange.length; | |
newRangeStart -= curRange.length; | |
} | |
} | |
curRange.push.apply(curRange, map(lines, function(entry) { return (current.added?'+':'-') + entry; })); | |
eofNL(curRange, i, current); | |
if (current.added) { | |
newLine += lines.length; | |
} else { | |
oldLine += lines.length; | |
} | |
} else { | |
if (oldRangeStart) { | |
// Close out any changes that have been output (or join overlapping) | |
if (lines.length <= 8 && i < diff.length-2) { | |
// Overlapping | |
curRange.push.apply(curRange, contextLines(lines)); | |
} else { | |
// end the range and output | |
var contextSize = Math.min(lines.length, 4); | |
ret.push( | |
'@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize) | |
+ ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize) | |
+ ' @@'); | |
ret.push.apply(ret, curRange); | |
ret.push.apply(ret, contextLines(lines.slice(0, contextSize))); | |
if (lines.length <= 4) { | |
eofNL(ret, i, current); | |
} | |
oldRangeStart = 0; newRangeStart = 0; curRange = []; | |
} | |
} | |
oldLine += lines.length; | |
newLine += lines.length; | |
} | |
} | |
return ret.join('\n') + '\n'; | |
}, | |
applyPatch: function(oldStr, uniDiff) { | |
var diffstr = uniDiff.split('\n'); | |
var diff = []; | |
var remEOFNL = false, | |
addEOFNL = false; | |
for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) { | |
if (diffstr[i][0] === '@') { | |
var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/); | |
diff.unshift({ | |
start:meh[3], | |
oldlength:meh[2], | |
oldlines:[], | |
newlength:meh[4], | |
newlines:[] | |
}); | |
} else if (diffstr[i][0] === '+') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '-') { | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === ' ') { | |
diff[0].newlines.push(diffstr[i].substr(1)); | |
diff[0].oldlines.push(diffstr[i].substr(1)); | |
} else if (diffstr[i][0] === '\\') { | |
if (diffstr[i-1][0] === '+') { | |
remEOFNL = true; | |
} else if (diffstr[i-1][0] === '-') { | |
addEOFNL = true; | |
} | |
} | |
} | |
var str = oldStr.split('\n'); | |
for (var i = diff.length - 1; i >= 0; i--) { | |
var d = diff[i]; | |
for (var j = 0; j < d.oldlength; j++) { | |
if (str[d.start-1+j] !== d.oldlines[j]) { | |
return false; | |
} | |
} | |
Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines)); | |
} | |
if (remEOFNL) { | |
while (!str[str.length-1]) { | |
str.pop(); | |
} | |
} else if (addEOFNL) { | |
str.push(''); | |
} | |
return str.join('\n'); | |
}, | |
convertChangesToXML: function(changes){ | |
var ret = []; | |
for ( var i = 0; i < changes.length; i++) { | |
var change = changes[i]; | |
if (change.added) { | |
ret.push('<ins>'); | |
} else if (change.removed) { | |
ret.push('<del>'); | |
} | |
ret.push(escapeHTML(change.value)); | |
if (change.added) { | |
ret.push('</ins>'); | |
} else if (change.removed) { | |
ret.push('</del>'); | |
} | |
} | |
return ret.join(''); | |
}, | |
// See: http://code.google.com/p/google-diff-match-patch/wiki/API | |
convertChangesToDMP: function(changes){ | |
var ret = [], change; | |
for ( var i = 0; i < changes.length; i++) { | |
change = changes[i]; | |
ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]); | |
} | |
return ret; | |
}, | |
canonicalize: canonicalize | |
}; | |
})(); | |
/*istanbul ignore next */ | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = JsDiff; | |
} | |
else if (typeof define === 'function' && define.amd) { | |
/*global define */ | |
define([], function() { return JsDiff; }); | |
} | |
else if (typeof global.JsDiff === 'undefined') { | |
global.JsDiff = JsDiff; | |
} | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment