summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-05-12 15:12:08 +0200
committerSamy Pessé <samypesse@gmail.com>2016-05-12 15:12:08 +0200
commit2e74fafa36c35c11148c09d4745402f23c53c1f8 (patch)
treef620a2c1dc056af4ff3e61b60c3ec53ed99571c7
parent28ac9ab88a009711837c379c3b18ab7f901299c6 (diff)
downloadgitbook-2e74fafa36c35c11148c09d4745402f23c53c1f8.zip
gitbook-2e74fafa36c35c11148c09d4745402f23c53c1f8.tar.gz
gitbook-2e74fafa36c35c11148c09d4745402f23c53c1f8.tar.bz2
Add template method "getPageByPath"
-rw-r--r--lib/models/output.js14
-rw-r--r--lib/output/helper/resolveFileToURL.js3
-rw-r--r--lib/output/website/createTemplateEngine.js20
3 files changed, 32 insertions, 5 deletions
diff --git a/lib/models/output.js b/lib/models/output.js
index 43e36f8..0f008ec 100644
--- a/lib/models/output.js
+++ b/lib/models/output.js
@@ -1,6 +1,7 @@
var Immutable = require('immutable');
var Book = require('./book');
+var LocationUtils = require('../utils/location');
var Output = Immutable.Record({
book: Book(),
@@ -53,6 +54,19 @@ Output.prototype.getState = function() {
};
/**
+ Return a page byt its file path
+
+ @param {String} filePath
+ @return {Page|undefined}
+*/
+Output.prototype.getPage = function(filePath) {
+ filePath = LocationUtils.normalize(filePath);
+
+ var pages = this.getPages();
+ return pages.get(filePath);
+};
+
+/**
Get root folder for output
@return {String}
diff --git a/lib/output/helper/resolveFileToURL.js b/lib/output/helper/resolveFileToURL.js
index 3dba8f7..026b0e5 100644
--- a/lib/output/helper/resolveFileToURL.js
+++ b/lib/output/helper/resolveFileToURL.js
@@ -13,8 +13,7 @@ function resolveFileToURL(output, filePath) {
// Convert /test.png -> test.png
filePath = LocationUtils.toAbsolute(filePath, '', '');
- var pages = output.getPages();
- var page = pages.get(filePath);
+ var page = output.getPage(filePath);
// if file is a page, return correct .html url
if (page) {
diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js
index 8c31ea7..c60b3a1 100644
--- a/lib/output/website/createTemplateEngine.js
+++ b/lib/output/website/createTemplateEngine.js
@@ -71,16 +71,29 @@ function createTemplateEngine(output, currentFile) {
/**
Return an article by its path
- @param {String} articlePath
+ @param {String} filePath
@return {Object|undefined}
*/
- function getArticleByPath(articlePath) {
- var article = summary.getByPath(articlePath);
+ function getArticleByPath(filePath) {
+ var article = summary.getByPath(filePath);
if (!article) return undefined;
return JSONUtils.encodeSummaryArticle(article);
}
+ /**
+ Return a page by its path
+
+ @param {String} filePath
+ @return {Object|undefined}
+ */
+ function getPageByPath(filePath) {
+ var page = output.getPage(filePath);
+ if (!page) return undefined;
+
+ return JSONUtils.encodePage(page, summary);
+ }
+
return TemplateEngine.create({
loader: loader,
@@ -89,6 +102,7 @@ function createTemplateEngine(output, currentFile) {
globals: {
getArticleByPath: getArticleByPath,
+ getPageByPath: getPageByPath,
fileExists: fileExists
},