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

markdown-it-find-and-replace

v1.0.2

Published

Basic Find and Replace functionality for Markdown It via Regex. Great for build-process text expanders

Downloads

21

Readme

Markdown It Find and Replace Plugin

Basic Find and Replace functionality as a plugin for Markdown It, leveraging Regex. Great for setting up text expanders that work at build-time with Markdown It. Intended to work on inline and paragraph text. Will not operate inside code blocks or inline code.

Install

As a dependency:

npm i markdown-it-find-and-replace

As a devDependency:

npm i --dev markdown-it-find-and-replace

Peer Dependency

This is a plugin for Markdown It and requires that it also be installed in your project.

Usage

const md = require('markdown-it')()
  .use(require('markdown-it-find-and-replace'), opts)

The opts object can contain:

| Name | Description | Default | |------------------------|---------------------------------------------------------------------------|----------------------------| | defaults | Activates find-and-replace's starter replace rules. | true | | replaceRules | An array of find and replace rules. | [] |

Rule Objects

The replaceRules property can contain an array of objects. If you choose to build your own rules you can do so by passing in the specific object format to the array as follows:

	{
		pattern: /(?<=[\t\s\S\( ]|^)DS9(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "Deep Space Nine",
	},

This would be passed into the plugin with the Markdown It use call:

const md = require('markdown-it')()
  .use(require('markdown-it-find-and-replace'), {
	  defaults: true,
	  replaceRules: [{
		pattern: /(?<=[\t\s\S\( ]|^)DS9(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "Deep Space Nine",
	}]
  })

With the result of the Markdown transforming from:

I watched DS9 last night.

to

<p data-wordfix="true">I watched Deep Space Nine last night.</p>

The object contains two properties pattern and replace which are used with the String prototype replace function in the style of aString.replace(pattern, replace). To support that functionality the plugin allows either a RegExp object or a string in the pattern property and only a string in the replace property. The plugin will throw errors for a malformed replaceRules value or for a malformed rule, if one exists in the array.

Default Rules

Markdown-It Find and Replace ships with a set of default rules that are intended as basic common-use replacements built on common text expanders. These are the rules you get in the default set:

[
	{
		pattern: /(?<=[\t\s\S\( ]|^)11ty(?=[\?\.\,\s\r\n\!\) ]|$)/gi,
		replace: "Eleventy",
	},
	{
		pattern: /(?<=[\t\s\( ])prob(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "probably",
	},
	{
		pattern: /(?<=[\t\s\( ]|^)Prob(?=[\?\.\,\s\r\n\!\) ])/g,
		replace: "Probably",
	},
	{
		pattern: /(?<=[\t\s\( ])graf(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "paragraph",
	},
	{
		pattern: /(?<=[\t\s\( ]|^)Graf(?=[\?\.\,\s\r\n\!\) ])/g,
		replace: "Paragraph",
	},
	{
		pattern: /(?<=[\t\s\( ])b\/c(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "because",
	},
	{
		pattern: /(?<=[\t\s\( ]|^)B\/c(?=[\?\.\,\s\r\n\!\) ])/g,
		replace: "Because",
	},
	{
		pattern: /(?<=[\t\s\( ])def(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "definitely",
	},
	{
		pattern: /(?<=[\t\s\( ]|^)Def(?=[\?\.\,\s\r\n\!\) ])/g,
		replace: "Definitely",
	},
	{
		pattern: /(?<=[\t\s\( ])tho(?=[\?\.\,\s\r\n\!\) ]|$)/g,
		replace: "though",
	},
	{
		pattern: /(?<=[\t\s\( ]|^)Tho(?=[\?\.\,\s\r\n\!\) ])/g,
		replace: "Though",
	},
]

HTML Data Property

The plugin will add a data property named wordfix with a value of true to the HTML tag that contains text the plugins treats. This is useful for debugging and verifying functionality.

Render Process Impact

Impact on your Markdown It rendering process should be fairly low, however, the more Regex rules you add to the plugin the more of an impact there will be.

Contributing

This plugin is maintained in its GitHub repository. All PRs and Issues filed to that repo are welcome and will be reviewed. The GitHub repo includes a test suite and it is recommended that you run tests before filing a PR to make sure you maintain the basic functionality of this plugin.

Contributor Covenant