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-one-replace

v0.1.7

Published

A single Metalsmith plugin for multiple types of replace action

Downloads

11

Readme

metalsmith-one-replace

A single Metalsmith/markdown plugin to perform multiple types of replace action within markdown files. The supported replace functions are:

  • Insert content from other file
  • Variable substitution
  • Regex replacement
  • Replace tags with strings build by a pattern

The default tags (override by defining custom regular expression in keyRegex config attribute) are :

  • {#insert ...} - to insert a file
  • {#var ...} - for variable substitution
  • {#banner ...} - to insert set of HTML tags based on pattern

For detailed usage, refer to wiki.

Installation

$ npm install metalsmith-one-replace

Config

Example - Simple (to insert file using {#insert} custom tag)

...
{
    actions:[{
        type:'file'
    }]
}
...

Example - Simple (to substitute variable using {#var} custom tag})

...
{
    actions:[{
        type:'var',
        varValues:{
            'f1':'Apple',
            'f2':'Orange'
        }
    }]
}
...

Example - Simple (defining custom tag {#img} to use instead of within markdown files)

...
{
    actions:[{
        type:'replace',
        fileFilter:'.md$',
        replacePatterns:{
            '{#img (.*?)}':'<img $1>'
        }
    }]
}
...

Example - Simple (defining custom tag {#img} to use instead of within markdown files)

...
{
    actions:[{
        type:'build',
        fileFilter:'.md$',
        outerBody:'<div class="banner">{{innerBody}}</div>',
        innerBody:'<div class="banner-item"><div class="item-box"><a href="{{1}}"><div class="item-box-image"><img src="{{2}}"></div><div class="item-box-title">{{0}}</div><div class="item-box-desc">{{3}}</div></a></div></div>'
    }]
}
...

Example - Detailed

...
{
    consoleLog: false,
    enabled: true,
    actions: [{
        priority: 10,
        type:'file'
    },{
        priority: 20,
        type:'var',
        varValues:{
            'f1':'Apple',
            'f2':'Orange',
            'f3':'Banana'
        }
    },{
        priority: 1,
        type:'replace',
        fileFilter:'.svg$',
        replacePatterns:{
            '\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>':'',
            '(xml([a-z:-A-Z0-9]+))=[\"]?((?:.(?![\"]?\s+(?:\S+)=|[>\"]))+.)[\"]?([ ]+)?':'',
            '^\s*$':'',
            '<(([a-z]+)*[^>]+)\/>':'<$1></$2>',
            '([a-z:-A-Z0-9]+)="null"?([ ]+)?':''
        }
    },{
        enabled: false,
        priority:5,
        type:'replace',
        fileFilter:'.md$',
        replacePatterns:{
            '{#img (.*?)}':'<img $1>',
            '{#bold (.*?)}':'<b>$1</b>'
        }
    },{
        enabled: true,
        priority:5,
        type:'build',
        fileFilter:'.md$',
        tag:'^{#banner (.*)}',
        outerBody:'<div class="banner">{{innerBody}}</div>',
        innerBody:'<div class="banner-item"><div class="item-box"><a href="{{1}}"><div class="item-box-image"><img src="{{2}}"></div><div class="item-box-title">{{0}}</div><div class="item-box-desc">{{3}}</div></a></div></div>'
    }]
}
...

More information about config attributes:

  • consoleLog (optional boolean default:false) - to log the info to console
  • enabled (optional boolean default:true) - to enable/disable the entire plugin
  • actions (array of action objects) is mandatory
    • priority (optional number) - to specify the sequence order while processing
    • enabled (optional boolean default:true) - to enable/disable a specific action
    • fileFilter (optional regex string default:(.*?)) - to filter files for processing under a specific action
    • type (string values are var, file, replace and build) is mandatory - to specify the routine while processing
      • attributes when type is var
        • keyRegex (optional regex string default:{#var (.*?)}) - regex to find the tag (example {#var}) with the variable name as the parameter
        • varValues (key-value pair object) is mandatory - with variable name and it's value as key-value pair
        • ignoreMissing (optional boolean default:false) - ignore (by substituting it with blank) if the variable is not defined
      • attributes when type is file
        • keyRegex (optional regex string default:^{#insert (.*)}) - regex to find the tag (example {#insert}) with the name of the file (with path) as parameter
        • ignoreMissing (optional boolean default:false) - ignore (by substituting it with blank) if the file is not found
        • removeFile (optional boolean default:true) - remove the file from target folder after inserting the content
      • attributes when type is replace
        • replacePatterns (key-value pair object) is mandatory - regex find and replace string as key-value pair
      • attributes when type is build
        • tag (optional regex string default:^{#banner (.*)}) = regex to find the tag (example {#banner} with the JSON object as parameter)
        • outerBody - string containing HTML tags (to be used once)
        • innerBody - string containing HTML tags with {{}} placeholders. Based on the elements of JSON object specified as the parameter, that many times this string will be duplicated, and {{}} placeholders will be replaced with the value of array within the JSON object.

Documentation

Refer to the wiki

License

MIT