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-inject-metadata

v1.0.7

Published

A Metalsmith plugin for injecting metadata constants into source file data.

Downloads

11

Readme

metalsmith-inject-metadata

npm version build status downloads

A Metalsmith plugin for injecting metadata constants into source file data.

This plugin injects Metalsmith metadata values into source file data during the build process. Metadata values can be used as program constants containing such things as file paths or configuration settings. Whereas template plugins pull data from the file object, this plugin injects data into the file object before the template build step occurs.

Table of Contents

  1. Installation
  2. Example
  3. Options
  4. License

Installation

$ npm install metalsmith-inject-metadata

⬆ back to top

Examples

Basic

Configuration in metalsmith.json:

{
  "metadata": {
    "images": "path/to/batman/images",
    "hero": "Batman"
  },
  "plugins": {
    "metalsmith-inject-metadata": {}
  }
}

Source file src/index.md:

---
gotham: {{ hero }} is one half of the dynamic duo!
---
Have you read the latest {{ hero }} comic?
Look at this amazing cover: {{ images }}/batman.jpg!

Results in file key gotham:

"Batman is one half of the dynamic duo!"

And file build/index.md (as well as the buffer at file key contents):

Have you read the latest Batman comic?
Look at this amazing cover: path/to/batman/images/batman.jpg!

Nested Objects

Configuration in metalsmith.json:

{
  "metadata": {
    "superheroes": {
      "bats": {
        "gotham": "Batman"
      }
    }
  },
  "plugins": {
    "metalsmith-inject-metadata": {
      "metadataKeys": "superheroes.bats.gotham"
    }
  }
}

Source file src/index.md:

---
gotham:
  vigilantes:
    people: {{ superheroes.bats.gotham }} is a vigilante!
---
Have you read the latest {{ superheroes.bats.gotham }} comic?

Results in file key gotham.vigilantes.people:

"Batman is a vigilante!"

And file build/index.md (as well as the buffer at file key contents):

Have you read the latest Batman comic?

⬆ back to top

Options

Pass options to metalsmith-inject-metadata using the Metalsmith Javascript API or CLI. These are the available plugin options:

|Option |Type |Description| |--------------------------------------------------|------------------|-----------| |pattern |string[]\|string|Pattern used to match file names.| |metadataKeys |string[]\|string|Metadata keys to inject into files.| |metadataKeyBounds |object |Key marker bounds used to match the metadata key.| |metadataKeyBounds.left |string |Left bound of the metadata key marker.| |metadataKeyBounds.right|string |Right bound of the metadata key marker.| |fileKeys |string[]\|string|File keys with injectable data.|

Defaults

{
  "plugins": {
    "metalsmith-inject-metadata": {
      "pattern": ["**/*"],
      "metadataKeys": ["*"],
      "metadataKeyBounds": {
        "left": "{{ ",
        "right": " }}",
      },
      "fileKeys": ["*"]
    }
  }
}

Option Details

pattern

Fed directly into multimatch as the mechanism to select source files.

metadataKeys

Key names matching the Metalsmith metadata object. Only the key(s) listed here will be injected into file data. Keys that do not match the metadata object will be ignored. Use wildcards '*' or ['*'] to select all metadata keys.

Nested metadata keys can be referenced using dot notation. When injecting nested metadata keys, the wildcard option will no longer work.

Example:

Configuration in metalsmith.json:

{
  "metadata": {
    "superheroes": {
      "bats": {
        "gotham": "Batman"
      }
    }
  },
  "plugins": {
    "metalsmith-inject-metadata": {
      "metadataKeys": "superheroes.bats.gotham"
    }
  }
}

Source file src/index.md:

---
hero: {{ superheroes.bats.gotham }} is one half of the dynamic duo!
---

metadataKeyBounds

Configuration object containing settings for the metadata key markers inserted into file data. Setting the bounds to empty strings will result in a "find and replace" for all words in the text data matching the metadata key(s).

Example: {{ myKey }} or myKey

metadataKeyBoundsLeft

Indicates the left bound of the metadata key marker inserted into the file data.

Example: {{

metadataKeyBoundsRight

Indicates the right bound of the metadata key marker inserted into the file data.

Example: }}

fileKeys

Key names matching the Metalsmith file object. Only the key(s) listed here will be checked against the list of injected metadata keys. Keys that do not match a file key will be ignored. Use wildcards '*' or ['*'] to select all file keys.

Nested front-matter file keys are referenced using the top-level key. Wildcards will also work.

Example:

Note: fileKeys is optional here.

Configuration in metalsmith.json:

{
  "metadata": {
    "hero": "Batman"
  },
  "plugins": {
    "metalsmith-inject-metadata": {
      "fileKeys": "gotham"
    }
  }
}

Source file src/index.md:

---
gotham:
  vigilantes:
    bats: {{ hero }} is a vigilante!
---

⬆ back to top

License

MIT

⬆ back to top