Skip to content

Commit

Permalink
v1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
hatakawas committed Oct 9, 2019
1 parent 8fff2cd commit 563aad9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ script:
after_script:
- npm install coveralls
- nyc report --reporter=text-lcov | coveralls

dist: trusty
31 changes: 13 additions & 18 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@ const fs = require('fs');
const PARTIAL_EXTNAME = '.hbs';

let isPartialsRegistered = false;
let hexo;
let hexo = {};


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

function doRenderInternal(data, locals) {
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) {
const partialBasedir = hexo ? path.join(hexo.theme_dir, 'layout') : data.rootPath;
if (!isPartialsRegistered && fs.existsSync(partialBasedir) && fs.statSync(partialBasedir).isDirectory()) {
registerPartials(partialBasedir);
isPartialsRegistered = true;
}
return template(Object.assign({filename: data.path}, locals));
const context = Object.assign({filename: data.path}, locals);
return Handlebars.compile(data.text)(context);
}

function registerPartials(basedir) {
const partialFiles = Object.assign([], listPartialFiles(basedir));

function registerPartials(partialBasedir) {
const partialFiles = listPartialFiles(partialBasedir);
const partialNames = [];
partialFiles.forEach(file => {
const start = basedir.length + 1;
const start = partialBasedir.length + 1;
const length = file.length - start - PARTIAL_EXTNAME.length;
const name = file.substr(start, length);
const data = fs.readFileSync(file, {encoding: 'utf-8'});
Expand All @@ -44,12 +39,12 @@ function registerPartials(basedir) {
}

function listPartialFiles(dir, list) {
list = list || [];
const files = fs.readdirSync(dir);
list = list || [];
files.forEach(file => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isFile()) {
// partial file must have '.hbs' extname.
// Partial file must have '.hbs' extname.
if (path.extname(filePath) === PARTIAL_EXTNAME) {
list.push(filePath);
}
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-renderer-hbars",
"version": "1.1.2",
"version": "1.1.3",
"description": "Handlebars renderer plugin for hexo",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -40,5 +40,8 @@
"hexo-fs": "^2.0.0",
"mocha": "^6.2.1",
"nyc": "^14.1.1"
},
"engines": {
"node": ">=6.0.0"
}
}

0 comments on commit 563aad9

Please sign in to comment.