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

metalsmith-regex-replace

v1.2.0

Published

A Metalsmith plugin for performing text search and replace in source files

Downloads

3

Readme

metalsmith-regex-replace Build Status Maintainability Test Coverage

A Metalsmith plugin for performing text search and replace in source files

This plugin allows for specifying a list of pairs of search/replace operations to perform during a Metalsmith build. This can be used to fix common typos, censor NSFW text, hide sensitive information, etc.

Install

npm install metalsmith-regex-replace

Usage

The plugin requires an object with a key called subs, containing an array of substition objects with keys for search and replace.

search

Type: string or RegExp

replace

Type: string or function

If using a function for replace argument, it should have a signature as described in String.prototype.replace (MDN reference).

Example

{
  subs: [
    {
      search: "some text or simple pattern",
      replace: "the replacement text"
    },
    {
      search: /more text/,
      replace: "another replacement"
    },
    {
      search: new RegExp("another search"),
      replace: "yet another replacement"
    }
    ...
  ]
}

You can pass either an object directly, a path to a JSON or YAML file, or a function that returns an object.

Passing an object

var Metalsmith = require('metalsmith');
var replace = require('metalsmith-regex-replace');

Metalsmith(__dirname)
    .use(replace({
        subs: [
            {
                search: 'teh',
                replace: 'the'
            }
        ]
    }))
    .build();

Passing a file

YAML file must end with extension .yaml or .yml.

subs:
  - search: teh
    replace: the

JSON file must end with extension .json.

{
    "subs": [
        {
            "search": "teh",
            "replace": "the"
        }
    ]
}

Javascript

var Metalsmith = require('metalsmith');
var replace = require('metalsmith-regex-replace');

// YAML
Metalsmith(__dirname)
    .use(replace('path/to/subs.yaml'))
    .build();

// JSON
Metalsmith(__dirname)
    .use(replace('path/to/subs.json'))
    .build();

Passing a function

var Metalsmith = require('metalsmith');
var replace = require('metalsmith-regex-replace');

function makeSubs() {
  // do stuff
  return {
      subs: [
          {
              search: 'teh',
              replace: 'the'
          }
      ]
  };
}

Metalsmith(__dirname)
    .use(replace(makeSubs))
    .build();

Substitution options

Options can be supplied in an object at the root of the plugin argument object, alongside the subs key. These will be global options applied to each search and replace pair. Additionally, each match in the subs object can be supplied an options object to override the global options and allow for options on a per-match basis.

These options are used when search is a string or when replace is a function. In those situations, the plugin gives you full control over the RegExp patterns and replacements.

Example

{
  options: {
    // global options here
  },
  subs: [
    {
      search: "some text or simple pattern",
      replace: "the replacement text",
      options: {
        // options for just this match here, overriding global options
      }
    },
    ...
  ]
}

caseSensitive

Type: Boolean Default: false

Sets the i flag for the RegExp expression. You can set the i flag explicitly for each match as usual, this just allows for setting it globally and making the subs list look nicer.

matchCase

Type: Boolean Default: false

Changes replacement text to the case of the original match. If original match is all caps, the replacement will be all caps. If the original match was capitalized, the replacement will be capitalized. If the original match was all lowercase, the replacement will be all lowercase. This option is ignored if caseSensitive is true.

isolatedWord

Type: Boolean Default: true

Wraps the search text in RegExp word boundaries (\b) automatically. This is just a helper to make the replacement strings look cleaner.

bypass

Type: String (one character) or null or false Default: "|"

When text in source is enclosed between the bypass string, it will be ignored by the substitution filter. This can be changed if necessary, or set to null or false to disable this behavior. This must be a one-character string. This option is always global and can't be overridden per-match, since it wouldn't make sense. For more information on bypassing, read on to "Bypassing regex-replace".

Bypassing regex-replace

To allow a word to bypass replacement (such as homonyms or other instances of words changing meaning with context), surround it with vertical pipes in your source, like |word|. This wrapper character can be overridden using the bypass option. For example:

Original source

There once was a man named Bob who liked to bob for apples.

Javascript

Metalsmith(__dirname)
    .use(replace({
        subs: [
            {
                search: 'bob',
                replace: 'Paul'
            }
        ]
    }))
    .build();

Bad output; we don't want to replace the second instance of "bob"

There once was a man named Paul who liked to Paul for apples.

Modified source

There once was a man named Bob who liked to |bob| for apples.

New output

There once was a man named Paul who liked to bob for apples.

License

MIT © Anthony Castle