From b5c6c1f6fb162663a7488e7bb707f07ad96b74af Mon Sep 17 00:00:00 2001 From: Felix van Oost Date: Mon, 4 Dec 2023 05:29:44 +0000 Subject: [PATCH] Define array of supported languages (#17) --- Gemfile.lock | 10 +++++----- lib/jekyll/kroki.rb | 35 ++++++++++------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 10cee1d..67115e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -30,10 +30,10 @@ GEM faraday (~> 2.0) ffi (1.16.3) forwardable-extended (2.6.0) - google-protobuf (3.25.0-x86_64-linux) + google-protobuf (3.25.1-x86_64-linux) http-2-next (1.0.1) http_parser.rb (0.8.0) - httpx (1.1.1) + httpx (1.1.3) http-2-next (>= 1.0.1) i18n (1.14.1) concurrent-ruby (~> 1.0) @@ -69,7 +69,7 @@ GEM rb-inotify (~> 0.9, >= 0.9.10) mercenary (0.4.0) minitest (5.20.0) - nokogiri (1.15.4-x86_64-linux) + nokogiri (1.15.5-x86_64-linux) racc (~> 1.4) parallel (1.23.0) parser (3.2.2.4) @@ -77,7 +77,7 @@ GEM racc pathutil (0.16.2) forwardable-extended (~> 2.6) - public_suffix (5.0.3) + public_suffix (5.0.4) racc (1.7.3) rainbow (3.1.1) rake (13.1.0) @@ -121,4 +121,4 @@ DEPENDENCIES rubocop (~> 1.21) BUNDLED WITH - 2.2.33 + 2.4.22 diff --git a/lib/jekyll/kroki.rb b/lib/jekyll/kroki.rb index 2c519c5..611078e 100644 --- a/lib/jekyll/kroki.rb +++ b/lib/jekyll/kroki.rb @@ -14,6 +14,9 @@ module Jekyll # Converts diagram descriptions into images using Kroki class Kroki KROKI_DEFAULT_URL = "https://kroki.io" + SUPPORTED_LANGUAGES = %w[actdiag blockdiag bpmn bytefield c4plantuml d2 dbml diagramsnet ditaa erd excalidraw + graphviz mermaid nomnoml nwdiag packetdiag pikchr plantuml rackdiag seqdiag structurizr + svgbob symbolator tikz umlet vega vegalite wavedrom wireviz].freeze HTTP_MAX_RETRIES = 3 class << self @@ -26,15 +29,14 @@ def embed_site(site) puts "[jekyll-kroki] Rendering diagrams using Kroki instance at '#{kroki_url}'" connection = setup_connection(kroki_url) - supported_languages = get_supported_languages(connection) site.documents.each do |doc| next unless embeddable?(doc) # Parse the HTML document, render and embed the diagrams, then convert it back into HTML - parsed_doc = Nokogiri::HTML(doc.output) - embed_doc(connection, supported_languages, parsed_doc) - doc.output = parsed_doc.to_html + parsed_page = Nokogiri::HTML(doc.output) + embed_page(connection, parsed_page) + doc.output = parsed_page.to_html end rescue StandardError => e exit(e) @@ -43,10 +45,9 @@ def embed_site(site) # Renders all diagram descriptions in a document and embeds them as inline SVGs in the HTML source. # # @param [Faraday::Connection] The Faraday connection to use - # @param [Array] The supported diagram languages # @param [Nokogiri::HTML4::Document] The parsed HTML document - def embed_doc(connection, supported_languages, parsed_doc) - supported_languages.each do |language| + def embed_page(connection, parsed_doc) + SUPPORTED_LANGUAGES.each do |language| parsed_doc.css("code[class~='language-#{language}']").each do |diagram_desc| # Replace the diagram description with the SVG representation rendered by Kroki diagram_desc.replace(render_diagram(connection, diagram_desc, language)) @@ -64,6 +65,8 @@ def render_diagram(connection, diagram_desc, language) begin response = connection.get("#{language}/svg/#{encode_diagram(diagram_desc.text)}") rescue Faraday::Error => e + raise "'#{connection.url_prefix}' does not point to a valid Kroki instance" if e.response.nil? + raise e.response[:body] end expected_content_type = "image/svg+xml" @@ -96,24 +99,6 @@ def encode_diagram(diagram_desc) Base64.urlsafe_encode64(Zlib.deflate(diagram_desc)) end - # Gets an array of supported diagram languages from the Kroki '/health' endpoint. - # - # This only shows which languages the Kroki project supports, not which ones are currently available from the - # configured Kroki instance. For example, Mermaid will still show up as a supported language even if the Mermaid - # companion container is not running. - # - # @param [Faraday::Connection] The Faraday connection to use - # @return [Array] The supported diagram languages - def get_supported_languages(connection) - response = connection.get("health") - supported_languages = response.body["version"]&.keys - raise "'#{connection.url_prefix}' does not point to a valid Kroki instance" if supported_languages.nil? - - supported_languages - rescue Faraday::Error => e - raise e - end - # Sets up a new Faraday connection. # # @param [URI::HTTP] The URL of the Kroki instance