Update fence logic to handle nested fences (per CommonMark spec; no handling of indentation).

http://spec.commonmark.org/0.22/#fenced-code-blocks
This commit is contained in:
David Anson 2015-08-27 22:49:59 -07:00
parent 5e3d9f8df4
commit bb0d8a36b3
2 changed files with 79 additions and 1 deletions

View file

@ -40,12 +40,17 @@ function filterTokens(params, type, callback) {
function forEachLine(params, callback) {
if (!params.forEachLine) {
var lineMetadata = new Array(params.lines.length);
var fenceStart = null;
var inFence = false;
// Find fenced code by pattern (parser ignores "``` close fence")
params.lines.forEach(function forLine(line, lineIndex) {
var metadata = 0;
if (/^(```|~~~)/.test(line)) {
var match = /^(`{3,}|~{3,})/.exec(line);
var fence = match && match[1];
if (fence &&
(!inFence || (fence.substr(0, fenceStart.length) === fenceStart))) {
metadata = inFence ? -2 : 2;
fenceStart = inFence ? null : fence;
inFence = !inFence;
} else if (inFence) {
metadata = 1;