remove-markdown

repository·main·Indexed 18 days ago

https://github.com/zuchka/remove-markdown

A Node.js module designed to strip Markdown formatting from text to produce plain text content. Version 0.6.4 supports GitHub-Flavored Markdown (GFM) and provides configurable options for handling list leaders, image alt-text, inline links, and HTML tags.

Tokens
1.8K
Snippets
5
Records
6
Agent score
13%

What's inside remove-markdown

  1. Basic usage of remove-markdown

    main

    Import remove-markdown and call the function with a Markdown string to receive a plain text version. The function removes syntax like square brackets, asterisks, and other non-text formatting.

    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.'
  2. Configure remove-markdown with options

    main

    The removeMd function accepts an optional configuration object to customize how Markdown is stripped.

    Key behaviors include:

    • List Leaders: Control whether list characters (*, -, +, (digit).) are removed using stripListLeaders (default: true). You can specify a replacement character via listUnicodeChar (default: '').
    • Links: Use replaceLinksWithURL to remove inline links or separateLinksAndTexts to replace them with a format like text: URL.
    • Images: Use useImgAltText to replace images with their alt-text (default: true).
    • GFM: Enable GitHub-Flavored Markdown support via gfm (default: true).
    • HTML: Specify tags to ignore using htmlTagsToSkip (default: []).
    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)
    });
  3. Configure remove-markdown options

    main

    The options object allows you to control how different Markdown elements are handled during the stripping process.

    OptionTypeDefaultDescription
    listUnicodeCharbooleanfalseIf true and stripListLeaders is enabled, replaces list markers with this character (Note: implementation uses this as a prefix string).
    stripListLeadersbooleantrueWhether to remove list markers (e.g., *, -, +, 1.).
    gfmbooleantrueEnables GitHub Flavored Markdown stripping (fenced code blocks, strikethrough, etc.).
    useImgAltTextbooleantrueIf true, replaces images ![alt](url) with their alt text. If false, removes them entirely.
    abbrbooleanfalseWhether to remove abbreviation definitions (e.g., *[abbr]: definition).
    replaceLinksWithURLbooleanfalseIf true, replaces inline links [text](url) with the URL. If false, keeps the link text.
    separateLinksAndTextsstringnullA string used to separate the link text and the URL when separateLinksAndTexts is provided (e.g., " ").
    htmlTagsToSkipArray<string>[]A list of HTML tags that should NOT be stripped.
    throwErrorbooleanfalseIf true, the function will throw an error if processing fails. Otherwise, it logs the error to console.error and returns the original input string.
    const removeMarkdown = require('remove-markdown');
    
    const options = {
      stripListLeaders: true,
      useImgAltText: true,
      replaceLinksWithURL: true,
      htmlTagsToSkip: ['code', 'pre']
    };
    
    const plainText = removeMarkdown(markdown, options);
  4. Reference: remove-markdown options object

    main

    The following options are available for the removeMd function:

    OptionTypeDefaultDescription
    stripListLeadersbooleantrueIf false, retains list characters (*, -, +, (digit).)
    listUnicodeCharstring''Character to insert instead of stripped list leaders
    gfmbooleantrueSupport GitHub-Flavored Markdown
    useImgAltTextbooleantrueReplace images with alt-text, if present
    abbrbooleanfalseRemove abbreviations, if present
    replaceLinksWithURLbooleanfalseRemove inline links, if present
    separateLinksAndTextsstringnullReplace inline links with text, separator and link, if present
    htmlTagsToSkipstring[][]HTML tags to skip, if present
    throwErrorbooleanfalseThrow errors instead of catching and logging
  5. Strip Markdown formatting with the main function

    main

    The default export is a function that accepts a Markdown string and an optional configuration object to strip Markdown syntax and return plain text.

    Signature: function(md, options)

    • md (String): The Markdown text to process. If null or undefined, it defaults to an empty string.
    • options (Object): Configuration to customize the stripping behavior. Defaults to an empty object if not provided.
    const removeMarkdown = require('remove-markdown');
    
    const markdown = '# Hello World\n\nThis is **bold** and [a link](https://example.com).';
    const plainText = removeMarkdown(markdown);
    // Output: "Hello World\n\nThis is bold and a link."