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/refs

v1.0.0

Published

A metalsmith plugin to refer to other files and global metadata from a file's refs property

Downloads

6

Readme

@metalsmith/refs

A metalsmith plugin to easily refer to other files and global metadata from file.refs

metalsmith: core plugin npm: version ci: build code coverage license: LGPL

Features

  • Gives your files a default, but customizable identity matching its relative file path
  • Substitutes a file's refs keypaths with their target file- or metadata values
  • Allows tracing a file's manipulations via its id property
  • Batch assign references with @metalsmith/default-values

Installation

NPM:

npm install @metalsmith/refs

Yarn:

yarn add @metalsmith/refs

Usage

Pass @metalsmith/refs to metalsmith.use :

import refs from '@metalsmith/refs'
import layouts from '@metalsmith/layouts'

metalsmith.use(refs()) // defaults
metalsmith.use(
  refs({
    // explicit defaults
    pattern: '**'
  })
)

Now you can, say, reference child documentation pages in the index src/docs/index.html:

---
id: docs_index
refs:
  # relative refs will be resolved vs the current file
  getting_started: file:./getting-started/index.html
  # absolute refs will be resolved vs metalsmith.source()
  usage_guide: file:/docs/usage-guide/index.html
---

Refs are defined in a protocol:path format. As a shortcut, not specifying the protocol: part will default to file:.

When @metalsmith/refs runs, it will substitute the refs with the actual files:

{
  'docs/index.html': {
    id: 'docs_index',
    refs: {
      getting_started: {
        id: 'docs/getting-started/index.html',
        contents: Buffer.from('...'),
        stats: {...}
      },
      usage_guide: {
        id: 'docs/usage-guide/index.html',
        contents: Buffer.from('...'),
        stats: {...}
      }
    }
  }
}

The file references are Proxy objects of the targets they refer to, i.e. they will be in sync when other plugins alter them later. To avoid circular references, you cannot access a ref's own refs.

As the example above shows, @metalsmith/refs will also add an id property to each processed file, unless you define an explicit id. By default the id is the file's path relative to metalsmith.source(), and with backslashes converted to forward slashes for cross-platform compatibility.

Plugin order

It is advised to use this plugin at the start of your build chain. Why? If you used plugins like @metalsmith/permalinks that change the file's path before this plugin, the id property will not match the source path of the file.

Using @metalsmith/refs at the start of your plugin chain gives you an easy way to trace file transforms by other plugins.

Custom ids

You can define an id property before @metalsmith/refs runs, and it will not be overwritten. In the example above, the getting-started and usage-guide pages could refer to the index with the id: protocol:

---
refs:
  docs_home: id:docs_index
---

This is useful if you wish to decouple a file's identity from its path on disk and to avoid having to commit a lot of small changes when/if you plan to reorganize source files.

Reference global metadata

Metalsmith.metadata() keys can be referenced with the metadata: protocol, e.g.:

docs/index.html

---
refs:
  navigation: metadata:navigation
  articles: metadata:collections.articles
---

As the example demonstrates dot.delimited.keypaths are also supported.

Referencing the entire metalsmith.metadata() object is not possible. Plugins like @metalsmith/in-place or @metalsmith/layouts already allow access to the global metadata in their render contexts.

Batch references by filepath

Use @metalsmith/default-values before @metalsmith/refs to automatically assign refs by glob patterns:

metalsmith
  .use(
    defaultValues([
      {
        pattern: 'docs/*/*.html',
        defaults: {
          refs: {
            docs_index: 'file:./docs/index.html',
            globalLinks: 'metadata:globalLinks'
          }
        }
      }
    ])
  )
  .use(refs())

Debug

To enable debug logs, set the DEBUG environment variable to @metalsmith/refs*:

metalsmith.env('DEBUG', '@metalsmith/refs*')

Alternatively you can set DEBUG to @metalsmith/* to debug all Metalsmith core plugins.

CLI usage

To use this plugin with the Metalsmith CLI, add @metalsmith/refs to the plugins key in your metalsmith.json file:

{
  "plugins": [
    {
      "@metalsmith/refs": {}
    }
  ]
}

License

LGPL-3.0