npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@btnguyen2k/mdr

v0.1.0

Published

Render Markdown to HTML with rich extensions.

Readme

mdr

npm Actions Status codecov

Render Markdown to HTML with rich extensions.

⭐ Based on markedjs with GFM flavor. Enabled extensions:

  • marked-base-url
  • marked-mangle
  • GFM header-id

⭐ Code highlight with highlight.js.

⭐ Support Katex math formula.

Installation

with npm

$ npm install -S @btnguyen2k/mdr

Usage

This package exports a function mdr that can be used to render Markdown to HTML:

import {mdr} from '@btnguyen2k/mdr'

const markdown = '# Hello World!'
const options = {} // optional
console.log(mdr(markdown, options)) // <h1 id="hello-world">Hello World!</h1>

Options

Function mdr takes an optional 2nd argument, which is an object with the following properties:

| Property | Type | Default | Description | |-------------------------------|----------|--------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | inline | boolean | false | If true, the output HTML will not be wrapped in <p> tag. | | safety | boolean | true | If true, the output HTML will be sanitized using DOMPurify. | | safety_opts | object | (see bellow) | Options for DOMPurify if safety=true. | | safety_opts.add_tags | array | ['iframe'] | Additional tags to allow [1]. | | safety_opts.add_data_uri_tags | array | ['iframe'] | Additional tags to allow data URI [1]. | | safety_opts.add_attrs | array | ['target', 'allow'] | Additional attributes to allow [1][2]. | | toc_container | array | undefined | If supplied, the generated table of content will be pushed to this array. | | katex | boolean | true | If true, KaTeX support is enabled. | | katex_opts | object | {output: 'htmlAndMathml', throwOnError: false} | Options for KaTeX if katex=true. See see https://katex.org/docs/options.html | | ghgist | boolean | true | If true, GitHub Gist support is enabled. | | ghgist_opts | object | {} | Options for GitHub Gist if ghgist=true. See below for details. | | video | boolean | true | If true, video embedding is enabled. | | video_opts | object | {} | Options for video embedding if video=true. See below for details. | | code_handlers | object | {} | Custom code block handlers, in format {code_id: handler}. If matched, the code enclosed between code_id and will be handled by the specified handler. See below for details. | | baseUrl () | string | undefined | If supplied, marked-base-url extension is enabled. | | headerIds () | boolean | true | If true, headings are generated with id attribute. | | headerPrefix () | string | '' | If present, values of generated id attributes are prefixed by this value (imply headingIds=true). | | mangle () | boolean | true | If true, marked-mangle extension is enabled. | | highlight () | function | undefined | If supplied, highlight function is used to highlight source code. | | langPrefix () | string | null | If present, langPrefix is used to prefix language name in generated code block. |

  • [1] iframe is used by GitHub Gist and Youtube video extensions. allow attribute is also used to enable Youtube features such as fullscreen, autoplay, picture-in-picture, etc.
  • [2] target attribute is useful for extrernal links.
  • (*) Do use these options with mdr, do not use marked's extensions (e.g. marked-highlight, etc) directly.

Embedding GitHub Gist

mdr supports embedding GitHub Gist in Markdown using the following syntax:

| Parameters/Options | Description | |--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | gistUrl | The GitHub Gist to embed, can be in short form (e.g. btnguyen2k/d7577db0981cb157ae95b67b9f6dd733) or full URL (e.g. https://gist.github.com/btnguyen2k/d7577db0981cb157ae95b67b9f6dd733). | | style="custom-css-style" | Custom CSS style to apply to the embedded Gist. | | class="custom-css-class" | Custom CSS class to apply to the embedded Gist. |

Example:

Default values for options style and class can be specified in ghgist_opts:

import {mdr} from '@btnguyen2k/mdr'

const input = '```gh-gist btnguyen2k/d7577db0981cb157ae95b67b9f6dd733\n```'
const output = mdr(input, {
  ghgist_opts: {
    style: 'width: 100%; padding-bottom: 8px;',
    class: 'gist-embed'
  }
})

Embedding video

mdr supports embedding video in Markdown using the following syntax:

| Parameters/Options | Description | |--------------------------|--------------------------------------------------| | videoUrl | The video embed. | | style="custom-css-style" | Custom CSS style to apply to the embedded video. | | class="custom-css-class" | Custom CSS class to apply to the embedded video. |

Example:

Default values for options style and class can be specified in video_opts:

import {mdr} from '@btnguyen2k/mdr'

const input = '```video my-video.mp4\n```'
const output = mdr(input, {
  video_opts: {
    style: 'width: 100%; padding-bottom: 8px;',
    class: 'gist-embed'
  }
})

Extending MDR

Application developers can extend mdr by adding custom code block handlers. For example, the following code adds a custom code block handler to support [Bootstrap Alerts](https://getbootstrap.com/docs/5.0/components/alerts/:

const opts = {
  code_handlers: {
    'bs-alert': (code, infoString) => {
      const alertStyle = infoString ? infoString.slice('bs-alert'.length).trim() : 'primary'
      return `<div class="alert alert-${alertStyle}" role="alert">${code}</div>`
    }
  }
}

const input = '```bs-alert info\nA simple primary alert—check it out!\n```'
const output = mdr(input, opts) // '<div class="alert alert-info" role="alert">A simple primary alert—check it out!</div>'

License

MIT - see LICENSE.md.