npm.io
0.7.0 • Published 1 week ago

remove-markdown

Licence
MIT
Version
0.7.0
Deps
0
Size
16 kB
Vulns
0
Weekly
0
Stars
376

npm version npm downloads GitHub Actions Build Status

What is it?

remove-markdown is a JavaScript module that removes (strips) Markdown formatting from text. It supports CommonJS and ES modules in Node.js, plus Deno through npm compatibility. Markdown formatting means pretty much anything that doesn’t look like regular text, like square brackets, asterisks etc.

When do I need it?

The typical use case is to display an excerpt from some Markdown text, without any of the actual Markdown syntax - for example in a list of posts.

Installation

npm install remove-markdown

Usage

CommonJS
const removeMd = require('remove-markdown');
const markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
const plainText = removeMd(markdown); // plainText is now 'This is a heading\n\nThis is a paragraph with a link in it.'
Node.js ES modules
import removeMd from 'remove-markdown';

const markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
const plainText = removeMd(markdown);
Browser ES modules

The ESM entry is self-contained and can be loaded directly from a CDN that serves JavaScript with the correct MIME type. Pin the URL to the version you have tested:

<script type="module">
  import removeMd from 'https://unpkg.com/remove-markdown@0.7.0/index.mjs';

  const plainText = removeMd('# This is a heading');
</script>
Deno
import removeMd from 'npm:remove-markdown@^0.7.0';

const markdown = '# This is a heading\n\nThis is a paragraph with [a link](http://www.disney.com/) in it.';
const plainText = removeMd(markdown);

Deno support is provided through the npm package; there is no separate JSR package. The package exposes one callable default export in every runtime. Named exports and direct imports from raw GitHub URLs, which do not reliably serve the correct JavaScript MIME type, are not supported.

You can also supply an options object to the function. Currently, the following options are supported:

const plainText = removeMd(markdown, {
  stripListLeaders: true ,     // strip list leaders (default: true)
  listUnicodeChar: '',         // char to insert instead of stripped list leaders (default: '')
  gfm: true,                   // support GitHub-Flavored Markdown (default: true)
  useImgAltText: true,         // replace images with alt-text, if present (default: true)
  abbr: true,                  // remove abbreviations, if present (default: false)
  replaceLinksWithURL: true,   // remove inline links, if present (default: false)
  separateLinksAndTexts: ': ', // replace inline links with text, separator and link, if present (default: null)
  htmlTagsToSkip: ['a', 'b'],  // HTML tags to skip, if present (default: [])
  throwError: false,           // throw errors instead of catching and logging (default: false)
});

Setting stripListLeaders to false will retain any list characters (*, -, +, (digit).).

TODO

PRs are very much welcome. Here are some ideas for future enhancements:

  • Allow the RegEx expressions to be customized per rule
  • Make the rules more robust, support more edge cases
  • Add more (comprehensive) tests

Credits

The code is based on Markdown Service Tools - Strip Markdown by Brett Terpstra.

Authors

Stian Grytøyr (original creator) zuchka (maintainer since 2023)

Keywords