marked-ts
A full-featured markdown parser and compiler, written in TypeScript.
This is fork of popular library marked
from this commit
(Merge pull request #961 from chjj/release-0.3.7, Dec 1, 2017).
Table of contents
Install
npm install marked-ts --save
Usage
Minimal usage:
; console;// Outputs: I am using <strong>markdown</strong>.
Example setting options with default values:
; MarkedsetOptions renderer: gfm: true tables: true breaks: false pedantic: false sanitize: false smartLists: true smartypants: false; console;
Example usage with highlight.js
npm install highlight.js @types/highlight.js --save
A function to highlight code blocks:
;; Marked.setOptions;;console.logMarked.parsemd;
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:
; Marked.setOptions; console.logMarked.parse'# heading {my-custom-hash}';
This code will output the following HTML:
heading
See also Renderer methods API.
Example of setting a simple block rule
If you do not need recursiveness or checks some conditions before execute a regular expression, you can use the
Marked.setBlockRule( regexp[, callback] )
method, which takes a regular expression as the first argument,
and returns result regexp.exec(string)
to callback(execArr)
, which can be passed as a second argument.
In regular expression very important adding symbol ^
from start. You should do this anyway.
; /** * KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web. */; Marked.setBlockRule/^@@@ *\n\n@@@/, ; ; ; console.loghtml;
API
Methods of Marked class and necessary types
/** * 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()`. * * Can also be set using the `Marked.setOptions` method as seen above. */static parsesrc: string, options?: MarkedOptions: string; /** * 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 debugsrc: string, options?: MarkedOptions: ; /** * Merges the default options with options that will be set. * * @param options Hash of options. */static setOptionsoptions: MarkedOptions: this; // This class also using as an interface.
Renderer methods API
//*** Block level renderer methods. *** codecode: string, lang?: string, escaped?: boolean: string; blockquotequote: string: string; htmlhtml: string: string; headingtext: string, level: number, raw: string: string; hr: string; listbody: string, ordered?: boolean: string; listitemtext: string: string; paragraphtext: string: string; tableheader: string, body: string: string; tablerowcontent: string: string; tablecellcontent: string, flags: : string; //*** Inline level renderer methods. *** strongtext: string: string; emtext: string: string; codespantext: string: string; br: string; deltext: string: string; linkhref: string, title: string, text: string: string; imagehref: string, title: string, text: string: string; texttext: string: string;
Benchmarks
node v8.9.x
git clone https://github.com/KostyaTretyak/marked-ts.gitcd marked-tsnpm installnpm run compilenpm run bench
By default, these benchmarks run the entire markdown test suite once. The test suite includes every markdown feature, it doesn't cater to specific aspects.
Lib | Load lib, ms | Init lib, ms | Bench work, ms | Total, ms | Memory usage, KB |
---|---|---|---|---|---|
marked-ts v1.0.0-beta.4 | 8 | 5 | 98 | 111 | 8 679 |
marked v0.3.12 | 4 | 2 | 106 | 112 | 9 323 |
remarkable v1.7.1 | 36 | 6 | 174 | 216 | 15 356 |
commonmark v0.28.1 | 35 | 3 | 186 | 224 | 24 749 |
markdown-it v8.4.0 | 29 | 10 | 227 | 266 | 18 890 |
showdown v1.8.6 | 10 | 14 | 353 | 377 | 36 833 |
markdown v0.5.0 | 4 | 3 | 314 | 321 | 22 664 |
Options for benchmarks
-l, --length Approximate string length in kilobytes. Default ~ 300 KB.-t, --times Number of runs this bench. Default - 1 times.
Test files are accumulated in one file. If you specify, for example, --length 100
the first file will be taken, checked whether it is longer than 100 kilobytes,
and if no - it will be attached to the next one and checked its length, and so on.
Example of usage bench options
In order for npm passing the parameters, they need to be separated via --
:
npm run bench -- -l 500 -t 1
Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. </legalese>
License
Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)
Copyright (c) 2018, Костя Третяк. (MIT License)
See LICENSE for more info.