Detalhes do pacote

@metalsmith/in-place

metalsmith2.2kMIT5.0.0

A metalsmith plugin for in-place templating

in-place, templating, metalsmith-plugin, metalsmith

readme (leia-me)

@metalsmith/in-place

A metalsmith plugin for transforming source files' contents. Complements @metalsmith/layouts

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

Features

  • renders source files' contents field with any existing or a custom 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/in-place jstransformer-handlebars

Yarn:

yarn add @metalsmith/in-place 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/in-place to metalsmith.use :

import inPlace from '@metalsmith/in-place'

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

// same as shorthand
metalsmith.use(
  inPlace({
    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(inPlace({ transform: 'handlebars' }))

for a file:

---
title: Article title
---
<h1>{{ title }}</h1>Node v{{ nodeVersion }}

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

Multiple transforms can be used to target different sets of files, or to reprocess the same files multiple times in the order they are metalsmith.use'd:

// this build will apply the marked transform to index.md, the handlebars transform to index.hbs,
// and handlebars first, marked second to both index.hbs.md, index.md.hbs, and html-minifier to all (only in production)
metalsmith
  .env('NODE_ENV', process.env.NODE_ENV)
  .use(inPlace({ transform: 'handlebars', extname: null }))
  .use(inPlace({ transform: 'marked' }))

if (metalsmith.env('NODE_ENV') !== 'development') {
  metalsmith.use(inPlace({ transform: 'html-minifier' }))
}

Options

In most cases, you will only need to specify the transform 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.

Extension handling

By default in-place will apply smart default extension handling based on transform.inputFormats and transform.outputFormat. For example, any of the source files below processed through inPlace({ 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

The example demonstrates that:

  • order of extensions doesn't matter, order of plugin execution does!: you can pick the final extension to match the most suitable editor syntax highlighting
  • a single in-place run only alters the rightmost extension matching transform.inputFormats
  • you may choose to include or omit the transform.outputFormat in the source file name (.html in this case).

engineOptions

Pass options to the jstransformer that's rendering your templates via engineOptions. The metalsmith.json:

{
  "source": "src",
  "destination": "build",
  "plugins": [
    {
      "@metalsmith/in-place": {
        "transform": "ejs",
        "engineOptions": {
          "cache": false
        }
      }
    }
  ]
}

..would pass { "cache": false } to jstransformer-ejs.

If you use Pug, make sure to pass engineOptions: { filename: true }. This will ensure the filename of each processed file is passed to the render method as expected by this engine.

Multiple transforms per file

Suppose a file tags.hbs that lists all the article tags used on your website

---
title: Tags
description: Browse articles by tag
---
<h1>{{ title }}</h1>
<p>{{ description }}</p>
<ul>
{{#each tags}}
  <li><a href="/tags/{{ . }}">{{ . }}</a></li>
{{/each}}
</ul>

To reduce Handlebars noise, you could add metalsmith.use(inPlace({ transform: 'marked' }) to your build and change the filename to tags.hbs.md to generate markdown syntax with Handlebars!

---
title: Tags
description: Browse articles by tag
---

# {{ title }}

{{ description }}

{{#each tags}}
- [{{.}}](/tags/{{ . }})
{{/each}}

More markdown here..

Caution: when using multiple templating transforms per file, make sure there is no conflicting syntax. For example markdown will transform blocks indented by 4 spaces to <pre> tags, and marked's smartypants can potentially garble the result.

Usage with @metalsmith/layouts

In most cases @metalsmith/in-place is intended to be used before @metalsmith/layouts. 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({ engineOptions })) // -> index.html

@metalsmith/layouts 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/in-place*:

metalsmith.env('DEBUG', '@metalsmith/in-place*')

Alternatively you can set DEBUG to @metalsmith/* to debug all Metalsmith core plugins.

Credits

License

MIT

changelog (log de mudanças)

Changelog

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

Generated by auto-changelog.

v5.0.0

  • Closes #179: adds extname option & reworks multiple transform-per-file feature #179
  • BREAKING: Provides dual ESM/CJS exports cc84b5b
  • test: replace nyc with c8 for coverage, update mocha to latest a10f9c8
  • BREAKING: Adds required transform option to support custom transformers, no longer supports multiple transformers per instantiation a92de2b
  • Updates README.md & documents transform option 13f7fff
  • Updates README (aligned with other core plugins) 22cff02
  • refactor: moves utils to own file & makes them private 287dda6
  • BREAKING: remove suppressNoFilesError in favor of debug.warn message 993f8ca
  • Adds Typescript types 7400b27
  • BREAKING (for Pug users): remaps setFilename option to engineOptions.filename 064cc37
  • Removes missing metalsmith.match < v2.4.1 error 8c98954
  • Drops support for Node < 14.14.0 (Node 12 EOL 2023-04-30) 96a804e
  • Renames default export to inPlace for better intellisense a87f265

v4.6.0

2 February 2023

  • switched debugger to metalsmith.debug 911ef5b
  • Drops support for Node < 12 & Metalsmith < 2.5.0 1db3f53
  • debug method to throw if not instantiated 194be6b

v4.5.0

16 February 2022

  • feat: org migration, core plugin alignment ce3c8f1
  • feat: use metalsmith.match instead of multimatch, drop Node < 10 support 1a1d83f
  • feat: better jsdoc typehints & defaults mgmt ae06fb9
  • fix: don't mistake dots in folder paths for extensions 0392d6f

v4.4.2

9 March 2021

  • Move pug jstransformer to dev dependencies #177
  • Prepare 4.4.2 419417f

4.4.1

27 September 2019

  • Fix infinite loop rendering bug #173
  • fix(package): update multimatch to version 4.0.0 #171
  • Return when done #170
  • Prepare 4.4.1 d6127eb

v4.4.0

14 April 2019

  • Add set filename option #169
  • Remove unnecessary line #168
  • Prepare 4.4.0 bb56391

v4.3.0

7 April 2019

  • Add test for filename prefix on rendering error message #167
  • Allow async transforms #166
  • Add current file name to error message #164
  • Update readme #165
  • fix(package): update multimatch to version 3.0.0 #160
  • fix(package): update debug to version 4.0.0 #157
  • Improve readme #155
  • Ignore dev dependencies for greenkeeper 1afa4e1
  • Prepare 4.3.0 e2ccc9a

v4.2.0

19 July 2018

  • Add flag to suppress error when there are no files to process #151
  • chore(package): update dependencies #153
  • Update links to metalsmith org #152
  • Remove shields.io badges #150
  • Update eslint-airbnb-base #148
  • Greenkeeper/update to node 10 #146
  • Update license #144
  • chore(package): update lint-staged to version 7.0.0 #143
  • Fix readme mistake #142
  • Update new feature wording f387f5c
  • Prepare 4.2.0 afc0366

4.1.1

25 January 2018

  • Prepare 4.1.1 #141

4.1.0

25 January 2018

4.0.0

4 January 2018

  • Prepare 4.0.0 #129
  • v4 #120
  • Update dependencies to enable Greenkeeper 🌴 #125
  • Update for move #124
  • Update readme #117

3.0.1

2 August 2017

  • Prepare 3.0.1 #112

3.0.0

26 July 2017

  • Prepare 3.0.0 #109
  • Accept array as pattern #108
  • chore(package): update husky to version 0.14.3 #103
  • chore(package): update lint-staged to version 4.0.1 #104
  • Ignore eslint for greenkeeper #105
  • Minor updates #99
  • Update travis config #97
  • Add prettier #95
  • Use babel-preset-env #94
  • Add coverage #93
  • Update dependencies to enable Greenkeeper 🌴 #92
  • Rename history #90
  • Add jest #89
  • Add issue template #88
  • Run eslint on travis as well #83
  • Update Readme.md #79
  • chore(package): update husky to version 0.14.3 (#103) #101
  • chore(package): update lint-staged to version 4.0.1 (#104) #100

2.0.1

1 January 2017

2.0.0

1 January 2017

  • Update readme #76
  • Abstract rendering #72
  • Simplify babel setup #71
  • Add yarn lockfile #70
  • Test on same node versions as metalsmith/metalsmith #66

2.0.0-beta.1

27 September 2016

  • 2.0.0-beta.1 #57
  • Move to jstransformer #52
  • Error: Added filename to render callback. #51
  • Expose consolidate.requires fix #13 #13

1.4.4

3 May 2016

  • Ensure partial names never contain backslashes #42
  • Remove eslint dependency #40
  • Chores #39
  • Update readme #28
  • Update readme badges #37
  • Remove makefile eb3d176
  • Update readme abd4337
  • Add hound f7a122a

1.4.3

11 February 2016

  • Refactor rename option #36
  • Finish a complete render to .html files #33
  • add rename option cc28b70
  • Update readme and move renaming logic f8632f4
  • fix tests d4b65b2
  • Prevent path issues on windows 1550d28
  • update travis ed6dd6b

1.3.3

27 January 2016

  • Update consolidate and lodash.omit #34

1.3.2

17 October 2015

  • Update fs-readdir-recursive #30
  • Update readme #27
  • Refactor partials tests #26
  • Update eslint config and modularize #25
  • Update readme and dependencies 4080260
  • Boilerplate as example 43a0511

1.3.1

6 August 2015

  • Revert discarding unrecognised partials options #24

1.3.0

6 August 2015

  • Update readme and only accept partials string #23
  • Update eslint #22
  • Update #19
  • Add test for swig includes #21
  • Update readme #5
  • Add support for partials string 8b0e4a8
  • Update code style: disallow single line ifs 3721aaf
  • Add error handling 4407710

1.2.1

23 July 2015

  • Update swig #18

1.2.0

23 July 2015

1.1.1

20 July 2015

1.1.0

19 July 2015

1.0.1

22 November 2014

1.0.0

18 November 2014

  • Update for metalsmith 1.x #25
  • Possibility to pass options to the template engine (with lodash.omit). #8
  • Update readme, closes #1 #1
  • Remove everything not related to in-place templating, closes #2 #2
  • first commit 53b63f4
  • add rendering of files in place 21d8cbf
  • add in place option, change pattern to just restrict dcf8402
  • Render files in place by default dee5a2a
  • fix style 5b35555
  • Using 'lodash.omit' instead of own implementation. f01b629
  • Add installation instructions c1cfd65
  • Update package.json 2ee797d
  • update readme 8700e2a
  • update readme 9348283

4.4.2 - March 9, 2021

  • move incorrectly installed jstransformer-pug dependency to devDependencies, was only used during testing

4.4.1 - September 27, 2019

  • fix bug where it would keep processing if the last extension matches a jstransformer and its output extension

4.4.0 - April 14, 2019

  • add setFilename option, to set the absolute file path in the engine options

4.3.0 - April 7, 2019

  • prefix rendering errors with filename that caused the error
  • allow async transforms

4.2.0 - July 19, 2018

  • add suppressNoFilesError feature

4.1.1 - January 25, 2018

  • documentation fix

4.1.0 - January 25, 2018

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

4.0.0 - January 4, 2018

So this library went through a bit of churny phase, my apologies for that. It was caused by a couple of factors; moving the library to a new home, a new rendering engine and me trying to abstract said rendering engine for reuse in metalsmith-layouts.

However, the end result is now a stable plugin, that's easy to use and easy to maintain. Jstransformers are way simpler to debug, and so far I haven't even encountered any bugs. We're not abstracting the rendering engine because it's just not worth it, and confusing apis have been removed. All in all I hope that you'll enjoy this release and feel free to let me know if you encounter anything!

4.0.0-alpha.2 - October 7, 2017

  • update metalsmith-engine-jstransformer to 1.0.0-alpha.2

4.0.0-alpha.1 - October 7, 2017

  • update metalsmith-engine-jstransformer to 1.0.0-alpha.1

3.0.1 - August 2, 2017

  • update metalsmith-engine-jstransformer to 0.1.2

3.0.0 - July 26, 2017

  • dropped support for iojs and node 0.12
  • allow arrays for pattern option as well

2.0.1 - January 1, 2017

  • correct publishing mistake

2.0.0 - January 1, 2017

  • abstract templating, allows user to choose which engine to use for rendering (breaking change)

2.0.0-beta.1 - September 11, 2016

  • switch to jstransformers for rendering (breaking change)

1.4.4 - May 3, 2016

  • normalize partial name for windows

1.4.3 - February 11, 2016

  • add rename option
  • prevent path issue on windows

1.3.3 - January 27, 2016

  • update consolidate and lodash.omit

1.3.2 - October 17, 2015

  • update fs-readdir-recursive

1.3.1 - August 6, 2015

  • pass unrecognised partials options to consolidate

1.3.0 - August 6, 2015

  • add swig include test
  • add error handling for unrecognised engines
  • code style, dependency and readme updates
  • add partials option and test

1.2.1 - July 23, 2015

  • update swig

1.2.0 - July 23, 2015

  • update dependencies
  • update and add badges

1.1.1 - July 20, 2015

  • is-utf8 should be a dependency
  • add eslint
  • add release badge

1.1.0 - July 19, 2015

  • update tests
  • ignore binary files
  • add travis ci and david dm badges to readme
  • add gitattributes and editorconfig to repo

1.0.1 - November 22, 2014

  • change name to metalsmith-in-place

1.0.0 - November 18, 2014

  • render files in-place by default
  • remove default, directory and inPlace

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: