You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81-66Lines changed: 81 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,102 +8,102 @@ A metalsmith plugin for layouts
8
8
[![code coverage][codecov-badge]][codecov-url]
9
9
[![license: MIT][license-badge]][license-url]
10
10
11
-
This plugin allows you to wrap your files in a template (a `layout`) and abstract repetitive html. The plugin will pass the contents of your files to the layout as the variable `contents`, and renders the result with the appropriate templating engine. It uses the file extension of your layout to infer which templating engine to use. So layouts with names ending in `.njk` will be processed as nunjucks, `.hbs` as handlebars, etc.
11
+
## Features
12
12
13
-
If you want to process templating syntax _in_ your files, instead of wrapping them in a template, you can use [@metalsmith/in-place](https://github.com/metalsmith/in-place). For usage examples check out our [wiki](https://github.com/metalsmith/layouts/wiki). Feel free to contribute an example if anything is missing, or update the existing ones. For support questions please use [stack overflow][stackoverflow-url] or our [slack channel][slack-url]. For templating engine specific questions try the aforementioned channels, as well as the documentation for [jstransformers](https://github.com/jstransformers) and your templating engine of choice.
14
-
15
-
## How does it work
16
-
17
-
Under the hood this plugin uses [jstransformers](https://github.com/jstransformers/jstransformer) to render your layouts. Since there are over a 100 jstransformers we don't install them automatically, so you'll need to install the jstransformer for the language you want to use.
18
-
19
-
For example, to render nunjucks you would install [jstransformer-nunjucks](https://github.com/jstransformers/jstransformer-nunjucks), to render handlebars you would install
20
-
[jstransformer-handlebars](https://github.com/jstransformers/jstransformer-handlebars), etc. The plugin will then automatically detect which jstransformers you've installed. See the [jstransformer organisation](https://github.com/jstransformers) for all available jstransformers and [this dictionary](https://github.com/jstransformers/inputformat-to-jstransformer/blob/master/dictionary.json)
21
-
to see which extensions map to which jstransformer.
13
+
- wraps source files' `contents` field in a layout rendered with a [Jstransformer templating engine](https://github.com/jstransformers/jstransformer)
14
+
- alters file extensions from `transform.inputFormats` to `transform.outputFormat`
15
+
- can be used multiple times with different configs per metalsmith pipeline
You can pass options to `@metalsmith/layouts` with the [Javascript API](https://github.com/segmentio/metalsmith#api) or [CLI](https://github.com/segmentio/metalsmith#cli). The options are:
-[default](#default): optional. The default layout to apply to files.
44
-
-[directory](#directory): optional. The directory for the layouts. The default is `layouts`.
45
-
-[pattern](#pattern): optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is `**`.
46
-
-[engineOptions](#engineoptions): optional. Use this to pass options to the jstransformer that's rendering your layouts. The default is `{}`.
30
+
```
47
31
48
-
#### `default`
32
+
This plugin works with [jstransformers](https://github.com/jstransformers/jstransformer) 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](https://github.com/jstransformers/jstransformer-marked). To render handlebars you would install [jstransformer-handlebars](https://github.com/jstransformers/jstransformer-handlebars). Other popular templating options include: [Nunjucks](https://github.com/jstransformers/jstransformer-nunjucks), [Twig](https://github.com/jstransformers/jstransformer-twig), [Pug](https://github.com/jstransformers/jstransformer-pug), or [EJS](https://github.com/jstransformers/jstransformer-ejs). See also [this map](https://github.com/jstransformers/inputformat-to-jstransformer/blob/master/dictionary.json) to see which extensions map to which jstransformer.
49
33
50
-
The default layout to use. Can be overridden with the `layout` key in each file's YAML frontmatter, by passing either a layout or `false`. Passing `false` will skip the file entirely.
34
+
## Usage
51
35
52
-
If a `default` layout has been specified, `@metalsmith/layouts`will apply layouts to all files, so you might want to ignore certain files with a pattern. Don't forget to specify the default template's file extension. The snippet below will apply the `default.hbs` layout to all files, unless overridden in the frontmatter:
You can change the directory where `@metalsmith/layouts` looks for layouts (default=`layouts`) by supplying the `directory` option. In the example below we use `templates` instead:
56
+
In the transformed file, you have access to `{ ...metalsmith.metadata(), ...fileMetadata }`, so that the following build
The directory path is resolved **relative to**`Metalsmith#directory`, not `Metalsmith#source`.
79
-
If you prefer having the layouts directory _inside_ the Metalsmith source folder, it is advisable to use `Metalsmith#ignore`:
64
+
for a file:
80
65
81
-
```js
82
-
importlayoutsfrom'@metalsmith/layouts'
66
+
```yml
67
+
---
68
+
title: Article title
69
+
layout: default.hbs
70
+
---
71
+
```
83
72
84
-
metalsmith.ignore('layouts').use(
85
-
layouts({
86
-
directory:'src/layouts'
87
-
})
88
-
)
73
+
with layout:
74
+
75
+
```hbs
76
+
<h1>{{title}}</h1>Node v{{nodeVersion}}
89
77
```
90
78
91
-
#### `pattern`
79
+
would render `<h1>Article title</h1>Node v16.20`.
80
+
81
+
### Options
82
+
83
+
In most cases, you will only need to specify the `transform`, `default`, and `engineOptions` option.
92
84
93
-
For example:
85
+
- 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](https://github.com/jstransformers/jstransformer#api)
86
+
-[extname](#extension-handling) (`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.
87
+
-[engineOptions](#engineoptions) (`Object<string, any>`): optional. Pass options to the jstransformer that's rendering the files. The default is `{}`.
88
+
- 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`.
89
+
- 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.
90
+
- directory (`string`): optional. The directory for the layouts (relative to `metalsmith.directory()`, not `metalsmith.source()`!). Defaults to `layouts`.
91
+
92
+
#### directory
93
+
94
+
The directory path is resolved **relative to**`Metalsmith#directory`, not `Metalsmith#source`.
95
+
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):
94
96
95
97
```js
96
98
importlayoutsfrom'@metalsmith/layouts'
97
99
98
-
metalsmith(__dirname).use(
100
+
metalsmith.ignore('layouts').use(
99
101
layouts({
100
-
pattern:'**/*.html'
102
+
directory:'src/layouts'
101
103
})
102
104
)
103
105
```
104
106
105
-
...would process all files that have the `.html` extension. Beware that the extensions might be changed by other plugins in the build chain, preventing the pattern from matching. We use [multimatch](https://github.com/sindresorhus/multimatch) for the pattern matching.
106
-
107
107
#### `engineOptions`
108
108
109
109
Use `engineOptions` to pass options to the jstransformer that's rendering your templates. For example:
@@ -122,6 +122,35 @@ metalsmith.use(
122
122
123
123
Would pass `{ "cache": false }` to the used jstransformer.
124
124
125
+
### Extension handling
126
+
127
+
By default layouts will apply smart default extension handling based on `transform.inputFormats` and `transform.outputFormat`.
128
+
For example, any of the source files below processed through `layouts({ transform: 'handlebars' })` will yield `index.html`.
129
+
130
+
| source | output |
131
+
| ------------------ | ---------------- |
132
+
| src/index.hbs | build/index.html |
133
+
| src/index.hbs.html | build/index.html |
134
+
| src/index.html.hbs | build/index.html |
135
+
136
+
### Usage with @metalsmith/in-place
137
+
138
+
In most cases `@metalsmith/layouts` is intended to be used after `@metalsmith/in-place`.
139
+
You can easily share `engineOptions` configs between both plugins:
@metalsmith/in-place uses a similar mechanism targeting `transform.inputFormats` file extensions by default.
152
+
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.
153
+
125
154
### Debug
126
155
127
156
To enable debug logs, set the `DEBUG` environment variable to `@metalsmith/layouts`:
@@ -150,20 +179,6 @@ To use this plugin with the Metalsmith CLI, add `@metalsmith/layouts` to the `pl
150
179
}
151
180
```
152
181
153
-
## FAQ
154
-
155
-
> I want to use handlebars partials and or helpers.
156
-
157
-
Use [metalsmith-discover-partials](https://www.npmjs.com/package/metalsmith-discover-partials) and [metalsmith-discover-helpers](https://www.npmjs.com/package/metalsmith-discover-helpers).
158
-
159
-
> I want to change the extension of my templates.
160
-
161
-
Use [metalsmith-rename](https://www.npmjs.com/package/metalsmith-rename).
162
-
163
-
> My templating language requires a filename property to be set.
164
-
165
-
Use [metalsmith-filenames](https://www.npmjs.com/package/metalsmith-filenames).
166
-
167
182
## Credits
168
183
169
184
-[Ismay Wolff](https://github.com/ismay) for the current shape of the layouts plugin
0 commit comments