Comparing version 0.0.0-alpha.5 to 0.0.0-alpha.6
@@ -1,14 +0,15 @@ | ||
import { RulesBlockMain, MarkedOptions, Token, Links, LexerReturns, RulesBlockGfm, RulesBlockTables } from './interfaces'; | ||
import { RulesBlockBase, MarkedOptions, Token, Links, LexerReturns, RulesBlockGfm, RulesBlockTables } from './interfaces'; | ||
export declare class BlockLexer<T extends typeof BlockLexer> { | ||
private staticThis; | ||
protected static block: RulesBlockMain; | ||
protected staticThis: typeof BlockLexer; | ||
static simpleRules: RegExp[]; | ||
protected static rulesBase: RulesBlockBase; | ||
/** | ||
* GFM Block Grammar. | ||
*/ | ||
protected static blockGfm: RulesBlockGfm; | ||
protected static rulesGfm: RulesBlockGfm; | ||
/** | ||
* GFM + Tables Block Grammar. | ||
*/ | ||
protected static blockTables: RulesBlockTables; | ||
protected rules: RulesBlockMain | RulesBlockGfm | RulesBlockTables; | ||
protected static rulesTables: RulesBlockTables; | ||
protected rules: RulesBlockBase | RulesBlockGfm | RulesBlockTables; | ||
protected options: MarkedOptions; | ||
@@ -19,7 +20,3 @@ protected links: Links; | ||
protected hasRulesTables: boolean; | ||
constructor(staticThis: T, options?: MarkedOptions); | ||
protected setRules(): void; | ||
protected static getRulesMain(): RulesBlockMain; | ||
protected static getRulesGfm(): RulesBlockGfm; | ||
protected static getRulesTables(): RulesBlockTables; | ||
constructor(staticThis: typeof BlockLexer, options?: object); | ||
/** | ||
@@ -32,2 +29,6 @@ * Accepts Markdown text and returns object with tokens and links. | ||
static lex(src: string, options?: MarkedOptions, top?: boolean, isBlockQuote?: boolean): LexerReturns; | ||
protected static getRulesBase(): RulesBlockBase; | ||
protected static getRulesGfm(): RulesBlockGfm; | ||
protected static getRulesTable(): RulesBlockTables; | ||
protected setRules(): void; | ||
/** | ||
@@ -34,0 +35,0 @@ * Lexing. |
@@ -12,4 +12,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const marked_1 = require("./marked"); | ||
const extend_regexp_1 = require("./extend-regexp"); | ||
const marked_1 = require("./marked"); | ||
const interfaces_1 = require("./interfaces"); | ||
@@ -19,26 +19,21 @@ class BlockLexer { | ||
this.staticThis = staticThis; | ||
this.options = options || marked_1.Marked.defaults; | ||
this.links = {}; | ||
this.tokens = []; | ||
this.options = options || marked_1.Marked.defaults; | ||
this.setRules(); | ||
} | ||
setRules() { | ||
if (this.options.gfm) { | ||
if (this.options.tables) { | ||
this.rules = this.staticThis.getRulesTables(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
} | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesMain(); | ||
} | ||
this.hasRulesGfm = this.rules.fences !== undefined; | ||
this.hasRulesTables = this.rules.table !== undefined; | ||
/** | ||
* 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); | ||
} | ||
static getRulesMain() { | ||
if (this.block) | ||
return this.block; | ||
const block = { | ||
static getRulesBase() { | ||
if (this.rulesBase) | ||
return this.rulesBase; | ||
const base = { | ||
newline: /^\n+/, | ||
@@ -56,38 +51,37 @@ code: /^( {4}[^\n]+\n*)+/, | ||
bullet: /(?:[*+-]|\d+\.)/, | ||
item: /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/, | ||
_tag: '' | ||
item: /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/ | ||
}; | ||
block.item = new extend_regexp_1.ExtendRegexp(block.item, 'gm') | ||
.setGroup(/bull/g, block.bullet) | ||
base.item = new extend_regexp_1.ExtendRegexp(base.item, 'gm') | ||
.setGroup(/bull/g, base.bullet) | ||
.getRegexp(); | ||
block.list = new extend_regexp_1.ExtendRegexp(block.list) | ||
.setGroup(/bull/g, block.bullet) | ||
base.list = new extend_regexp_1.ExtendRegexp(base.list) | ||
.setGroup(/bull/g, base.bullet) | ||
.setGroup('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))') | ||
.setGroup('def', '\\n+(?=' + block.def.source + ')') | ||
.setGroup('def', '\\n+(?=' + base.def.source + ')') | ||
.getRegexp(); | ||
block._tag = '(?!(?:' | ||
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'; | ||
block.html = new extend_regexp_1.ExtendRegexp(block.html) | ||
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, block._tag) | ||
.setGroup(/tag/g, tag) | ||
.getRegexp(); | ||
block.paragraph = new extend_regexp_1.ExtendRegexp(block.paragraph) | ||
.setGroup('hr', block.hr) | ||
.setGroup('heading', block.heading) | ||
.setGroup('lheading', block.lheading) | ||
.setGroup('blockquote', block.blockquote) | ||
.setGroup('tag', '<' + block._tag) | ||
.setGroup('def', block.def) | ||
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.block = block; | ||
return this.rulesBase = base; | ||
} | ||
static getRulesGfm() { | ||
if (this.blockGfm) | ||
return this.blockGfm; | ||
const block = this.getRulesMain(); | ||
const gfm = Object.assign({}, block, { | ||
if (this.rulesGfm) | ||
return this.rulesGfm; | ||
const base = this.getRulesBase(); | ||
const gfm = Object.assign({}, base, { | ||
fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/, | ||
@@ -98,12 +92,12 @@ paragraph: /^/, | ||
const group1 = gfm.fences.source.replace('\\1', '\\2'); | ||
const group2 = block.list.source.replace('\\1', '\\3'); | ||
gfm.paragraph = new extend_regexp_1.ExtendRegexp(block.paragraph) | ||
const group2 = base.list.source.replace('\\1', '\\3'); | ||
gfm.paragraph = new extend_regexp_1.ExtendRegexp(base.paragraph) | ||
.setGroup('(?!', `(?!${group1}|${group2}|`) | ||
.getRegexp(); | ||
return this.blockGfm = gfm; | ||
return this.rulesGfm = gfm; | ||
} | ||
static getRulesTables() { | ||
if (this.blockTables) | ||
return this.blockTables; | ||
return this.blockTables = Object.assign({}, this.getRulesGfm(), { | ||
static getRulesTable() { | ||
if (this.rulesTables) | ||
return this.rulesTables; | ||
return this.rulesTables = Object.assign({}, this.getRulesGfm(), { | ||
nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, | ||
@@ -113,11 +107,16 @@ table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ | ||
} | ||
/** | ||
* 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); | ||
setRules() { | ||
if (this.options.gfm) { | ||
if (this.options.tables) { | ||
this.rules = this.staticThis.getRulesTable(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
} | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesBase(); | ||
} | ||
this.hasRulesGfm = this.rules.fences !== undefined; | ||
this.hasRulesTables = this.rules.table !== undefined; | ||
} | ||
@@ -130,3 +129,3 @@ /** | ||
let execArr; | ||
while (nextPart) { | ||
mainLoop: while (nextPart) { | ||
// newline | ||
@@ -254,3 +253,3 @@ if (execArr = this.rules.newline.exec(nextPart)) { | ||
if (this.options.smartLists && i !== length - 1) { | ||
blockBullet = this.staticThis.getRulesMain().bullet.exec(str[i + 1])[0]; | ||
blockBullet = this.staticThis.getRulesBase().bullet.exec(str[i + 1])[0]; | ||
if (bull !== blockBullet && !(bull.length > 1 && blockBullet.length > 1)) { | ||
@@ -334,2 +333,14 @@ nextPart = str.slice(i + 1).join('\n') + nextPart; | ||
} | ||
// 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 = interfaces_1.TokenType.text + simpleRules.length; | ||
this.tokens.push({ type: type, execArr: execArr }); | ||
continue mainLoop; | ||
} | ||
} | ||
} | ||
// top-level paragraph | ||
@@ -366,2 +377,3 @@ if (top && (execArr = this.rules.paragraph.exec(nextPart))) { | ||
} | ||
BlockLexer.simpleRules = []; | ||
exports.BlockLexer = BlockLexer; |
import { Renderer } from './renderer'; | ||
import { RulesInlineMain, MarkedOptions, Links, Link, RulesInlineGfm, RulesInlineBreaks, RulesInlinePedantic } from './interfaces'; | ||
import { RulesInlineBase, MarkedOptions, Links, Link, RulesInlineGfm, RulesInlineBreaks, RulesInlinePedantic, RulesInlineCallback } from './interfaces'; | ||
/** | ||
* Inline Lexer & Compiler. | ||
* | ||
* @todo Remove from constructor reference to current class. | ||
*/ | ||
export declare class InlineLexer<T extends typeof InlineLexer> { | ||
private staticThis; | ||
protected static inline: RulesInlineMain; | ||
export declare class InlineLexer { | ||
protected staticThis: typeof InlineLexer; | ||
protected links: Links; | ||
protected options: MarkedOptions; | ||
protected static rulesBase: RulesInlineBase; | ||
/** | ||
* Pedantic Inline Grammar. | ||
*/ | ||
protected static inlinePedantic: RulesInlinePedantic; | ||
protected static rulesPedantic: RulesInlinePedantic; | ||
/** | ||
* GFM Inline Grammar | ||
*/ | ||
protected static inlineGfm: RulesInlineGfm; | ||
protected static rulesGfm: RulesInlineGfm; | ||
/** | ||
* GFM + Line Breaks Inline Grammar. | ||
*/ | ||
protected static inlineBreaks: RulesInlineBreaks; | ||
protected links: Links; | ||
protected rules: RulesInlineMain; | ||
protected options: MarkedOptions; | ||
protected static rulesBreaks: RulesInlineBreaks; | ||
protected rules: RulesInlineBase | RulesInlinePedantic | RulesInlineGfm | RulesInlineBreaks; | ||
protected renderer: Renderer; | ||
protected inLink: boolean; | ||
protected hasRulesGfm: boolean; | ||
constructor(staticThis: T, links: Links, options?: MarkedOptions, renderer?: Renderer); | ||
protected setRules(): void; | ||
protected static getRulesMain(): RulesInlineMain; | ||
protected static getRulesPedantic(): RulesInlinePedantic; | ||
protected static getRulesGfm(): RulesInlineGfm; | ||
protected static getRulesBreaks(): RulesInlineBreaks; | ||
protected ruleCallbacks: RulesInlineCallback[]; | ||
constructor(staticThis: typeof InlineLexer, links: Links, options?: MarkedOptions, renderer?: Renderer); | ||
/** | ||
@@ -39,2 +33,7 @@ * Static Lexing/Compiling Method. | ||
static output(src: string, links: Links, options: MarkedOptions): string; | ||
protected static getRulesBase(): RulesInlineBase; | ||
protected static getRulesPedantic(): RulesInlinePedantic; | ||
protected static getRulesGfm(): RulesInlineGfm; | ||
protected static getRulesBreaks(): RulesInlineBreaks; | ||
protected setRules(): void; | ||
/** | ||
@@ -47,11 +46,11 @@ * Lexing/Compiling. | ||
*/ | ||
outputLink(execArr: RegExpExecArray, link: Link): string; | ||
protected outputLink(execArr: RegExpExecArray, link: Link): string; | ||
/** | ||
* Smartypants Transformations. | ||
*/ | ||
smartypants(text: string): string; | ||
protected smartypants(text: string): string; | ||
/** | ||
* Mangle Links. | ||
*/ | ||
mangle(text: string): string; | ||
protected mangle(text: string): string; | ||
} |
@@ -13,15 +13,13 @@ "use strict"; | ||
const extend_regexp_1 = require("./extend-regexp"); | ||
const marked_1 = require("./marked"); | ||
const renderer_1 = require("./renderer"); | ||
const marked_1 = require("./marked"); | ||
/** | ||
* Inline Lexer & Compiler. | ||
* | ||
* @todo Remove from constructor reference to current class. | ||
*/ | ||
class InlineLexer { | ||
constructor(staticThis, links, options, renderer) { | ||
constructor(staticThis, links, options = marked_1.Marked.defaults, renderer) { | ||
this.staticThis = staticThis; | ||
this.options = options || marked_1.Marked.defaults; | ||
this.links = links; | ||
this.options = options; | ||
this.renderer = renderer || this.options.renderer || new renderer_1.Renderer(this.options); | ||
this.links = links; | ||
if (!this.links) | ||
@@ -31,26 +29,16 @@ throw new Error(`InlineLexer requires 'links' parameter.`); | ||
} | ||
setRules() { | ||
if (this.options.gfm) { | ||
if (this.options.breaks) { | ||
this.rules = this.staticThis.getRulesBreaks(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
} | ||
} | ||
else if (this.options.pedantic) { | ||
this.rules = this.staticThis.getRulesPedantic(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesMain(); | ||
} | ||
this.hasRulesGfm = this.rules.url !== undefined; | ||
/** | ||
* Static Lexing/Compiling Method. | ||
*/ | ||
static output(src, links, options) { | ||
const inlineLexer = new this(this, links, options); | ||
return inlineLexer.output(src); | ||
} | ||
static getRulesMain() { | ||
if (this.inline) | ||
return this.inline; | ||
static getRulesBase() { | ||
if (this.rulesBase) | ||
return this.rulesBase; | ||
/** | ||
* Inline-Level Grammar. | ||
*/ | ||
const inline = { | ||
const base = { | ||
escape: /^\\([\\`*{}\[\]()#+\-.!_>])/, | ||
@@ -70,15 +58,15 @@ autolink: /^<([^ <>]+(@|:\/)[^ <>]+)>/, | ||
}; | ||
inline.link = new extend_regexp_1.ExtendRegexp(inline.link) | ||
.setGroup('inside', inline._inside) | ||
.setGroup('href', inline._href) | ||
base.link = new extend_regexp_1.ExtendRegexp(base.link) | ||
.setGroup('inside', base._inside) | ||
.setGroup('href', base._href) | ||
.getRegexp(); | ||
inline.reflink = new extend_regexp_1.ExtendRegexp(inline.reflink) | ||
.setGroup('inside', inline._inside) | ||
base.reflink = new extend_regexp_1.ExtendRegexp(base.reflink) | ||
.setGroup('inside', base._inside) | ||
.getRegexp(); | ||
return this.inline = inline; | ||
return this.rulesBase = base; | ||
} | ||
static getRulesPedantic() { | ||
if (this.inlinePedantic) | ||
return this.inlinePedantic; | ||
return this.inlinePedantic = Object.assign({}, this.getRulesMain(), { | ||
if (this.rulesPedantic) | ||
return this.rulesPedantic; | ||
return this.rulesPedantic = Object.assign({}, this.getRulesBase(), { | ||
strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/, | ||
@@ -89,13 +77,13 @@ em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/ | ||
static getRulesGfm() { | ||
if (this.inlineGfm) | ||
return this.inlineGfm; | ||
const inline = this.getRulesMain(); | ||
const escape = new extend_regexp_1.ExtendRegexp(inline.escape) | ||
if (this.rulesGfm) | ||
return this.rulesGfm; | ||
const base = this.getRulesBase(); | ||
const escape = new extend_regexp_1.ExtendRegexp(base.escape) | ||
.setGroup('])', '~|])') | ||
.getRegexp(); | ||
const text = new extend_regexp_1.ExtendRegexp(inline.text) | ||
const text = new extend_regexp_1.ExtendRegexp(base.text) | ||
.setGroup(']|', '~]|') | ||
.setGroup('|', '|https?://|') | ||
.getRegexp(); | ||
return this.inlineGfm = Object.assign({}, inline, { | ||
return this.rulesGfm = Object.assign({}, base, { | ||
escape: escape, | ||
@@ -108,7 +96,7 @@ url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/, | ||
static getRulesBreaks() { | ||
if (this.inlineBreaks) | ||
return this.inlineBreaks; | ||
if (this.rulesBreaks) | ||
return this.rulesBreaks; | ||
const inline = this.getRulesGfm(); | ||
const gfm = this.getRulesGfm(); | ||
return this.inlineBreaks = Object.assign({}, gfm, { | ||
return this.rulesBreaks = Object.assign({}, gfm, { | ||
br: new extend_regexp_1.ExtendRegexp(inline.br).setGroup('{2,}', '*').getRegexp(), | ||
@@ -118,8 +106,18 @@ text: new extend_regexp_1.ExtendRegexp(gfm.text).setGroup('{2,}', '*').getRegexp() | ||
} | ||
/** | ||
* Static Lexing/Compiling Method. | ||
*/ | ||
static output(src, links, options) { | ||
const inlineLexer = new this(this, links, options); | ||
return inlineLexer.output(src); | ||
setRules() { | ||
if (this.options.gfm) { | ||
if (this.options.breaks) { | ||
this.rules = this.staticThis.getRulesBreaks(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesGfm(); | ||
} | ||
} | ||
else if (this.options.pedantic) { | ||
this.rules = this.staticThis.getRulesPedantic(); | ||
} | ||
else { | ||
this.rules = this.staticThis.getRulesBase(); | ||
} | ||
this.hasRulesGfm = this.rules.url !== undefined; | ||
} | ||
@@ -257,3 +255,4 @@ /** | ||
outputLink(execArr, link) { | ||
const href = this.options.escape(link.href), title = link.title ? this.options.escape(link.title) : null; | ||
const href = this.options.escape(link.href); | ||
const title = link.title ? this.options.escape(link.title) : null; | ||
return execArr[0].charAt(0) !== '!' | ||
@@ -260,0 +259,0 @@ ? this.renderer.link(href, title, this.output(execArr[1])) |
@@ -11,3 +11,3 @@ /** | ||
}; | ||
export interface RulesBlockMain { | ||
export interface RulesBlockBase { | ||
newline: RegExp; | ||
@@ -29,5 +29,4 @@ code: RegExp; | ||
item: RegExp; | ||
_tag: string; | ||
} | ||
export interface RulesBlockGfm extends RulesBlockMain { | ||
export interface RulesBlockGfm extends RulesBlockBase { | ||
fences: RegExp; | ||
@@ -66,3 +65,3 @@ } | ||
export interface Token { | ||
type?: TokenType; | ||
type: number | string; | ||
text?: string; | ||
@@ -77,4 +76,5 @@ lang?: string; | ||
escaped?: boolean; | ||
execArr?: RegExpExecArray; | ||
} | ||
export interface RulesInlineMain { | ||
export interface RulesInlineBase { | ||
escape: RegExp; | ||
@@ -94,3 +94,3 @@ autolink: RegExp; | ||
} | ||
export interface RulesInlinePedantic extends RulesInlineMain { | ||
export interface RulesInlinePedantic extends RulesInlineBase { | ||
} | ||
@@ -100,3 +100,3 @@ /** | ||
*/ | ||
export interface RulesInlineGfm extends RulesInlineMain { | ||
export interface RulesInlineGfm extends RulesInlineBase { | ||
url: RegExp; | ||
@@ -152,3 +152,7 @@ del: RegExp; | ||
} | ||
export declare type BlockRuleFunction = (top?: boolean, isBlockQuote?: boolean) => void; | ||
export declare type InlineRuleFunction = (top?: boolean, isBlockQuote?: boolean) => void; | ||
export interface RulesInlineCallback { | ||
condition(): RegExp; | ||
tokenize(execArr: RegExpExecArray): void; | ||
regexp?: RegExp; | ||
} | ||
export declare type SimpleRenderer = (execArr?: RegExpExecArray) => string; |
@@ -1,4 +0,5 @@ | ||
import { MarkedOptions, Token, Links, LexerReturns } from './interfaces'; | ||
import { MarkedOptions, Token, Links, LexerReturns, SimpleRenderer } from './interfaces'; | ||
export declare class Marked { | ||
static defaults: MarkedOptions; | ||
protected static simpleRenderers: SimpleRenderer[]; | ||
/** | ||
@@ -11,2 +12,6 @@ * Merges the default options with options that will be set. | ||
/** | ||
* Setting simple block rule. | ||
*/ | ||
static setBlockRule(regexp: RegExp, renderer: SimpleRenderer): typeof Marked; | ||
/** | ||
* Accepts Markdown text and returns text in HTML format. | ||
@@ -13,0 +18,0 @@ * |
@@ -26,2 +26,10 @@ "use strict"; | ||
/** | ||
* 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 text in HTML format. | ||
@@ -53,3 +61,10 @@ * | ||
static callParser(tokens, links, options) { | ||
return parser_1.Parser.parse(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); | ||
} | ||
} | ||
@@ -65,2 +80,3 @@ static callMe(err) { | ||
Marked.defaults = new interfaces_1.MarkedOptions; | ||
Marked.simpleRenderers = []; | ||
exports.Marked = Marked; |
@@ -1,4 +0,4 @@ | ||
import { MarkedOptions, Token, Links } from './interfaces'; | ||
import { Renderer } from './renderer'; | ||
import { InlineLexer } from './inline-lexer'; | ||
import { MarkedOptions, Token, Links, SimpleRenderer } from './interfaces'; | ||
/** | ||
@@ -8,5 +8,6 @@ * Parsing & Compiling. | ||
export declare class Parser { | ||
simpleRenderers: SimpleRenderer[]; | ||
protected tokens: Token[]; | ||
protected token: Token; | ||
protected inlineLexer: InlineLexer<any>; | ||
protected inlineLexer: InlineLexer; | ||
protected options: MarkedOptions; | ||
@@ -16,7 +17,7 @@ protected renderer: Renderer; | ||
static parse(tokens: Token[], links: Links, options?: MarkedOptions): string; | ||
protected parse(links: Links, tokens: Token[]): string; | ||
parse(links: Links, tokens: Token[]): string; | ||
protected next(): Token; | ||
protected getNextElement(): Token; | ||
protected parseText(): string; | ||
protected tok(): string; | ||
protected tok(): any; | ||
} |
@@ -13,5 +13,5 @@ "use strict"; | ||
const marked_1 = require("./marked"); | ||
const interfaces_1 = require("./interfaces"); | ||
const renderer_1 = require("./renderer"); | ||
const inline_lexer_1 = require("./inline-lexer"); | ||
const interfaces_1 = require("./interfaces"); | ||
/** | ||
@@ -22,2 +22,3 @@ * Parsing & Compiling. | ||
constructor(options) { | ||
this.simpleRenderers = []; | ||
this.tokens = []; | ||
@@ -143,2 +144,11 @@ this.token = null; | ||
} | ||
default: | ||
{ | ||
if (this.simpleRenderers.length) | ||
for (let i = 0; i < this.simpleRenderers.length; i++) { | ||
if (this.token.type == interfaces_1.TokenType.text + i + 1) { | ||
return this.simpleRenderers[i].call(this.renderer, this.token.execArr); | ||
} | ||
} | ||
} | ||
} | ||
@@ -145,0 +155,0 @@ } |
@@ -12,3 +12,3 @@ /** | ||
export declare class Renderer { | ||
private options; | ||
protected options: MarkedOptions; | ||
constructor(options?: MarkedOptions); | ||
@@ -15,0 +15,0 @@ code(code: string, lang?: string, escaped?: boolean): string; |
@@ -5,3 +5,3 @@ { | ||
"author": "Костя Третяк <ktretiak.in.ua@gmail.com>", | ||
"version": "0.0.0-alpha.5", | ||
"version": "0.0.0-alpha.6", | ||
"main": "dist/index", | ||
@@ -46,6 +46,7 @@ "typings": "dist/index", | ||
"test": "node dist-test/index.js", | ||
"compile": "./node_modules/.bin/tsc && ./node_modules/.bin/tsc --project test", | ||
"compile": "./node_modules/.bin/tsc && ./node_modules/.bin/tsc --project test && ./node_modules/.bin/tsc --project examples-usage", | ||
"bench": "node --expose-gc dist-test/benchmarks.js", | ||
"watch": "npm run compile && concurrently './node_modules/.bin/tsc -w' './node_modules/.bin/tsc -w --project test'" | ||
"watch": "npm run compile && concurrently 'tsc -w' 'tsc -w --project test' 'tsc -w --project examples-usage'", | ||
"examples": "node dist-examples/extends-block-lexer.js" | ||
} | ||
} |
202
README.md
@@ -12,2 +12,18 @@ [![Build Status](https://travis-ci.org/KostyaTretyak/marked-ts.svg?branch=master)](https://travis-ci.org/KostyaTretyak/marked-ts) | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Minimal usage](#minimal-usage) | ||
- [Example usage with highlight.js](#example-usage-with-highlightjs) | ||
- [Overriding renderer methods](#overriding-renderer-methods) | ||
- [Example of setting a simple block rule](#example-of-setting-a-simple-block-rule) | ||
- [API](#api) | ||
- [Methods of Marked class and necessary types](#methods-of-marked-class-and-necessary-types) | ||
- [Renderer methods API](#renderer-methods-api) | ||
- [Philosophy behind marked](#philosophy-behind-marked) | ||
- [Benchmarks](#benchmarks) | ||
- [Options for benchmarks](#options-for-benchmarks) | ||
- [Example of usage bench options](#example-of-usage-bench-options) | ||
- [Contribution and License Agreement](#contribution-and-license-agreement) | ||
- [License](#license) | ||
## Install | ||
@@ -21,3 +37,3 @@ | ||
Minimal usage: | ||
### Minimal usage: | ||
@@ -51,2 +67,118 @@ ```js | ||
### Example usage with highlight.js | ||
```bash | ||
npm install highlight.js @types/highlight.js --save | ||
``` | ||
A function to highlight code blocks: | ||
```ts | ||
import { Marked } from 'marked-ts'; | ||
import { highlightAuto } from 'highlight.js'; | ||
let md = '```js\n console.log("hello"); \n```'; | ||
Marked.setOptions({ highlight: code => highlightAuto(code).value }); | ||
console.log(Marked.parse(md)); | ||
``` | ||
### Overriding renderer methods | ||
The renderer option allows you to render tokens in a custom manner. Here is an | ||
example of overriding the default heading token rendering by adding custom head id: | ||
```ts | ||
import { Marked, Renderer, MarkedOptions } from 'marked-ts'; | ||
// Setting some options for Marked. | ||
const markedOptions: MarkedOptions = {}; | ||
const renderer = new Renderer(markedOptions); | ||
// Overriding renderer. | ||
renderer.heading = function (text, level) | ||
{ | ||
const patt = /\s?{([^}]+)}$/; | ||
const link = patt.exec(text); | ||
let linkStr: string; | ||
if(link && link.length && link[1]) | ||
{ | ||
text = text.replace(patt, ''); | ||
linkStr = link[1]; | ||
} | ||
else | ||
{ | ||
linkStr = text.toLocaleLowerCase().replace(/[^\wа-яіїє]+/gi, '-'); | ||
} | ||
return '<h' + level + ' id="' + linkStr + '">' + text + '</h' + level + '>'; | ||
}; | ||
markedOptions.renderer = renderer; | ||
Marked.setOptions(markedOptions); | ||
console.log(Marked.parse('# heading {my-custom-hash}')); | ||
``` | ||
This code will output the following HTML: | ||
```html | ||
<h1 id="my-custom-hash">heading</h1> | ||
``` | ||
### Example of setting a simple block rule | ||
If you need to set simple rules, when you do not need recursiveness or other advanced features, | ||
you can use the `Marked.setBlockRule()` method: | ||
```ts | ||
import { Marked } from 'marked-ts'; | ||
const blockStr = ` | ||
# Example usage with embed block code | ||
@@@ gist | ||
a9dfd77500990871fc58b97fdb57d91f.js | ||
@@@ | ||
@@@ youtube | ||
JgwnkM5WwWE | ||
@@@ | ||
`; | ||
Marked.setBlockRule(/^@@@ *(\w+)\n([\s\S]+?)\n@@@/, function (execArr) { | ||
const channel = execArr[1]; | ||
switch(channel) | ||
{ | ||
case 'youtube': | ||
{ | ||
const id = execArr[2]; | ||
return `<iframe width="420" height="315" src="https://www.youtube.com/embed/${id}"></iframe>\n`; | ||
} | ||
case 'gist': | ||
{ | ||
const id = execArr[2]; | ||
return `<script src="https://gist.github.com/${id}"></script>\n`; | ||
} | ||
} | ||
}); | ||
const html = Marked.parse(blockStr); | ||
console.log(html); | ||
``` | ||
This code output: | ||
```html | ||
<h1 id="example-usage-with-embed-block-code">Example usage with embed block code</h1> | ||
<script src="https://gist.github.com/a9dfd77500990871fc58b97fdb57d91f.js"></script> | ||
<iframe width="420" height="315" src="https://www.youtube.com/embed/JgwnkM5WwWE"></iframe> | ||
``` | ||
## API | ||
@@ -117,66 +249,2 @@ | ||
### Example usage with highlight.js | ||
```bash | ||
npm install highlight.js @types/highlight.js --save | ||
``` | ||
A function to highlight code blocks: | ||
```ts | ||
import { Marked } from 'marked-ts'; | ||
import { highlightAuto } from 'highlight.js'; | ||
let md = '```js\n console.log("hello"); \n```'; | ||
Marked.setOptions({ highlight: code => highlightAuto(code).value }); | ||
console.log(Marked.parse(md)); | ||
``` | ||
#### Overriding renderer methods | ||
The renderer option allows you to render tokens in a custom manner. Here is an | ||
example of overriding the default heading token rendering by adding custom head id: | ||
```ts | ||
import { Marked, Renderer, MarkedOptions } from 'marked-ts'; | ||
// Setting some options for Marked. | ||
const markedOptions: MarkedOptions = {}; | ||
const renderer = new Renderer(markedOptions); | ||
// Overriding renderer. | ||
renderer.heading = function (text, level) | ||
{ | ||
const patt = /\s?{([^}]+)}$/; | ||
const link = patt.exec(text); | ||
let linkStr: string; | ||
if(link && link.length && link[1]) | ||
{ | ||
text = text.replace(patt, ''); | ||
linkStr = link[1]; | ||
} | ||
else | ||
{ | ||
linkStr = text.toLocaleLowerCase().replace(/[^\wа-яіїє]+/gi, '-'); | ||
} | ||
return '<h' + level + ' id="' + linkStr + '">' + text + '</h' + level + '>'; | ||
}; | ||
markedOptions.renderer = renderer; | ||
Marked.setOptions(markedOptions); | ||
console.log(Marked.parse('# heading {my-custom-hash}')); | ||
``` | ||
This code will output the following HTML: | ||
```html | ||
<h1 id="my-custom-hash">heading</h1> | ||
``` | ||
### Renderer methods API | ||
@@ -281,3 +349,3 @@ | ||
### Example of usage bench options | ||
#### Example of usage bench options | ||
@@ -290,3 +358,3 @@ In order for npm passing the parameters, they need to be separated via ` -- `: | ||
### Contribution and License Agreement | ||
## Contribution and License Agreement | ||
@@ -293,0 +361,0 @@ If you contribute code to this project, you are implicitly allowing your code |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68581
374
21