@ts-stack/markdown
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -12,3 +12,3 @@ /** | ||
export declare class BlockLexer<T extends typeof BlockLexer> { | ||
protected staticThis: typeof BlockLexer; | ||
protected staticThis: T; | ||
static simpleRules: RegExp[]; | ||
@@ -30,3 +30,3 @@ protected static rulesBase: RulesBlockBase; | ||
protected hasRulesTables: boolean; | ||
constructor(staticThis: typeof BlockLexer, options?: object); | ||
constructor(staticThis: T, options?: object); | ||
/** | ||
@@ -33,0 +33,0 @@ * Accepts Markdown text and returns object with tokens and links. |
@@ -11,368 +11,366 @@ "use strict"; | ||
*/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var extend_regexp_1 = require("./extend-regexp"); | ||
var interfaces_1 = require("./interfaces"); | ||
var marked_1 = require("./marked"); | ||
var BlockLexer = /** @class */ (function () { | ||
function BlockLexer(staticThis, options) { | ||
this.staticThis = staticThis; | ||
this.links = {}; | ||
this.tokens = []; | ||
this.options = options || marked_1.Marked.options; | ||
this.setRules(); | ||
} | ||
/** | ||
* Accepts Markdown text and returns object with tokens and links. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. | ||
*/ | ||
BlockLexer.lex = function (src, options, top, isBlockQuote) { | ||
var lexer = new this(this, options); | ||
return lexer.getTokens(src, top, isBlockQuote); | ||
}; | ||
BlockLexer.getRulesBase = function () { | ||
if (this.rulesBase) { | ||
return this.rulesBase; | ||
exports.BlockLexer = void 0; | ||
const extend_regexp_1 = require("./extend-regexp"); | ||
const interfaces_1 = require("./interfaces"); | ||
const marked_1 = require("./marked"); | ||
let BlockLexer = /** @class */ (() => { | ||
class BlockLexer { | ||
constructor(staticThis, options) { | ||
this.staticThis = staticThis; | ||
this.links = {}; | ||
this.tokens = []; | ||
this.options = options || marked_1.Marked.options; | ||
this.setRules(); | ||
} | ||
var base = { | ||
newline: /^\n+/, | ||
code: /^( {4}[^\n]+\n*)+/, | ||
hr: /^( *[-*_]){3,} *(?:\n+|$)/, | ||
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/, | ||
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/, | ||
blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/, | ||
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/, | ||
html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/, | ||
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/, | ||
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/, | ||
text: /^[^\n]+/, | ||
bullet: /(?:[*+-]|\d+\.)/, | ||
item: /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/ | ||
}; | ||
base.item = new extend_regexp_1.ExtendRegexp(base.item, 'gm').setGroup(/bull/g, base.bullet).getRegexp(); | ||
base.list = new extend_regexp_1.ExtendRegexp(base.list) | ||
.setGroup(/bull/g, base.bullet) | ||
.setGroup('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))') | ||
.setGroup('def', '\\n+(?=' + base.def.source + ')') | ||
.getRegexp(); | ||
var tag = '(?!(?:' + | ||
'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code' + | ||
'|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo' + | ||
'|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b'; | ||
base.html = new extend_regexp_1.ExtendRegexp(base.html) | ||
.setGroup('comment', /<!--[\s\S]*?-->/) | ||
.setGroup('closed', /<(tag)[\s\S]+?<\/\1>/) | ||
.setGroup('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/) | ||
.setGroup(/tag/g, tag) | ||
.getRegexp(); | ||
base.paragraph = new extend_regexp_1.ExtendRegexp(base.paragraph) | ||
.setGroup('hr', base.hr) | ||
.setGroup('heading', base.heading) | ||
.setGroup('lheading', base.lheading) | ||
.setGroup('blockquote', base.blockquote) | ||
.setGroup('tag', '<' + tag) | ||
.setGroup('def', base.def) | ||
.getRegexp(); | ||
return (this.rulesBase = base); | ||
}; | ||
BlockLexer.getRulesGfm = function () { | ||
if (this.rulesGfm) { | ||
return this.rulesGfm; | ||
/** | ||
* Accepts Markdown text and returns object with tokens and links. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. | ||
*/ | ||
static lex(src, options, top, isBlockQuote) { | ||
const lexer = new this(this, options); | ||
return lexer.getTokens(src, top, isBlockQuote); | ||
} | ||
var base = this.getRulesBase(); | ||
var gfm = __assign(__assign({}, base), { | ||
fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/, | ||
paragraph: /^/, | ||
heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/ | ||
}); | ||
var group1 = gfm.fences.source.replace('\\1', '\\2'); | ||
var group2 = base.list.source.replace('\\1', '\\3'); | ||
gfm.paragraph = new extend_regexp_1.ExtendRegexp(base.paragraph).setGroup('(?!', "(?!" + group1 + "|" + group2 + "|").getRegexp(); | ||
return (this.rulesGfm = gfm); | ||
}; | ||
BlockLexer.getRulesTable = function () { | ||
if (this.rulesTables) { | ||
return this.rulesTables; | ||
static getRulesBase() { | ||
if (this.rulesBase) { | ||
return this.rulesBase; | ||
} | ||
const base = { | ||
newline: /^\n+/, | ||
code: /^( {4}[^\n]+\n*)+/, | ||
hr: /^( *[-*_]){3,} *(?:\n+|$)/, | ||
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/, | ||
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/, | ||
blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/, | ||
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/, | ||
html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/, | ||
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/, | ||
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/, | ||
text: /^[^\n]+/, | ||
bullet: /(?:[*+-]|\d+\.)/, | ||
item: /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/ | ||
}; | ||
base.item = new extend_regexp_1.ExtendRegexp(base.item, 'gm').setGroup(/bull/g, base.bullet).getRegexp(); | ||
base.list = new extend_regexp_1.ExtendRegexp(base.list) | ||
.setGroup(/bull/g, base.bullet) | ||
.setGroup('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))') | ||
.setGroup('def', '\\n+(?=' + base.def.source + ')') | ||
.getRegexp(); | ||
const tag = '(?!(?:' + | ||
'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code' + | ||
'|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo' + | ||
'|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b'; | ||
base.html = new extend_regexp_1.ExtendRegexp(base.html) | ||
.setGroup('comment', /<!--[\s\S]*?-->/) | ||
.setGroup('closed', /<(tag)[\s\S]+?<\/\1>/) | ||
.setGroup('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/) | ||
.setGroup(/tag/g, tag) | ||
.getRegexp(); | ||
base.paragraph = new extend_regexp_1.ExtendRegexp(base.paragraph) | ||
.setGroup('hr', base.hr) | ||
.setGroup('heading', base.heading) | ||
.setGroup('lheading', base.lheading) | ||
.setGroup('blockquote', base.blockquote) | ||
.setGroup('tag', '<' + tag) | ||
.setGroup('def', base.def) | ||
.getRegexp(); | ||
return (this.rulesBase = base); | ||
} | ||
return (this.rulesTables = __assign(__assign({}, this.getRulesGfm()), { | ||
nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, | ||
table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ | ||
})); | ||
}; | ||
BlockLexer.prototype.setRules = function () { | ||
if (this.options.gfm) { | ||
if (this.options.tables) { | ||
this.rules = this.staticThis.getRulesTable(); | ||
static getRulesGfm() { | ||
if (this.rulesGfm) { | ||
return this.rulesGfm; | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
const base = this.getRulesBase(); | ||
const gfm = { | ||
...base, | ||
...{ | ||
fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/, | ||
paragraph: /^/, | ||
heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/ | ||
} | ||
}; | ||
const group1 = gfm.fences.source.replace('\\1', '\\2'); | ||
const group2 = base.list.source.replace('\\1', '\\3'); | ||
gfm.paragraph = new extend_regexp_1.ExtendRegexp(base.paragraph).setGroup('(?!', `(?!${group1}|${group2}|`).getRegexp(); | ||
return (this.rulesGfm = gfm); | ||
} | ||
static getRulesTable() { | ||
if (this.rulesTables) { | ||
return this.rulesTables; | ||
} | ||
return (this.rulesTables = { | ||
...this.getRulesGfm(), | ||
...{ | ||
nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, | ||
table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ | ||
} | ||
}); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesBase(); | ||
} | ||
this.hasRulesGfm = this.rules.fences !== undefined; | ||
this.hasRulesTables = this.rules.table !== undefined; | ||
}; | ||
/** | ||
* Lexing. | ||
*/ | ||
BlockLexer.prototype.getTokens = function (src, top, isBlockQuote) { | ||
var nextPart = src; | ||
var execArr; | ||
mainLoop: while (nextPart) { | ||
// newline | ||
if ((execArr = this.rules.newline.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
if (execArr[0].length > 1) { | ||
this.tokens.push({ type: interfaces_1.TokenType.space }); | ||
setRules() { | ||
if (this.options.gfm) { | ||
if (this.options.tables) { | ||
this.rules = this.staticThis.getRulesTable(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
} | ||
} | ||
// code | ||
if ((execArr = this.rules.code.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var code = execArr[0].replace(/^ {4}/gm, ''); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.code, | ||
text: !this.options.pedantic ? code.replace(/\n+$/, '') : code | ||
}); | ||
continue; | ||
else { | ||
this.rules = this.staticThis.getRulesBase(); | ||
} | ||
// fences code (gfm) | ||
if (this.hasRulesGfm && (execArr = this.rules.fences.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.code, | ||
lang: execArr[2], | ||
text: execArr[3] || '' | ||
}); | ||
continue; | ||
} | ||
// heading | ||
if ((execArr = this.rules.heading.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.heading, | ||
depth: execArr[1].length, | ||
text: execArr[2] | ||
}); | ||
continue; | ||
} | ||
// table no leading pipe (gfm) | ||
if (top && this.hasRulesTables && (execArr = this.rules.nptable.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var item = { | ||
type: interfaces_1.TokenType.table, | ||
header: execArr[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), | ||
align: execArr[2].replace(/^ *|\| *$/g, '').split(/ *\| */), | ||
cells: [] | ||
}; | ||
for (var i = 0; i < item.align.length; i++) { | ||
if (/^ *-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'right'; | ||
this.hasRulesGfm = this.rules.fences !== undefined; | ||
this.hasRulesTables = this.rules.table !== undefined; | ||
} | ||
/** | ||
* Lexing. | ||
*/ | ||
getTokens(src, top, isBlockQuote) { | ||
let nextPart = src; | ||
let execArr; | ||
mainLoop: while (nextPart) { | ||
// newline | ||
if ((execArr = this.rules.newline.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
if (execArr[0].length > 1) { | ||
this.tokens.push({ type: interfaces_1.TokenType.space }); | ||
} | ||
else if (/^ *:-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'center'; | ||
} | ||
// code | ||
if ((execArr = this.rules.code.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const code = execArr[0].replace(/^ {4}/gm, ''); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.code, | ||
text: !this.options.pedantic ? code.replace(/\n+$/, '') : code | ||
}); | ||
continue; | ||
} | ||
// fences code (gfm) | ||
if (this.hasRulesGfm && (execArr = this.rules.fences.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.code, | ||
lang: execArr[2], | ||
text: execArr[3] || '' | ||
}); | ||
continue; | ||
} | ||
// heading | ||
if ((execArr = this.rules.heading.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.heading, | ||
depth: execArr[1].length, | ||
text: execArr[2] | ||
}); | ||
continue; | ||
} | ||
// table no leading pipe (gfm) | ||
if (top && this.hasRulesTables && (execArr = this.rules.nptable.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const item = { | ||
type: interfaces_1.TokenType.table, | ||
header: execArr[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), | ||
align: execArr[2].replace(/^ *|\| *$/g, '').split(/ *\| */), | ||
cells: [] | ||
}; | ||
for (let i = 0; i < item.align.length; i++) { | ||
if (/^ *-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'right'; | ||
} | ||
else if (/^ *:-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'center'; | ||
} | ||
else if (/^ *:-+ *$/.test(item.align[i])) { | ||
item.align[i] = 'left'; | ||
} | ||
else { | ||
item.align[i] = null; | ||
} | ||
} | ||
else if (/^ *:-+ *$/.test(item.align[i])) { | ||
item.align[i] = 'left'; | ||
const td = execArr[3].replace(/\n$/, '').split('\n'); | ||
for (let i = 0; i < td.length; i++) { | ||
item.cells[i] = td[i].split(/ *\| */); | ||
} | ||
else { | ||
item.align[i] = null; | ||
this.tokens.push(item); | ||
continue; | ||
} | ||
// lheading | ||
if ((execArr = this.rules.lheading.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.heading, | ||
depth: execArr[2] === '=' ? 1 : 2, | ||
text: execArr[1] | ||
}); | ||
continue; | ||
} | ||
// hr | ||
if ((execArr = this.rules.hr.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.hr }); | ||
continue; | ||
} | ||
// blockquote | ||
if ((execArr = this.rules.blockquote.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.blockquoteStart }); | ||
const str = execArr[0].replace(/^ *> ?/gm, ''); | ||
// Pass `top` to keep the current | ||
// "toplevel" state. This is exactly | ||
// how markdown.pl works. | ||
this.getTokens(str); | ||
this.tokens.push({ type: interfaces_1.TokenType.blockquoteEnd }); | ||
continue; | ||
} | ||
// list | ||
if ((execArr = this.rules.list.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const bull = execArr[2]; | ||
this.tokens.push({ type: interfaces_1.TokenType.listStart, ordered: bull.length > 1 }); | ||
// Get each top-level item. | ||
const str = execArr[0].match(this.rules.item); | ||
const length = str.length; | ||
let next = false; | ||
let space; | ||
let blockBullet; | ||
let loose; | ||
for (let i = 0; i < length; i++) { | ||
let item = str[i]; | ||
// Remove the list item's bullet so it is seen as the next token. | ||
space = item.length; | ||
item = item.replace(/^ *([*+-]|\d+\.) +/, ''); | ||
// Outdent whatever the list item contains. Hacky. | ||
if (item.indexOf('\n ') !== -1) { | ||
space -= item.length; | ||
item = !this.options.pedantic | ||
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') | ||
: item.replace(/^ {1,4}/gm, ''); | ||
} | ||
// Determine whether the next list item belongs here. | ||
// Backpedal if it does not belong in this list. | ||
if (this.options.smartLists && i !== length - 1) { | ||
blockBullet = this.staticThis.getRulesBase().bullet.exec(str[i + 1])[0]; | ||
if (bull !== blockBullet && !(bull.length > 1 && blockBullet.length > 1)) { | ||
nextPart = str.slice(i + 1).join('\n') + nextPart; | ||
i = length - 1; | ||
} | ||
} | ||
// Determine whether item is loose or not. | ||
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/ | ||
// for discount behavior. | ||
loose = next || /\n\n(?!\s*$)/.test(item); | ||
if (i !== length - 1) { | ||
next = item.charAt(item.length - 1) === '\n'; | ||
if (!loose) { | ||
loose = next; | ||
} | ||
} | ||
this.tokens.push({ type: loose ? interfaces_1.TokenType.looseItemStart : interfaces_1.TokenType.listItemStart }); | ||
// Recurse. | ||
this.getTokens(item, false, isBlockQuote); | ||
this.tokens.push({ type: interfaces_1.TokenType.listItemEnd }); | ||
} | ||
this.tokens.push({ type: interfaces_1.TokenType.listEnd }); | ||
continue; | ||
} | ||
var td = execArr[3].replace(/\n$/, '').split('\n'); | ||
for (var i = 0; i < td.length; i++) { | ||
item.cells[i] = td[i].split(/ *\| */); | ||
// html | ||
if ((execArr = this.rules.html.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const attr = execArr[1]; | ||
const isPre = attr === 'pre' || attr === 'script' || attr === 'style'; | ||
this.tokens.push({ | ||
type: this.options.sanitize ? interfaces_1.TokenType.paragraph : interfaces_1.TokenType.html, | ||
pre: !this.options.sanitizer && isPre, | ||
text: execArr[0] | ||
}); | ||
continue; | ||
} | ||
this.tokens.push(item); | ||
continue; | ||
} | ||
// lheading | ||
if ((execArr = this.rules.lheading.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.heading, | ||
depth: execArr[2] === '=' ? 1 : 2, | ||
text: execArr[1] | ||
}); | ||
continue; | ||
} | ||
// hr | ||
if ((execArr = this.rules.hr.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.hr }); | ||
continue; | ||
} | ||
// blockquote | ||
if ((execArr = this.rules.blockquote.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.blockquoteStart }); | ||
var str = execArr[0].replace(/^ *> ?/gm, ''); | ||
// Pass `top` to keep the current | ||
// "toplevel" state. This is exactly | ||
// how markdown.pl works. | ||
this.getTokens(str); | ||
this.tokens.push({ type: interfaces_1.TokenType.blockquoteEnd }); | ||
continue; | ||
} | ||
// list | ||
if ((execArr = this.rules.list.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var bull = execArr[2]; | ||
this.tokens.push({ type: interfaces_1.TokenType.listStart, ordered: bull.length > 1 }); | ||
// Get each top-level item. | ||
var str = execArr[0].match(this.rules.item); | ||
var length_1 = str.length; | ||
var next = false; | ||
var space = void 0; | ||
var blockBullet = void 0; | ||
var loose = void 0; | ||
for (var i = 0; i < length_1; i++) { | ||
var item = str[i]; | ||
// Remove the list item's bullet so it is seen as the next token. | ||
space = item.length; | ||
item = item.replace(/^ *([*+-]|\d+\.) +/, ''); | ||
// Outdent whatever the list item contains. Hacky. | ||
if (item.indexOf('\n ') !== -1) { | ||
space -= item.length; | ||
item = !this.options.pedantic | ||
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') | ||
: item.replace(/^ {1,4}/gm, ''); | ||
} | ||
// Determine whether the next list item belongs here. | ||
// Backpedal if it does not belong in this list. | ||
if (this.options.smartLists && i !== length_1 - 1) { | ||
blockBullet = this.staticThis.getRulesBase().bullet.exec(str[i + 1])[0]; | ||
if (bull !== blockBullet && !(bull.length > 1 && blockBullet.length > 1)) { | ||
nextPart = str.slice(i + 1).join('\n') + nextPart; | ||
i = length_1 - 1; | ||
// def | ||
if (top && (execArr = this.rules.def.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.links[execArr[1].toLowerCase()] = { | ||
href: execArr[2], | ||
title: execArr[3] | ||
}; | ||
continue; | ||
} | ||
// table (gfm) | ||
if (top && this.hasRulesTables && (execArr = this.rules.table.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const item = { | ||
type: interfaces_1.TokenType.table, | ||
header: execArr[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), | ||
align: execArr[2].replace(/^ *|\| *$/g, '').split(/ *\| */), | ||
cells: [] | ||
}; | ||
for (let i = 0; i < item.align.length; i++) { | ||
if (/^ *-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'right'; | ||
} | ||
else if (/^ *:-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'center'; | ||
} | ||
else if (/^ *:-+ *$/.test(item.align[i])) { | ||
item.align[i] = 'left'; | ||
} | ||
else { | ||
item.align[i] = null; | ||
} | ||
} | ||
// Determine whether item is loose or not. | ||
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/ | ||
// for discount behavior. | ||
loose = next || /\n\n(?!\s*$)/.test(item); | ||
if (i !== length_1 - 1) { | ||
next = item.charAt(item.length - 1) === '\n'; | ||
if (!loose) { | ||
loose = next; | ||
const td = execArr[3].replace(/(?: *\| *)?\n$/, '').split('\n'); | ||
for (let i = 0; i < td.length; i++) { | ||
item.cells[i] = td[i].replace(/^ *\| *| *\| *$/g, '').split(/ *\| */); | ||
} | ||
this.tokens.push(item); | ||
continue; | ||
} | ||
// simple rules | ||
if (this.staticThis.simpleRules.length) { | ||
const simpleRules = this.staticThis.simpleRules; | ||
for (let i = 0; i < simpleRules.length; i++) { | ||
if ((execArr = simpleRules[i].exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
const type = 'simpleRule' + (i + 1); | ||
this.tokens.push({ type, execArr }); | ||
continue mainLoop; | ||
} | ||
} | ||
this.tokens.push({ type: loose ? interfaces_1.TokenType.looseItemStart : interfaces_1.TokenType.listItemStart }); | ||
// Recurse. | ||
this.getTokens(item, false, isBlockQuote); | ||
this.tokens.push({ type: interfaces_1.TokenType.listItemEnd }); | ||
} | ||
this.tokens.push({ type: interfaces_1.TokenType.listEnd }); | ||
continue; | ||
} | ||
// html | ||
if ((execArr = this.rules.html.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var attr = execArr[1]; | ||
var isPre = attr === 'pre' || attr === 'script' || attr === 'style'; | ||
this.tokens.push({ | ||
type: this.options.sanitize ? interfaces_1.TokenType.paragraph : interfaces_1.TokenType.html, | ||
pre: !this.options.sanitizer && isPre, | ||
text: execArr[0] | ||
}); | ||
continue; | ||
} | ||
// def | ||
if (top && (execArr = this.rules.def.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.links[execArr[1].toLowerCase()] = { | ||
href: execArr[2], | ||
title: execArr[3] | ||
}; | ||
continue; | ||
} | ||
// table (gfm) | ||
if (top && this.hasRulesTables && (execArr = this.rules.table.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var item = { | ||
type: interfaces_1.TokenType.table, | ||
header: execArr[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), | ||
align: execArr[2].replace(/^ *|\| *$/g, '').split(/ *\| */), | ||
cells: [] | ||
}; | ||
for (var i = 0; i < item.align.length; i++) { | ||
if (/^ *-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'right'; | ||
// top-level paragraph | ||
if (top && (execArr = this.rules.paragraph.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
if (execArr[1].slice(-1) === '\n') { | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.paragraph, | ||
text: execArr[1].slice(0, -1) | ||
}); | ||
} | ||
else if (/^ *:-+: *$/.test(item.align[i])) { | ||
item.align[i] = 'center'; | ||
} | ||
else if (/^ *:-+ *$/.test(item.align[i])) { | ||
item.align[i] = 'left'; | ||
} | ||
else { | ||
item.align[i] = null; | ||
this.tokens.push({ | ||
type: this.tokens.length > 0 ? interfaces_1.TokenType.paragraph : interfaces_1.TokenType.text, | ||
text: execArr[1] | ||
}); | ||
} | ||
continue; | ||
} | ||
var td = execArr[3].replace(/(?: *\| *)?\n$/, '').split('\n'); | ||
for (var i = 0; i < td.length; i++) { | ||
item.cells[i] = td[i].replace(/^ *\| *| *\| *$/g, '').split(/ *\| */); | ||
// text | ||
// Top-level should never reach here. | ||
if ((execArr = this.rules.text.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.text, text: execArr[0] }); | ||
continue; | ||
} | ||
this.tokens.push(item); | ||
continue; | ||
} | ||
// simple rules | ||
if (this.staticThis.simpleRules.length) { | ||
var simpleRules = this.staticThis.simpleRules; | ||
for (var i = 0; i < simpleRules.length; i++) { | ||
if ((execArr = simpleRules[i].exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var type = 'simpleRule' + (i + 1); | ||
this.tokens.push({ type: type, execArr: execArr }); | ||
continue mainLoop; | ||
} | ||
if (nextPart) { | ||
throw new Error('Infinite loop on byte: ' + nextPart.charCodeAt(0) + `, near text '${nextPart.slice(0, 30)}...'`); | ||
} | ||
} | ||
// top-level paragraph | ||
if (top && (execArr = this.rules.paragraph.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
if (execArr[1].slice(-1) === '\n') { | ||
this.tokens.push({ | ||
type: interfaces_1.TokenType.paragraph, | ||
text: execArr[1].slice(0, -1) | ||
}); | ||
} | ||
else { | ||
this.tokens.push({ | ||
type: this.tokens.length > 0 ? interfaces_1.TokenType.paragraph : interfaces_1.TokenType.text, | ||
text: execArr[1] | ||
}); | ||
} | ||
continue; | ||
} | ||
// text | ||
// Top-level should never reach here. | ||
if ((execArr = this.rules.text.exec(nextPart))) { | ||
nextPart = nextPart.substring(execArr[0].length); | ||
this.tokens.push({ type: interfaces_1.TokenType.text, text: execArr[0] }); | ||
continue; | ||
} | ||
if (nextPart) { | ||
throw new Error('Infinite loop on byte: ' + nextPart.charCodeAt(0) + (", near text '" + nextPart.slice(0, 30) + "...'")); | ||
} | ||
return { tokens: this.tokens, links: this.links }; | ||
} | ||
return { tokens: this.tokens, links: this.links }; | ||
}; | ||
} | ||
BlockLexer.simpleRules = []; | ||
return BlockLexer; | ||
}()); | ||
})(); | ||
exports.BlockLexer = BlockLexer; |
@@ -12,5 +12,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ExtendRegexp = /** @class */ (function () { | ||
function ExtendRegexp(regex, flags) { | ||
if (flags === void 0) { flags = ''; } | ||
exports.ExtendRegexp = void 0; | ||
class ExtendRegexp { | ||
constructor(regex, flags = '') { | ||
this.source = regex.source; | ||
@@ -25,4 +25,4 @@ this.flags = flags; | ||
*/ | ||
ExtendRegexp.prototype.setGroup = function (groupName, groupRegexp) { | ||
var newRegexp = typeof groupRegexp == 'string' ? groupRegexp : groupRegexp.source; | ||
setGroup(groupName, groupRegexp) { | ||
let newRegexp = typeof groupRegexp == 'string' ? groupRegexp : groupRegexp.source; | ||
newRegexp = newRegexp.replace(/(^|[^\[])\^/g, '$1'); | ||
@@ -32,11 +32,10 @@ // Extend regexp. | ||
return this; | ||
}; | ||
} | ||
/** | ||
* Returns a result of extending a regular expression. | ||
*/ | ||
ExtendRegexp.prototype.getRegexp = function () { | ||
getRegexp() { | ||
return new RegExp(this.source, this.flags); | ||
}; | ||
return ExtendRegexp; | ||
}()); | ||
} | ||
} | ||
exports.ExtendRegexp = ExtendRegexp; |
@@ -12,5 +12,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var escapeTest = /[&<>"']/; | ||
var escapeReplace = /[&<>"']/g; | ||
var replacements = { | ||
exports.unescape = exports.escape = void 0; | ||
const escapeTest = /[&<>"']/; | ||
const escapeReplace = /[&<>"']/g; | ||
const replacements = { | ||
'&': '&', | ||
@@ -21,10 +22,10 @@ '<': '<', | ||
// tslint:disable-next-line:quotemark | ||
"'": ''' | ||
"'": ''', | ||
}; | ||
var escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/; | ||
var escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g; | ||
const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/; | ||
const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g; | ||
function escape(html, encode) { | ||
if (encode) { | ||
if (escapeTest.test(html)) { | ||
return html.replace(escapeReplace, function (ch) { return replacements[ch]; }); | ||
return html.replace(escapeReplace, (ch) => replacements[ch]); | ||
} | ||
@@ -34,3 +35,3 @@ } | ||
if (escapeTestNoEncode.test(html)) { | ||
return html.replace(escapeReplaceNoEncode, function (ch) { return replacements[ch]; }); | ||
return html.replace(escapeReplaceNoEncode, (ch) => replacements[ch]); | ||
} | ||
@@ -37,0 +38,0 @@ } |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./block-lexer")); | ||
__export(require("./helpers")); | ||
__export(require("./inline-lexer")); | ||
__export(require("./interfaces")); | ||
__export(require("./marked")); | ||
__export(require("./parser")); | ||
__export(require("./renderer")); | ||
__export(require("./extend-regexp")); | ||
__exportStar(require("./block-lexer"), exports); | ||
__exportStar(require("./helpers"), exports); | ||
__exportStar(require("./inline-lexer"), exports); | ||
__exportStar(require("./interfaces"), exports); | ||
__exportStar(require("./marked"), exports); | ||
__exportStar(require("./parser"), exports); | ||
__exportStar(require("./renderer"), exports); | ||
__exportStar(require("./extend-regexp"), exports); |
@@ -11,23 +11,12 @@ "use strict"; | ||
*/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var extend_regexp_1 = require("./extend-regexp"); | ||
var marked_1 = require("./marked"); | ||
var renderer_1 = require("./renderer"); | ||
exports.InlineLexer = void 0; | ||
const extend_regexp_1 = require("./extend-regexp"); | ||
const marked_1 = require("./marked"); | ||
const renderer_1 = require("./renderer"); | ||
/** | ||
* Inline Lexer & Compiler. | ||
*/ | ||
var InlineLexer = /** @class */ (function () { | ||
function InlineLexer(staticThis, links, options, renderer) { | ||
if (options === void 0) { options = marked_1.Marked.options; } | ||
class InlineLexer { | ||
constructor(staticThis, links, options = marked_1.Marked.options, renderer) { | ||
this.staticThis = staticThis; | ||
@@ -38,3 +27,3 @@ this.links = links; | ||
if (!this.links) { | ||
throw new Error("InlineLexer requires 'links' parameter."); | ||
throw new Error(`InlineLexer requires 'links' parameter.`); | ||
} | ||
@@ -46,7 +35,7 @@ this.setRules(); | ||
*/ | ||
InlineLexer.output = function (src, links, options) { | ||
var inlineLexer = new this(this, links, options); | ||
static output(src, links, options) { | ||
const inlineLexer = new this(this, links, options); | ||
return inlineLexer.output(src); | ||
}; | ||
InlineLexer.getRulesBase = function () { | ||
} | ||
static getRulesBase() { | ||
if (this.rulesBase) { | ||
@@ -58,3 +47,3 @@ return this.rulesBase; | ||
*/ | ||
var base = { | ||
const base = { | ||
escape: /^\\([\\`*{}\[\]()#+\-.!_>])/, | ||
@@ -80,41 +69,50 @@ autolink: /^<([^ <>]+(@|:\/)[^ <>]+)>/, | ||
return (this.rulesBase = base); | ||
}; | ||
InlineLexer.getRulesPedantic = function () { | ||
} | ||
static getRulesPedantic() { | ||
if (this.rulesPedantic) { | ||
return this.rulesPedantic; | ||
} | ||
return (this.rulesPedantic = __assign(__assign({}, this.getRulesBase()), { | ||
strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/, | ||
em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/ | ||
})); | ||
}; | ||
InlineLexer.getRulesGfm = function () { | ||
return (this.rulesPedantic = { | ||
...this.getRulesBase(), | ||
...{ | ||
strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/, | ||
em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/ | ||
} | ||
}); | ||
} | ||
static getRulesGfm() { | ||
if (this.rulesGfm) { | ||
return this.rulesGfm; | ||
} | ||
var base = this.getRulesBase(); | ||
var escape = new extend_regexp_1.ExtendRegexp(base.escape).setGroup('])', '~|])').getRegexp(); | ||
var text = new extend_regexp_1.ExtendRegexp(base.text) | ||
const base = this.getRulesBase(); | ||
const escape = new extend_regexp_1.ExtendRegexp(base.escape).setGroup('])', '~|])').getRegexp(); | ||
const text = new extend_regexp_1.ExtendRegexp(base.text) | ||
.setGroup(']|', '~]|') | ||
.setGroup('|', '|https?://|') | ||
.getRegexp(); | ||
return (this.rulesGfm = __assign(__assign({}, base), { | ||
escape: escape, | ||
url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/, | ||
del: /^~~(?=\S)([\s\S]*?\S)~~/, | ||
text: text | ||
})); | ||
}; | ||
InlineLexer.getRulesBreaks = function () { | ||
return (this.rulesGfm = { | ||
...base, | ||
...{ | ||
escape, | ||
url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/, | ||
del: /^~~(?=\S)([\s\S]*?\S)~~/, | ||
text | ||
} | ||
}); | ||
} | ||
static getRulesBreaks() { | ||
if (this.rulesBreaks) { | ||
return this.rulesBreaks; | ||
} | ||
var inline = this.getRulesGfm(); | ||
var gfm = this.getRulesGfm(); | ||
return (this.rulesBreaks = __assign(__assign({}, gfm), { | ||
br: new extend_regexp_1.ExtendRegexp(inline.br).setGroup('{2,}', '*').getRegexp(), | ||
text: new extend_regexp_1.ExtendRegexp(gfm.text).setGroup('{2,}', '*').getRegexp() | ||
})); | ||
}; | ||
InlineLexer.prototype.setRules = function () { | ||
const inline = this.getRulesGfm(); | ||
const gfm = this.getRulesGfm(); | ||
return (this.rulesBreaks = { | ||
...gfm, | ||
...{ | ||
br: new extend_regexp_1.ExtendRegexp(inline.br).setGroup('{2,}', '*').getRegexp(), | ||
text: new extend_regexp_1.ExtendRegexp(gfm.text).setGroup('{2,}', '*').getRegexp() | ||
} | ||
}); | ||
} | ||
setRules() { | ||
if (this.options.gfm) { | ||
@@ -135,10 +133,10 @@ if (this.options.breaks) { | ||
this.hasRulesGfm = this.rules.url !== undefined; | ||
}; | ||
} | ||
/** | ||
* Lexing/Compiling. | ||
*/ | ||
InlineLexer.prototype.output = function (nextPart) { | ||
output(nextPart) { | ||
nextPart = nextPart; | ||
var execArr; | ||
var out = ''; | ||
let execArr; | ||
let out = ''; | ||
while (nextPart) { | ||
@@ -153,4 +151,4 @@ // escape | ||
if ((execArr = this.rules.autolink.exec(nextPart))) { | ||
var text = void 0; | ||
var href = void 0; | ||
let text; | ||
let href; | ||
nextPart = nextPart.substring(execArr[0].length); | ||
@@ -170,4 +168,4 @@ if (execArr[2] === '@') { | ||
if (!this.inLink && this.hasRulesGfm && (execArr = this.rules.url.exec(nextPart))) { | ||
var text = void 0; | ||
var href = void 0; | ||
let text; | ||
let href; | ||
nextPart = nextPart.substring(execArr[0].length); | ||
@@ -209,4 +207,4 @@ text = this.options.escape(execArr[1]); | ||
nextPart = nextPart.substring(execArr[0].length); | ||
var keyLink = (execArr[2] || execArr[1]).replace(/\s+/g, ' '); | ||
var link = this.links[keyLink.toLowerCase()]; | ||
const keyLink = (execArr[2] || execArr[1]).replace(/\s+/g, ' '); | ||
const link = this.links[keyLink.toLowerCase()]; | ||
if (!link || !link.href) { | ||
@@ -263,17 +261,17 @@ out += execArr[0].charAt(0); | ||
return out; | ||
}; | ||
} | ||
/** | ||
* Compile Link. | ||
*/ | ||
InlineLexer.prototype.outputLink = function (execArr, link) { | ||
var href = this.options.escape(link.href); | ||
var title = link.title ? this.options.escape(link.title) : null; | ||
outputLink(execArr, link) { | ||
const href = this.options.escape(link.href); | ||
const title = link.title ? this.options.escape(link.title) : null; | ||
return execArr[0].charAt(0) !== '!' | ||
? this.renderer.link(href, title, this.output(execArr[1])) | ||
: this.renderer.image(href, title, this.options.escape(execArr[1])); | ||
}; | ||
} | ||
/** | ||
* Smartypants Transformations. | ||
*/ | ||
InlineLexer.prototype.smartypants = function (text) { | ||
smartypants(text) { | ||
if (!this.options.smartypants) { | ||
@@ -297,14 +295,14 @@ return text; | ||
.replace(/\.{3}/g, '\u2026')); | ||
}; | ||
} | ||
/** | ||
* Mangle Links. | ||
*/ | ||
InlineLexer.prototype.mangle = function (text) { | ||
mangle(text) { | ||
if (!this.options.mangle) { | ||
return text; | ||
} | ||
var out = ''; | ||
var length = text.length; | ||
for (var i = 0; i < length; i++) { | ||
var str = void 0; | ||
let out = ''; | ||
const length = text.length; | ||
for (let i = 0; i < length; i++) { | ||
let str; | ||
if (Math.random() > 0.5) { | ||
@@ -316,5 +314,4 @@ str = 'x' + text.charCodeAt(i).toString(16); | ||
return out; | ||
}; | ||
return InlineLexer; | ||
}()); | ||
} | ||
} | ||
exports.InlineLexer = InlineLexer; |
@@ -9,3 +9,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("./helpers"); | ||
exports.MarkedOptions = exports.TokenType = void 0; | ||
const helpers_1 = require("./helpers"); | ||
var TokenType; | ||
@@ -30,4 +31,4 @@ (function (TokenType) { | ||
})(TokenType = exports.TokenType || (exports.TokenType = {})); | ||
var MarkedOptions = /** @class */ (function () { | ||
function MarkedOptions() { | ||
class MarkedOptions { | ||
constructor() { | ||
this.gfm = true; | ||
@@ -60,4 +61,3 @@ this.tables = true; | ||
} | ||
return MarkedOptions; | ||
}()); | ||
} | ||
exports.MarkedOptions = MarkedOptions; |
@@ -11,122 +11,108 @@ "use strict"; | ||
*/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var block_lexer_1 = require("./block-lexer"); | ||
var interfaces_1 = require("./interfaces"); | ||
var parser_1 = require("./parser"); | ||
var Marked = /** @class */ (function () { | ||
function Marked() { | ||
} | ||
/** | ||
* Merges the default options with options that will be set. | ||
* | ||
* @param options Hash of options. | ||
*/ | ||
Marked.setOptions = function (options) { | ||
Object.assign(this.options, options); | ||
return this; | ||
}; | ||
/** | ||
* Setting simple block rule. | ||
*/ | ||
Marked.setBlockRule = function (regexp, renderer) { | ||
if (renderer === void 0) { renderer = function () { return ''; }; } | ||
block_lexer_1.BlockLexer.simpleRules.push(regexp); | ||
this.simpleRenderers.push(renderer); | ||
return this; | ||
}; | ||
/** | ||
* Accepts Markdown text and returns text in HTML format. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. They replace, but do not merge with the default options. | ||
* If you want the merging, you can to do this via `Marked.setOptions()`. | ||
*/ | ||
Marked.parse = function (src, options) { | ||
if (options === void 0) { options = this.options; } | ||
try { | ||
var _a = this.callBlockLexer(src, options), tokens = _a.tokens, links = _a.links; | ||
return this.callParser(tokens, links, options); | ||
exports.Marked = void 0; | ||
const block_lexer_1 = require("./block-lexer"); | ||
const interfaces_1 = require("./interfaces"); | ||
const parser_1 = require("./parser"); | ||
let Marked = /** @class */ (() => { | ||
class Marked { | ||
/** | ||
* Merges the default options with options that will be set. | ||
* | ||
* @param options Hash of options. | ||
*/ | ||
static setOptions(options) { | ||
Object.assign(this.options, options); | ||
return this; | ||
} | ||
catch (e) { | ||
return this.callMe(e); | ||
/** | ||
* Setting simple block rule. | ||
*/ | ||
static setBlockRule(regexp, renderer = () => '') { | ||
block_lexer_1.BlockLexer.simpleRules.push(regexp); | ||
this.simpleRenderers.push(renderer); | ||
return this; | ||
} | ||
}; | ||
/** | ||
* Accepts Markdown text and returns object with text in HTML format, | ||
* tokens and links from `BlockLexer.parser()`. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. They replace, but do not merge with the default options. | ||
* If you want the merging, you can to do this via `Marked.setOptions()`. | ||
*/ | ||
Marked.debug = function (src, options) { | ||
if (options === void 0) { options = this.options; } | ||
var _a = this.callBlockLexer(src, options), tokens = _a.tokens, links = _a.links; | ||
var origin = tokens.slice(); | ||
var parser = new parser_1.Parser(options); | ||
parser.simpleRenderers = this.simpleRenderers; | ||
var result = parser.debug(links, tokens); | ||
/** | ||
* Translates a token type into a readable form, | ||
* and moves `line` field to a first place in a token object. | ||
* Accepts Markdown text and returns text in HTML format. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. They replace, but do not merge with the default options. | ||
* If you want the merging, you can to do this via `Marked.setOptions()`. | ||
*/ | ||
origin = origin.map(function (token) { | ||
token.type = interfaces_1.TokenType[token.type] || token.type; | ||
var line = token.line; | ||
delete token.line; | ||
if (line) { | ||
return __assign({ line: line }, token); | ||
static parse(src, options = this.options) { | ||
try { | ||
const { tokens, links } = this.callBlockLexer(src, options); | ||
return this.callParser(tokens, links, options); | ||
} | ||
else { | ||
return token; | ||
catch (e) { | ||
return this.callMe(e); | ||
} | ||
}); | ||
return { tokens: origin, links: links, result: result }; | ||
}; | ||
Marked.callBlockLexer = function (src, options) { | ||
if (src === void 0) { src = ''; } | ||
if (typeof src != 'string') { | ||
throw new Error("Expected that the 'src' parameter would have a 'string' type, got '" + typeof src + "'"); | ||
} | ||
// Preprocessing. | ||
src = src | ||
.replace(/\r\n|\r/g, '\n') | ||
.replace(/\t/g, ' ') | ||
.replace(/\u00a0/g, ' ') | ||
.replace(/\u2424/g, '\n') | ||
.replace(/^ +$/gm, ''); | ||
return block_lexer_1.BlockLexer.lex(src, options, true); | ||
}; | ||
Marked.callParser = function (tokens, links, options) { | ||
if (this.simpleRenderers.length) { | ||
var parser = new parser_1.Parser(options); | ||
/** | ||
* Accepts Markdown text and returns object with text in HTML format, | ||
* tokens and links from `BlockLexer.parser()`. | ||
* | ||
* @param src String of markdown source to be compiled. | ||
* @param options Hash of options. They replace, but do not merge with the default options. | ||
* If you want the merging, you can to do this via `Marked.setOptions()`. | ||
*/ | ||
static debug(src, options = this.options) { | ||
const { tokens, links } = this.callBlockLexer(src, options); | ||
let origin = tokens.slice(); | ||
const parser = new parser_1.Parser(options); | ||
parser.simpleRenderers = this.simpleRenderers; | ||
return parser.parse(links, tokens); | ||
const result = parser.debug(links, tokens); | ||
/** | ||
* Translates a token type into a readable form, | ||
* and moves `line` field to a first place in a token object. | ||
*/ | ||
origin = origin.map(token => { | ||
token.type = interfaces_1.TokenType[token.type] || token.type; | ||
const line = token.line; | ||
delete token.line; | ||
if (line) { | ||
return { ...{ line }, ...token }; | ||
} | ||
else { | ||
return token; | ||
} | ||
}); | ||
return { tokens: origin, links, result }; | ||
} | ||
else { | ||
return parser_1.Parser.parse(tokens, links, options); | ||
static callBlockLexer(src = '', options) { | ||
if (typeof src != 'string') { | ||
throw new Error(`Expected that the 'src' parameter would have a 'string' type, got '${typeof src}'`); | ||
} | ||
// Preprocessing. | ||
src = src | ||
.replace(/\r\n|\r/g, '\n') | ||
.replace(/\t/g, ' ') | ||
.replace(/\u00a0/g, ' ') | ||
.replace(/\u2424/g, '\n') | ||
.replace(/^ +$/gm, ''); | ||
return block_lexer_1.BlockLexer.lex(src, options, true); | ||
} | ||
}; | ||
Marked.callMe = function (err) { | ||
err.message += '\nPlease report this to https://github.com/ts-stack/markdown'; | ||
if (this.options.silent) { | ||
return '<p>An error occured:</p><pre>' + this.options.escape(err.message + '', true) + '</pre>'; | ||
static callParser(tokens, links, options) { | ||
if (this.simpleRenderers.length) { | ||
const parser = new parser_1.Parser(options); | ||
parser.simpleRenderers = this.simpleRenderers; | ||
return parser.parse(links, tokens); | ||
} | ||
else { | ||
return parser_1.Parser.parse(tokens, links, options); | ||
} | ||
} | ||
throw err; | ||
}; | ||
static callMe(err) { | ||
err.message += '\nPlease report this to https://github.com/ts-stack/markdown'; | ||
if (this.options.silent) { | ||
return '<p>An error occured:</p><pre>' + this.options.escape(err.message + '', true) + '</pre>'; | ||
} | ||
throw err; | ||
} | ||
} | ||
Marked.options = new interfaces_1.MarkedOptions(); | ||
Marked.simpleRenderers = []; | ||
return Marked; | ||
}()); | ||
})(); | ||
exports.Marked = Marked; |
@@ -12,11 +12,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var inline_lexer_1 = require("./inline-lexer"); | ||
var interfaces_1 = require("./interfaces"); | ||
var marked_1 = require("./marked"); | ||
var renderer_1 = require("./renderer"); | ||
exports.Parser = void 0; | ||
const inline_lexer_1 = require("./inline-lexer"); | ||
const interfaces_1 = require("./interfaces"); | ||
const marked_1 = require("./marked"); | ||
const renderer_1 = require("./renderer"); | ||
/** | ||
* Parsing & Compiling. | ||
*/ | ||
var Parser = /** @class */ (function () { | ||
function Parser(options) { | ||
class Parser { | ||
constructor(options) { | ||
this.simpleRenderers = []; | ||
@@ -29,10 +30,10 @@ this.line = 0; | ||
} | ||
Parser.parse = function (tokens, links, options) { | ||
var parser = new this(options); | ||
static parse(tokens, links, options) { | ||
const parser = new this(options); | ||
return parser.parse(links, tokens); | ||
}; | ||
Parser.prototype.parse = function (links, tokens) { | ||
} | ||
parse(links, tokens) { | ||
this.inlineLexer = new inline_lexer_1.InlineLexer(inline_lexer_1.InlineLexer, links, this.options, this.renderer); | ||
this.tokens = tokens.reverse(); | ||
var out = ''; | ||
let out = ''; | ||
while (this.next()) { | ||
@@ -42,9 +43,9 @@ out += this.tok(); | ||
return out; | ||
}; | ||
Parser.prototype.debug = function (links, tokens) { | ||
} | ||
debug(links, tokens) { | ||
this.inlineLexer = new inline_lexer_1.InlineLexer(inline_lexer_1.InlineLexer, links, this.options, this.renderer); | ||
this.tokens = tokens.reverse(); | ||
var out = ''; | ||
let out = ''; | ||
while (this.next()) { | ||
var outToken = this.tok(); | ||
const outToken = this.tok(); | ||
this.token.line = this.line += outToken.split('\n').length - 1; | ||
@@ -54,12 +55,12 @@ out += outToken; | ||
return out; | ||
}; | ||
Parser.prototype.next = function () { | ||
} | ||
next() { | ||
return (this.token = this.tokens.pop()); | ||
}; | ||
Parser.prototype.getNextElement = function () { | ||
} | ||
getNextElement() { | ||
return this.tokens[this.tokens.length - 1]; | ||
}; | ||
Parser.prototype.parseText = function () { | ||
var body = this.token.text; | ||
var nextElement; | ||
} | ||
parseText() { | ||
let body = this.token.text; | ||
let nextElement; | ||
while ((nextElement = this.getNextElement()) && nextElement.type == interfaces_1.TokenType.text) { | ||
@@ -69,4 +70,4 @@ body += '\n' + this.next().text; | ||
return this.inlineLexer.output(body); | ||
}; | ||
Parser.prototype.tok = function () { | ||
} | ||
tok() { | ||
switch (this.token.type) { | ||
@@ -91,4 +92,4 @@ case interfaces_1.TokenType.space: { | ||
case interfaces_1.TokenType.listStart: { | ||
var body = ''; | ||
var ordered = this.token.ordered; | ||
let body = ''; | ||
const ordered = this.token.ordered; | ||
while (this.next().type != interfaces_1.TokenType.listEnd) { | ||
@@ -100,3 +101,3 @@ body += this.tok(); | ||
case interfaces_1.TokenType.listItemStart: { | ||
var body = ''; | ||
let body = ''; | ||
while (this.next().type != interfaces_1.TokenType.listItemEnd) { | ||
@@ -108,3 +109,3 @@ body += this.token.type == interfaces_1.TokenType.text ? this.parseText() : this.tok(); | ||
case interfaces_1.TokenType.looseItemStart: { | ||
var body = ''; | ||
let body = ''; | ||
while (this.next().type != interfaces_1.TokenType.listItemEnd) { | ||
@@ -119,17 +120,16 @@ body += this.tok(); | ||
case interfaces_1.TokenType.table: { | ||
var header = ''; | ||
var body = ''; | ||
var cell = void 0; | ||
let header = ''; | ||
let body = ''; | ||
let cell; | ||
// header | ||
cell = ''; | ||
for (var i = 0; i < this.token.header.length; i++) { | ||
var flags = { header: true, align: this.token.align[i] }; | ||
var out = this.inlineLexer.output(this.token.header[i]); | ||
for (let i = 0; i < this.token.header.length; i++) { | ||
const flags = { header: true, align: this.token.align[i] }; | ||
const out = this.inlineLexer.output(this.token.header[i]); | ||
cell += this.renderer.tablecell(out, flags); | ||
} | ||
header += this.renderer.tablerow(cell); | ||
for (var _i = 0, _a = this.token.cells; _i < _a.length; _i++) { | ||
var row = _a[_i]; | ||
for (const row of this.token.cells) { | ||
cell = ''; | ||
for (var j = 0; j < row.length; j++) { | ||
for (let j = 0; j < row.length; j++) { | ||
cell += this.renderer.tablecell(this.inlineLexer.output(row[j]), { | ||
@@ -145,3 +145,3 @@ header: false, | ||
case interfaces_1.TokenType.blockquoteStart: { | ||
var body = ''; | ||
let body = ''; | ||
while (this.next().type != interfaces_1.TokenType.blockquoteEnd) { | ||
@@ -156,3 +156,3 @@ body += this.tok(); | ||
case interfaces_1.TokenType.html: { | ||
var html = !this.token.pre && !this.options.pedantic ? this.inlineLexer.output(this.token.text) : this.token.text; | ||
const html = !this.token.pre && !this.options.pedantic ? this.inlineLexer.output(this.token.text) : this.token.text; | ||
return this.renderer.html(html); | ||
@@ -162,3 +162,3 @@ } | ||
if (this.simpleRenderers.length) { | ||
for (var i = 0; i < this.simpleRenderers.length; i++) { | ||
for (let i = 0; i < this.simpleRenderers.length; i++) { | ||
if (this.token.type == 'simpleRule' + (i + 1)) { | ||
@@ -169,3 +169,3 @@ return this.simpleRenderers[i].call(this.renderer, this.token.execArr); | ||
} | ||
var errMsg = "Token with \"" + this.token.type + "\" type was not found."; | ||
const errMsg = `Token with "${this.token.type}" type was not found.`; | ||
if (this.options.silent) { | ||
@@ -179,5 +179,4 @@ console.log(errMsg); | ||
} | ||
}; | ||
return Parser; | ||
}()); | ||
} | ||
} | ||
exports.Parser = Parser; |
@@ -12,10 +12,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var marked_1 = require("./marked"); | ||
var Renderer = /** @class */ (function () { | ||
function Renderer(options) { | ||
exports.Renderer = void 0; | ||
const marked_1 = require("./marked"); | ||
class Renderer { | ||
constructor(options) { | ||
this.options = options || marked_1.Marked.options; | ||
} | ||
Renderer.prototype.code = function (code, lang, escaped) { | ||
code(code, lang, escaped) { | ||
if (this.options.highlight) { | ||
var out = this.options.highlight(code, lang); | ||
const out = this.options.highlight(code, lang); | ||
if (out != null && out !== code) { | ||
@@ -35,56 +36,63 @@ escaped = true; | ||
'\n</code></pre>\n'); | ||
}; | ||
Renderer.prototype.blockquote = function (quote) { | ||
} | ||
blockquote(quote) { | ||
return '<blockquote>\n' + quote + '</blockquote>\n'; | ||
}; | ||
Renderer.prototype.html = function (html) { | ||
} | ||
html(html) { | ||
return html; | ||
}; | ||
Renderer.prototype.heading = function (text, level, raw) { | ||
var id = this.options.headerPrefix + raw.toLowerCase().replace(/[^\w]+/g, '-'); | ||
return "<h" + level + " id=\"" + id + "\">" + text + "</h" + level + ">\n"; | ||
}; | ||
Renderer.prototype.hr = function () { | ||
} | ||
heading(text, level, raw) { | ||
const id = this.options.headerPrefix + raw.toLowerCase().replace(/[^\w]+/g, '-'); | ||
return `<h${level} id="${id}">${text}</h${level}>\n`; | ||
} | ||
hr() { | ||
return this.options.xhtml ? '<hr/>\n' : '<hr>\n'; | ||
}; | ||
Renderer.prototype.list = function (body, ordered) { | ||
var type = ordered ? 'ol' : 'ul'; | ||
return "\n<" + type + ">\n" + body + "</" + type + ">\n"; | ||
}; | ||
Renderer.prototype.listitem = function (text) { | ||
} | ||
list(body, ordered) { | ||
const type = ordered ? 'ol' : 'ul'; | ||
return `\n<${type}>\n${body}</${type}>\n`; | ||
} | ||
listitem(text) { | ||
return '<li>' + text + '</li>\n'; | ||
}; | ||
Renderer.prototype.paragraph = function (text) { | ||
} | ||
paragraph(text) { | ||
return '<p>' + text + '</p>\n'; | ||
}; | ||
Renderer.prototype.table = function (header, body) { | ||
return "\n<table>\n<thead>\n" + header + "</thead>\n<tbody>\n" + body + "</tbody>\n</table>\n"; | ||
}; | ||
Renderer.prototype.tablerow = function (content) { | ||
} | ||
table(header, body) { | ||
return ` | ||
<table> | ||
<thead> | ||
${header}</thead> | ||
<tbody> | ||
${body}</tbody> | ||
</table> | ||
`; | ||
} | ||
tablerow(content) { | ||
return '<tr>\n' + content + '</tr>\n'; | ||
}; | ||
Renderer.prototype.tablecell = function (content, flags) { | ||
var type = flags.header ? 'th' : 'td'; | ||
var tag = flags.align ? '<' + type + ' style="text-align:' + flags.align + '">' : '<' + type + '>'; | ||
} | ||
tablecell(content, flags) { | ||
const type = flags.header ? 'th' : 'td'; | ||
const tag = flags.align ? '<' + type + ' style="text-align:' + flags.align + '">' : '<' + type + '>'; | ||
return tag + content + '</' + type + '>\n'; | ||
}; | ||
} | ||
// *** Inline level renderer methods. *** | ||
Renderer.prototype.strong = function (text) { | ||
strong(text) { | ||
return '<strong>' + text + '</strong>'; | ||
}; | ||
Renderer.prototype.em = function (text) { | ||
} | ||
em(text) { | ||
return '<em>' + text + '</em>'; | ||
}; | ||
Renderer.prototype.codespan = function (text) { | ||
} | ||
codespan(text) { | ||
return '<code>' + text + '</code>'; | ||
}; | ||
Renderer.prototype.br = function () { | ||
} | ||
br() { | ||
return this.options.xhtml ? '<br/>' : '<br>'; | ||
}; | ||
Renderer.prototype.del = function (text) { | ||
} | ||
del(text) { | ||
return '<del>' + text + '</del>'; | ||
}; | ||
Renderer.prototype.link = function (href, title, text) { | ||
} | ||
link(href, title, text) { | ||
if (this.options.sanitize) { | ||
var prot = void 0; | ||
let prot; | ||
try { | ||
@@ -102,3 +110,3 @@ prot = decodeURIComponent(this.options.unescape(href)) | ||
} | ||
var out = '<a href="' + href + '"'; | ||
let out = '<a href="' + href + '"'; | ||
if (title) { | ||
@@ -109,5 +117,5 @@ out += ' title="' + title + '"'; | ||
return out; | ||
}; | ||
Renderer.prototype.image = function (href, title, text) { | ||
var out = '<img src="' + href + '" alt="' + text + '"'; | ||
} | ||
image(href, title, text) { | ||
let out = '<img src="' + href + '" alt="' + text + '"'; | ||
if (title) { | ||
@@ -118,8 +126,7 @@ out += ' title="' + title + '"'; | ||
return out; | ||
}; | ||
Renderer.prototype.text = function (text) { | ||
} | ||
text(text) { | ||
return text; | ||
}; | ||
return Renderer; | ||
}()); | ||
} | ||
} | ||
exports.Renderer = Renderer; |
@@ -5,3 +5,3 @@ { | ||
"author": "Костя Третяк <ktretiak.in.ua@gmail.com>", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "dist/index", | ||
@@ -28,5 +28,2 @@ "typings": "dist/index", | ||
], | ||
"optionalDependencies": { | ||
"typescript": "^3.7.4" | ||
}, | ||
"devDependencies": { | ||
@@ -36,7 +33,7 @@ "@types/highlight.js": "^9.12.2", | ||
"@types/katex": "^0.11.0", | ||
"@types/markdown-it": "^0.0.9", | ||
"@types/markdown-it": "^10.0.1", | ||
"@types/node": "^13.1.6", | ||
"commonmark": "^0.29.1", | ||
"concurrently": "^5.0.2", | ||
"highlight.js": "^9.12.0", | ||
"highlight.js": "^10.0.1", | ||
"jasmine": "^3.5.0", | ||
@@ -46,11 +43,11 @@ "katex": "^0.11.1", | ||
"markdown-it": "^10.0.0", | ||
"marked": "^0.8.0", | ||
"prettier": "^1.16.4", | ||
"marked": "^1.0.0", | ||
"prettier": "^2.0.5", | ||
"remarkable": "^2.0.0", | ||
"rimraf": "^3.0.0", | ||
"showdown": "^1.8.6", | ||
"tslint": "^5.12.1", | ||
"tslint": "^6.1.2", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-plugin-prettier": "^2.0.1", | ||
"typescript": "^2.6.2" | ||
"typescript": "^3.8.3" | ||
}, | ||
@@ -57,0 +54,0 @@ "scripts": { |
@@ -1,3 +0,1 @@ | ||
[![Build Status](https://travis-ci.org/ts-stack/markdown.svg?branch=master)](https://travis-ci.org/ts-stack/markdown) | ||
# @ts-stack/markdown | ||
@@ -4,0 +2,0 @@ |
0
79722
1820
426