From f5ded5e5e1cccacd9288f8888943a105b6b3aaa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Wed, 28 Jan 2015 17:52:10 +0100 Subject: [PATCH] Fix absolute path calcul Add tests for links module --- lib/utils/links.js | 16 +++++++++------- lib/utils/page.js | 4 ++-- test/links.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 test/links.js diff --git a/lib/utils/links.js b/lib/utils/links.js index 9fe61270f..9a9582463 100644 --- a/lib/utils/links.js +++ b/lib/utils/links.js @@ -26,15 +26,17 @@ var isRelative = function(href) { // outdir: directory parent from the html output var toAbsolute = function(_href, dir, outdir) { - // Absolute file in source - _href = path.join(dir, _href); + if (isExternal(_href)) return _href; - // make it relative to output - _href = path.relative(outdir, _href); + // Path '_href' inside the base folder + var hrefInRoot = path.normalize(path.join(dir, _href)); + if (_href[0] == "/") hrefInRoot = path.normalize(_href.slice(1)); - if (process.platform === 'win32') { - _href = _href.replace(/\\/g, '/'); - } + // Make it relative to output + _href = path.relative(outdir, hrefInRoot); + + // Normalize windows paths + _href = _href.replace(/\\/g, '/'); return _href; }; diff --git a/lib/utils/page.js b/lib/utils/page.js index 7d21e9406..6a6ee9c15 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -168,7 +168,7 @@ function normalizeHtml(src, options) { // Convert svg images to png function convertImages(images, options) { - options.book.log.info("convert ", images.length, "images to png"); + options.book.log.info.ln("convert ", images.length, "images to png"); return _.reduce(images, function(prev, image) { return prev.then(function() { @@ -187,7 +187,7 @@ function convertImages(images, options) { }); }, Q()) .then(function() { - options.book.log.info.ok(); + options.book.log.info.ok(images.length+" images converted with success"); }); }; diff --git a/test/links.js b/test/links.js new file mode 100644 index 000000000..4dad4d478 --- /dev/null +++ b/test/links.js @@ -0,0 +1,33 @@ +var path = require('path'); +var _ = require('lodash'); +var assert = require('assert'); + +var links = require("../lib/utils/links"); + +describe('Links', function () { + it('should correctly test external links', function() { + assert(links.isExternal("http://google.fr")); + assert(links.isExternal("https://google.fr")); + assert(!links.isExternal("test.md")); + assert(!links.isExternal("folder/test.md")); + assert(!links.isExternal("/folder/test.md")); + }); + + describe('toAbsolute', function() { + it('should correctly transform as absolute', function() { + assert.equal(links.toAbsolute("http://google.fr"), "http://google.fr"); + assert.equal(links.toAbsolute("test.md", "./", "./"), "test.md"); + assert.equal(links.toAbsolute("folder/test.md", "./", "./"), "folder/test.md"); + }); + + it('should correctly handle windows path', function() { + assert.equal(links.toAbsolute("folder\\test.md", "./", "./"), "folder/test.md"); + }); + + it('should correctly handle absolute path', function() { + assert.equal(links.toAbsolute("/test.md", "./", "./"), "test.md"); + assert.equal(links.toAbsolute("/test.md", "test", "test"), "../test.md"); + assert.equal(links.toAbsolute("/sub/test.md", "test", "test"), "../sub/test.md"); + }); + }); +});