Skip to content

Commit

Permalink
Resolve #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterzl, Christian authored and Sterzl, Christian committed Mar 20, 2015
1 parent 1811440 commit 3597953
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ Metalsmith(__dirname)

Accepts an array of keys to ignore in the output.

#### bufferencoding

**Since: 0.4.3**
**Default: false**
**See: [Buffer.toString](https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end)**
**Resolves: [Issue #1](https://github.com/Waxolunist/metalsmith-writemetadata/issues/1)**

```js
Metalsmith(__dirname)
.use(writemetadata({
bufferencoding: 'utf8'
}));
```

If this value is set, the contents field which is internally represented as a Buffer, will be converted to a string
by calling the method Buffer.toString with the encoding specified in the options.

#### collections

If used together with the collections plugin, this plugin can also write collections.
Expand Down Expand Up @@ -101,6 +118,7 @@ Metalsmith(__dirname)
.use(writemetadata({
pattern: ['**/*.html'],
ignorekeys: ['next', 'previous'],
bufferencoding: 'utf8',
collections: {
projects: {
output: {
Expand Down
18 changes: 15 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function plugin(opts){
opts.pattern = opts.pattern || [];
opts.ignorekeys = Array.isArray(opts.ignorekeys) ? opts.ignorekeys : [];
opts.collections = opts.collections || {};
opts.space = opts.space || '';
opts.bufferencoding = opts.bufferencoding || false;
return function (files, metalsmith, done){
var metadata = metalsmith.metadata();
setImmediate(done);
Expand All @@ -27,7 +29,10 @@ function plugin(opts){
var json = basename(file, extname(file)) + '.json';
if ('.' != dir) json = dir + '/' + json;
debug('Write file ' + json);


if(opts.bufferencoding && files[file].contents instanceof Buffer) {
files[file].contents = files[file].contents.toString(opts.bufferencoding);
}
data.contents = new Buffer(
circularjson.stringify(
files[file],
Expand All @@ -37,7 +42,7 @@ function plugin(opts){
}
return v;
},
'\t'
opts.space
)
);

Expand All @@ -63,6 +68,12 @@ function plugin(opts){
contents = {};
contents.name = key;
contents.total = collection.length;
for(var i in collection) {
if(opts.bufferencoding && collection[i].contents instanceof Buffer) {
collection[i].contents = collection[i].contents.toString(opts.bufferencoding);
console.log(collection[i].contents);
}
}
contents.result = collection;
contents = extend(contents, config.output.metadata);
}
Expand All @@ -75,7 +86,7 @@ function plugin(opts){
}
return v;
},
'\t'
opts.space
)
);
files[config.output.path] = data;
Expand All @@ -85,3 +96,4 @@ function plugin(opts){
}



0 comments on commit 3597953

Please sign in to comment.