3

I am trying to use metalsmith-in-place to do some in-place templating on files in subdirectories of my source dir. It doesn't work. Template tags are not replaced by the frontmatter.

My build script:

var Metalsmith = require('metalsmith'),
  inplace = require('metalsmith-in-place'),
  nunjucks = require('nunjucks');

Metalsmith(__dirname)
  .source('./source')
  .use(inplace({
    engine: 'nunjucks',
    pattern: '*.html',
    directory: 'source/deeper'
  }))
  .destination('./build')
  .build(function(err) {
    if (err) {
      console.log(err);
    }
    else {
      console.info('Built it.');
    }
  });

My template:

metalsmith_debug$ cat source/deeper/index.html
---
title: My pets
---

{{title}}

My output:

metalsmith_debug$ cat build/deeper/index.html

{{title}}

It works on files in source; but I need it to work on subdirectories.

hoosteeno
  • 644
  • 5
  • 17

3 Answers3

3

A couple of changes: build.js:

var Metalsmith = require('metalsmith');
var inplace = require('metalsmith-in-place');
// var nunjucks = require('nunjucks');

Metalsmith(__dirname)
.source('./source')
.use(inplace({
    engine: 'nunjucks',
    pattern: '**/*.html' // modified pattern
    // directory: 'source/deeper' // Not needed
}))
.destination('./build')
.build(function(err) {
    if (err) {
        console.log(err);
    }
    else {
        console.info('Built it.');
    }
});
  1. You don't need to require nunjucks within the build file, metalsmith-in-place uses consolidate, this will require it where necessary. (Line can be removed)
  2. Modify pattern within inplace to **/*.html. For more information see Globbing patterns.
  3. directory isn't needed within inplace. (Line can be removed)

... and a minor change to source/deeper/index.html:

---
title: My pets
---

{{ title }}
  1. Added space around the placeholder {{ title }} - Nunjucks seems to think this is important.

Should work now for you, let me know if not.

Woody
  • 373
  • 2
  • 9
3

The accepted answer is outdated now, because metalsmith-in-place switched to use the jstransformer framework instead of consolidate.

I've written an article on how to use the in-place plugin to pair Nunjucks with Metalsmith:

Here's the minified working example:

const Metalsmith = require('metalsmith');
const inPlace = require('metalsmith-in-place');

Metalsmith(__dirname)
  .source('./src')
  .destination('./build')
  .use(inPlace({
    pattern: '**/*.njk',
    engineOptions: {
      path: __dirname + '/src'
    }
  }))
  .build(function (error) {
    if (error) {
      throw error;
    }
  })
;
Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
1

Your pattern in the inplace configuration should most likely be **/*.html rather than just *.html

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
  • I thought that might work, but in fact it causes a template error on build. As things stand, *.html gets the entire directory tree (so if I set the directory as source, I get the stuff in source/deeper). But the stuff in subdirectories is not processed. So it doesn't seem like an issue with the path? – hoosteeno Apr 08 '16 at 02:11
  • Just following up on my prior comment: The template error I was seeing was due to some invalid HTML in source tree. +1 for this answer, which was partial but correct. – hoosteeno Apr 08 '16 at 18:29