mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 02:23:30 +00:00
Fix resolve of images
This commit is contained in:
+2
-1
@@ -1,4 +1,5 @@
|
||||
var _ = require('lodash');
|
||||
var path = require('path');
|
||||
|
||||
var Book = require('../book');
|
||||
var NodeFS = require('../fs/node');
|
||||
@@ -36,7 +37,7 @@ var FORMATS = {
|
||||
// the root of the book is the first argument (or current directory)
|
||||
function bookCmd(fn) {
|
||||
return function(args, kwargs) {
|
||||
var input = args[0] || process.cwd();
|
||||
var input = path.resolve(args[0] || process.cwd());
|
||||
var book = new Book({
|
||||
fs: new NodeFS(),
|
||||
root: input,
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ module.exports = {
|
||||
helper.options.log
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var input = args[0] || process.cwd();
|
||||
var input = path.resolve(args[0] || process.cwd());
|
||||
var server = new Server();
|
||||
|
||||
// Init livereload server
|
||||
|
||||
+3
-2
@@ -76,12 +76,12 @@ Output.prototype.generate = function() {
|
||||
.value();
|
||||
|
||||
return Promise.serie(byTypes.asset, function(filename) {
|
||||
that.log.info.ln('copy asset', filename);
|
||||
that.log.debug.ln('copy asset', filename);
|
||||
return that.onAsset(filename);
|
||||
})
|
||||
.then(function() {
|
||||
return Promise.serie(byTypes.page, function(filename) {
|
||||
that.log.info.ln('process page', filename);
|
||||
that.log.debug.ln('process page', filename);
|
||||
return that.onPage(that.book.getPage(filename));
|
||||
});
|
||||
});
|
||||
@@ -154,6 +154,7 @@ Output.prototype.onOutputSVG = function(page, svg) {
|
||||
// Output an image as a file
|
||||
// Normalize the relative link
|
||||
Output.prototype.onOutputImage = function(page, imgFile) {
|
||||
imgFile = page.resolveLocal(imgFile);
|
||||
return page.relative(imgFile);
|
||||
};
|
||||
|
||||
|
||||
@@ -88,7 +88,12 @@ module.exports = function folderOutput(Base) {
|
||||
return Promise()
|
||||
.then(function() {
|
||||
to = that.resolve(to);
|
||||
var folder = path.dirname(to);
|
||||
|
||||
// Ensure folder exists
|
||||
return fs.mkdirp(folder);
|
||||
})
|
||||
.then(function() {
|
||||
return fs.copy(from, to);
|
||||
});
|
||||
};
|
||||
@@ -102,7 +107,7 @@ module.exports = function folderOutput(Base) {
|
||||
filename = that.resolve(filename);
|
||||
var folder = path.dirname(filename);
|
||||
|
||||
// Ensure fodler exists
|
||||
// Ensure folder exists
|
||||
return fs.mkdirp(folder);
|
||||
})
|
||||
|
||||
|
||||
+84
-21
@@ -11,6 +11,9 @@ describe('Page', function() {
|
||||
'links.md': '[link](hello.md) [link 2](variables/page/next.md) [readme](README.md)',
|
||||
'links/relative.md': '[link](../hello.md) [link 2](/variables/page/next.md) [readme](../README.md)',
|
||||
|
||||
'images.md': '  ',
|
||||
'images/relative.md': ' ',
|
||||
|
||||
'annotations/simple.md': 'A magicien say abracadabra!',
|
||||
'annotations/code.md': 'A magicien say `abracadabra`!',
|
||||
'annotations/class.md': 'A magicien say <div class="no-glossary"><b>abracadabra</b>, right?</div>!',
|
||||
@@ -82,6 +85,27 @@ describe('Page', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.resolve', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = book.addPage('links/relative.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (same folder)', function() {
|
||||
page.relative('links/test.md').should.equal('test.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (parent folder)', function() {
|
||||
page.relative('test.md').should.equal('../test.md');
|
||||
page.relative('hello/test.md').should.equal('../hello/test.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (child folder)', function() {
|
||||
page.relative('links/hello/test.md').should.equal('hello/test.md');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Headings', function() {
|
||||
it('should add a default ID to headings', function() {
|
||||
var page = book.addPage('heading.md');
|
||||
@@ -128,27 +152,6 @@ describe('Page', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.resolve', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = book.addPage('links/relative.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (same folder)', function() {
|
||||
page.relative('links/test.md').should.equal('test.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (parent folder)', function() {
|
||||
page.relative('test.md').should.equal('../test.md');
|
||||
page.relative('hello/test.md').should.equal('../hello/test.md');
|
||||
});
|
||||
|
||||
it('should resolve to a relative path (child folder)', function() {
|
||||
page.relative('links/hello/test.md').should.equal('hello/test.md');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Links', function() {
|
||||
describe('From base directory', function() {
|
||||
var page;
|
||||
@@ -217,6 +220,66 @@ describe('Page', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Images', function() {
|
||||
describe('From base directory', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = book.addPage('images.md');
|
||||
return page.toHTML(output);
|
||||
});
|
||||
|
||||
it('should resolve relative images', function() {
|
||||
page.content.should.be.html({
|
||||
'img[src="test.png"]': {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should resolve absolute images', function() {
|
||||
page.content.should.be.html({
|
||||
'img[src="test2.png"]': {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should keep external images path', function() {
|
||||
page.content.should.be.html({
|
||||
'img[src="https:/upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"]': {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('From sub-directory', function() {
|
||||
var page;
|
||||
|
||||
before(function() {
|
||||
page = book.addPage('images/relative.md');
|
||||
return page.toHTML(output);
|
||||
});
|
||||
|
||||
it('should resolve relative images', function() {
|
||||
page.content.should.be.html({
|
||||
'img[src="test.png"]': {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should resolve absolute images', function() {
|
||||
page.content.should.be.html({
|
||||
'img[src="../test2.png"]': {
|
||||
count: 1
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Templating Context', function() {
|
||||
it('should set file.mtime', function() {
|
||||
var page = book.addPage('variables/file/mtime.md');
|
||||
|
||||
Reference in New Issue
Block a user