diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-01 14:14:30 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-01 14:14:30 +0200 |
commit | da83d9e91d419e1c843e3017098d25dbde22b500 (patch) | |
tree | 0cf3860171062794ef66a292f46fdd62e66dc780 /packages/gitbook-core/src/models | |
parent | 2cf507a807b1070a445cadb28317a41385fbe50b (diff) | |
download | gitbook-da83d9e91d419e1c843e3017098d25dbde22b500.zip gitbook-da83d9e91d419e1c843e3017098d25dbde22b500.tar.gz gitbook-da83d9e91d419e1c843e3017098d25dbde22b500.tar.bz2 |
Change api for registering components
Diffstat (limited to 'packages/gitbook-core/src/models')
-rw-r--r-- | packages/gitbook-core/src/models/Context.js | 29 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/File.js | 43 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Plugin.js | 13 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/SummaryArticle.js | 57 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/SummaryPart.js | 17 |
5 files changed, 159 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/models/Context.js b/packages/gitbook-core/src/models/Context.js new file mode 100644 index 0000000..fa68bd8 --- /dev/null +++ b/packages/gitbook-core/src/models/Context.js @@ -0,0 +1,29 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + store: null, + actions: {} +}; + +class Context extends Record(DEFAULTS) { + + /** + * Return current state + * @return {Object} + */ + getState() { + const { store } = this; + return store.getState(); + } + + /** + * Dispatch an action + * @param {Action} action + */ + dispatch(action) { + const { store } = this; + return store.dispatch(action); + } +} + +module.exports = Context; diff --git a/packages/gitbook-core/src/models/File.js b/packages/gitbook-core/src/models/File.js new file mode 100644 index 0000000..09dfc1b --- /dev/null +++ b/packages/gitbook-core/src/models/File.js @@ -0,0 +1,43 @@ +const path = require('path'); +const { Record } = require('immutable'); + +const DEFAULTS = { + type: '', + mtime: '', + path: '' +}; + +class File extends Record(DEFAULTS) { + constructor(file) { + if (typeof file === 'string') { + file = { path: file }; + } + + super(file); + } + + /** + * Return url for a file in a GitBook context. + * @param {Context} context + * @return {String} url + */ + toURL(context) { + const { file } = context.getState(); + + return path.relative( + path.dirname(file.path), + this.path + ); + } + + static is(file) { + return (file instanceof File); + } + + static create(file) { + return file instanceof File ? + file : new File(file); + } +} + +module.exports = File; diff --git a/packages/gitbook-core/src/models/Plugin.js b/packages/gitbook-core/src/models/Plugin.js new file mode 100644 index 0000000..0654f85 --- /dev/null +++ b/packages/gitbook-core/src/models/Plugin.js @@ -0,0 +1,13 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + init: ((dispatch, getState) => {}), + reduce: ((state, action) => state), + actions: {} +}; + +class Plugin extends Record(DEFAULTS) { + +} + +module.exports = Plugin; diff --git a/packages/gitbook-core/src/models/SummaryArticle.js b/packages/gitbook-core/src/models/SummaryArticle.js new file mode 100644 index 0000000..b36fbe3 --- /dev/null +++ b/packages/gitbook-core/src/models/SummaryArticle.js @@ -0,0 +1,57 @@ +const url = require('url'); +const path = require('path'); +const { Record, List } = require('immutable'); +const File = require('./File'); + +const OUTPUT_EXTENSION = '.html'; + +const DEFAULTS = { + title: '', + depth: 0, + path: '', + ref: '', + level: '', + articles: List() +}; + +class SummaryArticle extends Record(DEFAULTS) { + constructor(article) { + super({ + ...article, + articles: (new List(article.articles)) + .map(art => new SummaryArticle(art)) + }); + } + + /** + * Return url for a file in a GitBook context. + * @param {Context} context + * @return {String} url + */ + toURL(context) { + const { readme } = context.getState(); + const fileReadme = readme.file; + const parts = url.parse(this.ref); + + if (parts.protocol) { + return this.ref; + } + + const file = new File(parts.pathname); + let filePath = file.toURL(context); + + // Change extension and resolve to .html + if ( + path.basename(filePath, path.extname(filePath)) == 'README' || + (fileReadme && filePath == fileReadme.path) + ) { + filePath = path.join(path.dirname(filePath), 'index' + OUTPUT_EXTENSION); + } else { + filePath = path.basename(filePath, path.extname(filePath)) + OUTPUT_EXTENSION; + } + + return filePath + (parts.hash || ''); + } +} + +module.exports = SummaryArticle; diff --git a/packages/gitbook-core/src/models/SummaryPart.js b/packages/gitbook-core/src/models/SummaryPart.js new file mode 100644 index 0000000..89c76d4 --- /dev/null +++ b/packages/gitbook-core/src/models/SummaryPart.js @@ -0,0 +1,17 @@ +const { Record, List } = require('immutable'); +const SummaryArticle = require('./SummaryArticle'); + +class SummaryPart extends Record({ + title: '', + articles: List() +}) { + constructor(state) { + super({ + ...state, + articles: (new List(state.articles)) + .map(article => new SummaryArticle(article)) + }); + } +} + +module.exports = SummaryPart; |