GithubHelp home page GithubHelp logo

markedjs / marked Goto Github PK

View Code? Open in Web Editor NEW
31.9K 387.0 3.4K 9.66 MB

A markdown parser and compiler. Built for speed.

Home Page: https://marked.js.org

License: Other

JavaScript 34.80% HTML 30.99% TypeScript 34.21%
markdown compiler parser commonmark gfm hacktoberfest

marked's Introduction

Marked

npm gzip size install size downloads github actions snyk

  • ⚡ built for speed
  • ⬇️ low-level compiler for parsing markdown without caching or blocking for long periods of time
  • ⚖️ light-weight while implementing all markdown features from the supported flavors & specifications
  • 🌐 works in a browser, on a server, or from a command line interface (CLI)

Demo

Checkout the demo page to see marked in action ⛹️

Docs

Our documentation pages are also rendered using marked 💯

Also read about:

Compatibility

Node.js: Only current and LTS Node.js versions are supported. End of life Node.js versions may become incompatible with Marked at any point in time.

Browser: Not IE11 :)

Installation

CLI:

npm install -g marked

In-browser:

npm install marked

Usage

Warning: 🚨 Marked does not sanitize the output HTML. Please use a sanitize library, like DOMPurify (recommended), sanitize-html or insane on the output HTML! 🚨

DOMPurify.sanitize(marked.parse(`<img src="x" onerror="alert('not happening')">`));

CLI

# Example with stdin input
$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>
# Print all options
$ marked --help

Browser

<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
</head>
<body>
  <div id="content"></div>
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  <script>
    document.getElementById('content').innerHTML =
      marked.parse('# Marked in the browser\n\nRendered by **marked**.');
  </script>
</body>
</html>

or import esm module

<script type="module">
  import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
  document.getElementById('content').innerHTML =
    marked.parse('# Marked in the browser\n\nRendered by **marked**.');
</script>

License

Copyright (c) 2011-2022, Christopher Jeffrey. (MIT License)

marked's People

Contributors

aprotim avatar barrywoolgar avatar benmccann avatar bent10 avatar bzwheeler avatar calculuschild avatar chjj avatar chriswren avatar davisjam avatar dependabot[bot] avatar feder1co5oave avatar flouc001 avatar joshbruce avatar jun-sheaf avatar kitsonk avatar koczkatamas avatar kostyatretyak avatar lepture avatar martii avatar matt- avatar mccraveiro avatar mithgol avatar paulroub avatar scrum avatar semantic-release-bot avatar styfle avatar trott avatar uzitech avatar vsemozhetbyt avatar x13machine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

marked's Issues

Arguably wrong treatment of code in link

% ../marked/bin/marked
[the `]` character](/url)
<p>[the <code>]</code> character](/url)
</p>

Markdown.pl's behavior is more intuitive:

% Markdown.pl 
[the `]` character](/url)
<p><a href="/url">the <code>]</code> character</a></p>

sequence of <em>'s is mis-rendered in some cases.

by itself,

#### *expression<sub>1</sub>* *expression<sub>2</sub>* ... *expression<sub>n</sub>*

renders correctly as

<h4><em>expression<sub>1</sub></em> <em>expression<sub>2</sub></em> ... <em>expression<sub>n</sub></em></h4>

but in the context of https://raw.github.com/dmajda/pegjs/master/README.md renders as

<h4><em>expression<sub>1</sub></em> / <em>expression<sub>2</sub></em> / ... / <em>expression<sub>n</sub></em></h4>

maybe a parser issue?
thanks!

Accessing the inline lexer

It would be great if you somehow could access the inline lexer.

One example where this would be useful is to transform relative urls to absolute.

for token in tokens
  if token.type is 'link' and is_relative(token.href)
    token.href = base + token.href

Mark tables as "table" instead of "paragraph" at lexer?

Hi,

Would it be possible for you to mark tables using "table" instead of "paragraph" at the lexer? This would make my work a lot easier. I use your lexer to apply some custom transformations to the source and then output as HTML using your parser. Having tables marked properly would make my life a lot easier. :)

Implement markdown extra features

Thanks for the excellent md parser!

There are a bunch of various extensions to markdown
eg
http://freewisdom.org/projects/python-markdown/Extra
http://michelf.com/projects/php-markdown/extra/
http://maruku.rubyforge.org/proposal.html

While not all are exactly critical, things like super/subscript, tables, definition lists, abbreviations can go a long way to make writing md documents easier.

