summaryrefslogtreecommitdiffstats
path: root/lib/output/modifiers/resolveImages.js
diff options
context:
space:
mode:
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;