diff options
Diffstat (limited to 'packages/gitbook-core/src/components')
-rw-r--r-- | packages/gitbook-core/src/components/Import.js | 15 | ||||
-rw-r--r-- | packages/gitbook-core/src/components/Link.js | 42 |
2 files changed, 28 insertions, 29 deletions
diff --git a/packages/gitbook-core/src/components/Import.js b/packages/gitbook-core/src/components/Import.js index 057ef7a..68318b9 100644 --- a/packages/gitbook-core/src/components/Import.js +++ b/packages/gitbook-core/src/components/Import.js @@ -2,8 +2,19 @@ const React = require('react'); const Head = require('react-helmet'); const ReactRedux = require('react-redux'); +/** + * Resolve a file url to a relative url in current state + * @param {String} href + * @param {State} state + * @return {String} + */ +function resolveForCurrentFile(href, state) { + const { file } = state; + return file.relative(href); +} + const ImportLink = ReactRedux.connect((state, {rel, href}) => { - href = href; // TODO: resolve using current page + href = resolveForCurrentFile(href, state); return { link: [ @@ -16,7 +27,7 @@ const ImportLink = ReactRedux.connect((state, {rel, href}) => { })(Head); const ImportScript = ReactRedux.connect((state, {type, src}) => { - src = src; // TODO: resolve using current page + src = resolveForCurrentFile(src, state); return { script: [ diff --git a/packages/gitbook-core/src/components/Link.js b/packages/gitbook-core/src/components/Link.js index 8c142ea..f5b9753 100644 --- a/packages/gitbook-core/src/components/Link.js +++ b/packages/gitbook-core/src/components/Link.js @@ -4,43 +4,29 @@ const ReactRedux = require('react-redux'); const File = require('../models/File'); const SummaryArticle = require('../models/SummaryArticle'); const SummaryArticleShape = require('../shapes/SummaryArticle'); -const ContextShape = require('../shapes/Context'); +const FileShape = require('../shapes/File'); const Link = React.createClass({ propTypes: { - children: React.PropTypes.node, - href: React.PropTypes.string, - to: React.PropTypes.oneOfType([ + currentFile: FileShape, + children: React.PropTypes.node, + + // Destination of the link + to: React.PropTypes.oneOfType([ React.PropTypes.string, - SummaryArticleShape + SummaryArticleShape, + FileShape ]) }, - contextTypes: { - gitbook: ContextShape.isRequired - }, - getHref() { - const { gitbook } = this.context; - let { to, href } = this.props; - - if (href) { - return href; - } - - if (SummaryArticle.is(to)) { - return to.toURL(gitbook); - } - - if (typeof to === 'string') { - to = new File(to); - } + let { currentFile, to } = this.props; - if (File.is(to)) { - return to.toURL(gitbook); + if (SummaryArticle.is(to) || File.is(to)) { + to = to.url; } - throw new Error('Invalid format for prop "to"'); + return currentFile.relative(to); }, render() { @@ -50,4 +36,6 @@ const Link = React.createClass({ } }); -module.exports = ReactRedux.connect()(Link); +module.exports = ReactRedux.connect(state => { + return { currentFile: state.file }; +})(Link); |