Skip to content

Commit

Permalink
Use correct options in specs, marked#1511
Browse files Browse the repository at this point in the history
  • Loading branch information
yahtnif committed Jul 6, 2019
1 parent b00493c commit f4e241e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
51 changes: 32 additions & 19 deletions src/block-lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,17 @@ export class BlockLexer {
bullet: /(?:[*+-]|\d{1,9}\.)/,
code: /^( {4}[^\n]+\n*)+/,
def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *(#+ *)?(?:\n+|$)/,
fences: /^ {0,3}(`{3,}|~{3,})([^`~\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
html: new RegExp(html),
item: /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/,
lheading: /^([^\n]+)\n {0,3}(=|-){2,} *(?:\n+|$)/,
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
newline: /^\n+/,
paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading| {0,3}>|<\/?(?:tag)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\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]+)*)/,
text: /^[^\n]+/
}

Expand Down Expand Up @@ -156,10 +159,14 @@ export class BlockLexer {
.setGroup('attribute', attribute)
.getRegex()

base.paragraph = new ExtendRegexp(base.paragraph)
base.paragraph = new ExtendRegexp(base._paragraph)
.setGroup('hr', base.hr)
.setGroup('heading', base.heading)
.setGroup('lheading', base.lheading)
.setGroup('heading', ' {0,3}#{1,6} +')
.setGroup('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.setGroup('blockquote', ' {0,3}>')
.setGroup('fences', ' {0,3}(?:`{3,}|~{3,})[^`\\n]*\\n')
.setGroup('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
.setGroup('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
.setGroup('tag', tag) // pars can be interrupted by type (6) html blocks
.getRegex()

Expand All @@ -170,6 +177,7 @@ export class BlockLexer {
return (this.baseRules = base)
}

// Pedantic grammar (original John Gruber's loose markdown specification)
private static getPedanticRules(): PedanticBlockRules {
if (this.pedanticRules) return this.pedanticRules

Expand All @@ -193,6 +201,17 @@ export class BlockLexer {
...base,
...{
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
fences: noopRegex, // fences not supported
paragraph: new ExtendRegexp(base._paragraph)
.setGroup('hr', base.hr)
.setGroup('heading', ' *#{1,6} *[^\n]')
.setGroup('lheading', base.lheading)
.setGroup('blockquote', ' {0,3}>')
.setGroup('|fences', '')
.setGroup('|list', '')
.setGroup('|html', '')
.getRegex(),
html: htmlRegex
}
}
Expand All @@ -208,22 +227,12 @@ export class BlockLexer {
const gfm: GfmBlockRules = {
...base,
...{
fences: /^ {0,3}(`{3,}|~{3,})([^`\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
checkbox: /^\[([ xX])\] +/,
paragraph: /^/,
heading: /^ *(#{1,6}) +([^\n]+?) *(#*) *(?:\n+|$)/,
nptable: /^ *([^|\n ].*\|.*)\n *([-:]+ *\|[-| :]*)(?:\n((?:.*[^>\n ].*(?:\n|$))*)\n*|$)/,
table: /^ *\|(.+)\n *\|?( *[-:]+[-| :]*)(?:\n((?: *[^>\n ].*(?:\n|$))*)\n*|$)/
}
}

const group1: string = gfm.fences.source.replace('\\1', '\\2')
const group2: string = base.list.source.replace('\\1', '\\3')

gfm.paragraph = new ExtendRegexp(base.paragraph)
.setGroup('(?!', `(?!${group1}|${group2}|`)
.getRegex()

return (this.gfmRules = gfm)
}

Expand Down Expand Up @@ -321,7 +330,7 @@ export class BlockLexer {
continue
}

// fences code (gfm)
// fences
if (
this.isGfm &&
(execArr = (<GfmBlockRules>this.rules).fences.exec(nextPart))
Expand Down Expand Up @@ -558,7 +567,11 @@ export class BlockLexer {
this.tokens.push({
type: this.options.sanitize ? TokenType.paragraph : TokenType.html,
pre: !this.options.sanitizer && isPre,
text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(execArr[0]) : escape(execArr[0])) : execArr[0]
text: this.options.sanitize
? this.options.sanitizer
? this.options.sanitizer(execArr[0])
: escape(execArr[0])
: execArr[0]
})
continue
}
Expand Down Expand Up @@ -644,7 +657,7 @@ export class BlockLexer {

this.tokens.push({
type: TokenType.heading,
depth: execArr[2] === '=' ? 1 : 2,
depth: execArr[2].charAt(0) === '=' ? 1 : 2,
text: execArr[1]
})
continue
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export interface BaseBlockRules {
bullet: RegExp
code: RegExp
def: RegExp
fences: RegExp
heading: RegExp
hr: RegExp
html: RegExp
item: RegExp // List item (<li>)
lheading: RegExp
list: RegExp
newline: RegExp
paragraph: RegExp
paragraph?: RegExp
text: RegExp
_paragraph: RegExp
}

export interface PedanticBlockRules extends BaseBlockRules {}
Expand Down

0 comments on commit f4e241e

Please sign in to comment.