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

v1.0.3

Published

A Metalsmith plugin for processing shortcodes.

Downloads

4

Readme

metalsmith-shortcodes-replace

A Metalsmith plugin for processing shortcodes. You can use the built in matching function or specify your own replacement function (in which case you could match just about anything, not necessarily shortcodes).

Install

npm install metalsmith-shortcodes-replace

Usage

const metalsmith = require('metalsmith');
const shortcodes = require('metalsmith-shortcodes-replace');
metalsmith
    //best to place before any markdown plugins
    .use(shortcodes({
        shortcodes: [
            {//built in matcher
                name: "shortcode",
                replace: function (params, match) {
                    var inner = params.inner
                    return "<shortcode>"+inner+"</shortcode>"
                },
            },
            {//custom replacement function
                find: /__(.*?)__/g, //string or regex
                replace: function(match, contents) { //parameters are defined by the find regex
                    return "<u>"+contents+"</u>"
                }
                //OR
                //replace: "<u>$1</u>"
            }
        ],
        //options
    }))

Options

shortcodes

Shortcodes should be an array of objects, with each object being a shortcode. They are processed in order.

clean_cache

(default false)

I needed several of my shortcodes to fetch the code from an api (see notes) and I didn't want to have more and more requests piling up as the blog expanded so I added a cache. Setting this to true will clear it (or you can delete the cache.shortcodes.json file. It cannot detect changes in the shortcode replacement function so you should set this to true when changing what your shortcode returns.

If a shortcode matches the same thing as another shortcode but returns something different the cache would need to be cleared every time, so for those cases or for repetitively testing just one shortcode you can specify this option for individual shortcodes as well, and that shortcode will never look for itself in the cache.

{
    clean_cache: true,
    name: "shortcode"
    replace: function (params, match) {
        var inner = params.inner
        return "<shortcode>"+inner+"</shortcode>"
    },
}

Shortcode Options

name

(string)

This will use the built in matcher so name: "shortcode" will match [shortcode][/shortcode] and if there's any parameters specified, for example:

[shortcode id="1" other="attribute"]

Interior

Including newlines and even other unprocessed shortcodes.

[/shortcode]

In the replace function you can access them with params.id and anything between the shortcode tags can be accessed with params.inner.

find

(string or regex)

You should not specify a name when using find and vice versa. This is just the first parameter of the string replace() method and will allow you to match either a string or a custom regex.

replace

Using the built matcher:

(function) It has two parameters, params, which holds all the information gathered from the shortcode, and match, which is just the original match.

Custom Replacement

(function or string)

This behaves just like the second parameter of the string replace() method and it can be a function or a string. For example, in the underline example above, replace could just be replace: "<u>$1</u>".

To-Do

  • [ ] Tests

Notes

If you are fetching the code from some api, know that async functions do not mix well with the replace method. I ended up using the sync-request library for simplicities sake.