Browse Source

Allow multiline setext headers

pull/204/merge
Alex Kocharin 9 years ago
parent
commit
637c776ec5
  1. 85
      lib/rules_block/lheading.js
  2. 38
      test/fixtures/commonmark/bad.txt
  3. 24
      test/fixtures/commonmark/good.txt
  4. 26
      test/fixtures/markdown-it/commonmark_extras.txt

85
lib/rules_block/lheading.js

@ -4,42 +4,67 @@
module.exports = function lheading(state, startLine, endLine/*, silent*/) {
var marker, pos, max, token, level,
next = startLine + 1;
if (next >= endLine) { return false; }
if (state.sCount[next] < state.blkIndent) { return false; }
// Scan next line
if (state.sCount[next] - state.blkIndent > 3) { return false; }
pos = state.bMarks[next] + state.tShift[next];
max = state.eMarks[next];
if (pos >= max) { return false; }
marker = state.src.charCodeAt(pos);
if (marker !== 0x2D/* - */ && marker !== 0x3D/* = */) { return false; }
pos = state.skipChars(pos, marker);
pos = state.skipSpaces(pos);
if (pos < max) { return false; }
pos = state.bMarks[startLine] + state.tShift[startLine];
state.line = next + 1;
level = (marker === 0x3D/* = */ ? 1 : 2);
var content, terminate, i, l, token, pos, max, level, marker,
nextLine = startLine + 1,
terminatorRules = state.md.block.ruler.getRules('paragraph');
// jump line-by-line until empty one or EOF
for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
// this would be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if (state.sCount[nextLine] - state.blkIndent > 3) { continue; }
//
// Check for underline in setext header
//
if (state.sCount[nextLine] >= state.blkIndent) {
pos = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (pos < max) {
marker = state.src.charCodeAt(pos);
if (marker === 0x2D/* - */ || marker === 0x3D/* = */) {
pos = state.skipChars(pos, marker);
pos = state.skipSpaces(pos);
if (pos >= max) {
level = (marker === 0x3D/* = */ ? 1 : 2);
break;
}
}
}
}
// quirk for blockquotes, this line should already be checked by that rule
if (state.sCount[nextLine] < 0) { continue; }
// Some tags can terminate paragraph without empty line.
terminate = false;
for (i = 0, l = terminatorRules.length; i < l; i++) {
if (terminatorRules[i](state, nextLine, endLine, true)) {
terminate = true;
break;
}
}
if (terminate) { break; }
}
if (!level) {
// Didn't find valid underline
return false;
}
content = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
state.line = nextLine + 1;
token = state.push('heading_open', 'h' + String(level), 1);
token.markup = String.fromCharCode(marker);
token.map = [ startLine, state.line ];
token = state.push('inline', '', 0);
token.content = state.src.slice(pos, state.eMarks[startLine]).trim();
token.content = content;
token.map = [ startLine, state.line - 1 ];
token.children = [];

38
test/fixtures/commonmark/bad.txt

@ -1,41 +1,3 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 877
.
Foo *bar
baz*
====
.
<h1>Foo <em>bar
baz</em></h1>
.
error:
<p>Foo <em>bar
baz</em>
====</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1057
.
Foo
Bar
---
.
<h2>Foo
Bar</h2>
.
error:
<p>Foo
Bar</p>
<hr />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3777

24
test/fixtures/commonmark/good.txt

@ -528,6 +528,18 @@ Foo *bar*
<h2>Foo <em>bar</em></h2>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 877
.
Foo *bar
baz*
====
.
<h1>Foo <em>bar
baz</em></h1>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 889
@ -694,6 +706,18 @@ src line: 1042
<hr />
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1057
.
Foo
Bar
---
.
<h2>Foo
Bar</h2>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1070

26
test/fixtures/markdown-it/commonmark_extras.txt

@ -108,6 +108,32 @@ test
</code></pre>
.
Setext header text supports lazy continuations:
.
- foo
bar
===
.
<ul>
<li>
<h1>foo
bar</h1>
</li>
</ul>
.
But setext header underline doesn't:
.
- foo
bar
===
.
<ul>
<li>foo
bar
===</li>
</ul>
.
Coverage. Directive can terminate paragraph.
.

Loading…
Cancel
Save