summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2014-04-18 10:17:20 -0700
committerAaron O'Mullan <aaron.omullan@gmail.com>2014-04-18 10:17:20 -0700
commit0d9d62f1a9987d32299eb8e5a65d5de2cf7b0758 (patch)
tree27534a3644a28f1a535ad5ecbd05f1f5e2e351e5
parentf7e9abf984f7cf5e062ec4ce929bd77d3b2528e3 (diff)
parentdd0579b60d29da744ef49aae26423b91529ac8cd (diff)
downloadgitbook-0d9d62f1a9987d32299eb8e5a65d5de2cf7b0758.zip
gitbook-0d9d62f1a9987d32299eb8e5a65d5de2cf7b0758.tar.gz
gitbook-0d9d62f1a9987d32299eb8e5a65d5de2cf7b0758.tar.bz2
Merge pull request #117 from GitbookIO/fix/99
Fix #99
-rw-r--r--lib/parse/renderer.js10
-rw-r--r--test/fixtures/IMAGES.md12
-rw-r--r--test/page.js34
3 files changed, 50 insertions, 6 deletions
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js
index 2a72d48..949a9ee 100644
--- a/lib/parse/renderer.js
+++ b/lib/parse/renderer.js
@@ -79,8 +79,14 @@ GitBookRenderer.prototype.image = function(href, title, text) {
// Relative image, rewrite it depending output
if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.dir && o.outdir) {
- var outdir = o.outdir.charAt(o.outdir.length - 1) === '/' ? o.outdir : o.outdir + '/';
- _href = url.resolve(outdir, [o.dir, href].join('/'));
+ // o.dir: directory parent of the file currently in rendering process
+ // o.outdir: directory parent from the html output
+
+ // Absolute file in source
+ _href = path.join(o.dir, _href);
+
+ // make it relative to output
+ _href = path.relative(o.outdir, _href);
}
return GitBookRenderer.super_.prototype.image.call(this, _href, title, text);
diff --git a/test/fixtures/IMAGES.md b/test/fixtures/IMAGES.md
new file mode 100644
index 0000000..99ffa00
--- /dev/null
+++ b/test/fixtures/IMAGES.md
@@ -0,0 +1,12 @@
+# Images Test
+
+This is a test for images path calculation. It supposed this fiel is in a syntax/ folder
+
+### #
+
+[![Screen](./preview.png)](./preview.png)
+
+### 2
+
+[![Screen](../preview2.png)](./preview2.png)
+
diff --git a/test/page.js b/test/page.js
index 52a3df6..fb6fd70 100644
--- a/test/page.js
+++ b/test/page.js
@@ -36,10 +36,6 @@ describe('Page parsing', function() {
assert(LEXED[2].content);
});
- it('should make image URLs relative', function() {
- assert(LEXED[2].content.indexOf('_book/assets/my-pretty-picture.png') !== -1);
- });
-
it('should gen code and content for exercise sections', function() {
assert(LEXED[1].content);
assert(LEXED[1].code);
@@ -94,3 +90,33 @@ describe('Relative links', function() {
assert(LEXED[0].content.indexOf('https://github.com/GitBookIO/javascript/blob/src/something.cpp') !== -1);
});
});
+
+describe('Relative images', function() {
+ it('should keep image relative with considering output directory in site format', function() {
+ var LEXED = loadPage('IMAGES', {
+ // GitHub repo ID
+ repo: 'GitBookIO/javascript',
+
+ // Imaginary folder of markdown file
+ dir: 'syntax',
+ outdir: 'syntax'
+ });
+
+ assert(LEXED[0].content.indexOf('"preview.png"') !== -1);
+ assert(LEXED[0].content.indexOf('"../preview2.png"') !== -1);
+ });
+
+ it('should keep image relative with considering output directory in page format', function() {
+ var LEXED = loadPage('IMAGES', {
+ // GitHub repo ID
+ repo: 'GitBookIO/javascript',
+
+ // Imaginary folder of markdown file
+ dir: 'syntax',
+ outdir: './'
+ });
+
+ assert(LEXED[0].content.indexOf('"syntax/preview.png"') !== -1);
+ assert(LEXED[0].content.indexOf('"preview2.png"') !== -1);
+ });
+});