diff options
Diffstat (limited to 'packages/gitbook-core/src/components/Link.js')
-rw-r--r-- | packages/gitbook-core/src/components/Link.js | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/packages/gitbook-core/src/components/Link.js b/packages/gitbook-core/src/components/Link.js index 83ecdd5..8c142ea 100644 --- a/packages/gitbook-core/src/components/Link.js +++ b/packages/gitbook-core/src/components/Link.js @@ -1,8 +1,10 @@ const React = require('react'); const ReactRedux = require('react-redux'); -const path = require('path'); -const url = require('url'); + +const File = require('../models/File'); +const SummaryArticle = require('../models/SummaryArticle'); const SummaryArticleShape = require('../shapes/SummaryArticle'); +const ContextShape = require('../shapes/Context'); const Link = React.createClass({ propTypes: { @@ -14,25 +16,31 @@ const Link = React.createClass({ ]) }, + contextTypes: { + gitbook: ContextShape.isRequired + }, + getHref() { - const { to, href } = this.props; + const { gitbook } = this.context; + let { to, href } = this.props; if (href) { return href; } - if (typeof to === 'string') { - return to; + if (SummaryArticle.is(to)) { + return to.toURL(gitbook); } - // Article - if (typeof to.ref === 'string') { - const parts = url.parse(to.ref); - const ext = path.extname(parts.pathname); - const pathname = path.basename(parts.pathname, ext) + '.html'; + if (typeof to === 'string') { + to = new File(to); + } - return pathname + (parts.hash || ''); + if (File.is(to)) { + return to.toURL(gitbook); } + + throw new Error('Invalid format for prop "to"'); }, render() { |