diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-12 12:19:00 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-05-12 12:19:00 +0200 |
commit | a091c5b331282bbcc4e0a3b79fbc32dd6f842747 (patch) | |
tree | 39bcc8a395111f059f4e65dc44f072b28a3d10a3 /lib/templating | |
parent | 784b3379b50bb0c2986efaca55940d14feb10d3a (diff) | |
download | gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.zip gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.tar.gz gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.tar.bz2 |
Fix path resolution when generating page with conrefs
Diffstat (limited to 'lib/templating')
-rw-r--r-- | lib/templating/__tests__/conrefsLoader.js | 38 | ||||
-rw-r--r-- | lib/templating/conrefsLoader.js | 9 | ||||
-rw-r--r-- | lib/templating/render.js | 2 | ||||
-rw-r--r-- | lib/templating/renderFile.js | 7 |
4 files changed, 52 insertions, 4 deletions
diff --git a/lib/templating/__tests__/conrefsLoader.js b/lib/templating/__tests__/conrefsLoader.js index 8bf4ad2..bcbee05 100644 --- a/lib/templating/__tests__/conrefsLoader.js +++ b/lib/templating/__tests__/conrefsLoader.js @@ -35,6 +35,44 @@ describe('ConrefsLoader', function() { }); }); + describe('Local', function() { + var engine = TemplateEngine({ + loader: new ConrefsLoader(dirName) + }); + + describe('Relative', function() { + it('should resolve basic relative filepath', function() { + return renderTemplate(engine, fileName, '{% include "include.md" %}') + .then(function(out) { + expect(out.getContent()).toBe('Hello World'); + }); + }); + + it('should resolve basic parent filepath', function() { + return renderTemplate(engine, path.join(dirName, 'hello/test.md'), '{% include "../include.md" %}') + .then(function(out) { + expect(out.getContent()).toBe('Hello World'); + }); + }); + }); + + describe('Absolute', function() { + it('should resolve absolute filepath', function() { + return renderTemplate(engine, fileName, '{% include "/include.md" %}') + .then(function(out) { + expect(out.getContent()).toBe('Hello World'); + }); + }); + + it('should resolve absolute filepath when in a directory', function() { + return renderTemplate(engine, path.join(dirName, 'hello/test.md'), '{% include "/include.md" %}') + .then(function(out) { + expect(out.getContent()).toBe('Hello World'); + }); + }); + }); + }); + describe('transform', function() { function transform(filePath, source) { return 'test-' + source + '-endtest'; diff --git a/lib/templating/conrefsLoader.js b/lib/templating/conrefsLoader.js index 52d2a95..475aa92 100644 --- a/lib/templating/conrefsLoader.js +++ b/lib/templating/conrefsLoader.js @@ -62,7 +62,14 @@ var ConrefsLoader = nunjucks.Loader.extend({ resolve: function(from, to) { // If origin is in the book, we enforce result file to be in the book if (PathUtils.isInRoot(this.rootFolder, from)) { - var href = LocationUtils.toAbsolute(to, path.dirname(from), this.rootFolder); + + // Path of current template in the rootFolder (not absolute to fs) + var fromRelative = path.relative(this.rootFolder, from); + + // Resolve "to" to a filepath relative to rootFolder + var href = LocationUtils.toAbsolute(to, path.dirname(fromRelative), ''); + + // Return absolute path return PathUtils.resolveInRoot(this.rootFolder, href); } diff --git a/lib/templating/render.js b/lib/templating/render.js index a20548b..68396f7 100644 --- a/lib/templating/render.js +++ b/lib/templating/render.js @@ -7,7 +7,7 @@ var replaceShortcuts = require('./replaceShortcuts'); Render a template @param {TemplateEngine} engine - @param {String} filePath + @param {String} filePath: absolute path for the loader @param {String} content @param {Object} context @return {Promise<TemplateOutput>} diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js index 185bec1..9204090 100644 --- a/lib/templating/renderFile.js +++ b/lib/templating/renderFile.js @@ -13,14 +13,17 @@ var render = require('./render'); function renderTemplateFile(engine, filePath, context) { var loader = engine.getLoader(); + // Resolve the filePath + var resolvedFilePath = loader.resolve(null, filePath); + return Promise() .then(function() { if (!loader.async) { - return loader.getSource(filePath); + return loader.getSource(resolvedFilePath); } var deferred = Promise.defer(); - loader.getSource(filePath, deferred.makeNodeResolver()); + loader.getSource(resolvedFilePath, deferred.makeNodeResolver()); return deferred.promise; }) .then(function(result) { |