summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-25 11:28:21 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-25 11:28:21 +0200
commite1e4e7f01177d968e63f0b3a1830eda1adacb56b (patch)
treede1864bac7bce3f7e0c3a881b70e899a563347da
parent814b6ff489bc1f0bfd03db6409d49185e8c7b93e (diff)
downloadgitbook-e1e4e7f01177d968e63f0b3a1830eda1adacb56b.zip
gitbook-e1e4e7f01177d968e63f0b3a1830eda1adacb56b.tar.gz
gitbook-e1e4e7f01177d968e63f0b3a1830eda1adacb56b.tar.bz2
Add tests for link resolution
-rw-r--r--lib/output/modifiers/__tests__/resolveLinks.js60
-rw-r--r--lib/output/modifiers/resolveLinks.js4
-rw-r--r--lib/utils/__tests__/location.js29
3 files changed, 91 insertions, 2 deletions
diff --git a/lib/output/modifiers/__tests__/resolveLinks.js b/lib/output/modifiers/__tests__/resolveLinks.js
index 36511a2..3d50d80 100644
--- a/lib/output/modifiers/__tests__/resolveLinks.js
+++ b/lib/output/modifiers/__tests__/resolveLinks.js
@@ -1,10 +1,70 @@
jest.autoMockOff();
+var path = require('path');
var cheerio = require('cheerio');
describe('resolveLinks', function() {
var resolveLinks = require('../resolveLinks');
+ function resolveFileBasic(href) {
+ return href;
+ }
+
+ function resolveFileCustom(href) {
+ if (path.extname(href) == '.md') {
+ return href.slice(0, -3) + '.html';
+ }
+
+ return href;
+ }
+
+ describe('Absolute path', function() {
+ var TEST = '<p>This is a <a href="/test/cool.md"></a></p>';
+
+ pit('should resolve path starting by "/" in root directory', function() {
+ var $ = cheerio.load(TEST);
+
+ return resolveLinks('hello.md', resolveFileBasic, $)
+ .then(function() {
+ var link = $('a');
+ expect(link.attr('href')).toBe('test/cool.md');
+ });
+ });
+
+ pit('should resolve path starting by "/" in child directory', function() {
+ var $ = cheerio.load(TEST);
+
+ return resolveLinks('afolder/hello.md', resolveFileBasic, $)
+ .then(function() {
+ var link = $('a');
+ expect(link.attr('href')).toBe('../test/cool.md');
+ });
+ });
+ });
+
+ describe('Custom Resolver', function() {
+ var TEST = '<p>This is a <a href="/test/cool.md"></a> <a href="afile.png"></a></p>';
+
+ pit('should resolve path correctly for absolute path', function() {
+ var $ = cheerio.load(TEST);
+
+ return resolveLinks('hello.md', resolveFileCustom, $)
+ .then(function() {
+ var link = $('a').first();
+ expect(link.attr('href')).toBe('test/cool.html');
+ });
+ });
+
+ pit('should resolve path correctly for absolute path (2)', function() {
+ var $ = cheerio.load(TEST);
+
+ return resolveLinks('afodler/hello.md', resolveFileCustom, $)
+ .then(function() {
+ var link = $('a').first();
+ expect(link.attr('href')).toBe('../test/cool.html');
+ });
+ });
+ });
});
diff --git a/lib/output/modifiers/resolveLinks.js b/lib/output/modifiers/resolveLinks.js
index b5ad4f3..bf3fd10 100644
--- a/lib/output/modifiers/resolveLinks.js
+++ b/lib/output/modifiers/resolveLinks.js
@@ -17,13 +17,13 @@ function resolveLinks(currentFile, resolveFile, $) {
return editHTMLElement($, 'a', function($a) {
var href = $a.attr('href');
- if (location.isExternal(href)) {
+ if (LocationUtils.isExternal(href)) {
$a.attr('_target', 'blank');
return;
}
// Calcul absolute path for this
- href = LocationUtils.toAbsolute(href, currentDirectory, currentDirectory);
+ href = LocationUtils.toAbsolute(href, currentDirectory, '.');
// Resolve file
href = resolveFile(href);
diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js
new file mode 100644
index 0000000..183086d
--- /dev/null
+++ b/lib/utils/__tests__/location.js
@@ -0,0 +1,29 @@
+jest.autoMockOff();
+
+describe('LocationUtils', function() {
+ var LocationUtils = require('../location');
+
+ describe('toAbsolute', function() {
+
+ it('should resolve path starting by "/" in root directory', function() {
+ expect(
+ LocationUtils.toAbsolute('/test/hello.md', './', './')
+ ).toBe('test/hello.md');
+ });
+
+ it('should resolve path starting by "/" in child directory', function() {
+ expect(
+ LocationUtils.toAbsolute('/test/hello.md', './hello', './')
+ ).toBe('test/hello.md');
+ });
+
+ it('should resolve path starting by "/" in child directory, with same output directory', function() {
+ expect(
+ LocationUtils.toAbsolute('/test/hello.md', './hello', './hello')
+ ).toBe('../test/hello.md');
+ });
+ });
+
+});
+
+