diff options
Diffstat (limited to 'lib/page/html.js')
-rw-r--r-- | lib/page/html.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/page/html.js b/lib/page/html.js index b19d5ed..948462a 100644 --- a/lib/page/html.js +++ b/lib/page/html.js @@ -22,7 +22,10 @@ function HTMLPipeline(htmlString, opts) { convertImages: true, // Calcul new href for a relative link - onRelativeLink: _.identity + onRelativeLink: _.identity, + + // Output a svg, if returns null the svg is kept inlined + onOutputSVG: _.constant(null) }); this.$ = cheerio.load(htmlString, { @@ -70,6 +73,28 @@ HTMLPipeline.prototype.addHeadingIDs = function() { }); }; +// Outline SVG from the HML +HTMLPipeline.prototype.outlineSVG = function() { + var that = this; + + var $svgs = this.$('svg'); + + return Promise.serie($svgs, function(svg) { + var $svg = that.$(svg); + var content = [ + '<?xml version="1.0" encoding="UTF-8"?>', + renderDOM(that.$, $svg) + ].join('\n'); + + return Promise(that.opts.onOutputSVG(content)) + .then(function(filename) { + if (!filename) return; + + $svg.replaceWith(that.$('<img>').attr('src', filename)); + }); + }); +}; + // Write content to the pipeline HTMLPipeline.prototype.output = function() { var that = this; @@ -77,6 +102,7 @@ HTMLPipeline.prototype.output = function() { return Promise() .then(this.normalizeLinks) .then(this.addHeadingIDs) + .then(this.outlineSVG) .then(function() { return renderDOM(that.$); }); |