Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,16 @@ module.exports = function(opts) {
// compose them together into one function
var filter = filterlist.reduce(compose);

// check match for filter options object
var options = match.match('{([^}]*)}');

// and apply the composed function to the stringified content
return filter(String(includeContent));
if(options) {
options = JSON.parse(options[0]);
return filter(String(includeContent), options);
} else {
return filter(String(includeContent));
}
}
};

Expand Down
37 changes: 37 additions & 0 deletions test/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ describe('## gulp-file-include', () => {
stream.write(file);
stream.end();
});

it('file - filters: custom filter handler options', done => {
var file = new gutil.File({
path: 'test/fixtures/index-handler-options.html',
contents: fs.createReadStream('test/fixtures/index-handler-options.html')
});

var stream = fileIncludePlugin({
prefix: '@@',
basepath: '@file',
filters: {
handler: function(content, options) {
should.exist(options);
should.exist(options.age);
should.exist(options.name);

// other handler code or
// template engine compiling here, i.e.:
// var template = Handlebars.compile(content);
// return template(options);

return content;
}
}
});

stream.on('data', newFile => {
should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(result);
done();
});

stream.write(file);
stream.end();
});
});
});

Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/index-handler-options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>
@@include('view.html')
@@include(handler('var.html', {
"name": "haoxin",
"age": 12345
}))
</body>
</html>