- Notifications
You must be signed in to change notification settings - Fork0
A markdown parser and compiler. Built for speed.
License
Drew-S/marked
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A full-featured markdown parser and compiler, written in JavaScript. Builtfor speed.
npm install marked --save
Minimal usage:
varmarked=require('marked');console.log(marked('I am using __markdown__.'));// Outputs: <p>I am using <strong>markdown</strong>.</p>
Example setting options with default values:
varmarked=require('marked');marked.setOptions({renderer:newmarked.Renderer(),gfm:true,tables:true,breaks:false,pedantic:false,sanitize:false,smartLists:true,smartypants:false});console.log(marked('I am using __markdown__.'));
<!doctype html><html><head><metacharset="utf-8"/><title>Marked in the browser</title><scriptsrc="lib/marked.js"></script></head><body><divid="content"></div><script>document.getElementById('content').innerHTML=marked('# Marked in browser\n\nRendered by **marked**.');</script></body></html>
Type:string
String of markdown source to be compiled.
Type:object
Hash of options. Can also be set using themarked.setOptions
method as seenabove.
Type:function
Function called when themarkdownString
has been fully parsed when usingasync highlighting. If theoptions
argument is omitted, this can be used asthe second argument.
Type:function
A function to highlight code blocks. The first example below uses async highlighting withnode-pygmentize-bundled, and the second is a synchronous example usinghighlight.js:
varmarked=require('marked');varmarkdownString='```js\n console.log("hello"); \n```';// Async highlighting with pygmentize-bundledmarked.setOptions({highlight:function(code,lang,callback){require('pygmentize-bundled')({lang:lang,format:'html'},code,function(err,result){callback(err,result.toString());});}});// Using async version of markedmarked(markdownString,function(err,content){if(err)throwerr;console.log(content);});// Synchronous highlighting with highlight.jsmarked.setOptions({highlight:function(code){returnrequire('highlight.js').highlightAuto(code).value;}});console.log(marked(markdownString));
code
Type:string
The section of code to pass to the highlighter.
lang
Type:string
The programming language specified in the code block.
callback
Type:function
The callback function to call when using an async highlighter.
Type:object
Default:new Renderer()
An object containing functions to render tokens to HTML.
The renderer option allows you to render tokens in a custom manner. Here is anexample of overriding the default heading token rendering by adding an embedded anchor tag like on GitHub:
varmarked=require('marked');varrenderer=newmarked.Renderer();renderer.heading=function(text,level){varescapedText=text.toLowerCase().replace(/[^\w]+/g,'-');return'<h'+level+'><a name="'+escapedText+'" href="#'+escapedText+'"><span></span></a>'+text+'</h'+level+'>';},console.log(marked('# heading+',{renderer:renderer}));
This code will output the following HTML:
<h1><aname="heading-"class="anchor"href="#heading-"><spanclass="header-link"></span></a> heading+</h1>
- code(string code,string language)
- blockquote(string quote)
- html(string html)
- heading(string text,number level)
- hr()
- list(string body,boolean ordered)
- listitem(string text)
- paragraph(string text)
- table(string header,string body)
- tablerow(string content)
- tablecell(string content,object flags)
flags
has the following properties:
{header:true||false,align:'center'||'left'||'right'}
- strong(string text)
- em(string text)
- codespan(string code)
- br()
- del(string text)
- link(string href,string title,string text)
- image(string href,string title,string text)
Type:boolean
Default:true
EnableGitHub flavored markdown.
Type:boolean
Default:true
Enable GFMtables.This option requires thegfm
option to be true.
Type:boolean
Default:false
Enable GFMline breaks.This option requires thegfm
option to be true.
Type:boolean
Default:false
Conform to obscure parts ofmarkdown.pl
as much as possible. Don't fix any ofthe original markdown bugs or poor behavior.
Type:boolean
Default:false
Sanitize the output. Ignore any HTML that has been input.
Type:boolean
Default:true
Use smarter list behavior than the original markdown. May eventually bedefault with the old behavior moved intopedantic
.
Type:boolean
Default:false
Use "smart" typograhic punctuation for things like quotes and dashes.
You also have direct access to the lexer and parser if you so desire.
vartokens=marked.lexer(text,options);console.log(marked.parser(tokens));
varlexer=newmarked.Lexer(options);vartokens=lexer.lex(text);console.log(tokens);console.log(lexer.rules);
$ marked -o hello.htmlhello world^D$ cat hello.html<p>hello world</p>
The point of marked was to create a markdown compiler where it was possible tofrequently parse huge chunks of markdown without having to worry aboutcaching the compiled output somehow...or blocking for an unnecesarily long time.
marked is very concise and still implements all markdown features. It is alsonow fully compatible with the client-side.
marked more or less passes the official markdown test suite in itsentirety. This is important because a surprising number of markdown compilerscannot pass more than a few tests. It was very difficult to get marked ascompliant as it is. It could have cut corners in several areas for the sakeof performance, but did not in order to be exactly what you expect in termsof a markdown rendering. In fact, this is why marked could be considered at adisadvantage in the benchmarks above.
Along with implementing every markdown feature, marked also implementsGFMfeatures.
node v0.8.x
$ nodetest --benchmarked completedin 3411ms.marked (gfm) completedin 3727ms.marked (pedantic) completedin 3201ms.robotskirt completedin 808ms.showdown (reuse converter) completedin 11954ms.showdown (new converter) completedin 17774ms.markdown-js completedin 17191ms.
Marked is now faster than Discount, which is written in C.
For those feeling skeptical: These benchmarks run the entire markdown test suite 1000 times. The test suite tests every feature. It doesn't cater to specific aspects.
You also have direct access to the lexer and parser if you so desire.
vartokens=marked.lexer(text,options);console.log(marked.parser(tokens));
varlexer=newmarked.Lexer(options);vartokens=lexer.lex(text);console.log(tokens);console.log(lexer.rules);
$ node> require('marked').lexer('> i am using marked.')[ { type:'blockquote_start' }, { type:'paragraph', text:'i am using marked.' }, { type:'blockquote_end' }, links: {} ]
If you want to submit a pull request, make sure your changes pass the testsuite. If you're adding a new feature, be sure to add your own test.
The marked test suite is set up slightly strangely:test/new
is for all teststhat are not part of the original markdown.pl test suite (this is where yourtest should go if you make one).test/original
is only for the originalmarkdown.pl tests.test/tests
houses both types of tests after they have beencombined and moved/generated by runningnode test --fix
ormarked --test --fix
.
In other words, if you have a test to add, add it totest/new/
and thenregenerate the tests withnode test --fix
. Commit the result. If your testuses a certain feature, for example, maybe it assumes GFM isnot enabled, youcan add.nogfm
to the filename. So,my-test.text
becomesmy-test.nogfm.text
. You can do this with any marked option. Say you wantline breaks and smartypants enabled, your filename should be:my-test.breaks.smartypants.text
.
To run the tests:
cd marked/nodetest
If you contribute code to this project, you are implicitly allowing your codeto be distributed under the MIT license. You are also implicitly verifying thatall code is your original work.</legalese>
Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)
See LICENSE for more info.
About
A markdown parser and compiler. Built for speed.
Resources
License
Stars
Watchers
Forks
Packages0
Languages
- HTML71.6%
- JavaScript28.3%
- Makefile0.1%