diff --git a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb index 8b2acb5e..4f8ab09b 100644 --- a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb +++ b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb @@ -154,7 +154,7 @@ def process(processor, parent, attrs, diagram_type, diagram_text, logger) format = get_format(doc, attrs, diagram_type) attrs['role'] = get_role(format, attrs['role']) attrs['format'] = format - kroki_diagram = KrokiDiagram.new(diagram_type, format, diagram_text) + kroki_diagram = KrokiDiagram.new(diagram_type, format, diagram_text, attrs['target']) kroki_client = KrokiClient.new({ server_url: server_url(doc), http_method: http_method(doc), @@ -269,12 +269,13 @@ class KrokiDiagram require 'zlib' require 'digest' - attr_reader :type, :text, :format + attr_reader :type, :text, :format, :target - def initialize(type, format, text) + def initialize(type, format, text, target = nil) @text = text @type = type @format = format + @target = target end def get_diagram_uri(server_url) @@ -287,7 +288,7 @@ def encode def save(output_dir_path, kroki_client) diagram_url = get_diagram_uri(kroki_client.server_url) - diagram_name = "diag-#{Digest::SHA256.hexdigest diagram_url}.#{@format}" + diagram_name = "#{@target || 'diag'}-#{Digest::SHA256.hexdigest diagram_url}.#{@format}" file_path = File.join(output_dir_path, diagram_name) encoding = case @format when 'txt', 'atxt', 'utxt', 'svg' diff --git a/ruby/spec/asciidoctor_kroki_diagram_spec.rb b/ruby/spec/asciidoctor_kroki_diagram_spec.rb index f4a88275..13321d47 100644 --- a/ruby/spec/asciidoctor_kroki_diagram_spec.rb +++ b/ruby/spec/asciidoctor_kroki_diagram_spec.rb @@ -32,7 +32,28 @@ output_dir_path = "#{__dir__}/../.asciidoctor/kroki" diagram_name = kroki_diagram.save(output_dir_path, kroki_client) diagram_path = File.join(output_dir_path, diagram_name) - expect(File.exist?(diagram_path)).to be_truthy, "expected diagram to be saved at #{diagram_path}" + expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}" + content = <<-TXT.chomp + ,-----. ,---. + |alice| |bob| + `--+--' `-+-' + | hello | + |-------------->| + ,--+--. ,-+-. + |alice| |bob| + `-----' `---' + TXT + expect(File.read(diagram_path).split("\n").map(&:rstrip).join("\n")).to eq(content) + end + it 'should fetch a diagram from Kroki and save it to disk using the target name' do + kroki_diagram = ::AsciidoctorExtensions::KrokiDiagram.new('plantuml', 'txt', ' alice -> bob: hello', 'hello-world') + kroki_http_client = ::AsciidoctorExtensions::KrokiHttpClient + kroki_client = ::AsciidoctorExtensions::KrokiClient.new(server_url: 'https://kroki.io', http_method: 'get', http_client: kroki_http_client) + output_dir_path = "#{__dir__}/../.asciidoctor/kroki" + diagram_name = kroki_diagram.save(output_dir_path, kroki_client) + diagram_path = File.join(output_dir_path, diagram_name) + expect(diagram_name).to start_with('hello-world-'), "diagram name should use the target as a prefix, got: #{diagram_name}" + expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}" content = <<-TXT.chomp ,-----. ,---. |alice| |bob| @@ -55,9 +76,9 @@ expect(kroki_http_client).to receive(:get).once.and_return(diagram_contents) diagram_name = kroki_diagram.save(output_dir_path, kroki_client) diagram_path = File.join(output_dir_path, diagram_name) - expect(File.exist?(diagram_path)).to be_truthy, "expected diagram to be saved at #{diagram_path}" + expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}" # calling again... should read the file from disk (and not do a GET request) kroki_diagram.save(output_dir_path, kroki_client) - expect(File.size(diagram_path)).to be_eql(diagram_contents.length), 'expected diagram to be fully saved on disk' + expect(File.size(diagram_path)).to be_eql(diagram_contents.length), 'diagram should be fully saved on disk' end end diff --git a/src/fetch.js b/src/fetch.js index 30d18d9c..d021c6f9 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -29,7 +29,7 @@ module.exports.save = function (krokiDiagram, doc, target, vfs, krokiClient) { const imagesOutputDirectory = getImagesOutputDirectory(doc) const diagramUrl = krokiDiagram.getDiagramUri(krokiClient.getServerUrl()) const format = krokiDiagram.format - const diagramName = `diag-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}` + const diagramName = `${target || 'diag'}-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}` const filePath = path.format({ dir: imagesOutputDirectory, base: diagramName }) let encoding let mediaType diff --git a/test/antora/test.spec.js b/test/antora/test.spec.js index 463c5bd4..1827f87f 100644 --- a/test/antora/test.spec.js +++ b/test/antora/test.spec.js @@ -20,7 +20,7 @@ describe('Antora integration', () => { expect(imageElements.length).to.equal(6) imageElements.each((i, imageElement) => { const src = $(imageElement).attr('src') - expect(src).to.startWith('_images/diag-') + expect(src).to.startWith('_images/ab-') }) }).timeout(50000) }) diff --git a/test/test.spec.js b/test/test.spec.js index cde41e1d..ddeac1a6 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -240,7 +240,7 @@ Hello -> World extension_registry: registry, attributes: { 'kroki-fetch-diagram': true } }) - expect(html).to.contain('hello-world') + expect(html).to.contain('hello-world') }).timeout(5000) it('should download and save an image to a local folder using a generated unique name (md5sum)', () => { const input = `