Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked frommarkedjs/marked

A markdown parser and compiler. Built for speed.

License

NotificationsYou must be signed in to change notification settings

Drew-S/marked

 
 

Repository files navigation

A full-featured markdown parser and compiler, written in JavaScript. Builtfor speed.

NPM version

Install

npm install marked --save

Usage

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__.'));

Browser

<!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>

marked(markdownString [,options] [,callback])

markdownString

Type:string

String of markdown source to be compiled.

options

Type:object

Hash of options. Can also be set using themarked.setOptions method as seenabove.

callback

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.

Options

highlight

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));

highlight arguments

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.

renderer

Type:objectDefault:new Renderer()

An object containing functions to render tokens to HTML.

Overriding renderer methods

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>

Block level renderer methods

  • 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'}

Inline level renderer methods

  • 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)

gfm

Type:booleanDefault:true

EnableGitHub flavored markdown.

tables

Type:booleanDefault:true

Enable GFMtables.This option requires thegfm option to be true.

breaks

Type:booleanDefault:false

Enable GFMline breaks.This option requires thegfm option to be true.

pedantic

Type:booleanDefault:false

Conform to obscure parts ofmarkdown.pl as much as possible. Don't fix any ofthe original markdown bugs or poor behavior.

sanitize

Type:booleanDefault:false

Sanitize the output. Ignore any HTML that has been input.

smartLists

Type:booleanDefault:true

Use smarter list behavior than the original markdown. May eventually bedefault with the old behavior moved intopedantic.

smartypants

Type:booleanDefault:false

Use "smart" typograhic punctuation for things like quotes and dashes.

Access to lexer and parser

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);

CLI

$ marked -o hello.htmlhello world^D$ cat hello.html<p>hello world</p>

Philosophy behind marked

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.

Benchmarks

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.

Pro level

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: {} ]

Running Tests & Contributing

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

Contribution and License Agreement

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>

License

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

Packages

No packages published

Languages

  • HTML71.6%
  • JavaScript28.3%
  • Makefile0.1%

[8]ページ先頭

©2009-2025 Movatter.jp