Metalsmith

An extremely simple, pluggable static site generator.

README

Metalsmith

[![npm: version][npm-badge]][npm-url] [![ci: build][ci-badge]][ci-url] [![code coverage][codecov-badge]][codecov-url] [![license: MIT][license-badge]][license-url] [![Gitter chat][gitter-badge]][gitter-url]

An extremely simple, _pluggable_ static site generator.


In Metalsmith, all of the logic is handled by plugins. You simply chain them together.

Here's what the simplest blog looks like:

  1. ``` js
  2. const Metalsmith = require('metalsmith')
  3. const layouts = require('@metalsmith/layouts')
  4. const markdown = require('@metalsmith/markdown')

  5. Metalsmith(__dirname)
  6.   .use(markdown())
  7.   .use(layouts())
  8.   .build(function (err) {
  9.     if (err) throw err
  10.     console.log('Build finished!')
  11.   })
  12. ```

Installation


NPM:

  1. ```
  2. npm install metalsmith
  3. ```

Yarn:

  1. ```
  2. yarn add metalsmith
  3. ```

Quickstart


What if you want to get fancier by hiding unfinished drafts, grouping posts in collections, and using custom permalinks? Just add plugins...

  1. ``` js
  2. const Metalsmith = require('metalsmith')
  3. const collections = require('@metalsmith/collections')
  4. const layouts = require('@metalsmith/layouts')
  5. const markdown = require('@metalsmith/markdown')
  6. const permalinks = require('@metalsmith/permalinks')

  7. Metalsmith(__dirname)
  8.   .source('./src')
  9.   .destination('./build')
  10.   .clean(true)
  11.   .frontmatter({
  12.     excerpt: true
  13.   })
  14.   .env({
  15.     NAME: process.env.NODE_ENV,
  16.     DEBUG: '@metalsmith/*',
  17.     DEBUG_LOG: 'metalsmith.log'
  18.   })
  19.   .metadata({
  20.     sitename: 'My Static Site & Blog',
  21.     siteurl: 'https://example.com/',
  22.     description: "It's about saying »Hello« to the world.",
  23.     generatorname: 'Metalsmith',
  24.     generatorurl: 'https://metalsmith.io/'
  25.   })
  26.   .use(
  27.     collections({
  28.       posts: 'posts/*.md'
  29.     })
  30.   )
  31.   .use(markdown())
  32.   .use(
  33.     permalinks({
  34.       relative: false
  35.     })
  36.   )
  37.   .use(layouts())
  38.   .build(function (err) {
  39.     if (err) throw err
  40.   })
  41. ```

How does it work?


Metalsmith works in three simple steps:

1. Read all the files in a source directory.
2. Invoke a series of plugins that manipulate the files.
3. Write the results to a destination directory!

Each plugin is invoked with the contents of the source directory, and each file can contain YAML front-matter that will be attached as metadata, so a simple file like...

  1. ```
  2. ---
  3. title: A Catchy Title
  4. date: 2021-12-01
  5. ---

  6. An informative article.
  7. ```

...would be parsed into...

  1. ```
  2. {
  3.   'path/to/my-file.md': {
  4.     title: 'A Catchy Title',
  5.     date: <Date >,
  6.     contents: <Buffer 7a 66 7a 67...>,
  7.     stats: {
  8.       ...
  9.     }
  10.   }
  11. }
  12. ```

...which any of the plugins can then manipulate however they want. Writing plugins is incredibly simple, just take a look at the example drafts plugin.

Of course they can get a lot more complicated too. That's what makes Metalsmith powerful; the plugins can do anything you want!

Plugins


A Metalsmith plugin is a function that is passed the file list, the metalsmith instance, and a done callback.
It is often wrapped in a plugin initializer that accepts configuration options.

Check out the official plugin registry at: https://metalsmith.io/plugins.  
Find all the core plugins at: https://github.com/search?q=org%3Ametalsmith+metalsmith-plugin  
See the draft plugin for a simple plugin example.

API


Check out the full API reference at: https://metalsmith.io/api.

CLI


