-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
72 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
handlebars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, {{> 'test/the-partial.hbs' }}! |