diff options
Diffstat (limited to 'lib/output/modifiers/svgToImg.js')
-rw-r--r-- | lib/output/modifiers/svgToImg.js | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/lib/output/modifiers/svgToImg.js b/lib/output/modifiers/svgToImg.js index d088e3e..f31b06d 100644 --- a/lib/output/modifiers/svgToImg.js +++ b/lib/output/modifiers/svgToImg.js @@ -1,8 +1,10 @@ var path = require('path'); +var crc = require('crc'); var domSerializer = require('dom-serializer'); var editHTMLElement = require('./editHTMLElement'); var fs = require('../../utils/fs'); +var LocationUtils = require('../../utils/location'); /** Render a cheerio DOM as html @@ -26,23 +28,27 @@ function renderDOM($, dom, options) { @param {String} baseFolder @param {HTMLDom} $ */ -function svgToImg(baseFolder, $) { +function svgToImg(baseFolder, currentFile, $) { + var currentDirectory = path.dirname(currentFile); + 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); + // We avoid generating twice the same PNG + var hash = crc.crc32(content).toString(16); + var fileName = hash + '.svg'; + var filePath = path.join(baseFolder, fileName); - // Write the svg to the file - return fs.writeFile(filePath, content, 'utf8') + // Write the svg to the file + return fs.assertFile(filePath, function() { + return fs.writeFile(filePath, content, 'utf8'); + }) - // Return as image - .then(function() { - $svg.replaceWith('<img src="/' + fileName + '" />'); - }); + // Return as image + .then(function() { + var src = LocationUtils.relative(currentDirectory, fileName); + $svg.replaceWith('<img src="' + src + '" />'); }); }); } |