Détail du package

@metalsmith/layouts

metalsmith16.7kMIT3.0.0

A metalsmith plugin for layouts

readme

@metalsmith/layouts

A metalsmith plugin for layouts

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

Features

  • wraps source files' contents field in a layout rendered with a Jstransformer templating engine
  • alters file extensions from transform.inputFormats to transform.outputFormat
  • can be used multiple times with different configs per metalsmith pipeline

Installation

NPM:

npm install @metalsmith/layouts jstransformer-handlebars

Yarn:

yarn add @metalsmith/layouts jstransformer-handlebars

This plugin works with jstransformers but they should be installed separately. jstransformer-handlebars is just an example, you could use any transformer. To render markdown you could install jstransformer-marked. To render handlebars you would install jstransformer-handlebars. Other popular templating options include: Nunjucks, Twig, Pug, or EJS. See also this map to see which extensions map to which jstransformer.

Usage

Pass @metalsmith/layouts to metalsmith.use :

import layouts from '@metalsmith/layouts'

// shorthand
metalsmith.use(layouts({ transform: 'nunjucks' }))

// same as shorthand
metalsmith.use(
  layouts({
    directory: 'layouts' // === path.join(metalsmith.directory(), 'layouts')
    transform: jsTransformerNunjucks, // resolved
    extname: '.html',
    pattern: '**/*.{njk,nunjucks}*',
    engineOptions: {}
  })
)

In the transformed file, you have access to { ...metalsmith.metadata(), ...fileMetadata }, so that the following build

metalsmith
  .metadata({ title: 'Default title', nodeVersion: process.version })
  .use(layouts({ transform: 'handlebars' }))

for a file:

---
title: Article title
layout: default.hbs
---

with layout:

<h1>{{title}}</h1>Node v{{nodeVersion}}

would render <h1>Article title</h1>Node v16.20.

Options

