Skip to content

Commit

Permalink
Fix parsing of header when not separated with double-newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
mishoo committed Jun 25, 2013
1 parent 368b33a commit ab5f89a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ function count_lines( str ) {
// Internal - split source into rough blocks
Markdown.prototype.split_blocks = function splitBlocks( input, startLine ) {
input = input.replace(/(\r\n|\n|\r)/g, "\n");
// [\s\S] matches _anything_ (newline or space)
var re = /([\s\S]+?)($|\n(?:\s*\n|$)+)/g,
// [^] matches _anything_ (newline or space)
var re = /([^]+?)($|\n#|\n(?:\s*\n|$)+)/g,
blocks = [],
m;

Expand All @@ -177,6 +177,10 @@ Markdown.prototype.split_blocks = function splitBlocks( input, startLine ) {
}

while ( ( m = re.exec(input) ) !== null ) {
if (m[2] == "\n#") {
m[2] = "\n";
re.lastIndex--;
}
blocks.push( mk_block( m[1], m[2], line_no ) );
line_no += count_lines( m[0] );
}
Expand Down
9 changes: 9 additions & 0 deletions test/regressions.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,12 @@ test( "line_endings", function(t, md) {
t.equivalent( md.toTree( "Foo\r\rBar", [ "markdown" ] ), tree, "Mac line endings" );
t.equivalent( md.toTree( "Foo\r\n\nBar", [ "markdown" ] ), tree, "Mixed line endings" );
});

test( "header_in_paragraph", function(t, md){
var tree = [ "markdown",
[ "para", "Foo" ],
[ "header", { level: 1 }, "Title" ],
[ "para", "Bar" ] ];
t.equivalent( md.toTree("Foo\n#Title\nBar", [ "markdown" ]), tree, "Header in praragraph" );
t.equivalent( md.toTree("Foo\n\n#Title\n\nBar", [ "markdown" ]), tree, "Header in praragraph" );
});

0 comments on commit ab5f89a

Please sign in to comment.