diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-09-15 12:27:07 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-09-15 12:27:07 +0200 |
commit | 57a0c3bad8333d93815257d7ff603bc34b8b3f4d (patch) | |
tree | cf5f447e84e3b4ac2d74d7092d8c522bd6e67b79 /lib/book.js | |
parent | 463a947df1e5c8c862c555a5b0ae675e356a0d5c (diff) | |
parent | 2a52326a454a23444bd8f0395a9ab8a1f5f68831 (diff) | |
download | gitbook-57a0c3bad8333d93815257d7ff603bc34b8b3f4d.zip gitbook-57a0c3bad8333d93815257d7ff603bc34b8b3f4d.tar.gz gitbook-57a0c3bad8333d93815257d7ff603bc34b8b3f4d.tar.bz2 |
Merge pull request #929 from GitbookIO/feature/improve_resolve
Improve paths resolving for conrefs
Diffstat (limited to 'lib/book.js')
-rw-r--r-- | lib/book.js | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/book.js b/lib/book.js index b306c51..08dd6dc 100644 --- a/lib/book.js +++ b/lib/book.js @@ -10,6 +10,7 @@ var fs = require("./utils/fs"); var parseNavigation = require("./utils/navigation"); var parseProgress = require("./utils/progress"); var pageUtil = require("./utils/page"); +var pathUtil = require("./utils/path"); var batch = require("./utils/batch"); var links = require("./utils/links"); var i18n = require("./utils/i18n"); @@ -630,21 +631,26 @@ Book.prototype.findFile = function(filename) { // Check if a file exists in the book Book.prototype.fileExists = function(filename) { return fs.exists( - path.join(this.root, filename) + this.resolve(filename) ); }; +// Check if a file path is inside the book +Book.prototype.fileIsInBook = function(filename) { + return pathUtil.isInRoot(this.root, filename); +}; + // Read a file Book.prototype.readFile = function(filename) { return fs.readFile( - path.join(this.root, filename), + this.resolve(filename), { encoding: "utf8" } ); }; // Return stat for a file Book.prototype.statFile = function(filename) { - return fs.stat(path.join(this.root, filename)); + return fs.stat(this.resolve(filename)); }; // List all files in the book @@ -702,9 +708,20 @@ Book.prototype.isEntryPoint = function(fp) { return fp == this.readmeFile; }; -// Resolve a path in book -Book.prototype.resolve = function(p) { - return path.resolve(this.root, p); +// Alias to book.config.get +Book.prototype.getConfig = function(key, def) { + return this.config.get(key, def); +}; + +// Resolve a path in the book source +// Enforce that the output path in the root folder +Book.prototype.resolve = function() { + return pathUtil.resolveInRoot.apply(null, [this.root].concat(_.toArray(arguments))); +}; + +// Convert an abslute path into a relative path to this +Book.prototype.relative = function(p) { + return path.relative(this.root, p); }; // Normalize a path to .html and convert README -> index |