marky-markdown
is a markdown parser, written in NodeJS, that aims for
parity with GitHub-style markdown. It is built on top of markdown-it
,
a CommonMark markdown parser. You can use marky-markdown:
- programmatically in NodeJS
- in your terminal
- in the browser *does not yet support syntax highlighting
marky-markdown
is the thing that parses package READMEs on
http://www.npmjs.com. If you see a markdown parsing bug there,
file an issue here!
npm install marky-markdown --save
marky-markdown exports a single function. For basic use, that function takes a single argument: a string to convert.
var marky = require("marky-markdown")
marky("# hello, I'm markdown").html()
The exported function takes an optional options object as its second argument:
marky("some trusted string", {sanitize: false}).html()
The default options are as follows:
{
sanitize: true, // remove script tags and stuff
linkify: true, // turn orphan URLs into hyperlinks
highlightSyntax: true, // run highlights on fenced code blocks
prefixHeadingIds: true, // prevent DOM id collisions
enableHeadingLinkIcons: true, // render icons inside generated section links
serveImagesWithCDN: false, // use npm's CDN to proxy images over HTTPS
debug: false, // console.log() all the things
package: null // npm package metadata
}
You can use marky-markdown to parse markdown files in the shell. The easiest way to do this is to install globally:
npm i -g marky-markdown
marky-markdown some.md > some.html
This module mostly works in the browser, with the exception of the highlights
module.
You can require('marky-markdown')
in scripts you browserify yourself,
or just use the standalone file in [dist/marky-markdown.js].
Here is an example using HTML5 to render text inside <marky-markdown>
tags.
<script src="marky-markdown.js"></script>
<marky-markdown>**Here** _is_ some [Markdown](https://github.com/)</marky-markdown>
<script>
for (el of document.getElementsByTagName('marky-markdown')) {
el.innerHTML = markyMarkdown(el.innerText, {highlightSyntax: false}).html()
}
</script>
Note: Usage with webpack requires that your
webpack.config.js
configure a loader (such as
json-loader) for .json files.
npm install
npm test
- Parses markdown with markdown-it, a fast and commonmark-compliant parser.
- Removes broken and malicious user input with sanitize-html
- Applies syntax highlighting to GitHub-flavored code blocks using the highlights library from Atom.
- Uses cheerio to perform various feats of DOM manipulation.
- Converts
:emoji:
-style shortcuts to unicode emojis. - Converts headings (h1, h2, etc) into anchored hyperlinks.
- Converts relative GitHub links to their absolute equivalents.
- Converts relative GitHub images sources to their GitHub raw equivalents.
- Converts insecure Gravatar URLs to HTTPS.
- Converts list items with leading
[ ]
and[x]
into GitHub-style task lists - Wraps embedded YouTube videos so they can be styled.
- Parses and sanitizes
package.description
as markdown. - Applies CSS classes to redundant content that closely matches npm package name and description.
- Applies CSS classes to badge images, so we can do something interesting with them one day.
marky-markdown always returns the generated HTML document as a cheerio DOM object that can be queried using a familiar jQuery syntax:
var $ = marky("![cat](cat.png)")
$("img").length
// => 1
$("img").attr("src")
// => "cat.png"
Pass in an npm package
object to do stuff like rewriting relative URLs
to their absolute equivalent on GitHub, normalizing package metadata
with redundant readme content, etc
var package = {
name: "foo",
description: "foo is a thing",
repository: {
type: "git",
url: "https://github.com/kung/foo"
}
}
marky(
"# hello, I am the foo readme",
{package: package}
).html()
- cheerio: Tiny, fast, and elegant implementation of core jQuery designed specifically for the server
- github-slugger: Generate a slug just like GitHub does for markdown headings
- github-url-to-object: Extract user, repo, and other interesting properties from GitHub URLs
- highlights: Syntax highlighter
- highlights-tokens: A list of the language tokens used by the Atom.app highlights syntax highlighter
- lodash: A utility library delivering consistency, customization, performance, & extras.
- markdown-it: Markdown-it - modern pluggable markdown parser.
- markdown-it-emoji: Markdown-it-emoji extension for Markdown-it that parses markdown emoji syntax to unicode.
- markdown-it-expand-tabs: Replace leading tabs with spaces in fenced code blocks
- markdown-it-lazy-headers: Lazy ATX headers plugin for markdown-it
- markdown-it-task-lists: Render GitHub-style task lists
- property-ttl: Save memory by nulling out a property after ttl if it has not been accessed
- sanitize-html: Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis
- similarity: How similar are these two strings?
Extra syntax highlighting, in addition to what comes with highlights:
- atom-language-diff: Diff/patch files
- atom-language-nginx: NGINX configuration files
- language-dart: Dart language
- language-erlang: Erlang language
- language-glsl: OpenGL Shading Language files
- language-haxe: Haxe language
- language-ini: .ini configuration files
- language-rust: Rust language
- language-stylus: Stylus CSS preprocessor
ISC