Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

metalsmith-inject-metadata

1.0.3 • Public • Published

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

install

npm i metalsmith-inject-metadata

Downloadsweekly downloads

0

version

1.0.3

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability