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-keymaster

v1.0.2

Published

A metalsmith plugin to copy or create file properties, and save under new keys.

Downloads

13

Readme

Build Status License NPM Downloads Known Vulnerabilities Coverage Status

metalsmith-keymaster

A general-purpose Metalsmith plugin to copy or add file properties, and save under new keys

Usage

Install as usual, npm install metalsmith-keymaster.

Javascript: use(keymaster({from: from, to: to, filter: filter}))

CLI: You'll lose a few options since it can't support functions or regular expressions.

from is required and defines the value to be added/copied to the file object (here named fileData):

  • if from is '.', use fileData.contents.toString().
  • if from is a String, use fileData[from] (shallow copy).
  • if from is a function, use from(fileData, filePath, metalsmith).

to is required, the value derived from from will be placed there, i.e. fileData[to] = value;

filter is optional and filters which files will be processed

  • if missing, process all files.
  • if a string or Regex, only process matching filePaths.
  • if a function, only process when filter(filePath, data, metalsmith) returns true.
    e.g. If you want to use multimatch, pass something like function(filePath) { return multimatch([filePath], ["blogs/**", ...])[0] };

Possible uses:

You have existing markdown files and template files, but the names of some fields have changed over time and are incompatible.

e.g., if the template now uses {{ userName }} instead of {{ name }}, but you don't want to redo all the YAML:

.use(keymaster({from: 'name', to: 'userName'}))

You want to preserve .contents before the next step processes them.

e.g., to preserve the raw contents before markdown changes them, add the following before use(markdown()):

.use(keymaster({from: '.', to: 'raw'}))  // save contents
   // then
.use(markdown())            // before markdown changes them
You are lazy and want a quick-and-dirty plugin of your own.

This plugin serves as an excellent framework for writing your own plugin. e.g., to quickly hack your own not-very-smart excerpt plugin:

.use(keymaster({from: function(data) {
                  return data.contents.toString().substring(0, 50);
               },
               to: 'excerpt'}))

Notes, Todos, and Caveats

If you want a deep copy, pass in a function for from and do it there.

Currently only supports a single level of keys, i.e. from can be "foo" but not "foo.bar". If you want deeper use the function version for from.

To copy multiple fields, just use keymaster multiple times, e.g.

.use(keymaster({from: 'foo', to: 'bar'}))
.use(keymaster({from: 'abc', to: 'xyz'}))

Warning: The code does not check if the property already exists, so be careful not to overwrite something you need!

More Examples

Instead of using metalsmith-filenames, use

.use(keymaster({from: function(data, filePath) {
                   return filePath;
              },
              to: 'yourKeyHere'}));

and you have the options to filter files or keymaster the key.