Skip to content

Commit

Permalink
BREAKING (for Pug users): remaps setFilename option to engineOptions.…
Browse files Browse the repository at this point in the history
…filename
  • Loading branch information
webketje committed Jul 3, 2023
1 parent a92de2b commit 064cc37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
24 changes: 3 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ You can pass options to `@metalsmith/in-place` with the [Javascript API](https:/

- [pattern](#pattern): optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is `**`.
- [engineOptions](#engineoptions): optional. Use this to pass options to the jstransformer that's rendering your files. The default is `{}`.
- [setFilename](#setfilename): optional. Some templating engines, like [pug](https://github.com/pugjs/pug), need a `filename` property to be present in the options to be able to process relative includes, extends, etc. Setting this option to `true` will add the current filename to the options passed to each jstransformer. The default is `false`.

### `pattern`

Expand Down Expand Up @@ -71,6 +70,7 @@ Use this to pass options to the jstransformer that's rendering your templates. S
"destination": "build",
"plugins": {
"@metalsmith/in-place": {
"transform": "nunjucks",
"engineOptions": {
"cache": false
}
Expand All @@ -79,27 +79,9 @@ Use this to pass options to the jstransformer that's rendering your templates. S
}
```

Would pass `{ "cache": false }` to each used jstransformer.
Would pass `{ "cache": false }` to `jstransformer-nunjucks`.

### `setFilename`

Set this option to `true` if you want to pass the current filename to each jstransformer. The default is `false`. So this `metalsmith.json`:

```json
{
"source": "src",
"destination": "build",
"plugins": [
{
"@metalsmith/in-place": {
"setFilename": true
}
}
]
}
```

Would overwrite `engineOptions.filename` with the absolute path for the file that's currently being processed, and pass that to the jstransformer. For now we're just passing `filename`, but if you encounter a jstransformer that requires a different property, like `path` or something else, let us know and we can add it.
If you are using [Pug](https://pugjs.org/api/getting-started.html), make sure to pass `engineOptions: { filename: true }`. This will ensure the filename of each processed file is passed to the render method.

## Errors and debugging

Expand Down
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ async function render({ filename, files, metalsmith, settings, transform }) {
const { dirname, base, extensions } = parseFilepath(filename)
const file = files[filename]
const engineOptions = Object.assign({}, settings.engineOptions)
if (settings.engineOptions.filename) {
Object.assign(engineOptions, {
// set the filename in options for jstransformers requiring it (like Pug)
filename: metalsmith.path(metalsmith.source(), filename)
})
}
const metadata = metalsmith.metadata()
const isLastExtension = extensions.length === 1

debug(`rendering ${filename}`)

const ext = extensions.pop()
Expand All @@ -70,12 +75,6 @@ async function render({ filename, files, metalsmith, settings, transform }) {
extensions.push(transform.outputFormat)
}

// Check if the filename should be set in the engine options
if (settings.setFilename) {
debug(`setting filename in the engine options`)
engineOptions.filename = path.join(metalsmith.source(), filename)
}

// Transform the contents
debug(`rendering ${ext} extension for ${filename}`)

Expand Down Expand Up @@ -129,14 +128,12 @@ function validate({ filename, files, transform }) {
* @typedef {Object} Options
* @property {string} [pattern='**'] (*optional*) Limit the files to process by 1 or more glob patterns. Defaults to `'**'` (all)
* @property {Object} [engineOptions={}] (*optional*) Pass options to the jstransformer templating engine that's rendering your files. The default is `{}`
* @property {boolean} [setFilename=false] (*optional*) Some templating engines, like [pug](https://github.com/pugjs/pug), need a `filename` property to be present in the options to be able to process relative includes, extends, etc. Setting this option to `true` will add the current filename to the options passed to each jstransformer. The default is `false`
**/

/** @type {Options} */
const defaultOptions = {
pattern: '**',
engineOptions: {},
setFilename: false
engineOptions: {}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ describe('@metalsmith/in-place', () => {
})
})

it('should accept an option to set the filename in engine options', (done) => {
it('should understand the filename option in engine options to allow working with Pug and other transformers with a filename option', (done) => {
Metalsmith(fixture('set-filename'))
.use(plugin({ setFilename: true, transform: 'pug' }))
.use(plugin({ transform: 'pug', engineOptions: { filename: true } }))
.build((err) => {
if (err) done(err)
equal(fixture('set-filename/build'), fixture('set-filename/expected'))
Expand Down

0 comments on commit 064cc37

Please sign in to comment.