In most cases, you will only need to specify the transform, default, and engineOptions option.

  • transform (string|JsTransformer): required. Which transformer to use. The full name of the transformer, e.g. jstransformer-handlebars, its shorthand handlebars, a relative JS module path starting with ., e.g. ./my-transformer.js, whose default export is a jstransformer or an actual jstransformer: an object with name, inputFormats,outputFormat, and at least one of the render methods render, renderAsync, compile or compileAsync described in the jstransformer API docs
  • extname (string|false|null): optional. How to transform a file's extensions: ''|false|null to remove the last transform.inputFormat matching extension, .<ext> to force an extension rename.
  • engineOptions (Object<string, any>): optional. Pass options to the jstransformer that's rendering the files. The default is {}.
  • pattern (string|string[]): optional. Override default glob pattern matching **/*.<transform.inputFormats>*. Useful to limit the scope of the transform by path or glob to a subfolder, or to include files not matching transform.inputFormats.
  • default (string): optional. The default layout to apply to files matched with pattern. If none is given, files matched without defined layout will be skipped. Files whose layout is set to false will also be skipped.
  • directory (string): optional. The directory for the layouts (relative to metalsmith.directory(), not metalsmith.source()!). Defaults to layouts.

directory

The directory path is resolved relative to Metalsmith#directory, not Metalsmith#source. If you prefer having the layouts directory inside the Metalsmith source folder, it is advisable to use Metalsmith#ignore to avoid loading the layouts twice (once via Metalsmith and once via the JSTransformer):

import layouts from '@metalsmith/layouts'

metalsmith.ignore('layouts').use(
  layouts({
    directory: 'src/layouts'
  })
)

engineOptions

Use engineOptions to pass options to the jstransformer that's rendering your templates. For example:

import layouts from '@metalsmith/layouts'

metalsmith.use(
  layouts({
    engineOptions: {
      cache: false
    }
  })
)

Would pass { "cache": false } to the used jstransformer.

Extension handling

By default layouts will apply smart default extension handling based on transform.inputFormats and transform.outputFormat. For example, any of the source files below processed through layouts({ transform: 'handlebars' }) will yield index.html.

source output
src/index.hbs build/index.html
src/index.hbs.html build/index.html
src/index.html.hbs build/index.html

Usage with @metalsmith/in-place

In most cases @metalsmith/layouts is intended to be used after @metalsmith/in-place. You can easily share engineOptions configs between both plugins:

import inPlace from '@metalsmith/in-place'
import layouts from '@metalsmith/layouts'

const engineOptions = {}
metalsmith // index.hbs.hbs
  .use(inPlace({ transform: 'handlebars', extname: '', engineOptions })) // -> index.hbs
  .use(layouts({ transform: 'handlebars', engineOptions })) // -> index.html

@metalsmith/in-place uses a similar mechanism targeting transform.inputFormats file extensions by default. The example requires files ending in .hbs.hbs extension, but if you don't like this, you can just have a single .hbs extension, and change the in-place invocation to inPlace({ engineOptions, transform, extname: '.hbs' }) for the same result.

Debug

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

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

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/layouts to the plugins key in your metalsmith.json file:

{
  "plugins": [
    {
      "@metalsmith/layouts": {
        "default": null,
        "directory": "layouts",
        "engineOptions": {}
      }
    }
  ]
}

Credits

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v3.0.0

  • test: swap nyc for c8 for test coverage, migrate tests to ESM, update dotfiles 9855386
  • BREAKING: solves #179, loosen dependency on jstransformers: Adds required transform option to support custom transformers, no longer supports multiple transformers per instantiation c1f7c4c
  • BREAKING: defaults pattern to '*/.{${inputFormats.join(',')}', reworks file filtering. Allows omitting .ext from layout property ec1c288
  • Updates README.md for layouts 3.x 89af28b
  • BREAKING: remove suppressNoFilesError in favor of debug.warn message 29c3b96
  • feat: adds extname option, as aligned with metalsmith/in-place 8d998ad
  • Fix TS & JSDoc descriptions, add defaults & example 0a77660
  • Drops Node < v14.18.0 support, use node: namespaced imports 4cf3c1d
  • Fix: only log rename when actual rename occurs 5a25943
  • BREAKING: default export no longer exposes getTransformer d7c523a
  • Drops support for Node < 14.14.0 6d4dbd8

v2.7.0

3 April 2023

  • Fix docs for pattern option #188
  • Swaps out debug for metalsmith.debug & enhances debug log output f8cd111
  • Renames default export to layouts for better intellisense 10eaa0c

v2.6.0

4 September 2022

  • feat: dual bundling ESM/CJS 771212c
  • feat: added Typescript support 507bb47

v2.5.1

16 May 2022

  • Resolves #183: pass the transformed list of files to metalsmith.match… #184
  • Merge pull request #184 from doteco/master #183
  • Resolves #183: pass the transformed list of files to metalsmith.match so that renamed files can be matched #183

v2.5.0

2 May 2022

  • Resolves #181, use metalsmith.match instead of multimatch #181
  • Remove semicolons, run formatting. Remove devDependencies 605559a
  • feat: add JSdocs, named plugin function 30411dd
  • Dependencies: updates debug to 4.3.4 1a33ff2
  • Drop Node < 12 support 4cd7c4d

2.4.0

  • Return when done #173
  • Improve readme #172
  • Resolves #180: Align dotfiles, repo with core plugin setup @metalsmith #180
  • deps: update dependencies and fix prefer-object-spread b10b175

2.3.1 - April 7, 2019

  • prefix rendering errors with filename that caused the error

2.3.0 - November 1, 2018

  • add support for async rendering

2.2.0 - September 4, 2018

  • add suppressNoFilesError feature

2.1.0 - January 25, 2018

  • add a documentation link to the error messages
  • add debug for better debugging

2.0.0 - January 10, 2018

  • moved to jstransformers (and dropped consolidate)
  • breaking: dropped node 0.12 and node 4 support
  • breaking: removed options:
    • engine: inferred automatically from the layout extension by jstransformer
    • partials: use metalsmith-discover-partials (and metalsmith-discover-helpers) instead
    • partialExtension: no longer necessary
    • rename: use metalsmith-rename instead
    • exposeConsolidate: no longer necessary

1.8.1 - April 20, 2017

  • fix "too many opened files" error

1.8.0 - February 3, 2017

  • add partialExtension option, to fix partials matching on the wrong extension

1.7.0 - November 6, 2016

  • expose consolidate.requires fix (#18)
  • check if paths are already absolute before joining the metalsmith path

1.6.5 - May 3, 2016

  • normalize partial name for windows

1.6.4 - February 22, 2016

  • allow layout: false to override default template

1.5.4 - February 14, 2016

  • add rename option
  • prevent path issue on windows

1.4.4 - January 27, 2016

  • update lodash.omit

1.4.3 - January 27, 2016

  • update consolidate

1.4.2 - October 17, 2015

  • update fs-readdir-recursive

1.4.1 - September 19, 2015

  • move check for unspecified layout, #41
  • update devdependencies

1.4.0 - August 14, 2015

  • ignore files with unspecified layout
  • update readme

1.3.1 - August 8, 2015

  • improve error message for unspecified layout
  • update readme

1.3.0 - August 7, 2015

  • add swig include test
  • code style, dependency and readme updates
  • add partials option and test

1.2.1 - July 25, 2015

  • add error handling
  • add test for partials

1.2.0 - July 25, 2015

  • ignore binary files
  • add dotfiles
  • update readme

1.1.0 - July 19, 2015

  • update dependencies and devDependencies

1.0.1 - July 16, 2015

  • update tests and add Travis CI

1.0.0 - November 18, 2014

  • remove inPlace option
  • use layout instead of template in the front-matter to specify a layout
  • set default folder for layouts to layouts instead of templates

0.6.0 - October 3, 2014

  • fix to use path for metalsmith 1.0.0

0.5.2 - July 9, 2014

  • fix breaking binary files

0.5.1 - June 11, 2014

  • fix race condition with stringify file contents

0.5.0 - April 29, 2014

  • pass in options to consolidate.js

0.4.0 - April 2, 2014

  • add default option

0.3.0 - March 10, 2014

  • add inPlace option
  • change pattern option to just filter

0.2.1 - March 10, 2014

  • fix bug in matching pattern logic

0.2.0 - March 8, 2014

  • add rendering files in place with a pattern

0.1.0 - March 5, 2014

  • add string engine convenience

0.0.6 - February 7, 2014

  • fix directory option bug

0.0.5 - February 7, 2014

  • stringify contents on the original file data

0.0.4 - February 6, 2014

  • switch to extend from defaults to avoid cloning
  • add debug statements

0.0.3 - February 6, 2014

  • fix to use buffers

0.0.2 - February 5, 2014

  • mix in metadata

0.0.1 - February 4, 2014

:sparkles: