diff options
-rw-r--r-- | lib/cli/helper.js | 3 | ||||
-rw-r--r-- | lib/cli/index.js | 2 | ||||
-rw-r--r-- | lib/output/base.js | 5 | ||||
-rw-r--r-- | lib/output/folder.js | 7 | ||||
-rw-r--r-- | test/page.js | 105 |
5 files changed, 96 insertions, 26 deletions
diff --git a/lib/cli/helper.js b/lib/cli/helper.js index 1086b4d..6dae58c 100644 --- a/lib/cli/helper.js +++ b/lib/cli/helper.js @@ -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, diff --git a/lib/cli/index.js b/lib/cli/index.js index 0492c29..07c4888 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -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 diff --git a/lib/output/base.js b/lib/output/base.js index 6678bb0..2a7ec48 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -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); }; diff --git a/lib/output/folder.js b/lib/output/folder.js index 14e3f22..9e139c0 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -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); }) diff --git a/test/page.js b/test/page.js index 41a0893..52d86d8 100644 --- a/test/page.js +++ b/test/page.js @@ -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'); |