Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render diagrams in all Jekyll document types #16

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/jekyll/kroki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Kroki
HTTP_MAX_RETRIES = 3

class << self
# Renders all diagram descriptions in any Kroki-supported language and embeds them throughout a Jekyll site.
# Renders and embeds all diagram descriptions in a Jekyll site using Kroki.
#
# @param [Jekyll::Site] The site to embed diagrams in
# @param [Jekyll::Site] The Jekyll site to embed diagrams in
def embed_site(site)
# Get the URL of the Kroki instance
kroki_url = kroki_url(site.config)
Expand All @@ -28,33 +28,33 @@ def embed_site(site)
connection = setup_connection(kroki_url)
supported_languages = get_supported_languages(connection)

site.pages.each do |page|
next unless embeddable?(page)
site.documents.each do |doc|
next unless embeddable?(doc)

# Parse the page, render and embed the diagrams, then convert it back into HTML
parsed_page = Nokogiri::HTML(page.output)
embed_page(connection, supported_languages, parsed_page)
page.output = parsed_page.to_html
# 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
end
rescue StandardError => e
exit(e)
end

# Renders all diagram descriptions in any Kroki-supported language and embeds them in an HTML document.
# 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_page(connection, supported_languages, parsed_page)
def embed_doc(connection, supported_languages, parsed_doc)
supported_languages.each do |language|
parsed_page.css("code[class~='language-#{language}']").each do |diagram_desc|
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))
end
end
end

# Renders a diagram description using Kroki.
# Renders a single diagram description using Kroki.
#
# @param [Faraday::Connection] The Faraday connection to use
# @param [String] The diagram description
Expand Down Expand Up @@ -173,6 +173,6 @@ def exit(error, caller_index = 1)
end
end

Jekyll::Hooks.register :site, :post_render do |doc|
Jekyll::Kroki.embed_site(doc)
Jekyll::Hooks.register :site, :post_render do |site|
Jekyll::Kroki.embed_site(site)
end