summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-10-06 16:29:32 +0200
committerSamy Pesse <samypesse@gmail.com>2016-10-06 16:29:32 +0200
commit1354582c4e0d61c7608965f36b3c27ee3e39dc78 (patch)
tree1f1883ffec98f762d19d6a03c7506676f19822e1 /packages/gitbook-core
parent4119917555f827b0bff256a8e34a1deef5f4b87e (diff)
downloadgitbook-1354582c4e0d61c7608965f36b3c27ee3e39dc78.zip
gitbook-1354582c4e0d61c7608965f36b3c27ee3e39dc78.tar.gz
gitbook-1354582c4e0d61c7608965f36b3c27ee3e39dc78.tar.bz2
Generate an url index for generation
Diffstat (limited to 'packages/gitbook-core')
-rw-r--r--packages/gitbook-core/src/components/Import.js15
-rw-r--r--packages/gitbook-core/src/components/Link.js42
-rw-r--r--packages/gitbook-core/src/models/File.js14
-rw-r--r--packages/gitbook-core/src/models/SummaryArticle.js35
4 files changed, 34 insertions, 72 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);
diff --git a/packages/gitbook-core/src/models/File.js b/packages/gitbook-core/src/models/File.js
index 03032a1..88138c3 100644
--- a/packages/gitbook-core/src/models/File.js
+++ b/packages/gitbook-core/src/models/File.js
@@ -21,16 +21,14 @@ class File extends Record(DEFAULTS) {
}
/**
- * Return url for a file in a GitBook context.
- * @param {Context} context
- * @return {String} url
+ * Returns the relative path from this file to "to"
+ * @param {String} to
+ * @return {String}
*/
- toURL(context) {
- const { file } = context.getState();
-
+ relative(to) {
return path.relative(
- path.dirname(file.path),
- this.path
+ path.dirname(this.path),
+ to
);
}
diff --git a/packages/gitbook-core/src/models/SummaryArticle.js b/packages/gitbook-core/src/models/SummaryArticle.js
index 80ef5ae..0f8ca0d 100644
--- a/packages/gitbook-core/src/models/SummaryArticle.js
+++ b/packages/gitbook-core/src/models/SummaryArticle.js
@@ -1,9 +1,4 @@
-const url = require('url');
-const path = require('path');
const { Record, List } = require('immutable');
-const File = require('./File');
-
-const OUTPUT_EXTENSION = '.html';
const DEFAULTS = {
title: '',
@@ -24,36 +19,6 @@ class SummaryArticle extends Record(DEFAULTS) {
}
/**
- * Return url for a file in a GitBook context.
- * @param {Context} context
- * @return {String} url
- */
- toURL(context) {
- const { readme } = context.getState();
- const fileReadme = readme.file;
- const parts = url.parse(this.ref);
-
- if (parts.protocol) {
- return this.ref;
- }
-
- const file = new File(parts.pathname);
- let filePath = file.toURL(context);
-
- // Change extension and resolve to .html
- if (
- path.basename(filePath, path.extname(filePath)) == 'README' ||
- (fileReadme && filePath == fileReadme.path)
- ) {
- filePath = path.join(path.dirname(filePath), 'index' + OUTPUT_EXTENSION);
- } else {
- filePath = path.basename(filePath, path.extname(filePath)) + OUTPUT_EXTENSION;
- }
-
- return filePath + (parts.hash || '');
- }
-
- /**
* Return true if article is an instance of SummaryArticle
* @param {Mixed} article
* @return {Boolean}