包详细信息

metalsmith-in-place

metalsmith18.4kMIT不推荐使用4.4.2

This package is no longer maintained and has moved to the @metalsmith org; Please migrate to the following package: @metalsmith/in-place

A metalsmith plugin for in-place templating.

自述文件

metalsmith-in-place

build status greenkeeper

A metalsmith plugin for transforming your source files

This plugin allows you to render templating syntax in your source files. It uses file extensions to infer which templating engine to use. So files ending in .njk will be processed as nunjucks, .md as markdown, etc. You can even chain transformations by appending multiple extensions, which will be processed right-to-left.

If you want to wrap your source files in a common template, you can use metalsmith-layouts. For usage examples check out our wiki. Feel free to contribute an example if anything is missing, or update the existing ones. For support questions please use stack overflow or our slack channel. For templating engine specific questions try the aforementioned channels, as well as the documentation for jstransformers and your templating engine of choice.

Installation

$ npm install metalsmith-in-place

This plugin uses jstransformers to transform files. Since there are a lot of jstransformers we don't install them automatically, so you'll also need to install the appropriate jstransformers.

For example, to render markdown you would install jstransformer-markdown. To render handlebars you would install jstransformer-handlebars. See the jstransformer organisation for all available jstransformers and this dictionary to see which extensions map to which jstransformer.

Options

You can pass options to metalsmith-in-place with the Javascript API or CLI. The options are:

  • pattern: optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is **.
  • engineOptions: optional. Use this to pass options to the jstransformer that's rendering your files. The default is {}.
  • suppressNoFilesError: optional. The no-files-to-process error will be suppressed. The default is false.
  • setFilename: optional. Some templating engines, like 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

Only files that match this pattern will be processed. So this metalsmith.json:

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-in-place": {
      "pattern": "blog/**/*"
    }
  }
}

Would only process files within the ./src/blog folder, because the pattern is relative to your source folder. See multimatch for further details.

engineOptions

Use this to pass options to the jstransformer that's rendering your templates. So this metalsmith.json:

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-in-place": {
      "engineOptions": {
        "cache": false
      }
    }
  }
}

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

suppressNoFilesError

metalsmith-in-place exits with an error if it can’t find any files to process. If you’re doing any kind of incremental builds via something like metalsmith-watch, this is problematic as you’re likely only rebuilding files that have changed. This flag allows you to suppress that error. So this metalsmith.json:

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-in-place": {
      "suppressNoFilesError": true
    }
  }
}

Would suppress the error if there aren't any files to process. Note that when this option is turned on, if you're logging debug messages, you’ll still see a message denoting when there aren't any files for metalsmith-layouts to process.

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:

{
  "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.

Errors and debugging

If you're encountering problems you can use debug to enable verbose logging. To enable debug prefix your build command with DEBUG=metalsmith-in-place. So if you normally run metalsmith to build, use DEBUG=metalsmith-in-place metalsmith (on windows the syntax is slightly different).

No files to process

There are several things that might cause you to get a no files to process error:

  • Your pattern does not match any files
  • None of your files pass validation, validation fails for files that:
    • Have no extension
    • Are not utf-8
    • Need a jstransformer that hasn't been installed

Credits

License

MIT

更新日志

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: