summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/components/Link.js
blob: 65f4eb5249ac994ea91a9b6a92f56333b9ab4d38 (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
const React = require('react');
const ReactRedux = require('react-redux');

const File = require('../models/File');
const SummaryArticle = require('../models/SummaryArticle');
const SummaryArticleShape = require('../shapes/SummaryArticle');
const FileShape = require('../shapes/File');

const Link = React.createClass({
    propTypes: {
        currentFile: FileShape,
        children:    React.PropTypes.node,

        // Destination of the link
        to: React.PropTypes.oneOfType([
            React.PropTypes.string,
            SummaryArticleShape,
            FileShape
        ])
    },

    render() {
        const { currentFile, to, children, ...props } = this.props;
        let href = to;

        if (SummaryArticle.is(to) || File.is(to)) {
            href = to.url;
        }

        href = currentFile.relative(href);
        return <a href={href} {...props}>{children}</a>;
    }
});

module.exports = ReactRedux.connect(state => {
    return { currentFile: state.file };
})(Link);