diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-11 10:51:36 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-11 10:51:36 +0100 |
commit | 093005bfd926fdae42706e0554ad1a985196d9d7 (patch) | |
tree | 6f8366d09e4b5630b4cbcdea311f2e8806703d00 /lib | |
parent | 514883f0bd7e44ac9f40482a6f4a44bbcd2e6f53 (diff) | |
download | gitbook-093005bfd926fdae42706e0554ad1a985196d9d7.zip gitbook-093005bfd926fdae42706e0554ad1a985196d9d7.tar.gz gitbook-093005bfd926fdae42706e0554ad1a985196d9d7.tar.bz2 |
Fix FSLoader for relative paths
Diffstat (limited to 'lib')
-rw-r--r-- | lib/template/fs-loader.js | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/template/fs-loader.js b/lib/template/fs-loader.js index 5720cb5..00c4743 100644 --- a/lib/template/fs-loader.js +++ b/lib/template/fs-loader.js @@ -7,12 +7,18 @@ var nunjucks = require('nunjucks'); Nunjucks loader similar to FileSystemLoader, but avoid infinite looping */ +function isRelative(filename) { + return (filename.indexOf('./') === 0 || filename.indexOf('../') === 0); +} + var Loader = nunjucks.Loader.extend({ init: function(searchPaths) { this.searchPaths = searchPaths.map(path.normalize); }, getSource: function(fullpath) { + if (!fullpath) return null; + fullpath = this.resolve(null, fullpath); if(!fullpath) { @@ -26,7 +32,18 @@ var Loader = nunjucks.Loader.extend({ }; }, + // We handle absolute paths ourselves in ".resolve" + isRelative: function() { + return true; + }, + resolve: function(from, to) { + // Relative template like "./test.html" + if (isRelative(to) && from) { + return path.resolve(path.dirname(from), to); + } + + // Absolute template to resolve in root folder var resultFolder = _.find(this.searchPaths, function(basePath) { var p = path.resolve(basePath, to); @@ -36,7 +53,7 @@ var Loader = nunjucks.Loader.extend({ && fs.existsSync(p) ); }); - + if (!resultFolder) return null; return path.resolve(resultFolder, to); } }); |