Skip to content

Commit

Permalink
Define array of supported languages
Browse files Browse the repository at this point in the history
  • Loading branch information
felixvanoost committed Dec 4, 2023
1 parent 61f3181 commit 17cecba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -69,15 +69,15 @@ 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)
ast (~> 2.4.1)
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)
Expand Down Expand Up @@ -121,4 +121,4 @@ DEPENDENCIES
rubocop (~> 1.21)

BUNDLED WITH
2.2.33
2.4.22
35 changes: 10 additions & 25 deletions lib/jekyll/kroki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17cecba

Please sign in to comment.