-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmermaid.rb
35 lines (31 loc) · 1020 Bytes
/
mermaid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'uri'
require 'net/http'
require 'base64'
module Jekyll
class MermaidBlock < Liquid::Block
def cache
@@cache ||= Jekyll::Cache.new("Jekyll::Cocov::MermaidBlock")
end
BASE_PATH = "assets/images/diagrams"
def render(context)
contents = super
id = Digest::SHA1.hexdigest(contents)
FileUtils.mkdir_p BASE_PATH
target_file = File.join(BASE_PATH, "#{id}.svg")
unless File.exist? target_file
svg = cache.getset(id) do
data = Base64.urlsafe_encode64(contents, padding: false)
uri = URI("https://mermaid.ink/svg/#{data}")
res = Net::HTTP.get_response(uri)
unless res.is_a?(Net::HTTPSuccess)
raise RuntimeError, "Failed to perform mermaid.ink request: #{res.body}"
end
res.body
end
File.write(target_file, svg)
end
"<img src=\"/#{target_file}\" alt=\"A Mermaid Diagram\" />"
end
end
end
Liquid::Template.register_tag('mermaid', Jekyll::MermaidBlock)