Skip to content

Commit

Permalink
Move handlebars helpers to own folder
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Jun 21, 2020
1 parent 5cbb15f commit 2a2d024
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
11 changes: 5 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const fs = require('fs').promises;
const pkg = require('./package.json');

module.exports = function(grunt) {

require('time-grunt')(grunt);
require('load-grunt-tasks')(grunt);

Expand Down Expand Up @@ -84,7 +83,7 @@ module.exports = function(grunt) {
expand: true,
flatten: true,
assets: '<%= config.dist %>/assets',
helpers: ['<%= config.src %>/assets/js/translation.js', '<%= config.src %>/assets/js/cache-busting.js'],
helpers: ['helpers/translation.js', 'helpers/cache-busting.js'],
layout: '<%= config.src %>/templates/layouts/default.hbs',
data: [
'<%= config.src %>/data/**/*.{json,yml}',
Expand All @@ -98,24 +97,24 @@ module.exports = function(grunt) {
i18n: {
languages: pkg.languages,
templates: [
"<%= config.src %>/templates/pages/**/*.hbs",
'<%= config.src %>/templates/pages/**/*.hbs',
]
},
permalinks: {
structure: ':lang/:slug/:base:ext',
patterns: [
{
pattern: ':lang',
replacement: function () {
replacement: function() {
return this.language.toLowerCase() === 'en' ? '' : this.language;
}
},
{
pattern: ':base',
replacement: function () {
var check = this.basename.lastIndexOf(this.language.toLowerCase());
if (check > -1){
return this.basename.substring(0, check-1);
if (check > -1) {
return this.basename.substring(0, check - 1);
} else return this.basename;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/assets/js/cache-busting.js → helpers/cache-busting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ module.exports.register = function(Handlebars, options) {
const filePath = options.fn(this);
const hash = crypto.createHash('sha256');
// creating full path from the root dir to the file
const fullPath = path.join(__dirname, "../../..", "dist", filePath);
let hashCode = "";
const fullPath = path.join(__dirname, '..', 'dist', filePath);
let hashCode = '';
hash.on('readable', () => {
const data = hash.read();
if (data) hashCode = data.toString('hex');
})
hash.write(fs.readFileSync(fullPath, function (err) { if (err) return 0 }).toString());
});
hash.write(fs.readFileSync(fullPath, {
encoding: 'utf8'
}));
hash.end();
// creating a version string from first 6 hash characters
hashCode = "/" + filePath + "?v=" + hashCode.substr(0, 6);
hashCode = '/' + filePath + '?v=' + hashCode.substr(0, 6);
return hashCode;
});
};
File renamed without changes.

0 comments on commit 2a2d024

Please sign in to comment.