diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-12 15:04:30 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-05-12 15:04:30 +0200 |
commit | 28ac9ab88a009711837c379c3b18ab7f901299c6 (patch) | |
tree | a8e923968905e31c289f556aadb754a60adc2c7e | |
parent | 26acab4dfb9837df5d775e85b2f308956c732972 (diff) | |
download | gitbook-28ac9ab88a009711837c379c3b18ab7f901299c6.zip gitbook-28ac9ab88a009711837c379c3b18ab7f901299c6.tar.gz gitbook-28ac9ab88a009711837c379c3b18ab7f901299c6.tar.bz2 |
Fix #1299: pass fileExists as global function for template, accept null/undefined as param
-rw-r--r-- | lib/output/website/createTemplateEngine.js | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js index daa591a..8c31ea7 100644 --- a/lib/output/website/createTemplateEngine.js +++ b/lib/output/website/createTemplateEngine.js @@ -3,6 +3,7 @@ var nunjucks = require('nunjucks'); var DoExtension = require('nunjucks-do')(nunjucks); var Api = require('../../api'); +var deprecate = require('../../api/deprecate'); var JSONUtils = require('../../json'); var LocationUtils = require('../../utils/location'); var fs = require('../../utils/fs'); @@ -51,24 +52,44 @@ function createTemplateEngine(output, currentFile) { // Create API context var context = Api.encodeGlobal(output); + + /** + Check if a file exists + + @param {String} fileName + @return {Boolean} + */ + function fileExists(fileName) { + if (!fileName) { + return false; + } + + var filePath = PathUtils.resolveInRoot(outputFolder, fileName); + return fs.existsSync(filePath); + } + + /** + Return an article by its path + + @param {String} articlePath + @return {Object|undefined} + */ + function getArticleByPath(articlePath) { + var article = summary.getByPath(articlePath); + if (!article) return undefined; + + return JSONUtils.encodeSummaryArticle(article); + } + + return TemplateEngine.create({ loader: loader, context: context, globals: { - /** - Return an article by its path - - @param {String} articlePath - @return {Object|undefined} - */ - getArticleByPath: function(articlePath) { - var article = summary.getByPath(articlePath); - if (!article) return undefined; - - return JSONUtils.encodeSummaryArticle(article); - } + getArticleByPath: getArticleByPath, + fileExists: fileExists }, filters: defaultFilters.merge({ @@ -102,13 +123,9 @@ function createTemplateEngine(output, currentFile) { return LocationUtils.normalize(filePath); }, - /** - Check if a file exists - */ - fileExists: function(fileName) { - var filePath = PathUtils.resolveInRoot(outputFolder, fileName); - return fs.existsSync(filePath); - }, + + fileExists: deprecate.method(book, 'fileExists', fileExists, 'Filter "fileExists" is deprecated, use "fileExists(filename)" '), + getArticleByPath: deprecate.method(book, 'getArticleByPath', fileExists, 'Filter "getArticleByPath" is deprecated, use "getArticleByPath(filename)" '), contentURL: function(filePath) { return fileToURL(output, filePath); |