summaryrefslogtreecommitdiffstats
path: root/lib/output/modifiers/svgToImg.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-23 21:10:53 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-23 21:10:53 +0200
commita3df6c6f0c1068762f9d48cdff97ab5d4c583082 (patch)
treee43d732f7676430d0075814e11fcb4372803e9da /lib/output/modifiers/svgToImg.js
parente1fa977b5b1b3c03790de6e2c21ee39ba55d9555 (diff)
downloadgitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.zip
gitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.tar.gz
gitbook-a3df6c6f0c1068762f9d48cdff97ab5d4c583082.tar.bz2
Add assets inliner modifier for HTML
Diffstat (limited to 'lib/output/modifiers/svgToImg.js')
-rw-r--r--lib/output/modifiers/svgToImg.js46
1 files changed, 34 insertions, 12 deletions
diff --git a/lib/output/modifiers/svgToImg.js b/lib/output/modifiers/svgToImg.js
index b36770a..d088e3e 100644
--- a/lib/output/modifiers/svgToImg.js
+++ b/lib/output/modifiers/svgToImg.js
@@ -1,7 +1,17 @@
-var cheerio = require('cheerio');
+var path = require('path');
var domSerializer = require('dom-serializer');
-// Render a cheerio DOM as html
+var editHTMLElement = require('./editHTMLElement');
+var fs = require('../../utils/fs');
+
+/**
+ Render a cheerio DOM as html
+
+ @param {HTMLDom} $
+ @param {HTMLElement} dom
+ @param {Object}
+ @return {String}
+*/
function renderDOM($, dom, options) {
if (!dom && $._root && $._root.children) {
dom = $._root.children;
@@ -11,18 +21,30 @@ function renderDOM($, dom, options) {
}
/**
+ Replace SVG tag by IMG
+ @param {String} baseFolder
+ @param {HTMLDom} $
*/
-var svgToImg = HTMLModifier('svg', function($svg, $) {
- var content = '<?xml version="1.0" encoding="UTF-8"?>' + renderDOM($, $svg);
-
-
-
-});
-
-function svgToImg(page) {
- var $ = cheerio.load(page.content);
-
+function svgToImg(baseFolder, $) {
+ return editHTMLElement($, 'svg', function($svg) {
+ var content = '<?xml version="1.0" encoding="UTF-8"?>' +
+ renderDOM($, $svg);
+
+ // Generate a filename
+ return fs.uniqueFilename(baseFolder, 'image.svg')
+ .then(function(fileName) {
+ var filePath = path.join(baseFolder, fileName);
+
+ // Write the svg to the file
+ return fs.writeFile(filePath, content, 'utf8')
+
+ // Return as image
+ .then(function() {
+ $svg.replaceWith('<img src="/' + fileName + '" />');
+ });
+ });
+ });
}
module.exports = svgToImg;