Skip to content

Commit

Permalink
Don't re-read files when stripping BOM, fixes #132
Browse files Browse the repository at this point in the history
  • Loading branch information
gakimball committed Jul 20, 2017
1 parent 884ed1d commit 79746db
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
8 changes: 5 additions & 3 deletions lib/loadPartials.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var path = require('path');
var utils = require('./utils');
var fs = require('fs');
var path = require('path');
var stripBom = require('strip-bom');
var utils = require('./utils');

/**
* Looks for files with .html, .hbs, or .handlebars extensions within the given directory, and adds them as Handlebars partials matching the name of the file.
Expand All @@ -10,7 +12,7 @@ module.exports = function(dir) {

for (var i in partials) {
var ext = path.extname(partials[i]);
var file = utils.readWithoutBOM(partials[i]);
var file = stripBom(fs.readFileSync(partials[i]));
var name = path.basename(partials[i], ext);

this.Handlebars.registerPartial(name, file.toString());
Expand Down
4 changes: 2 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ var extend = require('deepmerge');
var fm = require('front-matter');
var path = require('path');
var through = require('through2');
var stripBom = require('strip-bom');
var processRoot = require('./processRoot');
var utils = require('./utils');

module.exports = function() {
return through.obj(render.bind(this));
Expand All @@ -18,7 +18,7 @@ module.exports = function() {
function render(file, enc, cb) {
try {
// Get the HTML for the current page and layout
var page = fm(utils.readWithoutBOM(file.path).toString());
var page = fm(stripBom(file.contents.toString()));
var pageData;

// Determine which layout to use
Expand Down
19 changes: 0 additions & 19 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,3 @@ exports.loadFiles = function(dir, pattern) {

return files;
}

/**
* Skips over the UTF-8 BOM if it exists in the file
* @param {string} path File to load
* @return {Buffer} File contents without BOM
*/
exports.readWithoutBOM = function(path) {
var bomBytes = [0xEF, 0xBB, 0xBF];
var bomSize = bomBytes.length;
var fileData = fs.readFileSync(path);
var dataLength = fileData.length;
var hasBom = true;

for (var i = 0; (i < bomSize) && (i < dataLength) && hasBom; ++i) {
hasBom = fileData[i] === bomBytes[i];
}

return hasBom ? fileData.slice(bomSize) : fileData;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"marked": "^0.3.6",
"nopt": "^4.0.1",
"slash": "^1.0.0",
"strip-bom": "2.0.0",
"through2": "^2.0.0",
"vinyl-fs": "^2.4.4"
},
Expand Down

0 comments on commit 79746db

Please sign in to comment.