A custom Pug filter to render Markdown (with options)
npm i @metamodern/pug-filter-markdown
This package is released as an ES module only. Minimum Node.js version is 12 (latest LTS as of release date). CommonJS require()
is not supported.
// render.js
import pug from 'pug';
import pugFilterMarkdown from '@metamodern/pug-filter-markdown';
const html = pug.renderFile('path/to/file.pug', {
filters: {
md: pugFilterMarkdown,
},
});
// file.pug
div.text-center
:md(inline tag='h1' class='text-lg text-red')
This is *Markdown*
The module's default export is a function with the following parameters:
function(content: string, {
linkify = true,
typographer = true,
inline = false,
...options
} = {}): string
- content: the Markdown string to be rendered to HTML
For details, see the markdown-it docs and live demo.
- linkify: autoconvert URL-like text to links
- typographer: enable some language-neutral replacement and quotes beautification
- inline: render a single line without paragraph wrap
When one or more of the following options is passed, rendered paragraphs or inline text will be wrapped in an enclosing element. The tag defaults to div
when only class or attrs is defined.
- tag: sets the HTML tag of the enclosing element (e.g., 'article')
- class: set the class attribute on the enclosing element (using space-separated class names)
- attrs: set additional attributes on the enclosing element (using space-separated key="value" assignments)
See the markdown-it docs for additional options that may be passed to the rendering function.