Skip to content

Commit

Permalink
Merge pull request #1 from UziTech/heading-correct
Browse files Browse the repository at this point in the history
make header up to spec
  • Loading branch information
davisjam authored Apr 25, 2018
2 parents 536bd8d + 4cdaa11 commit 4cc97f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var block = {
fences: noop,
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
// cap[2] might be ' HEADING # ' and must be trimmed appropriately.
heading: /^ *(#{1,6})(.*)(?:\n+|$)/,
heading: /^ {0,3}(#{1,6})(?:[^\S\n](.*))?(?:\n+|$)/,
nptable: noop,
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
Expand Down Expand Up @@ -93,9 +93,7 @@ block.normal = merge({}, block);

block.gfm = merge({}, block.normal, {
fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\n? *\1 *(?:\n+|$)/,
paragraph: /^/,
// cap[2] might be ' HEADING # ' and must be trimmed appropriately.
heading: /^ *(#{1,6}) (.+)(?:\n+|$)/
paragraph: /^/
});

block.gfm.paragraph = edit(block.paragraph)
Expand All @@ -118,6 +116,7 @@ block.tables = merge({}, block.gfm, {
*/

block.pedantic = merge({}, block.normal, {
heading: /^ *(#{1,6})(.*)(?:\n+|$)/,
html: edit(
'^ *(?:comment *(?:\\n|\\s*$)'
+ '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
Expand Down Expand Up @@ -238,13 +237,18 @@ Lexer.prototype.token = function(src, top) {
if (cap = this.rules.heading.exec(src)) {
src = src.substring(cap[0].length);
// cap[2] might be ' HEADING # '
item = cap[2].trim();
item = (cap[2] || '').trim();
if (item.slice(-1) === '#') {
// NB replace(/#+$/) is quadratic on mismatch because it's unanchored,
// so we protect with if-check to ensure it won't mismatch.
item = item.replace(/#+$/, '');
if (this.options.pedantic) {
item = item.replace(/#+$/, '');
} else {
// CM requires a space before additional #s
item = item.replace(/(\s|^)#+$/, '');
}
}
item = item.trim()
item = item.trim();
this.tokens.push({
type: 'heading',
depth: cap[1].length,
Expand Down
2 changes: 1 addition & 1 deletion test/new/nogfm_hashtag.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
gfm: false
pedantic: true
---
#header

Expand Down
2 changes: 1 addition & 1 deletion test/specs/commonmark/commonmark-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('CommonMark 0.28 ATX headings', function() {
var section = 'ATX headings';

// var shouldPassButFails = [];
var shouldPassButFails = [40, 45, 46, 49];
var shouldPassButFails = [40];

var willNotBeAttemptedByCoreTeam = [];

Expand Down

0 comments on commit 4cc97f6

Please sign in to comment.