From f8b5cde1a91861c307461fe2271d5d4b0c9138cf Mon Sep 17 00:00:00 2001 From: Andre Bossert Date: Sun, 10 May 2020 21:41:08 +0200 Subject: [PATCH] Use second optional positional attribute (target) as generated-file-name Closes #48 --- src/fetch.js | 7 ++++++- test/test.js | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/fetch.js b/src/fetch.js index f7f6a9cc..3d4cc893 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -17,7 +17,12 @@ module.exports.save = function (diagramUrl, doc, target, format, vfs) { } else { dirPath = path.join(baseDir, imagesDir) } - const diagramName = `diag-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}` + let diagramName + if (target) { + diagramName = `${target}.${format}` + } else { + diagramName = `diag-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}` + } let exists if (typeof vfs === 'undefined' || typeof vfs.exists !== 'function') { exists = require('./node-fs').exists diff --git a/test/test.js b/test/test.js index bfc6dbdb..06a3a45b 100644 --- a/test/test.js +++ b/test/test.js @@ -83,7 +83,7 @@ plantuml::test/fixtures/alice.puml[svg,role=sequence] const hash = rusha.createHash().update(`https://kroki.io/plantuml/svg/${encode(file)}`).digest('hex') expect(html).to.contain(`diagram`) }) - it('should download and save an image to a local folder', () => { + it('should download and save an image to a local folder and target name', () => { const input = ` :imagesdir: .asciidoctor/kroki @@ -95,7 +95,21 @@ Hello -> World const registry = asciidoctor.Extensions.create() asciidoctorKroki.register(registry) const html = asciidoctor.convert(input, { extension_registry: registry, attributes: { 'kroki-fetch-diagram': true } }) - expect(html).to.contain('hello-world') + expect(html).to.contain('hello-world') + }) + it('should download and save an image to a local folder and generated name', () => { + const input = ` +:imagesdir: .asciidoctor/kroki + +[plantuml,"",svg,role=sequence] +.... +Hello -> World +.... +` + const registry = asciidoctor.Extensions.create() + asciidoctorKroki.register(registry) + const html = asciidoctor.convert(input, { extension_registry: registry, attributes: { 'kroki-fetch-diagram': true } }) + expect(html).to.contain('diagram') }) it('should apply substitutions in diagram block', () => { const input = ` @@ -132,7 +146,27 @@ plantuml::{fixtures-dir}/alice.puml[svg,role=sequence] const hash = rusha.createHash().update(`https://kroki.io/plantuml/svg/${encode(file)}`).digest('hex') expect(html).to.contain(`diagram`) }) - it('should not download twice the same image', () => { + it('should not download twice the same image with generated name', () => { + const input = ` +:imagesdir: .asciidoctor/kroki + +[plantuml,"",svg,role=sequence] +.... +AsciiDoc -> HTML5: convert +.... +` + sinon.spy(http, 'get') + try { + const registry = asciidoctor.Extensions.create() + asciidoctorKroki.register(registry) + const html = asciidoctor.convert(input, { extension_registry: registry, attributes: { 'kroki-fetch-diagram': true } }) + expect(html).to.contain('diagram') + expect(http.get.calledOnce).to.be.true() + } finally { + http.get.restore() + } + }) + it('should not download twice the same image with target name', () => { const input = ` :imagesdir: .asciidoctor/kroki @@ -146,7 +180,7 @@ AsciiDoc -> HTML5: convert const registry = asciidoctor.Extensions.create() asciidoctorKroki.register(registry) const html = asciidoctor.convert(input, { extension_registry: registry, attributes: { 'kroki-fetch-diagram': true } }) - expect(html).to.contain('asciidoc-html5') + expect(html).to.contain('asciidoc-html5') expect(http.get.calledOnce).to.be.true() } finally { http.get.restore()