Metalsmith 2.6 released

← News overview

Metalsmith 2.6 is out!

Github release | Github Roadmap 2.6 issue | NPM package | Node >= 14.14.0

Highlights

metalsmith.mjs
import { readFileSync } from 'node:fs'
import markdown from '@metalsmith/markdown'
import dotenv from 'dotenv'

const env = dotenv.config()
const metadata = JSON.parse(readFileSync('./metadata.json'))

const build = {
  clean: true,
  env: env,
  metadata: metadata,
  plugins: [
    { '@metalsmith/markdown': {}}
  ]
}

export default build

Alternatively you can also run a full metalsmith build with the CLI now if the build is exported without actual build() or process() call:

metalsmith.mjs
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { readFileSync } from 'node:fs'
import Metalsmith from 'metalsmith'
import markdown from '@metalsmith/markdown'
import dotenv from 'dotenv'

const workingDir = dirname(fileURLToPath(import.meta.url))
const env = dotenv.config()
const metadata = JSON.parse(readFileSync('./metadata.json'))

const build = Metalsmith(workingDir)
  .clean(true)
  .env(env)
  .metadata(metadata)
  .use(markdown())

export default build

It supports full rebuilds and partial rebuilds by setting metalsmith.clean() to true or false, respectively. A side-effect of using watching is that the build and process methods can not be awaited. Instead pass a callback to them that will be executed on each rebuild/ reprocess. In partial rebuild mode, the files parameter passed to the callback will only contain changed files!

Have a look at the snippet below to get a better idea of how it works:

import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import Metalsmith from 'metalsmith'

const __dirname = dirname(fileURLToPath(import.meta.url))
Metalsmith(__dirname)
  // watches metalsmith.source() by default
  .watch(true)
  // watch custom dirs, override chokidar options
  .watch(['layouts', 'src'], { interval: 2000 })
  // clean: true for full rebuild, false for partial
  .clean(false)
  // output files...
  .build(funtion onEachRebuild(err, files) {
    // if clean:false, files will only be the changed files!
    console.log('Rebuild finished!')
  })
  // or only do a 'dry-run' (no writes)
  .process(funtion onEachReprocess(err, files) {
    // if clean:false, files will only be the changed files!
    console.log('Reprocessed!')
  })

The snippet below demonstrates usage of these methods:

// syncs with metalsmith.frontmatter() options
metalsmith.matter.options({ excerpt: true })
metalsmith.matter.parse(`---\ntitle: Hello world\n---\nExcerpt\n---`)
// returns { title: 'Hello World', excerpt: 'Excerpt' }
metalsmith.matter.stringify({ title: 'Hello World', excerpt: 'Excerpt' })
// returns '---\ntitle: Hello world\nexcerpt: Excerpt---'
metalsmith.matter.wrap('title: Hello world\nexcerpt: Excerpt')
// returns '---\ntitle: Hello world\nexcerpt: Excerpt---'

Full Release notes

Added

Removed

Updated

Fixed

× This website may use local storage for purely functional purposes (for example to remember preferences), and anonymous cookies to gather information about how visitors use the site. By continuing to browse this site, you agree to its use of cookies and local storage.