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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@deco313/markdown-it-replace

v1.2.0

Published

this plugin lets you replace all occurencies of given words with anything you want including html

Downloads

12

Readme

markdown-it-replace

This markdown-it plugin lets you replace patterns of text using regular expressions with other text or HTML

Setup

npm install @deco313/markdown-it-replace

Usage

const MarkdownIt = require('markdown-it')
const mdReplacePlugin = require('@deco313/markdown-it-replace')

const md = MarkdownIt({ html: true })
md.use(
  mdReplacePlugin
    // inline rules
    .addRule(pattern, replaceFunction1, replaceFunction2, ...)
    // block rules
    .addBlockRule(pattern,
      { open: '<b>', close: '</b>'},
      { open: '<em>', close: '</em>' },
      ...
    )
)

API

addRule

The addRule() method replaces all matches of a pattern using the replaceFunction. It applies on the parsed text, not the source markdown code

pattern - string, array of strings or regular expression

replaceFunction - accepts a string and returns modified string

md.use(
  mdReplacePlugin
    .addRule(/\d+/, number => `<em>${number}</em>`)
)

addBlockRule

The addBlockRule() method wraps all matches of a pattern in a tag. Applies to the source markdown code.

pattern - string, array of strings or regular expression

tag - Object with the following fields:

  • open - open tag. Example: "<span>"
  • close - close tag. Example "</span>"
md.use(
  mdReplacePlugin
    .addBlockRule(/\d+/, { open: '<span class="number">', close: '</span>'})
)

When to use inline or block rules

addRule

If the text you want to replace or wrap does NOT contain markdown markups, it is recommended to use addRule method. But you can use addBlockRule if you want though.

addBlockRule

If the text you want to replace or wrap DOES contain markdown markups, you have to use addBlockRule method.

Examples

Make all numbers bold:

Input:

const md = require('markdown-it')({ html: true })
  .use(
    require('@deco313/markdown-it-replace')
      .addRule(/\d+/, match => `<b>${match}</b>`)
  )

md.render('1984 was published on 8 June 1949 by Secker & Warburg')

Output:

<p><b>1984</b> was published on <b>8</b> June <b>1949</b> by Secker & Warburg
</p>

Make all numbers bold but match them only if they are in the beginning of the paragraph:

Input:

const md = require('markdown-it')({ html: true })
  .use(
    require('@deco313/markdown-it-replace')
      .addRule(/^\d+/, match => `<b>${match}</b>`)
  )

md.render('1984 was published on 8 June 1949 by Secker & Warburg')

Output:

<p><b>1984</b> was published on 8 June 1949 by Secker & Warburg
</p>

Make all numbers bold and non-numbers italic:

Input:

const bold = s => `<b>${s}</b>`
const emphasis = s => `<em>${s}</em>`

const md = require('markdown-it')({ html: true })
  .use(
    require('@deco313/markdown-it-replace')
      .addRule(/(\d+)(\D+)/, bold, emphasis)
  )

md.render('1984 was published on 8 June 1949 by Secker & Warburg')

Output:

<p><b>1984</b><em> was published on </em><b>8</b><em> June </em><b>1949</b><em> by Secker & Warburg</em>
</p>

Split text into two columns. Place the names of characters on the left side, and their lines on the right side

Input:

const characters = ['Bernardo', 'Francisco']

const md = require('markdown-it')({ html: true })
  .use(
    require('@deco313/markdown-it-replace')
      // wrap matches in tags
      .addBlockRule(/^(Bernardo|Francisco): (.*)$/,
        { open: '<span class="left">', close: '</span>' },
        { open: '<span class="right">', close: '</span>' },
      )
      // erase colon
      .addRule(/^(Bernardo:|Francisco:)/, nameWithColon => nameWithColon.replace(':', ''))
  )

md.render(
`Bernardo: Who's there?

Francisco: Nay, answer me: stand, and unfold yourself.

Bernardo: Long live the king! Francisco: Bernardo?`
)

Output:

<p><span class="left">Bernardo</span><span class="right">Who's there?</span></p>
<p><span class="left">Francisco</span><span class="right">Nay, answer me: stand, and unfold yourself.</span></p>
<p><span class="left">Bernardo</span><span class="right">Long live the king! Francisco: Bernardo?</span></p>