summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-headings/src/index.js
blob: 76563d8fcab681982df7b18249c8ad4d177aef56 (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 GitBook    = require('gitbook-core');
const { React }  = GitBook;
const classNames = require('classnames');

function mapStateToProps({ config }) {
    return {
        position: config.getForPlugin('headings').get('position','left')
    };
}

let Heading = React.createClass({
    propTypes: {
        id:       React.PropTypes.string.isRequired,
        children: React.PropTypes.node.isRequired,
        position: React.PropTypes.string.isRequired
    },

    render() {
        const { position, children, id } = this.props;
        const className = classNames('Headings-Container', {
            'Headings-Right': (position !== 'left')
        });

        return (
            <div className={className}>
                <GitBook.ImportCSS href="gitbook/headings/headings.css" />

                {position == 'left' ?
                <a className="Headings-Anchor-Left" href={`#${id}`}>
                    <i className="fa fa-link" />
                </a>
                : null}

                {children}

                {position != 'left' ?
                <a className="Headings-Anchor-Right" href={`#${id}`}>
                    <i className="fa fa-link" />
                </a>
                : null}
            </div>
        );
    }
});

Heading = GitBook.connect(Heading, mapStateToProps);

module.exports = GitBook.createPlugin({
    activate: (dispatch, getState, { Components }) => {
        // Attach component to titles
        dispatch(Components.registerComponent(Heading, { role: 'html:h1' }));
        dispatch(Components.registerComponent(Heading, { role: 'html:h2' }));
        dispatch(Components.registerComponent(Heading, { role: 'html:h3' }));
        dispatch(Components.registerComponent(Heading, { role: 'html:h4' }));
        dispatch(Components.registerComponent(Heading, { role: 'html:h5' }));
        dispatch(Components.registerComponent(Heading, { role: 'html:h6' }));
    },
    deactivate: (dispatch, getState) => {},
    reduce: (state, action) => state
});