Skip to content

Commit

Permalink
Merge bf83ecf into 298a0aa
Browse files Browse the repository at this point in the history
  • Loading branch information
hatakawas committed Oct 4, 2019
2 parents 298a0aa + bf83ecf commit d5042d1
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
63 changes: 58 additions & 5 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
'use strict';


const handlebars = require('handlebars');
const Handlebars = require('handlebars');
const path = require('path');
const fs = require('fs');

// const fs = require('fs');
let isPartialsRegistered = false;
const PARTIAL_EXTNAME = '.hbs';

function render(data, locals) {
return doRenderInternal(data, locals);
}

function doRenderInternal(data, locals) {
let template = handlebars.compile(data.text);
const template = Handlebars.compile(data.text);
// Actually, data.path always be effective in Hexo
if (!isPartialsRegistered && data.path) {
const basedir = data.rootPath || path.dirname(data.path);
registerPartials(basedir);
}
if (data.path) {
isPartialsRegistered = true;
}
return template(Object.assign({filename: data.path}, locals));
}

function render(data, locals) {
return doRenderInternal(data, locals);
function registerPartials(basedir) {
const files = fs.readdirSync(basedir);
const partialDirs = [];
files.forEach(file => {
const filePath = path.join(basedir, file);
const stat = fs.statSync(filePath);
// partial dir must start with '_'
if (stat.isDirectory() && file.startsWith('_')) {
partialDirs.push(filePath);
}
});
const partialFiles = [];
partialDirs.forEach(d => {
partialFiles.push(...listPartialFiles(d));
});
partialFiles.forEach(file => {
const start = basedir.length + 1;
const length = file.length - start - PARTIAL_EXTNAME.length;
const name = file.substr(start, length);
const data = fs.readFileSync(file, {encoding: 'utf-8'});
Handlebars.registerPartial(name, data);
});
}


function listPartialFiles(dir, list = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isFile()){
// partial file must have '.hbs' extname.
if (path.extname(filePath) === PARTIAL_EXTNAME) {
list.push(filePath);
}
} else {
listPartialFiles(filePath, list);
}
});
return list;
}


module.exports = render;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-renderer-handlebars13",
"version": "0.0.1",
"name": "hexo-renderer-hbars",
"version": "0.0.3",
"description": "Handlebars renderer plugin for hexo",
"main": "index.js",
"scripts": {
Expand Down
Empty file added test/_partial/inner/file.hbs
Empty file.
1 change: 1 addition & 0 deletions test/_partial/the-partial.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
handlebars
16 changes: 8 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('Handlebars renderer tests', function() {

it('escaped variable test', function() {
const source = 'Hello, {{name}}!';

const result = render({text: source}, {
name: 'handlebars'
});
Expand Down Expand Up @@ -123,11 +122,12 @@ describe('Handlebars renderer tests', function() {
// }).should.eql('Hello world!');
// });
//
// it('partials test', function () {
// var body = [
// 'Hello {{> _partial/test-partial }}!'
// ].join('\n')
//
// r({text: body, path: 'test/test.mustache'}).should.eql('Hello world!')
// })
it('partials test', function() {
const source = 'Hello, {{> _partial/the-partial }}!';
// const source = 'Hello, {{name}}!';
const result = render({text: source, path: 'test/main', rootPath: 'test'}, {
name: 'handlebars'
});
assert.equal(result, 'Hello, handlebars!');
});
});
1 change: 1 addition & 0 deletions test/main.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, {{> 'test/the-partial.hbs' }}!

0 comments on commit d5042d1

Please sign in to comment.