diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-09-20 15:12:48 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-09-20 15:12:53 +0200 |
commit | 6d58a90613802b1e65cbf8c027093a1868119781 (patch) | |
tree | bf0eaf8b2abb4eae0179e273a60918127c05a7f8 /packages | |
parent | 2796065886d824ae7fe7060dafa6fdcbd26d5f93 (diff) | |
download | gitbook-6d58a90613802b1e65cbf8c027093a1868119781.zip gitbook-6d58a90613802b1e65cbf8c027093a1868119781.tar.gz gitbook-6d58a90613802b1e65cbf8c027093a1868119781.tar.bz2 |
Prepare loading of plugin
Diffstat (limited to 'packages')
-rw-r--r-- | packages/gitbook-core/src/createPlugin.js | 9 | ||||
-rw-r--r-- | packages/gitbook/src/browser/loadPlugins.js | 21 | ||||
-rw-r--r-- | packages/gitbook/src/browser/render.js (renamed from packages/gitbook/src/output/website/render.js) | 4 | ||||
-rw-r--r-- | packages/gitbook/src/models/plugin.js | 246 | ||||
-rw-r--r-- | packages/gitbook/src/output/website/onPage.js | 10 | ||||
-rw-r--r-- | packages/gitbook/src/plugins/findInstalled.js | 2 |
6 files changed, 164 insertions, 128 deletions
diff --git a/packages/gitbook-core/src/createPlugin.js b/packages/gitbook-core/src/createPlugin.js index 1fcf495..c26d745 100644 --- a/packages/gitbook-core/src/createPlugin.js +++ b/packages/gitbook-core/src/createPlugin.js @@ -7,10 +7,17 @@ * @return {Plugin} */ function createPlugin(onInitialState, onReduceState) { - return { + const plugin = { onInitialState, onReduceState }; + + if (typeof window !== 'undefined') { + window.gitbookPlugins = window.gitbookPlugins || []; + window.gitbookPlugins.push(plugin); + } + + return plugin; } module.exports = createPlugin; diff --git a/packages/gitbook/src/browser/loadPlugins.js b/packages/gitbook/src/browser/loadPlugins.js new file mode 100644 index 0000000..ba5381c --- /dev/null +++ b/packages/gitbook/src/browser/loadPlugins.js @@ -0,0 +1,21 @@ +const path = require('path'); + +/** + * Load all browser plugins + * @param {List<Plugin>} plugins + * @return {List} + */ +function loadPlugins(plugins) { + return plugins + .filter(plugin => plugin.getPackage().has('browser')) + .map(plugin => { + const browserFile = path.resolve( + plugin.getPath(), + plugin.getPackage().get('browser') + ); + + return require(browserFile); + }); +} + +module.exports = loadPlugins; diff --git a/packages/gitbook/src/output/website/render.js b/packages/gitbook/src/browser/render.js index 1028b92..af71b87 100644 --- a/packages/gitbook/src/output/website/render.js +++ b/packages/gitbook/src/browser/render.js @@ -31,11 +31,11 @@ HTML.propTypes = { /** * Render a page + * @param {List<Plugin>} plugin * @param {Object} initialState * @return {String} html */ -function render(initialState) { - const plugins = []; +function render(plugins, initialState) { const store = GitBook.createStore(plugins, initialState); const { el, head } = GitBook.renderComponent(store, { role: 'Body' }); diff --git a/packages/gitbook/src/models/plugin.js b/packages/gitbook/src/models/plugin.js index 0e4c774..52a4b0f 100644 --- a/packages/gitbook/src/models/plugin.js +++ b/packages/gitbook/src/models/plugin.js @@ -1,4 +1,4 @@ -const Immutable = require('immutable'); +const { Record, Map } = require('immutable'); const TemplateBlock = require('./templateBlock'); const PluginDependency = require('./pluginDependency'); @@ -6,7 +6,7 @@ const THEME_PREFIX = require('../constants/themePrefix'); const DEFAULT_VERSION = '*'; -const Plugin = Immutable.Record({ +const DEFAULTS = { name: String(), // Requirement version (ex: ">1.0.0") @@ -22,147 +22,149 @@ const Plugin = Immutable.Record({ parent: String(), // Content of the "package.json" - package: Immutable.Map(), + package: Map(), // Content of the package itself - content: Immutable.Map() -}, 'Plugin'); - -Plugin.prototype.getName = function() { - return this.get('name'); + content: Map() }; -Plugin.prototype.getPath = function() { - return this.get('path'); -}; +class Plugin extends Record(DEFAULTS) { + getName() { + return this.get('name'); + } -Plugin.prototype.getVersion = function() { - return this.get('version'); -}; + getPath() { + return this.get('path'); + } -Plugin.prototype.getPackage = function() { - return this.get('package'); -}; + getVersion() { + return this.get('version'); + } -Plugin.prototype.getContent = function() { - return this.get('content'); -}; + getPackage() { + return this.get('package'); + } -Plugin.prototype.getDepth = function() { - return this.get('depth'); -}; + getContent() { + return this.get('content'); + } -Plugin.prototype.getParent = function() { - return this.get('parent'); -}; + getDepth() { + return this.get('depth'); + } -/** - * Return the ID on NPM for this plugin - * @return {String} - */ -Plugin.prototype.getNpmID = function() { - return PluginDependency.nameToNpmID(this.getName()); -}; + getParent() { + return this.get('parent'); + } -/** - * Check if a plugin is loaded - * @return {Boolean} - */ -Plugin.prototype.isLoaded = function() { - return Boolean(this.getPackage().size > 0); -}; + /** + * Return the ID on NPM for this plugin + * @return {String} + */ + getNpmID() { + return PluginDependency.nameToNpmID(this.getName()); + } -/** - * Check if a plugin is a theme given its name - * @return {Boolean} - */ -Plugin.prototype.isTheme = function() { - const name = this.getName(); - return (name && name.indexOf(THEME_PREFIX) === 0); -}; + /** + * Check if a plugin is loaded + * @return {Boolean} + */ + isLoaded() { + return Boolean(this.getPackage().size > 0); + } -/** - * Return map of hooks - * @return {Map<String:Function>} - */ -Plugin.prototype.getHooks = function() { - return this.getContent().get('hooks') || Immutable.Map(); -}; + /** + * Check if a plugin is a theme given its name + * @return {Boolean} + */ + isTheme() { + const name = this.getName(); + return (name && name.indexOf(THEME_PREFIX) === 0); + } -/** - * Return infos about resources for a specific type - * @param {String} type - * @return {Map<String:Mixed>} - */ -Plugin.prototype.getResources = function(type) { - if (type != 'website' && type != 'ebook') { - throw new Error('Invalid assets type ' + type); + /** + * Return map of hooks + * @return {Map<String:Function>} + */ + getHooks() { + return this.getContent().get('hooks') || Map(); } - const content = this.getContent(); - return (content.get(type) - || (type == 'website' ? content.get('book') : null) - || Immutable.Map()); -}; + /** + * Return infos about resources for a specific type + * @param {String} type + * @return {Map<String:Mixed>} + */ + getResources(type) { + if (type != 'website' && type != 'ebook') { + throw new Error('Invalid assets type ' + type); + } + + const content = this.getContent(); + return (content.get(type) + || (type == 'website' ? content.get('book') : null) + || Map()); + } -/** - * Return map of filters - * @return {Map<String:Function>} - */ -Plugin.prototype.getFilters = function() { - return this.getContent().get('filters'); -}; + /** + * Return map of filters + * @return {Map<String:Function>} + */ + getFilters() { + return this.getContent().get('filters'); + } -/** - * Return map of blocks - * @return {Map<String:TemplateBlock>} - */ -Plugin.prototype.getBlocks = function() { - let blocks = this.getContent().get('blocks'); - blocks = blocks || Immutable.Map(); - - return blocks - .map(function(block, blockName) { - return TemplateBlock.create(blockName, block); - }); -}; + /** + * Return map of blocks + * @return {Map<String:TemplateBlock>} + */ + getBlocks() { + let blocks = this.getContent().get('blocks'); + blocks = blocks || Map(); + + return blocks + .map(function(block, blockName) { + return TemplateBlock.create(blockName, block); + }); + } -/** - * Return a specific hook - * @param {String} name - * @return {Function|undefined} - */ -Plugin.prototype.getHook = function(name) { - return this.getHooks().get(name); -}; + /** + * Return a specific hook + * @param {String} name + * @return {Function|undefined} + */ + getHook(name) { + return this.getHooks().get(name); + } -/** - * Create a plugin from a string - * @param {String} - * @return {Plugin} - */ -Plugin.createFromString = function(s) { - const parts = s.split('@'); - const name = parts[0]; - const version = parts.slice(1).join('@'); - - return new Plugin({ - name, - version: version || DEFAULT_VERSION - }); -}; + /** + * Create a plugin from a string + * @param {String} + * @return {Plugin} + */ + static createFromString(s) { + const parts = s.split('@'); + const name = parts[0]; + const version = parts.slice(1).join('@'); + + return new Plugin({ + name, + version: version || DEFAULT_VERSION + }); + } -/** - * Create a plugin from a dependency - * @param {PluginDependency} - * @return {Plugin} - */ -Plugin.createFromDep = function(dep) { - return new Plugin({ - name: dep.getName(), - version: dep.getVersion() - }); -}; + /** + * Create a plugin from a dependency + * @param {PluginDependency} + * @return {Plugin} + */ + static createFromDep(dep) { + return new Plugin({ + name: dep.getName(), + version: dep.getVersion() + }); + } +} Plugin.nameToNpmID = PluginDependency.nameToNpmID; diff --git a/packages/gitbook/src/output/website/onPage.js b/packages/gitbook/src/output/website/onPage.js index eb97d20..27bc0b2 100644 --- a/packages/gitbook/src/output/website/onPage.js +++ b/packages/gitbook/src/output/website/onPage.js @@ -8,7 +8,8 @@ const writeFile = require('../helper/writeFile'); const fileToOutput = require('../helper/fileToOutput'); const getModifiers = require('../getModifiers'); const createTemplateEngine = require('./createTemplateEngine'); -const render = require('./render'); +const render = require('../../browser/render'); +const loadBrowserPlugins = require('../../browser/loadPlugins'); /** * Write a page as a json file @@ -36,8 +37,13 @@ function onPage(output, page) { // We should probabbly move it to "template" or a "site" namespace // context.basePath = basePath; + console.log('render page'); + + // Load the plugins + const browserPlugins = []; //loadBrowserPlugins(plugins); + // Render the theme - const html = render(initialState); + const html = render(browserPlugins, initialState); // Write it to the disk return writeFile(output, filePath, html); diff --git a/packages/gitbook/src/plugins/findInstalled.js b/packages/gitbook/src/plugins/findInstalled.js index 15556b6..fcf135a 100644 --- a/packages/gitbook/src/plugins/findInstalled.js +++ b/packages/gitbook/src/plugins/findInstalled.js @@ -45,7 +45,7 @@ function findInstalled(folder) { if (!validateId(name)) { if (parent) return; } else { - results = results.set(pluginName, Plugin({ + results = results.set(pluginName, new Plugin({ name: pluginName, version, path: pkgPath, |