summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-02-13 10:43:22 +0100
committerSamy Pesse <samypesse@gmail.com>2016-02-13 10:43:22 +0100
commit272f30af532b247e2b13d2d0b59c89a041723e9a (patch)
tree5a0ee666ab8dfafaa30addf5171a0a52b3695be7
parent4c6717e23488656686f276aa2b40ce1d1c7641f8 (diff)
downloadgitbook-272f30af532b247e2b13d2d0b59c89a041723e9a.zip
gitbook-272f30af532b247e2b13d2d0b59c89a041723e9a.tar.gz
gitbook-272f30af532b247e2b13d2d0b59c89a041723e9a.tar.bz2
Complete test for convertion of svgs
-rw-r--r--lib/output/assets-inliner.js13
-rw-r--r--lib/page/index.js8
-rw-r--r--lib/utils/location.js8
-rw-r--r--test/assets-inliner.js14
-rw-r--r--test/page.js12
5 files changed, 50 insertions, 5 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js
index 5f33956..65ecbfa 100644
--- a/lib/output/assets-inliner.js
+++ b/lib/output/assets-inliner.js
@@ -22,8 +22,13 @@ AssetsInliner.prototype.onOutputSVG = function(page, svg) {
this.log.debug.ln('output svg from', page.path);
var filename = _.uniqueId('svg_') + '.png';
+ // Convert svg buffer to a png file
return imagesUtil.convertSVGBufferToPNG(svg, this.resolve(filename))
- .thenResolve('/' + filename);
+
+ // Return relative path from the page
+ .thenResolve(function() {
+ return page.relative('/' + filename);
+ });
};
// Output an image as a file
@@ -35,7 +40,11 @@ AssetsInliner.prototype.onOutputImage = function(page, imgFile) {
// Convert SVG to PNG
var filename = _.uniqueId('svg_') + '.png';
return imagesUtil.convertSVGToPNG(page.resolve(imgFile), this.resolve(filename))
- .thenResolve('/' + filename);
+
+ // Return relative path from the page
+ .thenResolve(function() {
+ return page.relative('/' + filename);
+ });
};
diff --git a/lib/page/index.js b/lib/page/index.js
index e96f89b..77b9950 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -78,6 +78,14 @@ Page.prototype.resolve = function() {
return this.book.resolve(this.resolveLocal.apply(this, arguments));
};
+// Convert an absolite path to a relative path from this page
+Page.prototype.relative = function(name) {
+ return location.relative(
+ this.resolve('.'),
+ this.resolve(name)
+ );
+};
+
// Update content of the page
Page.prototype.update = function(content) {
this.content = content;
diff --git a/lib/utils/location.js b/lib/utils/location.js
index 3f3cb37..09fa93a 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -51,10 +51,16 @@ function toAbsolute(_href, dir, outdir) {
return _href;
}
+// Convert an absolute path to a relative patg
+function relative(dir, file) {
+ return normalize(path.relative(dir, file));
+}
+
module.exports = {
isExternal: isExternal,
isRelative: isRelative,
isAnchor: isAnchor,
normalize: normalize,
- toAbsolute: toAbsolute
+ toAbsolute: toAbsolute,
+ relative: relative
};
diff --git a/test/assets-inliner.js b/test/assets-inliner.js
index 479d788..0636a1b 100644
--- a/test/assets-inliner.js
+++ b/test/assets-inliner.js
@@ -1,3 +1,5 @@
+var cheerio = require('cheerio');
+
var mock = require('./mock');
var AssetsInliner = require('../lib/output/assets-inliner');
@@ -9,7 +11,7 @@ describe('Assets Inliner Output', function() {
before(function() {
return mock.outputDefaultBook(AssetsInliner, {
'README.md': '![image](test.svg)',
- 'test.svg': '<svg width="100" height="100"><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /></svg>'
+ 'test.svg': '<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>'
})
.then(function(_output) {
output = _output;
@@ -18,7 +20,15 @@ describe('Assets Inliner Output', function() {
it('should correctly convert to PNG', function() {
var readme = output.book.getPage('README.md');
- console.log(readme.content)
+ var $ = cheerio.load(readme.content);
+
+ // Is there an image?
+ var $img = $('img');
+ $img.length.should.equal(1);
+
+ // Does the file exists
+ var src = $img.attr('src');
+ output.should.have.file(src);
});
});
diff --git a/test/page.js b/test/page.js
index 9007c45..d844d13 100644
--- a/test/page.js
+++ b/test/page.js
@@ -38,6 +38,18 @@ describe('Page', function() {
});
});
+ describe('.relative', function() {
+ it('should correctly resolve absolute path in the book', function() {
+ var page = book.addPage('heading.md');
+ var page2 = book.addPage('folder/paths.md');
+
+ page.relative('/test.png').should.equal('test.png');
+ page.relative('test.png').should.equal('test.png');
+ page2.relative('/test.png').should.equal('../test.png');
+ page2.relative('test.png').should.equal('test.png');
+ });
+ });
+
describe('Headings', function() {
it('should add a default ID to headings', function() {
var page = book.addPage('heading.md');