Also simple transformations (like texttile http://rpc.textpattern.com/help/?item=intro) can make the text much more readable
For example
... -> …
' ' -> ‘ ’
" " -> “ ”
-- -> –
(c) -> ©
(r) -> ®
and so on .

Ideally the goal should be to never write pure html for any typographical feature.

Marked doesn't work with literal newlines

Re: OscarGodson/EpicEditor#75

Think i figured it out. Marked doesn't know what to do with literal \n like this:

marked('hello  \
world');

I think this is in fact a bug with Marked since in this example, native JSON.parse will convert that and to get it to work with Marked we'd have to go and add \n (the text, not literal) in place of it. What do think?

GFM Tables

I.e.

First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell

becomes

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

Marked crash Firefox (Windows XP)

I've been using marked for a while, but today I was doing cross-platform test and I noticed that Firefox crashed when marked tried to parse my *.md file. Firefox just crash. I test it in chrome (still on windows XP) and it works fine. Also I test it on Firefox (Windows 7) and works fine. I don't know what is the main problem in here.

ps. Firefox v6.0

Support Strikethrough?

Any chance you could add support for strikethrough? I know it's not in the official Markdown spec but it's something I need for my application.

Add newline between tokens

I see on line 506, in parse(), you separate tokens with a space which makes viewing the rendered HTML source really hard to read. If you could change that single space to a newline then it makes all the difference and is no more or less efficient in bandwidth as the resulting filesize is the same.

- return out.join(' ');
+ return out.join('\n');

Speed?

Curious about how this compares to
https://github.com/visionmedia/node-discount

Either way, I like yours because I was able to install it, no C-compiling, yay!

Also, I like that GFM is on your roadmap :)
(Hopefully you can do the newer version that they use on the site, but if not that's cool too).

Cheers and thanks for working code!
D

Full syntax for code spans not implemented

Read the syntax document's explanation of code spans carefully - it's a bit more complicated than marked seems to assume.

% Markdown.pl 
````` hi ther `` ok ``` `````
<p><code>hi ther `` ok ```</code></p>

% ../marked/bin/marked
````` hi ther `` ok ``` `````
<p><code>`</code> hi ther <code> ok </code><code> </code>````
</p>

A link containing # in a header

I'm not sure whether this is a bug in marked or a symbol I need to escape, but parsing:

### Something [blabla](#/bla) is *great*.

results in a new paragraph starting and the header ending on the # in #/bla and doesn't give me a link (to #/bla) in the header.

Thanks for marked, I'm using it in coffeekup in a zappa project and it's awesome to parse markdown so easily.

Chrome Renderer crashes

I've been doing a few tests with marked, and I've found that if I feed it large documents with embedded HTML (or plain HTML alone), it will crash Chrome Renderer.

I've been able to reproduce this reliably by force-feeding it raw HTML - notably the contents of the container div at http://oss.sapo.pt/ (which I'm re-doing using marked, BTW - you'll notice the site currently fetches Markdown and parses it on the fly using showdown...).

Further testing reveals that Safari and Firefox are immune to this, BTW - I can toss in the same documents and they come out fine.

optional escape

Hi!

If output from marked is fed further to syntax highlighter (say, [https://github.com/andris9/highlight]) the resulting output contains html entities codes instead of symbols.

Wonder if you could make call to escape() optional, or, better, make escape configurable from outside function.

update: marked.escape would fit perfectly, i believe.

TIA,
--Vladimir

Issue with lists

Input:

**v0.1.3** _(16 Apr 2012)_

* Removed `getFileName()` internal function.

**v0.1.2** _(15 Apr 2012)_

* The property value is now converted to a string before persisting.

**v0.1.1** _(15 Apr 2012)_

* Now it's possible to add comments for each key-value pair. A header comment can also be added.

**v0.1.0** _(15 Apr 2012)_

* First commit.

Output:

<p><strong>v0.1.3</strong> <em>(16 Apr 2012)</em>

</p>
<ul>
<li><p>Removed <code>getFileName()</code> internal function.</p>
<p><strong>v0.1.2</strong> <em>(15 Apr 2012)</em></p>
</li>
<li><p>The property value is now converted to a string before persisting.</p>
<p><strong>v0.1.1</strong> <em>(15 Apr 2012)</em></p>
</li>
<li><p>Now it&#39;s possible to add comments for each key-value pair. A header comment can also be added.</p>
<p><strong>v0.1.0</strong> <em>(15 Apr 2012)</em></p>
</li>
<li><p>First commit.</p>
</li>
</ul>

Expected:

<p><strong>v0.1.3</strong> <em>(16 Apr 2012)</em></p>
<ul>
<li><p>Removed <code>getFileName()</code> internal function.</p>
</li>
</ul>
<p><strong>v0.1.2</strong> <em>(15 Apr 2012)</em></p>
<ul>
<li><p>The property value is now converted to a string before persisting.</p>
</li>
</ul>
<p><strong>v0.1.1</strong> <em>(15 Apr 2012)</em></p>
<ul>
<li><p>Now it&#39;s possible to add comments for each key-value pair. A header comment can also be added.</p>
</li>
</ul>
<p><strong>v0.1.0</strong> <em>(15 Apr 2012)</em></p>
<ul>
<li><p>First commit.</p>
</li>
</ul>

links parsed twice + nested if href == content?

[http://www.facebook.com/developers/](http://www.facebook.com/developers/)

gives

<p><a href="http://www.facebook.com/developers/"><a href="http://www.facebook.com/developers/">http://www.facebook.com/developers/</a></a></p>

should be

<p><a href="http://www.facebook.com/developers/">http://www.facebook.com/developers/</a></p>

gfm

Part of the Github Flavored Markdown is to support output of links that will link to users, commit sha's, repo, and issues. This is my understanding, when reading through showdown.js which is included in 'github-flavored-markdown' available on NPM. Do you have any plan for the future to add these features to the parser?

Problem when converting headings

It appears to be an inconsistency in how marked handles single line conversions of headings.

If I have the following test.js:

var marked = require('marked');
console.log(marked('# title'));

I would expect the output to be <h1>title</h1>, but I get:

$ node test.js 
<p># title</p>

If I however do the following:

$ marked -o test.html
# title
^D

I get:

$ cat test.html 
<h1>title</h1>

Which is correct.

If I change my test.js to the following:

var marked = require('marked');
console.log(marked('# title\n\ntest'));

It works properly:

$ node test.js
<h1>title</h1>
<p>test</p>

It would be nice to parce ```

Github understands ``` as block comments:

``` javascript
var a = 2;
console.log(a);
```

will produce

var a = 2;
console.log(a);

It would be nice to make marked understand such blocks. That's significant sometime, when you like to use existing readme in generated docs.

Links titles with parentheses are parsed wrongly

Example:

[Test](http://google.com "Google (Test)")

... should be:

Test

It doesn't work in GitHub's markdown parser too (Redcarpet), but it works in other markdown parsers like Showdown (JS) and RDiscount (Ruby).

Crashes on input

% ../marked/bin/marked
\\[test](not a link)
^D
/Users/jgm/src/marked/lib/marked.js:386
        href: text[1],
                  ^
TypeError: Cannot read property '1' of null
    at Object.lexer (/Users/jgm/src/marked/lib/marked.js:386:19)
    at /Users/jgm/src/marked/lib/marked.js:578:18
    at /Users/jgm/src/marked/lib/marked.js:606:14
    at /Users/jgm/src/marked/lib/marked.js:652:10
    at write (/Users/jgm/src/marked/bin/marked:111:9)
    at ReadStream.<anonymous> (/Users/jgm/src/marked/bin/marked:101:7)
    at ReadStream.emit (events.js:61:17)
    at ReadStream._onReadable (net.js:652:51)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

"Loose" Lists

When "loose" lists, as they're called (lists with their items separated by 2 line feeds), get implemented, it introduces some ambiguity to parsing. This is a bit of a problem with the markdown grammar itself.

* List 1
  Text
* List 1
  Text

* Loose List 1
  Text

* Loose List 1
  Text

What is that? Is it 2 separate lists? One tight, one loose? Is it three lists? One with 2 items, and 2 with one item? Is it a single list with the items spaced differently? The only solution I see, is to actually always separate consecutive lists by 3 line feeds:

* List 1
  Text
* List 1
  Text


* Loose List 1
  Text

* Loose List 1
  Text

Which would be rendered as 2 lists: one tight, one loose. This is probably what will happen once I merge the experimental branch.

html with line breaks

I have run across a slight problem with marked when trying to render html with line breaks between the attributes:

<input type="text"
  name="email"
  value=""
  id="email-input"
  class="etc" />

I know this is not a normal use case but as far as I know it is valid html, I am willing to submit a patch if I can get some pointers on where/ how to modify this behavior.

GFM line breaks

GFM line breaks are not supported :

Line 1
Line 2

Sould be rendered as

Line 1<br/>Line 2

When the gfm flag is set to true.

GFM code with syntax highlighting

GitHub flavored markdown supports syntax highlighted code like this:

/* comment */
var s = "string", i = 123;
function foo () {
   for (var x in xs) {
      console.log(x, xs[x]);
   }
}

This is not supported by marked. It should either state this or (better) implement it.

Link titles in references can be in parentheses

The syntax doc is explicit on this. Many implementations don't notice.

% Markdown.pl
[hi]

[hi]: /url (there)
<p><a href="/url" title="there">hi</a></p>
% ../marked/bin/marked
[hi]

[hi]: /url (there)
<p>[hi]

</p>
<p>[hi]: /url (there)
</p>

async "highlight" function support?

It would be nice to be able to have the "highlight" callback function return asynchronously, as it's the only way to integrate with something like pygments. I realize that this means that marked itself would have to have a new API that returns asynchronously, but that's probably ok. Thoughts?

Option to filter unsafe tags

I'm really surprised to see that marked will not filter out the <script> tag, and there's not an option to do it. If a website uses marked on its comment system, then this can be a very severe security issue. And the unfiltered style attribute for html tags can also be used by malicious visitors to make the site dysfunctional.

So, I wonder if there's a plan to add in options for safe parsing ?

And another problem: the lexer fails for the following case (the type should be html, not paragraph IMO)

> m.lexer('<script>document.write(\'Oops\')</script><br>')
[ { type: 'paragraph',
    text: '<script>document.write(\'Oops\')</script><br>' },
  links: {} ]

Improper parsing of nested strong/emph

% ../marked/bin/marked
*test **test***

<p><em>test *</em>test<em>*</em>

</p>

Of course it is arguable what counts as proper, since the standard is so vague. But I would expect (and good markdown implementations provide):

<p><em>test <strong>test</strong></em></p>

Should not link hyperlinks found in anchor text

Test case:

> marked.parse('<p>Already linked: <a href="http://foo.com">http://foo.com</a>  </p>')
'<p>Already linked: <a href="http://foo.com"><a href="http://foo.com&lt;/a&gt;">http://foo.com&lt;/a&gt;</a>  </p>'

This is quite common when a bit of markdown contains some inline HTML.

A simple workaround that is probably not ideal or free of edge case failures:

result = result.replace(/<a href="([^"]+)&lt;\/a&gt;">\1&lt;\/a&gt;/, '$1');

Allow options.highlight to specify custom classes

I'm using marked together with highlight.js, but there's a bit of snag: highlight.js (and/or its styles) requires the plain name of the language (like "python") as a class on the <code> tag, but marked uses class names that look like "lang-python". I hacked around this in my copy of marked.js by adding the class name -- you can see the change here -- but it'd be nicer if marked let me specify additional/alternative class names.

I suggest that options.highlight() be allowed (optionally -- use a type check) to return an object that has two properties: the code string (i.e., what is returned now) and an array of class names to be applied. I can certainly code it and submit a pull request, if desired.

(For completeness: I could alternatively ask highlight.js to make the class names it expects be configurable. But it's not clear how to do that in any sane way -- the classes are explicit in the CSS. Making the change in marked feels like the correct approach anyway.)

Thanks for the great tool. It was utterly essential in the development of my Markdown-email browser extension.

Sublists

The markdown syntax document is pretty explicit that the marker doesn't matter:

% ../marked/bin/marked
* test
+ test
- test

<ul><li>test <ul><li>test <ul><li>test </li></ul></li></ul></li></ul>
% Markdown.pl 
* test
+ test
- test

<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>

Issue with ordered list immediately following unordered list

I would expect the following input:

* list
* list
* list

1. list
2. list
3. list

To produce an unordered list followed by an ordered list, but instead it produces a single unordered list (with a bit of funky spacing). Inserting an extra line break between the two resolves the issue.

GitHub handles this case as expected.

  • list
  • list
  • list
  1. list
  2. list
  3. list

rel=nofollow

Would it be possible to have the option of adding no follow for all output anchor tags?

If this is relatively trivial to add, it would be nice.

Otherwise, it's not that important and I could just hack it directly onto the links.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.