Arrow keys: ← previous · → next 15 of 35
@nodedk/markdown
0.2.2 stable

markdown#

Lazy-loaded Markdown → HTML parser with GFM and syntax highlighting.

Install#

npm i @nodedk/markdown

Usage#

var markdown = require('@nodedk/markdown')
var parser = markdown()

var html = parser.block('# Hello')
var inline = parser.inline('Hello **world**')

Output:

<h1>Hello</h1>

Create separately configured parsers by passing Marked options:

var parser = markdown({ breaks: false })

Options#

markdown(options = {}) returns a parser with block(source) and inline(source). Both accept Markdown strings. The defaults are:

var options = {
  breaks: true,
  gfm: true,
  pedantic: false,
  silent: false
}

Options override these defaults. Marked renderers, tokenizers, hooks, and extensions are supported. A custom renderer.code overrides the built-in syntax highlighting. Set renderer: null to use Marked's renderer without syntax highlighting.

parser.block() returns an HTML string by default. With { async: true }, it returns a Promise as defined by Marked.

Use parser.inline() for inline HTML without paragraph wrappers. Newlines use <br> tags because breaks is enabled by default:

parser.inline('one\ntwo')
// one<br>two

Syntax highlighting#

Fenced code blocks are highlighted with Highlight.js:

```js
var x = 1
```
  • Languages are loaded on first use.
  • Aliases include js, ts, py, rb, sh, and yml.
  • Unsupported and unspecified languages render as plain escaped code.
  • Languages are never auto-detected.

Use xml for HTML and markdown for Markdown code blocks.

Copy a Highlight.js stylesheet into the application’s public assets:

mkdir -p app/assets/css
cp node_modules/highlight.js/styles/github.css app/assets/css/highlight.css

Add /css/highlight.css to app/config/bundle.js in a NodeDK application, or load the copied stylesheet directly:

<link rel="stylesheet" href="/css/highlight.css" />

Security#

Raw HTML in Markdown is allowed. Only parse trusted Markdown, or sanitize the rendered HTML before displaying it.

Created by Vidar Eldøy