summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-23 11:22:20 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-23 11:22:20 +0200
commitce95f316b9ce1eac1e615db3540c4d0f30408d63 (patch)
tree2cb773317a937887c216d93afd6d92fcdca64475
parenta162af5a75453a6ecb818447540cbffdc774715f (diff)
downloadgitbook-ce95f316b9ce1eac1e615db3540c4d0f30408d63.zip
gitbook-ce95f316b9ce1eac1e615db3540c4d0f30408d63.tar.gz
gitbook-ce95f316b9ce1eac1e615db3540c4d0f30408d63.tar.bz2
Add method to be article by level
-rw-r--r--lib/api/decodePage.js15
-rw-r--r--lib/api/encodePage.js14
-rw-r--r--lib/api/index.js6
-rw-r--r--lib/models/__tests__/summary.js40
-rw-r--r--lib/models/plugin.js18
-rw-r--r--lib/models/summary.js15
-rw-r--r--lib/models/summaryArticle.js45
-rw-r--r--lib/models/summaryPart.js22
-rw-r--r--lib/output/callPageHook.js9
9 files changed, 169 insertions, 15 deletions
diff --git a/lib/api/decodePage.js b/lib/api/decodePage.js
new file mode 100644
index 0000000..b9941c1
--- /dev/null
+++ b/lib/api/decodePage.js
@@ -0,0 +1,15 @@
+
+/**
+ Decode changes from a JS API to a page boject
+
+ @param {Output} output
+ @param {Page} page
+ @param {Object} result
+ @return {Page}
+*/
+function decodePage(output, page, result) {
+
+
+}
+
+module.exports = decodePage;
diff --git a/lib/api/encodePage.js b/lib/api/encodePage.js
new file mode 100644
index 0000000..9ec55bc
--- /dev/null
+++ b/lib/api/encodePage.js
@@ -0,0 +1,14 @@
+
+/**
+ Encode a page in a context to a JS API
+
+ @param {Output} output
+ @param {Page} page
+ @return {Object}
+*/
+function encodePage(output, page) {
+
+
+}
+
+module.exports = encodePage;
diff --git a/lib/api/index.js b/lib/api/index.js
index 8b4f0ff..213ef1a 100644
--- a/lib/api/index.js
+++ b/lib/api/index.js
@@ -1,3 +1,5 @@
-
-module.exports = {};
+module.exports = {
+ encodePage: require('./encodePage'),
+ decodePage: require('./decodePage')
+};
diff --git a/lib/models/__tests__/summary.js b/lib/models/__tests__/summary.js
new file mode 100644
index 0000000..e641efe
--- /dev/null
+++ b/lib/models/__tests__/summary.js
@@ -0,0 +1,40 @@
+jest.autoMockOff();
+
+describe('Summary', function() {
+ var File = require('../file');
+ var Summary = require('../summary');
+
+ describe('createFromEntries', function() {
+ var summary = Summary.createFromParts(File(), [
+ {
+ articles: [
+ {
+ title: 'My First Article',
+ path: 'README.md'
+ },
+ {
+ title: 'My Second Article',
+ path: 'article.md'
+ }
+ ]
+ },
+ {
+ title: 'Test'
+ }
+ ]);
+
+ it('must add all parts', function() {
+ var parts = summary.getParts();
+ expect(parts.size).toBe(2);
+ });
+
+ it('must index by level', function() {
+ var part1 = summary.getByLevel('0');
+
+ expect(part1).toBeDefined();
+ expect(part1.getArticles().size).toBe(2);
+ });
+ });
+});
+
+
diff --git a/lib/models/plugin.js b/lib/models/plugin.js
index 2e8fb03..6d322f4 100644
--- a/lib/models/plugin.js
+++ b/lib/models/plugin.js
@@ -65,6 +65,24 @@ Plugin.prototype.isLoaded = function() {
};
/**
+ Return map of hooks
+ @return {Map<String:Function>}
+*/
+Plugin.prototype.getHooks = function() {
+ return this.getContent().get('hooks');
+};
+
+/**
+ Return a specific hook
+
+ @param {String} name
+ @return {Function|undefined}
+*/
+Plugin.prototype.getHook = function(name) {
+ return this.getHooks().get(name);
+};
+
+/**
Create a plugin from a string
@param {String}
diff --git a/lib/models/summary.js b/lib/models/summary.js
index 3918df7..f295a16 100644
--- a/lib/models/summary.js
+++ b/lib/models/summary.js
@@ -2,6 +2,7 @@ var Immutable = require('immutable');
var File = require('./file');
var SummaryPart = require('./summaryPart');
+var SummaryArticle = require('./summaryArticle');
var Summary = Immutable.Record({
file: File(),
@@ -16,6 +17,16 @@ Summary.prototype.getParts = function() {
return this.get('parts');
};
+/**
+ Return a part/article by its level
+
+ @param {String} level
+ @return {Part|Article}
+*/
+Summary.prototype.getByLevel = function(level) {
+ return SummaryArticle.getByLevel(this, level, 'getParts');
+};
+
/**
Create a new summary for a list of parts
@@ -24,12 +35,12 @@ Summary.prototype.getParts = function() {
@return {Summary}
*/
Summary.createFromParts = function createFromParts(file, parts) {
- parts = parts.map(function(part) {
+ parts = parts.map(function(part, i) {
if (part instanceof SummaryPart) {
return part;
}
- return SummaryPart.create(part);
+ return SummaryPart.create(part, i);
});
return new Summary({
diff --git a/lib/models/summaryArticle.js b/lib/models/summaryArticle.js
index 3d642fc..4a448c8 100644
--- a/lib/models/summaryArticle.js
+++ b/lib/models/summaryArticle.js
@@ -30,6 +30,16 @@ SummaryArticle.prototype.getArticles = function() {
};
/**
+ Return an article by its level
+
+ @param {String} level
+ @return {Article}
+*/
+SummaryArticle.prototype.getByLevel = function(level) {
+ return SummaryArticle.getByLevel(this, level);
+};
+
+/**
Get path (without anchor) to the pointing file
@return {String}
@@ -81,15 +91,16 @@ SummaryArticle.prototype.isExternal = function() {
@param {Object} def
@return {SummaryArticle}
*/
-SummaryArticle.create = function(def) {
- var articles = (def.articles || []).map(function(article) {
+SummaryArticle.create = function(def, level) {
+ var articles = (def.articles || []).map(function(article, i) {
if (article instanceof SummaryArticle) {
return article;
}
- return SummaryArticle.create(article);
+ return SummaryArticle.create(article, [level, i].join('.'));
});
return new SummaryArticle({
+ level: level,
title: def.title,
ref: def.ref || def.path,
articles: Immutable.List(articles)
@@ -97,4 +108,32 @@ SummaryArticle.create = function(def) {
};
+/**
+ Return an article by its level
+
+ @param {Article|Part} base
+ @param {String} level
+ @param {String} method
+ @return {Article}
+*/
+SummaryArticle.getByLevel = function(base, level, method) {
+ method = method || 'getArticles';
+ var articles = base[method]();
+ var levelParts = level.split('.');
+ var baseLevel = levelParts.shift();
+
+ var result = articles.find(function(a) {
+ return a.getLevel() === baseLevel;
+ });
+
+ if (!result) {
+ return undefined;
+ }
+ if (levelParts.length === 0) {
+ return result;
+ }
+
+ return SummaryArticle.getByLevel(result, levelParts.join('.'));
+};
+
module.exports = SummaryArticle;
diff --git a/lib/models/summaryPart.js b/lib/models/summaryPart.js
index 4b41621..345cf83 100644
--- a/lib/models/summaryPart.js
+++ b/lib/models/summaryPart.js
@@ -7,10 +7,15 @@ var SummaryArticle = require('./summaryArticle');
*/
var SummaryPart = Immutable.Record({
+ level: String(),
title: String(),
articles: Immutable.List()
});
+SummaryPart.prototype.getLevel = function() {
+ return this.get('level');
+};
+
SummaryPart.prototype.getTitle = function() {
return this.get('title');
};
@@ -20,20 +25,31 @@ SummaryPart.prototype.getArticles = function() {
};
/**
+ Return an article by its level
+
+ @param {String} level
+ @return {Article}
+*/
+SummaryPart.prototype.getByLevel = function(level) {
+ return SummaryArticle.getByLevel(this, level);
+};
+
+/**
Create a SummaryPart
@param {Object} def
@return {SummaryPart}
*/
-SummaryPart.create = function(def) {
- var articles = (def.articles || []).map(function(article) {
+SummaryPart.create = function(def, level) {
+ var articles = (def.articles || []).map(function(article, i) {
if (article instanceof SummaryArticle) {
return article;
}
- return SummaryArticle.create(article);
+ return SummaryArticle.create(article, [level, i].join('.'));
});
return new SummaryPart({
+ level: String(level),
title: def.title,
articles: Immutable.List(articles)
});
diff --git a/lib/output/callPageHook.js b/lib/output/callPageHook.js
index f7d5bfa..ed80823 100644
--- a/lib/output/callPageHook.js
+++ b/lib/output/callPageHook.js
@@ -1,3 +1,4 @@
+var Api = require('../api');
var callHook = require('./callHook');
/**
@@ -6,20 +7,18 @@ var callHook = require('./callHook');
@param {String} name
@param {Output} output
@param {Page} page
- @return {Page}
+ @return {Promise<Page>}
*/
function callPageHook(name, output, page) {
return callHook(
name,
function(out) {
-
-
+ return Api.encodePage(output, page);
},
function(result) {
-
-
+ return Api.decodePage(output, page, result);
},
output