diff options
Diffstat (limited to 'packages/gitbook-core/src/components/Link.js')
-rw-r--r-- | packages/gitbook-core/src/components/Link.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/components/Link.js b/packages/gitbook-core/src/components/Link.js new file mode 100644 index 0000000..ab364bb --- /dev/null +++ b/packages/gitbook-core/src/components/Link.js @@ -0,0 +1,37 @@ +const React = require('react'); +const ReactRedux = require('react-redux'); + +const File = require('../models/File'); +const SummaryArticle = require('../models/SummaryArticle'); +const SummaryArticleShape = require('../propTypes/SummaryArticle'); +const FileShape = require('../propTypes/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); |