diff --git a/types/marked/index.d.ts b/types/marked/index.d.ts index 57f7f115f3..4623f838ca 100644 --- a/types/marked/index.d.ts +++ b/types/marked/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Marked 0.7 +// Type definitions for Marked 1.1 // Project: https://github.com/markedjs/marked, https://marked.js.org // Definitions by: William Orr // BendingBender @@ -6,6 +6,7 @@ // Mike Wickett // Hitomi Hatsukaze // Ezra Celli +// Romain LE BARO // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export as namespace marked; @@ -99,6 +100,14 @@ declare namespace marked { */ function getDefaults(): MarkedOptions; + function walkTokens(tokens: TokensList, callback: (token: Token) => void): typeof marked; + + /** + * Use Extension + * @param Renderer + */ + function use(options: MarkedOptions): void; + class InlineLexer { constructor(links: string[], options?: MarkedOptions); options: MarkedOptions; @@ -114,6 +123,37 @@ declare namespace marked { mangle(text: string): string; } + class Tokenizer { + constructor(options?: MarkedOptions); + options: MarkedOptions; + space(src: string): Tokens.Space; + code(src: string, token: Token): Tokens.Code; + fences(src: string): Tokens.Code; + heading(src: string): Tokens.Heading; + nptable(src: string): Tokens.Table; + hr(src: string): Tokens.Hr; + blockquote(src: string): Tokens.Blockquote; + list(src: string): Tokens.List; + html(src: string): Tokens.HTML; + def(src: string): Tokens.Def; + table(src: string): Tokens.Table; + lheading(src: string): Tokens.Heading; + paragraph(src: string): Tokens.Paragraph; + text(src: string): Tokens.Text; + escape(src: string): Tokens.Escape; + tag(src: string, inLink: boolean, inRawBlock: boolean): Tokens.Tag; + link(src: string): Tokens.Image | Tokens.Link; + reflink(src: string, links: Tokens.Link[] |Tokens.Image[]): Tokens.Link |Tokens.Image | Tokens.Text; + strong(src: string): Tokens.Strong; + em(src: string): Tokens.Em; + codespan(src: string): Tokens.Codespan; + br(src: string): Tokens.Br; + del(src: string): Tokens.Del; + autolink(src: string, mangle: (cap: string) => string): Tokens.Link; + url(src: string, mangle: (cap: string) => string): Tokens.Link; + inlineText(src: string, inRawBlock: boolean, smartypants: (cap: string) => string): Tokens.Text; + } + class Renderer { constructor(options?: MarkedOptions); options: MarkedOptions; @@ -151,6 +191,7 @@ declare namespace marked { link(href: string | null, title: string | null, text: string): string; image(href: string | null, title: string | null, text: string): string; br(): string; + html(text: string): string; } class Parser { @@ -177,6 +218,7 @@ declare namespace marked { static lex(src: TokensList, options?: MarkedOptions): TokensList; lex(src: string): TokensList; token(src: string, top: boolean): TokensList; + inline(tokens: TokensList): TokensList; } class Slugger { @@ -200,24 +242,34 @@ declare namespace marked { | Tokens.Heading | Tokens.Table | Tokens.Hr + | Tokens.Blockquote | Tokens.BlockquoteStart | Tokens.BlockquoteEnd - | Tokens.ListStart - | Tokens.LooseItemStart - | Tokens.ListItemStart - | Tokens.ListItemEnd - | Tokens.ListEnd + | Tokens.List + | Tokens.ListItem | Tokens.Paragraph | Tokens.HTML - | Tokens.Text; + | Tokens.Text + | Tokens.Def + | Tokens.Escape + | Tokens.Tag + | Tokens.Image + | Tokens.Link + | Tokens.Strong + | Tokens.Em + | Tokens.Codespan + | Tokens.Br + | Tokens.Del; namespace Tokens { interface Space { type: 'space'; + raw: string; } interface Code { type: 'code'; + raw: string; codeBlockStyle?: 'indented'; lang?: string; text: string; @@ -225,12 +277,14 @@ declare namespace marked { interface Heading { type: 'heading'; + raw: string; depth: number; text: string; } interface Table { type: 'table'; + raw: string; header: string[]; align: Array<'center' | 'left' | 'right' | null>; cells: string[][]; @@ -238,53 +292,128 @@ declare namespace marked { interface Hr { type: 'hr'; + raw: string; + } + + interface Blockquote { + type: 'blockquote'; + raw: string; + text: string; } interface BlockquoteStart { type: 'blockquote_start'; + raw: string; } interface BlockquoteEnd { type: 'blockquote_end'; + raw: string; } - interface ListStart { + interface List { type: 'list_start'; + raw: string; ordered: boolean; + start: boolean; + loose: boolean; + items: ListItem[]; } - interface LooseItemStart { - type: 'loose_item_start'; - } - - interface ListItemStart { - type: 'list_item_start'; - } - - interface ListItemEnd { - type: 'list_item_end'; - } - - interface ListEnd { - type: 'list_end'; + interface ListItem { + type: 'list_item'; + raw: string; + task: boolean; + checked: boolean; + loose: boolean; + text: string; } interface Paragraph { type: 'paragraph'; + raw: string; pre?: boolean; text: string; } interface HTML { type: 'html'; + raw: string; pre: boolean; text: string; } interface Text { type: 'text'; + raw: string; text: string; } + + interface Def { + raw: string; + href: string; + title: string; + } + + interface Escape { + type: 'escape'; + raw: string; + text: string; + } + + interface Tag { + type: 'text' | 'html'; + raw: string; + inLink: boolean; + inRawBlock: boolean; + text: string; + } + + interface Link { + type: 'link'; + raw: string; + href: string; + title: string; + text: string; + tokens?: Text[]; + } + + interface Image { + type: 'image'; + raw: string; + href: string; + title: string; + text: string; + } + + interface Strong { + type: 'strong'; + raw: string; + text: string; + } + + interface Em { + type: 'em'; + raw: string; + text: string; + } + + interface Codespan { + type: 'codespan'; + raw: string; + text: string; + } + + interface Br { + type: 'br'; + raw: string; + } + + interface Del { + type: 'del'; + raw: string; + text: string; + } } interface MarkedOptions { @@ -368,6 +497,18 @@ declare namespace marked { */ smartypants?: boolean; + /** + * The tokenizer defines how to turn markdown text into tokens. + */ + tokenizer?: Tokenizer; + + /** + * The walkTokens function gets called with every token. + * Child tokens are called before moving on to sibling tokens. + * Each token is passed by reference so updates are persisted when passed to the parser. + * The return value of the function is ignored. + */ + walkTokens?: (tokens: TokensList, callback: (token: Token) => void) => any; /** * Generate closing slash for self-closing tags (
instead of
) */ diff --git a/types/marked/marked-tests.ts b/types/marked/marked-tests.ts index 747010c941..a6ce23e275 100644 --- a/types/marked/marked-tests.ts +++ b/types/marked/marked-tests.ts @@ -13,7 +13,9 @@ let options: marked.MarkedOptions = { }, langPrefix: 'lang-', smartypants: false, + tokenizer: new marked.Tokenizer(), renderer: new marked.Renderer(), + walkTokens: (tokens: marked.TokensList, callback: (token: marked.Token) => void) => {} }; options.highlight = (code: string, lang: string, callback: (error: any | undefined, code?: string) => void) => { @@ -49,6 +51,8 @@ console.log(marked.parser(tokens)); const lexer = new marked.Lexer(options); const tokens2 = lexer.lex(text); console.log(tokens2); +const tokens3 = lexer.inline(tokens); +console.log(tokens3); const re: RegExp | marked.Rules = marked.Lexer.rules['code']; console.log(lexer.token(text, true)); const lexerOptions: marked.MarkedOptions = lexer.options; @@ -81,3 +85,5 @@ console.log(inlineLexer.output('http://')); console.log(marked.InlineLexer.output('http://', links)); console.log(marked.InlineLexer.rules); const inlineLexerOptions: marked.MarkedOptions = inlineLexer.options; + +marked.use({ renderer });