@@ -26,33 +26,44 @@ class << self
26
26
def embed_site ( site )
27
27
# Get the URL of the Kroki instance
28
28
kroki_url = kroki_url ( site . config )
29
- puts "[jekyll-kroki] Rendering diagrams using Kroki instance at '#{ kroki_url } '"
30
-
31
29
connection = setup_connection ( kroki_url )
32
30
33
- site . documents . each do |doc |
31
+ rendered_diag = 0
32
+ ( site . pages + site . documents ) . each do |doc |
34
33
next unless embeddable? ( doc )
35
34
36
- # Parse the HTML document, render and embed the diagrams, then convert it back into HTML
37
- parsed_page = Nokogiri ::HTML ( doc . output )
38
- embed_page ( connection , parsed_page )
39
- doc . output = parsed_page . to_html
35
+ # Render all supported diagram descriptions in the document
36
+ rendered_diag += embed_doc ( connection , doc )
37
+ end
38
+
39
+ unless rendered_diag . zero?
40
+ puts "[jekyll-kroki] Rendered #{ rendered_diag } diagrams using Kroki instance at '#{ kroki_url } '"
40
41
end
41
42
rescue StandardError => e
42
43
exit ( e )
43
44
end
44
45
45
- # Renders all diagram descriptions in a document and embeds them as inline SVGs in the HTML source.
46
+ # Renders all supported diagram descriptions in a document and embeds them as inline SVGs in the HTML source.
46
47
#
47
48
# @param [Faraday::Connection] The Faraday connection to use
48
49
# @param [Nokogiri::HTML4::Document] The parsed HTML document
49
- def embed_page ( connection , parsed_doc )
50
+ # @param [Integer] The number of rendered diagrams
51
+ def embed_doc ( connection , doc )
52
+ # Parse the HTML document
53
+ parsed_doc = Nokogiri ::HTML ( doc . output )
54
+
55
+ rendered_diag = 0
50
56
SUPPORTED_LANGUAGES . each do |language |
51
57
parsed_doc . css ( "code[class~='language-#{ language } ']" ) . each do |diagram_desc |
52
58
# Replace the diagram description with the SVG representation rendered by Kroki
53
59
diagram_desc . replace ( render_diagram ( connection , diagram_desc , language ) )
60
+ rendered_diag += 1
54
61
end
55
62
end
63
+
64
+ # Convert the document back to HTML
65
+ doc . output = parsed_doc . to_html
66
+ rendered_diag
56
67
end
57
68
58
69
# Renders a single diagram description using Kroki.
@@ -80,10 +91,10 @@ def render_diagram(connection, diagram_desc, language)
80
91
end
81
92
82
93
# Sanitises a rendered diagram. Only <script> elements are removed, which is the most minimal / naive
83
- # implementation possible and is definitely not secure .
94
+ # implementation possible.
84
95
#
85
96
# @param [String] The diagram to santise in SVG format
86
- # @return [String] The sanitized diagram in SVG format
97
+ # @return [String] The sanitised diagram
87
98
def sanitise_diagram ( diagram_svg )
88
99
parsed_svg = Nokogiri ::XML ( diagram_svg )
89
100
parsed_svg . xpath ( '//*[name()="script"]' ) . each ( &:remove )
0 commit comments