Skip to content

Commit 892ec04

Browse files
committed
Merge pull request GitbookIO#78 from mrpotes/mocha-windows
Fix URL creation on windows
2 parents 02d8272 + 8f457a7 commit 892ec04

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

lib/parse/renderer.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ GitBookRenderer.prototype.link = function(href, title, text) {
4646
var o = this._extra_options;
4747
// Relative link, rewrite it to point to github repo
4848
if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.repo && o.dir) {
49-
href = 'https://github.com/' + o.repo + '/blob' + path.normalize(path.join(
50-
'/',
51-
o.dir,
52-
href
53-
));
49+
href = url.resolve('https://github.com/' + o.repo + '/blob/', [o.dir, href].join("/"));
5450
parsed = url.parse(href);
5551
}
5652

5753
// Generate HTML for link
58-
var out = '<a href="' + href + '"';
54+
var out = '<a href="' + parsed.href + '"';
5955
// Title if no null
6056
if (title) {
6157
out += ' title="' + title + '"';
@@ -80,10 +76,8 @@ GitBookRenderer.prototype.image = function(href, title, text) {
8076

8177
// Relative image, rewrite it depending output
8278
if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.dir && o.outdir) {
83-
_href = path.relative(o.outdir, path.normalize(path.join(
84-
o.dir,
85-
href
86-
)));
79+
var outdir = o.outdir.charAt(o.outdir.length - 1) === '/' ? o.outdir : o.outdir + '/';
80+
_href = url.resolve(outdir, [o.dir, href].join('/'));
8781
}
8882

8983
return GitBookRenderer.super_.prototype.image.call(this, _href, title, text);

test/fixtures/PAGE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Some more nice content ....
3131

3232
[Link to another Markdown file](./xyz/file.md)
3333

34+
And look at this pretty picture:
35+
![Pretty](../assets/my-pretty-picture.png "Pretty")
36+
3437
Lets go for another exercise but this time with some context :
3538

3639
---

test/page.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ var page = require('../').parse.page;
66

77

88
var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/PAGE.md'), 'utf8');
9-
var LEXED = page(CONTENT);
9+
var LEXED = page(CONTENT, {
10+
dir: 'course',
11+
outdir: '_book'
12+
});
1013

1114
var HR_CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/HR_PAGE.md'), 'utf8');
1215
var HR_LEXED = page(HR_CONTENT);
@@ -30,6 +33,10 @@ describe('Page parsing', function() {
3033
assert(LEXED[2].content);
3134
});
3235

36+
it('should make image URLs relative', function() {
37+
assert(LEXED[2].content.indexOf('_book/assets/my-pretty-picture.png') !== -1);
38+
})
39+
3340
it('should gen code and content for exercise sections', function() {
3441
assert(LEXED[1].content);
3542
assert(LEXED[1].code);
@@ -67,7 +74,7 @@ describe('Relative links', function() {
6774
repo: 'GitBookIO/javascript',
6875

6976
// Imaginary folder of markdown file
70-
dir: 'course',
77+
dir: 'course'
7178
});
7279

7380
assert(LEXED[0].content.indexOf('https://github.com/GitBookIO/javascript/blob/src/something.cpp') !== -1);

0 commit comments

Comments
 (0)