summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-18 14:47:53 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-18 14:47:53 +0100
commit709b388dfcc641fab25d297618b6ffe49f5cd677 (patch)
tree661973ac5d7de4bb32db33648ecb23e9bba6b00e /lib
parent6e83240233e6168aa6567eb6fcac62508fe7fd0e (diff)
downloadgitbook-709b388dfcc641fab25d297618b6ffe49f5cd677.zip
gitbook-709b388dfcc641fab25d297618b6ffe49f5cd677.tar.gz
gitbook-709b388dfcc641fab25d297618b6ffe49f5cd677.tar.bz2
Fix path calcul to be coherant
Diffstat (limited to 'lib')
-rw-r--r--lib/output/assets-inliner.js2
-rw-r--r--lib/output/base.js12
-rw-r--r--lib/output/website.js2
-rw-r--r--lib/page/html.js13
-rw-r--r--lib/page/index.js22
-rw-r--r--lib/utils/location.js3
6 files changed, 39 insertions, 15 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js
index 8d7c48e..6768830 100644
--- a/lib/output/assets-inliner.js
+++ b/lib/output/assets-inliner.js
@@ -73,7 +73,7 @@ module.exports = function assetsInliner(Base) {
// Return relative path from the page
.then(function(filename) {
- return page.relative('/' + filename);
+ return page.relative(filename);
});
};
diff --git a/lib/output/base.js b/lib/output/base.js
index d22fda1..6678bb0 100644
--- a/lib/output/base.js
+++ b/lib/output/base.js
@@ -130,10 +130,18 @@ Output.prototype.finish = function() {
// Resolve an HTML link
Output.prototype.onRelativeLink = function(currentPage, href) {
- var to = this.book.getPage(href);
+ var to = currentPage.followPage(href);
// Replace by an .html link
- if (to) href = this.outputUrl(to.path);
+ if (to) {
+ href = to.path;
+
+ // Recalcul as relative link
+ href = currentPage.relative(href);
+
+ // Replace .md by .html
+ href = this.outputUrl(href);
+ }
return href;
};
diff --git a/lib/output/website.js b/lib/output/website.js
index b595897..d856229 100644
--- a/lib/output/website.js
+++ b/lib/output/website.js
@@ -87,7 +87,7 @@ WebsiteOutput.prototype.prepare = function() {
// Transform a '.md' into a '.html' (README -> index)
that.env.addFilter('contentURL', function(s) {
- return that.onRelativeLink(null, s);
+ return that.outputUrl(s);
});
// Relase path to an asset
diff --git a/lib/page/html.js b/lib/page/html.js
index 45fae0d..bd9ec91 100644
--- a/lib/page/html.js
+++ b/lib/page/html.js
@@ -1,4 +1,5 @@
var _ = require('lodash');
+var url = require('url');
var cheerio = require('cheerio');
var domSerializer = require('dom-serializer');
var slug = require('github-slugid');
@@ -63,7 +64,11 @@ HTMLPipeline.prototype.transformLinks = function() {
if (location.isAnchor(href)) {
// Don't "change" anchor links
} else if (location.isRelative(href)) {
- $a.attr('href', this.opts.onRelativeLink(href));
+ // Preserve anchor
+ var parsed = url.parse(href);
+ var filename = this.opts.onRelativeLink(parsed.pathname);
+
+ $a.attr('href', filename + (parsed.hash || ''));
} else {
// External links
$a.attr('target', '_blank');
@@ -178,12 +183,16 @@ HTMLPipeline.prototype.output = function() {
var that = this;
return Promise()
- .then(this.transformLinks)
.then(this.transformImages)
.then(this.transformHeadings)
.then(this.transformCodeBlocks)
.then(this.transformSvgs)
.then(this.applyAnnotations)
+
+ // Transform of links should be applied after annotations
+ // because annotations are created as links
+ .then(this.transformLinks)
+
.then(function() {
return renderDOM(that.$);
});
diff --git a/lib/page/index.js b/lib/page/index.js
index bdf3c81..bc12529 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -60,14 +60,23 @@ Page.prototype.resolve = function() {
return this.book.resolve(this.resolveLocal.apply(this, arguments));
};
-// Convert an absolite path to a relative path from this page
+// Convert an absolute path (in the book) to a relative path from this page
Page.prototype.relative = function(name) {
+ // Convert /test.png -> test.png
+ name = location.toAbsolute(name, '', '');
+
return location.relative(
- this.resolve('.'),
- this.resolve(name)
+ this.resolve('.') + '/',
+ this.book.resolve(name)
);
};
+// Return a page result of a relative page from this page
+Page.prototype.followPage = function(filename) {
+ var absPath = this.resolveLocal(filename);
+ return this.book.getPage(absPath);
+};
+
// Update content of the page
Page.prototype.update = function(content) {
this.content = content;
@@ -148,10 +157,7 @@ Page.prototype.toHTML = function(output) {
// Normalize HTML output
.then(function() {
var pipelineOpts = {
- onRelativeLink: function (href) {
- href = that.relative(href);
- return output.onRelativeLink(that, href);
- },
+ onRelativeLink: _.partial(output.onRelativeLink, that),
onImage: _.partial(output.onOutputImage, that),
onOutputSVG: _.partial(output.onOutputSVG, that),
@@ -166,7 +172,7 @@ Page.prototype.toHTML = function(output) {
},
// Convert glossary entries to annotations
- annotations: that.book.glosary.annotations()
+ annotations: that.book.glossary.annotations()
};
var pipeline = new HTMLPipeline(that.content, pipelineOpts);
diff --git a/lib/utils/location.js b/lib/utils/location.js
index 09fa93a..d96cf85 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -51,7 +51,8 @@ function toAbsolute(_href, dir, outdir) {
return _href;
}
-// Convert an absolute path to a relative patg
+// Convert an absolute path to a relative path for a specific folder (dir)
+// ('test/', 'hello.md') -> '../hello.md'
function relative(dir, file) {
return normalize(path.relative(dir, file));
}