summaryrefslogtreecommitdiffstats
path: root/lib/output/modifiers/resolveImages.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-27 22:36:59 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-27 22:36:59 +0200
commit139e487998a620af06837ab75396be3e169b00c0 (patch)
treeb16fe14fdf8bc82f81aba6957937c79efca2189e /lib/output/modifiers/resolveImages.js
parent9bb0a3fbd355b6dda2fe33e3a83884baa8f30917 (diff)
downloadgitbook-139e487998a620af06837ab75396be3e169b00c0.zip
gitbook-139e487998a620af06837ab75396be3e169b00c0.tar.gz
gitbook-139e487998a620af06837ab75396be3e169b00c0.tar.bz2
Correctly resolve images starting with /
Diffstat (limited to 'lib/output/modifiers/resolveImages.js')
-rw-r--r--lib/output/modifiers/resolveImages.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/output/modifiers/resolveImages.js b/lib/output/modifiers/resolveImages.js
new file mode 100644
index 0000000..e401cf5
--- /dev/null
+++ b/lib/output/modifiers/resolveImages.js
@@ -0,0 +1,33 @@
+var path = require('path');
+
+var LocationUtils = require('../../utils/location');
+var editHTMLElement = require('./editHTMLElement');
+
+/**
+ Resolve all HTML images:
+ - /test.png in hello -> ../test.html
+
+ @param {String} currentFile
+ @param {HTMLDom} $
+*/
+function resolveImages(currentFile, $) {
+ var currentDirectory = path.dirname(currentFile);
+
+ return editHTMLElement($, 'img', function($img) {
+ var src = $img.attr('src');
+
+ if (LocationUtils.isExternal(src)) {
+ return;
+ }
+
+ // Calcul absolute path for this
+ src = LocationUtils.toAbsolute(src, currentDirectory, '.');
+
+ // Convert back to relative
+ src = LocationUtils.relative(currentDirectory, src);
+
+ $img.attr('src', src);
+ });
+}
+
+module.exports = resolveImages;