【问题标题】:What's wrong with this metalsmith-in-place build script?这个 metalsmith-in-place 构建脚本有什么问题?
【发布时间】:2016-07-29 01:49:37
【问题描述】:

我正在尝试使用 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 中的文件;但我需要它来处理子目录。

【问题讨论】:

    标签: nunjucks metalsmith


    【解决方案1】:

    一些变化: 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 中不需要directory。 (线可以去掉)

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

    ---
    title: My pets
    ---
    
    {{ title }}
    
    1. 在占位符 {{ title }} - Nunjucks 周围添加空间似乎认为这很重要。

    现在应该对你有用,如果没有,请告诉我。

    【讨论】:

      【解决方案2】:

      现在接受的答案已经过时,因为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;
          }
        })
      ;
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我认为这可能有效,但实际上它会导致构建时出现模板错误。就目前的情况而言,*.html 获取整个目录树(因此,如果我将目录设置为源,我会在源/更深的目录中获取内容)。但是子目录中的东西没有被处理。所以这似乎不是路径的问题?
        • 只是跟进我之前的评论:我看到的模板错误是由于源代码树中的一些无效 HTML 造成的。为这个答案 +1,这是部分但正确的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-27
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多