From fb7653bd14c9fa08ad7cc5db1f5acfddd7d37d0b Mon Sep 17 00:00:00 2001 From: James Phillpotts Date: Thu, 10 Apr 2014 10:31:32 +0100 Subject: [PATCH 1/3] Fix URL creation on windows --- lib/parse/renderer.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 86576f0ca..534a85974 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -46,16 +46,11 @@ GitBookRenderer.prototype.link = function(href, title, text) { var o = this._extra_options; // Relative link, rewrite it to point to github repo if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.repo && o.dir) { - href = 'https://github.com/' + o.repo + '/blob' + path.normalize(path.join( - '/', - o.dir, - href - )); - parsed = url.parse(href); + parsed = url.parse('https://github.com/' + o.repo + '/blob/').resolveObject([o.dir, href].join("/")); } // Generate HTML for link - var out = ' Date: Thu, 10 Apr 2014 10:45:54 +0100 Subject: [PATCH 2/3] Use API from node 0.8 --- lib/parse/renderer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 534a85974..d0ca59793 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -46,7 +46,8 @@ GitBookRenderer.prototype.link = function(href, title, text) { var o = this._extra_options; // Relative link, rewrite it to point to github repo if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.repo && o.dir) { - parsed = url.parse('https://github.com/' + o.repo + '/blob/').resolveObject([o.dir, href].join("/")); + href = url.resolve('https://github.com/' + o.repo + '/blob/', [o.dir, href].join("/")); + parsed = url.parse(href); } // Generate HTML for link From 8f457a752eef936eba95ce3de7bb8e490b1ba4ea Mon Sep 17 00:00:00 2001 From: James Phillpotts Date: Thu, 10 Apr 2014 11:00:52 +0100 Subject: [PATCH 3/3] Use OS-agnostic path generation for images too --- lib/parse/renderer.js | 6 ++---- test/fixtures/PAGE.md | 3 +++ test/page.js | 11 +++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index d0ca59793..6b45a2204 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -76,10 +76,8 @@ 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) { - _href = path.relative(o.outdir, path.normalize(path.join( - o.dir, - href - ))); + var outdir = o.outdir.charAt(o.outdir.length - 1) === '/' ? o.outdir : o.outdir + '/'; + _href = url.resolve(outdir, [o.dir, href].join('/')); } return GitBookRenderer.super_.prototype.image.call(this, _href, title, text); diff --git a/test/fixtures/PAGE.md b/test/fixtures/PAGE.md index 92ee707e6..7dcaa4016 100644 --- a/test/fixtures/PAGE.md +++ b/test/fixtures/PAGE.md @@ -31,6 +31,9 @@ Some more nice content .... [Link to another Markdown file](./xyz/file.md) +And look at this pretty picture: +![Pretty](../assets/my-pretty-picture.png "Pretty") + Lets go for another exercise but this time with some context : --- diff --git a/test/page.js b/test/page.js index 658559ef6..fa6db06f3 100644 --- a/test/page.js +++ b/test/page.js @@ -6,7 +6,10 @@ var page = require('../').parse.page; var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/PAGE.md'), 'utf8'); -var LEXED = page(CONTENT); +var LEXED = page(CONTENT, { + dir: 'course', + outdir: '_book' +}); var HR_CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/HR_PAGE.md'), 'utf8'); var HR_LEXED = page(HR_CONTENT); @@ -30,6 +33,10 @@ 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); @@ -67,7 +74,7 @@ describe('Relative links', function() { repo: 'GitBookIO/javascript', // Imaginary folder of markdown file - dir: 'course', + dir: 'course' }); assert(LEXED[0].content.indexOf('https://github.com/GitBookIO/javascript/blob/src/something.cpp') !== -1);