[marked] add missing typings, add more specific typings, lint (#18327)

This commit is contained in:
Dimitri Benin
2017-08-05 02:10:16 +02:00
committed by Sheetal Nandi
parent 4bb9d6ce15
commit 1d7334ebed
3 changed files with 263 additions and 142 deletions

View File

@@ -1,9 +1,38 @@
// Type definitions for Marked
// Type definitions for Marked 0.3
// Project: https://github.com/chjj/marked
// Definitions by: William Orr <https://github.com/worr>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface MarkedStatic {
export as namespace marked;
export = marked;
/**
* Compiles markdown to HTML.
*
* @param src String of markdown source to be compiled
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
declare function marked(src: string, callback: (error: any | undefined, parseResult: string) => void): string;
/**
* Compiles markdown to HTML.
*
* @param src String of markdown source to be compiled
* @param options Hash of options
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
declare function marked(src: string, options?: marked.MarkedOptions, callback?: (error: any | undefined, parseResult: string) => void): string;
declare namespace marked {
/**
* @param src String of markdown source to be compiled
* @param options Hash of options
*/
function lexer(src: string, options?: MarkedOptions): TokensList;
/**
* Compiles markdown to HTML.
*
@@ -11,7 +40,7 @@ interface MarkedStatic {
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
(src: string, callback: Function): string;
function parse(src: string, callback: (error: any | undefined, parseResult: string) => void): string;
/**
* Compiles markdown to HTML.
@@ -21,144 +50,232 @@ interface MarkedStatic {
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
(src: string, options?: MarkedOptions, callback?: Function): string;
function parse(src: string, options?: MarkedOptions, callback?: (error: any | undefined, parseResult: string) => void): string;
/**
* @param src String of markdown source to be compiled
* @param src Tokenized source as array of tokens
* @param options Hash of options
*/
lexer(src: string, options?: MarkedOptions): any[];
/**
* Compiles markdown to HTML.
*
* @param src String of markdown source to be compiled
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
parse(src: string, callback: Function): string;
/**
* Compiles markdown to HTML.
*
* @param src String of markdown source to be compiled
* @param options Hash of options
* @param callback Function called when the markdownString has been fully parsed when using async highlighting
* @return String of compiled HTML
*/
parse(src: string, options?: MarkedOptions, callback?: Function): string;
/**
* @param options Hash of options
*/
parser(src: any[], options?: MarkedOptions): string;
function parser(src: TokensList, options?: MarkedOptions): string;
/**
* Sets the default options.
*
* @param options Hash of options
*/
setOptions(options: MarkedOptions): MarkedStatic;
Renderer: {
new(): MarkedRenderer;
function setOptions(options: MarkedOptions): typeof marked;
class Renderer {
constructor(options?: MarkedOptions);
code(code: string, language: string, isEscaped: boolean): string;
blockquote(quote: string): string;
html(html: string): string;
heading(text: string, level: number, raw: string): string;
hr(): string;
list(body: string, ordered: boolean): string;
listitem(text: string): string;
paragraph(text: string): string;
table(header: string, body: string): string;
tablerow(content: string): string;
tablecell(content: string, flags: {
header: boolean;
align: 'center' | 'left' | 'right' | null;
}): string;
strong(text: string): string;
em(text: string): string;
codespan(code: string): string;
br(): string;
del(text: string): string;
link(href: string, title: string, text: string): string;
image(href: string, title: string, text: string): string;
text(text: string): string;
}
Parser: {
new(options: MarkedOptions): MarkedParser;
class Lexer {
rules: Rules;
tokens: TokensList;
constructor(options?: MarkedOptions);
lex(src: string): TokensList;
}
interface Rules {
[ruleName: string]: RegExp | Rules;
}
type TokensList = Token[] & {
links: {
[key: string]: { href: string; title: string; }
}
};
type Token =
Tokens.Space
| Tokens.Code
| Tokens.Heading
| Tokens.Table
| Tokens.Hr
| Tokens.BlockquoteStart
| Tokens.BlockquoteEnd
| Tokens.ListStart
| Tokens.LooseItemStart
| Tokens.ListItemStart
| Tokens.ListItemEnd
| Tokens.ListEnd
| Tokens.Paragraph
| Tokens.HTML
| Tokens.Text;
namespace Tokens {
interface Space {
type: 'space';
}
interface Code {
type: 'code';
lang?: string;
text: string;
}
interface Heading {
type: 'heading';
depth: number;
text: string;
}
interface Table {
type: 'table';
header: string[];
align: Array<'center' | 'left' | 'right' | null>;
cells: string[][];
}
interface Hr {
type: 'hr';
}
interface BlockquoteStart {
type: 'blockquote_start';
}
interface BlockquoteEnd {
type: 'blockquote_end';
}
interface ListStart {
type: 'list_start';
ordered: boolean;
}
interface LooseItemStart {
type: 'loose_item_start';
}
interface ListItemStart {
type: 'list_item_start';
}
interface ListItemEnd {
type: 'list_item_end';
}
interface ListEnd {
type: 'list_end';
}
interface Paragraph {
type: 'paragraph';
pre?: boolean;
text: string;
}
interface HTML {
type: 'html';
pre: boolean;
text: string;
}
interface Text {
type: 'text';
text: string;
}
}
interface MarkedOptions {
/**
* Type: object Default: new Renderer()
*
* An object containing functions to render tokens to HTML.
*/
renderer?: Renderer;
/**
* Enable GitHub flavored markdown.
*/
gfm?: boolean;
/**
* Enable GFM tables. This option requires the gfm option to be true.
*/
tables?: boolean;
/**
* Enable GFM line breaks. This option requires the gfm option to be true.
*/
breaks?: boolean;
/**
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
*/
pedantic?: boolean;
/**
* Sanitize the output. Ignore any HTML that has been input.
*/
sanitize?: boolean;
/**
* Optionally sanitize found HTML with a sanitizer function.
*/
sanitizer?(html: string): string;
/**
* Mangle autolinks (<email@domain.com>).
*/
mangle?: boolean;
/**
* Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
*/
smartLists?: boolean;
/**
* Shows an HTML error message when rendering fails.
*/
silent?: boolean;
/**
* A function to highlight code blocks. The function takes three arguments: code, lang, and callback.
*/
highlight?(code: string, lang: string, callback?: (error: any | undefined, code: string) => void): string;
/**
* Set the prefix for code block classes.
*/
langPrefix?: string;
/**
* Use "smart" typograhic punctuation for things like quotes and dashes.
*/
smartypants?: boolean;
/**
* Set the prefix for header tag ids.
*/
headerPrefix?: string;
/**
* Generate closing slash for self-closing tags (<br/> instead of <br>)
*/
xhtml?: boolean;
}
}
interface MarkedRenderer {
code(code: string, language: string): string;
blockquote(quote: string): string;
html(html: string): string;
heading(text: string, level: number, raw: string): string;
hr(): string;
list(body: string, ordered: boolean): string;
listitem(text: string): string;
paragraph(text: string): string;
table(header: string, body: string): string;
tablerow(content: string): string;
tablecell(content: string, flags: {
header: boolean,
align: string
}): string;
strong(text: string): string;
em(text: string): string;
codespan(code: string): string;
br(): string;
del(text: string): string;
link(href: string, title: string, text: string): string;
image(href: string, title: string, text: string): string;
text(text: string): string;
}
interface MarkedParser {
parse(source: any[]): string
}
interface MarkedOptions {
/**
* Type: object Default: new Renderer()
*
* An object containing functions to render tokens to HTML.
*/
renderer?: MarkedRenderer;
/**
* Enable GitHub flavored markdown.
*/
gfm?: boolean;
/**
* Enable GFM tables. This option requires the gfm option to be true.
*/
tables?: boolean;
/**
* Enable GFM line breaks. This option requires the gfm option to be true.
*/
breaks?: boolean;
/**
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
*/
pedantic?: boolean;
/**
* Sanitize the output. Ignore any HTML that has been input.
*/
sanitize?: boolean;
/**
* Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
*/
smartLists?: boolean;
/**
* Shows an HTML error message when rendering fails.
*/
silent?: boolean;
/**
* A function to highlight code blocks. The function takes three arguments: code, lang, and callback.
*/
highlight? (code: string, lang: string, callback?: Function): string;
/**
* Set the prefix for code block classes.
*/
langPrefix?: string;
/**
* Use "smart" typograhic punctuation for things like quotes and dashes.
*/
smartypants?: boolean;
}
declare module "marked" {
export = marked;
}
declare var marked: MarkedStatic;

View File

@@ -1,8 +1,6 @@
import * as marked from 'marked';
var options: MarkedOptions = {
const options: marked.MarkedOptions = {
gfm: true,
tables: true,
breaks: false,
@@ -10,8 +8,8 @@ var options: MarkedOptions = {
sanitize: true,
smartLists: true,
silent: false,
highlight: function (code: string, lang: string) {
return '';
highlight(code: string, lang: string) {
return '';
},
langPrefix: 'lang-',
smartypants: false,
@@ -19,10 +17,10 @@ var options: MarkedOptions = {
};
function callback() {
console.log('callback called');
console.log('callback called');
}
marked.setOptions(options);
const myOldMarked: typeof marked = marked.setOptions(options);
console.log(marked('i am using __markdown__.'));
console.log(marked('i am using __markdown__.', options));
@@ -34,11 +32,16 @@ console.log(marked.parse('i am using __markdown__.', options));
console.log(marked.parse('i am using __markdown__.', callback));
console.log(marked.parse('i am using __markdown__.', options, callback));
var text = 'something';
var tokens = marked.lexer(text, options);
const text = 'something';
const tokens: marked.TokensList = marked.lexer(text, options);
console.log(marked.parser(tokens));
var renderer = new marked.Renderer();
renderer.heading = function(text, level, raw) {
const lexer = new marked.Lexer(options);
const tokens2 = lexer.lex(text);
console.log(tokens2);
const re: RegExp | marked.Rules = lexer.rules['code'];
const renderer = new marked.Renderer();
renderer.heading = (text, level, raw) => {
return text + level.toString() + raw;
};

1
types/marked/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }