这个 metalsmith-in-place 构建脚本有什么问题?

What's wrong with this metalsmith-in-place build script?

我正在尝试使用 metalsmith-in-place 对源目录子目录中的文件进行一些就地模板化。它不起作用。模板标签不会被 frontmatter 替换。

我的构建脚本:

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.');
    }
  });

我的模板:

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

{{title}}

我的输出:

metalsmith_debug$ cat build/deeper/index.html

{{title}}

它适用于 source 中的文件;但我需要它来处理子目录。

您在 inplace 配置中的 pattern 很可能是 **/*.html 而不仅仅是 *.html

一些变化: 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. 您不需要在构建文件中要求 nunjucks,metalsmith-in-place 使用 consolidate,这将在必要时要求它。 (可以删除行)
  2. inplace内的pattern修改为**/*.html。有关详细信息,请参阅 Globbing patterns
  3. inplace 中不需要
  4. directory。 (可以删除行)

...以及对 source/deeper/index.html 的微小改动:

---
title: My pets
---

{{ title }}
  1. 在占位符 {{ title }} 周围添加了 space - Nunjucks 似乎认为这很重要。

现在应该可以为您工作,如果不行请告诉我。

接受的答案现在已经过时,因为 metalsmith-in-place 切换到使用 jstransformer 框架而不是 consolidate

我写了一篇关于如何使用 in-place 插件将 Nunjucks 与 Metalsmith 配对的文章:

这是缩小的工作示例:

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;
    }
  })
;