Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Last active February 3, 2018 05:56
Show Gist options
  • Save jonschlinkert/debab424de26a0225cea to your computer and use it in GitHub Desktop.
Save jonschlinkert/debab424de26a0225cea to your computer and use it in GitHub Desktop.
Assemble collections example
---
tags:
- a
- b
- c
---
Make a few pages like this, with the same or different tags in the front-matter.
var assemble = require('assemble');
var through = require('through2');
var extname = require('gulp-extname');
var File = require('vinyl');
/**
* Create a new template type for caching and loading
* "index" templates
*/
assemble.create('index', 'indices', {isRenderable: true});
assemble.indices('templates/indices/*.hbs');
/**
* Task with:
* - 'tags' plugin, which currently only will generate a list (index) of tags from front-matter of all files in the task
* - 'extname' plugin for renaming extensions to 'html'
*/
assemble.task('default', function () {
assemble.src('templates/*.hbs')
.pipe(tags('basic'))
.pipe(extname())
.pipe(assemble.dest('result/'));
});
/**
* 'tags' plugin
*
* - in the main function, we push the file into a `files`
* array, then we push the file back into the stream
* - in the flush function we take the `files` array that we built up,
* then we create a new vinyl file using the template defined by the
* user in the task. in this case, the template name is `basic`
*/
function tags(template) {
var files = [];
return through.obj(function (file, enc, cb) {
files.push(file);
this.push(file);
cb();
}, function (cb) {
var stream = this;
var arr = [];
// here, we add the tags from front-matter to an array.
// we could make this more useful by also adding the dest
// path of the file and other info.
files.forEach(function (file) {
arr = arr.concat(file.data.tags);
});
var tmpl = assemble.views.indices[template];
tmpl.data.tags = arr;
assemble.render(tmpl, function (err, content) {
if (err) console.log(err);
var file = new File({path : 'index.html'});
// `data` needed for assemble
file.data = {};
file.contents = new Buffer(content);
stream.push(file);
cb();
});
});
}
{{#tags}}
<span>{{.}}</span>
{{/tags}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment