summaryrefslogtreecommitdiffstats
path: root/packages/gitbook/src/browser/render.js
blob: e39727969bbd9d7693e2a2f2e0e59e7ad04a7b53 (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
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const GitBook = require('gitbook-core');

function HTML({head, innerHTML, props}) {
    const attrs = head.htmlAttributes.toComponent();
    const propsJSON = JSON.stringify(props);

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

/**
 * Render a page
 * @param  {List<Plugin>} plugin
 * @param  {Object} initialState
 * @return {String} html
 */
function render(plugins, initialState) {
    const store = GitBook.createStore(plugins, initialState);
    const { el, getHead } = GitBook.renderComponent(store, { role: 'Body' });

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

    // Get headers
    const head = getHead();

    // Render whole HTML page
    const htmlEl = <HTML
        head={head}
        innerHTML={innerHTML}
        props={initialState}
    />;

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

module.exports = render;