summaryrefslogtreecommitdiffstats
path: root/packages/gitbook/src/browser/render.js
blob: 8fb4711e58f3a9b5ae26cdd579fca85aa76a36f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const GitBook = require('gitbook-core');

const timing = require('../utils/timing');
const loadPlugins = require('./loadPlugins');

function HTML({head, innerHTML, payload, scripts, bootstrap}) {
    const attrs = head.htmlAttributes.toComponent();

    return (
        <html {...attrs}>
            <head>
                {head.title.toComponent()}
                {head.meta.toComponent()}
                {head.link.toComponent()}
                {head.style.toComponent()}
            </head>
            <body>
                <div id="content" dangerouslySetInnerHTML={{__html: innerHTML}} />
                {scripts.map(script => {
                    return <script key={script} src={script} />;
                })}
                <script type="application/payload+json" dangerouslySetInnerHTML={{__html: payload}} />
                <script type="application/javascript" dangerouslySetInnerHTML={{__html: bootstrap}} />
                {head.script.toComponent()}
            </body>
        </html>
    );
}
HTML.propTypes = {
    head:      React.PropTypes.object,
    innerHTML: React.PropTypes.string,
    payload:   React.PropTypes.string,
    bootstrap: React.PropTypes.string,
    scripts:   React.PropTypes.arrayOf(React.PropTypes.string)
};

/**
 * Get bootstrap code for a role
 * @param  {String} role
 * @return {String}
 */
function getBootstrapCode(role) {
    return `(function() { require("gitbook-core").bootstrap({ role: "${role}" }) })()`;
}

/**
 * Render a view using plugins.
 *
 * @param  {OrderedMap<String:Plugin>} plugin
 * @param  {Object} initialState
 * @param  {String} type ("ebook" or "browser")
 * @param  {String} role
 * @return {String} html
 */
function render(plugins, initialState, type, role) {
    return timing.measure(
        'browser.render',
        () => {
            // Load the plugins
            const browserPlugins = loadPlugins(plugins, type);
            const payload = JSON.stringify(initialState);
            const context = GitBook.createContext(browserPlugins, initialState);

            const currentFile = context.getState().file;

            const scripts = plugins.toList()
                .filter(plugin => plugin.getPackage().has(type))
                .map(plugin => {
                    return currentFile.relative('gitbook/plugins/' + plugin.getName() + '.js');
                })
                .toArray();

            const el = GitBook.renderWithContext(context, { role });

            // We're done with the context
            context.deactivate();

            // Render inner body
            const innerHTML = ReactDOMServer.renderToString(el);

            // Get headers
            const head = GitBook.Head.rewind();

            // Render whole HTML page
            const htmlEl = <HTML
                head={head}
                innerHTML={innerHTML}
                payload={payload}
                bootstrap={getBootstrapCode(role)}
                scripts={[
                    currentFile.relative('gitbook/core.js')
                ].concat(scripts)}
            />;

            const html = ReactDOMServer.renderToStaticMarkup(htmlEl);
            return html;
        }
    );
}

module.exports = render;