In addition to a simple Javascript API, the Metalsmith CLI can read configuration from ametalsmith.json file, so that you can build static-site generators similar to Jekyll or Hexo easily. The example blog above would be configured like this:

metalsmith.json

  1. ``` json
  2. {
  3.   "source": "src",
  4.   "destination": "build",
  5.   "clean": true,
  6.   "metadata": {
  7.     "sitename": "My Static Site & Blog",
  8.     "siteurl": "https://example.com/",
  9.     "description": "It's about saying »Hello« to the world.",
  10.     "generatorname": "Metalsmith",
  11.     "generatorurl": "https://metalsmith.io/"
  12.   },
  13.   "plugins": [
  14.     { "@metalsmith/drafts": true },
  15.     { "@metalsmith/collections": { "posts": "posts/*.md" } },
  16.     { "@metalsmith/markdown": true },
  17.     { "@metalsmith/permalinks": "posts/:title" },
  18.     { "@metalsmith/layouts": true }
  19.   ]
  20. }
  21. ```

Then run:

  1. ``` sh
  2. metalsmith

  3. # Metalsmith · reading configuration from: /path/to/metalsmith.json
  4. # Metalsmith · successfully built to: /path/to/build
  5. ```

Options recognised by metalsmith.json are source, destination, concurrency, metadata, clean and frontmatter.
Checkout the static site, Jekyll examples to see the CLI in action.

Local plugins


If you want to use a custom plugin, but feel like it's too domain-specific to be published to the world, you can include plugins as local npm modules: (simply use a relative path from your root directory)

  1. ``` json
  2. {
  3.   "plugins": [{ "./lib/metalsmith/plugin.js": true }]
  4. }
  5. ```

The secret...


We often refer to Metalsmith as a "static site generator", but it's a lot more than that. Since everything is a plugin, the core library is just an abstraction for manipulating a directory of files.

Which means you could just as easily use it to make...


Resources


- Gitter community chat for chat, questions
- Awesome Metalsmith - great collection of resources, examples, and tutorials
- emmer.dev on metalsmith - A good collection of various how to's for metalsmith
- glinka.co on metalsmith - Another great collection of advanced approaches for developing metalsmith
- Getting to Know Metalsmith - a great series about how to use Metalsmith for your static site.

Troubleshooting


Use debug to debug your build with `export DEBUG=metalsmith-,@metalsmith/ (Linux) or set DEBUG=metalsmith-,@metalsmith/` for Windows.  
Use the excellent metalsmith-debug-ui plugin to get a snapshot UI for every build step.

Node Version Requirements


Future Metalsmith releases will at least support the oldest supported Node LTS versions.

Metalsmith 2.5.x supports NodeJS versions 12 and higher.  
Metalsmith 2.4.x supports NodeJS versions 8 and higher.  
Metalsmith 2.3.0 and below support NodeJS versions all the way back to 0.12.

Compatibility & support policy


Metalsmith is supported on all common operating systems (Windows, Linux, Mac).
Metalsmith releases adhere to semver (semantic versioning) with 2 minor gray-area exceptions for what could be considered breaking changes:

- Major Node version support for EOL (End of Life) versions can be dropped in minor releases
- If a change represents a major improvement that is backwards-compatible with 99% of use cases (not considering outdated plugins), they will be considered eligible for inclusion in minor version updates.

Credits




[npm-badge]: https://img.shields.io/npm/v/metalsmith.svg
[npm-url]: https://www.npmjs.com/package/metalsmith
[ci-badge]: https://github.com/metalsmith/metalsmith/actions/workflows/test.yml/badge.svg
[ci-url]: https://github.com/metalsmith/metalsmith/actions/workflows/test.yml
[codecov-badge]: https://coveralls.io/repos/github/metalsmith/metalsmith/badge.svg?branch=master
[codecov-url]: https://coveralls.io/github/metalsmith/metalsmith?branch=master
[license-badge]: https://img.shields.io/github/license/metalsmith/metalsmith
[license-url]: LICENSE
[gitter-badge]: https://img.shields.io/badge/GITTER-Join-blue.svg
[gitter-url]: https://gitter.im/metalsmith/community