Skip to content

Commit

Permalink
update to upstream v1.2.2, can support both #, ##, ###... and #1, #2, #3
Browse files Browse the repository at this point in the history
... #108.
  • Loading branch information
vagra committed Nov 1, 2020
1 parent 6d246e7 commit e9b9366
Show file tree
Hide file tree
Showing 8 changed files with 5,460 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ module.exports = class Lexer {
continue;
}

// kheading
if (token = this.tokenizer.kheading(src)) {
src = src.substring(token.raw.length);
tokens.push(token);
continue;
}

// table no leading pipe (gfm)
if (token = this.tokenizer.nptable(src)) {
src = src.substring(token.raw.length);
Expand Down
12 changes: 12 additions & 0 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ module.exports = class Tokenizer {
}
}

kheading(src) {
const cap = this.rules.block.kheading.exec(src);
if (cap) {
return {
type: 'heading',
raw: cap[0],
depth: parseInt(cap[1]),
text: cap[2]
};
}
}

nptable(src) {
const cap = this.rules.block.nptable.exec(src);
if (cap) {
Expand Down
12 changes: 8 additions & 4 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const block = {
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
kheading: /^ {0,3}#(\d{1,3}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: '^ {0,3}(?:' // optional indentation
Expand All @@ -31,7 +32,7 @@ const block = {
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
// regex template, placeholders will be replaced according to different paragraph
// interruption rules of commonmark and the original markdown spec:
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
_paragraph: /^([^\n]+(?:\n(?!hr|heading|kheading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
text: /^[^\n]+/
};

Expand All @@ -56,7 +57,7 @@ block.list = edit(block.list)

block._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
+ '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
+ '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
+ '|figure|footer|form|frame|frameset|h[1-6]|h\\d{1,3}|head|header|hr|html|iframe'
+ '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
+ '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
+ '|track|ul';
Expand All @@ -70,6 +71,7 @@ block.html = edit(block.html, 'i')
block.paragraph = edit(block._paragraph)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('kheading', ' {0,3}#\\d{1,3} ')
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
Expand All @@ -95,15 +97,16 @@ block.normal = merge({}, block);
block.gfm = merge({}, block.normal, {
nptable: '^ *([^|\\n ].*\\|.*)\\n' // Header
+ ' {0,3}([-:]+ *\\|[-| :]*)' // Align
+ '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
+ '(?:\\n((?:(?!\\n|hr|heading|kheading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
table: '^ *\\|(.+)\\n' // Header
+ ' {0,3}\\|?( *[-:]+[-| :]*)' // Align
+ '(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
+ '(?:\\n *((?:(?!\\n|hr|heading|kheading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
});

block.gfm.nptable = edit(block.gfm.nptable)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('kheading', ' {0,3}#\\d{1,3} ')
.replace('blockquote', ' {0,3}>')
.replace('code', ' {4}[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
Expand All @@ -115,6 +118,7 @@ block.gfm.nptable = edit(block.gfm.nptable)
block.gfm.table = edit(block.gfm.table)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('kheading', ' {0,3}#\\d{1,3} ')
.replace('blockquote', ' {0,3}>')
.replace('code', ' {4}[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
Expand Down
2 changes: 1 addition & 1 deletion test/specs/commonmark/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
},
{
"markdown": "#5 bolt\n\n#hashtag\n",
"html": "<p>#5 bolt</p>\n<p>#hashtag</p>\n",
"html": "<h5>bolt</h5>\n<p>#hashtag</p>\n",
"example": 34,
"start_line": 802,
"end_line": 809,
Expand Down
2 changes: 1 addition & 1 deletion test/specs/gfm/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
},
{
"markdown": "#5 bolt\n\n#hashtag\n",
"html": "<p>#5 bolt</p>\n<p>#hashtag</p>\n",
"html": "<h5>bolt</h5>\n<p>#hashtag</p>\n",
"example": 34,
"start_line": 802,
"end_line": 809,
Expand Down
Loading

0 comments on commit e9b9366

Please sign in to comment.