diff options
Diffstat (limited to 'lib/output/modifiers/svgToImg.js')
-rw-r--r-- | lib/output/modifiers/svgToImg.js | 46 |
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; |