生成页面的路径与Assemble

9

我在处理 grunt-assemble 的 grunt 任务配置方面遇到了困难,它看起来像这样:

assemble: {
  options: {
    flatten: false,
    expand: true,

    assets: '',

    layout: 'default.hbs',
    layoutdir: 'templates/layouts',

    partials: ['templates/includes/*.hbs'],
    helpers: ['templates/helpers/*.js'],
    data: ['templates/data/*.{json,yml}']
  },

  dev: {
    src: 'templates/pages/**/*.hbs',
    dest: 'build/'
  }

项目模板的脚手架在assemble.io上看起来像这样:
templates
├── helpers
├── includes
│   ├── page-footer.hbs
│   ├── page-header.hbs
│   └── scripts.hbs
├── layouts
│   └── default.hbs
└── pages
    ├── en
    │   └── index.hbs
    ├── fr
    │   └── index.hbs
    └── index.hbs

我的愿望是获得类似于以下内容的东西:
build
├── en
│   └── index.html
├── fr
│   └── index.html
└── index.html

但是我得到的结果却像这样:
build
└── templates
    └── pages
        ├── en
           └── index.html
        ├── fr
           └── index.html
        └── index.html

我尝试了很多组合(使用flattenexpand选项以及cwd选项),但卡住了。
使用flatten会导致index.html文件相互覆盖。
所以,我实际上将其渲染到一个.tmp目录中,然后将文件移动到build目录中。我不喜欢这个解决方案,因为page.assets仍然损坏(其值将为../../..,对于根目录的index.html)。
2个回答

8

grunt-assemble

(请注意,此信息特指 grunt-assemble 0.4.x,它是 assemble 的 grunt 插件,但具有完全不同的 API)

@doowb 差不多对了,尝试将 expand: trueext: '.html' 添加到文件配置中:

assemble: {
  options: {
    flatten: false,
    expand: true,

    assets: '',

    layout: 'default.hbs',
    layoutdir: 'templates/layouts',

    partials: ['templates/includes/*.hbs'],
    helpers: ['templates/helpers/*.js'],
    data: ['templates/data/*.{json,yml}']
  },

  dev: {
    files: [
      {expand: true, cwd: 'templates/pages/', src: '**/*.hbs', dest: 'build/', ext: '.html'}
    ]
  }
}

此外,请参考https://github.com/assemble/assemble-contrib-permalinks

assemble 0.7.x

在assemble 0.7.0中,集合和插件均为一等公民,因此生成相对链接、创建自定义固定链接以及构建分页等操作变得更加容易。

如果您使用的是assemble 0.7.x及以上版本,则应使用assemble-permalinks插件实现相关功能。


2
非常感谢!我一直在看正确的地方,但是files对象或其他可能的语法有点令人困惑。 - zeropaper
使用Node Assemble @jonschlinkert @doowb,应该如何实现呢? 是这样吗... app.pages([页面路径], {[文件配置]}),还是目前只能用Grunt? - Tom Gillard

2

你尝试过使用在grunt目标中带有cwd属性的扩展files对象吗?

assemble: {
  options: {
    flatten: false,
    expand: true,

    assets: '',

    layout: 'default.hbs',
    layoutdir: 'templates/layouts',

    partials: ['templates/includes/*.hbs'],
    helpers: ['templates/helpers/*.js'],
    data: ['templates/data/*.{json,yml}']
  },

  dev: {
    files: [
      { cwd: 'templates/pages/', src: '**/*.hbs', dest: 'build/' }
    ]
  }
}

1
我确实遇到了文件找不到的错误,看起来是这样的:错误:无法读取“index.hbs”文件(错误代码:ENOENT)。 位于 Object.util.error(..../node_modules/grunt/lib/grunt/util.js:57:39) 位于 Object.file.read(..../node_modules/grunt/lib/grunt/file.js:237:22) 位于 buildPage(..../node_modules/assemble/tasks/assemble.js:289:78) 位于 ..../node_modules/assemble/tasks/assemble.js:345:23 - zeropaper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接