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 | |
parent | 784b3379b50bb0c2986efaca55940d14feb10d3a (diff) | |
download | gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.zip gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.tar.gz gitbook-a091c5b331282bbcc4e0a3b79fbc32dd6f842747.tar.bz2 |
Fix path resolution when generating page with conrefs
-rw-r--r-- | lib/output/generatePage.js | 5 | ||||
-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 | ||||
-rw-r--r-- | lib/utils/location.js | 13 |
6 files changed, 65 insertions, 9 deletions
diff --git a/lib/output/generatePage.js b/lib/output/generatePage.js index 7e4e454..5b0d4f6 100644 --- a/lib/output/generatePage.js +++ b/lib/output/generatePage.js @@ -1,3 +1,5 @@ +var path = require('path'); + var Promise = require('../utils/promise'); var error = require('../utils/error'); var timing = require('../utils/timing'); @@ -44,7 +46,8 @@ function generatePage(output, page) { // Render templating syntax .then(function(content) { - return Templating.render(engine, filePath, content, context); + var absoluteFilePath = path.join(book.getContentRoot(), filePath); + return Templating.render(engine, absoluteFilePath, content, context); }) .then(function(output) { 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) { diff --git a/lib/utils/location.js b/lib/utils/location.js index b2ac813..17edc00 100644 --- a/lib/utils/location.js +++ b/lib/utils/location.js @@ -40,7 +40,7 @@ function normalize(s) { } /** - Convert relative to absolute path + Convert a relative path to absolute @param {String} href @param {String} dir: directory parent of the file currently in rendering process @@ -48,7 +48,10 @@ function normalize(s) { @return {String} */ function toAbsolute(_href, dir, outdir) { - if (isExternal(_href) || isDataURI(_href)) return _href; + if (isExternal(_href) || isDataURI(_href)) { + return _href; + } + outdir = outdir == undefined? dir : outdir; _href = normalize(_href); @@ -56,8 +59,10 @@ function toAbsolute(_href, dir, outdir) { outdir = normalize(outdir); // Path "_href" inside the base folder - var hrefInRoot = path.normalize(path.join(dir, _href)); - if (_href[0] == '/') hrefInRoot = path.normalize(_href.slice(1)); + var hrefInRoot = normalize(path.join(dir, _href)); + if (_href[0] == '/') { + hrefInRoot = normalize(_href.slice(1)); + } // Make it relative to output _href = path.relative(outdir, hrefInRoot); |