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

add support for rendering diagrams with mermaid #184

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions example/source/code.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ An example of a code block with a short length
RSpec.describe ContentItem do
end
```

An example of a [mermaid](https://mermaid-js.github.io/mermaid) diagram

```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```

34 changes: 33 additions & 1 deletion lib/govuk_tech_docs/tech_docs_html_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "middleman-core/renderers/redcarpet"
require "tmpdir"
require "timeout"

module GovukTechDocs
class TechDocsHTMLRenderer < Middleman::Renderers::MiddlemanRedcarpetHTML
Expand Down Expand Up @@ -80,7 +82,9 @@ def table_row(body)
end

def block_code(text, lang)
if defined?(super)
if lang == "mermaid"
block_diagram(text)
elsif defined?(super)
# Post-processing the block_code HTML to implement tabbable code blocks.
#
# Middleman monkey patches the Middleman::Renderers::MiddlemanRedcarpetHTML
Expand Down Expand Up @@ -108,5 +112,33 @@ def block_code(text, lang)
pre.to_html
end
end

def block_diagram(code)
mmdc = "#{__dir__}/../../node_modules/.bin/mmdc"
Dir.mktmpdir do |tmp|
input_path = "#{tmp}/input"
output_path = "#{tmp}/output.svg"
File.open(input_path, "w") { |f| f.write(code) }
ok = exec_with_timeout("#{mmdc} -i #{input_path} -o #{output_path} --theme neutral", 10)
if ok && File.exist?(output_path)
File.read(output_path)
else
%({<pre tabindex="0">#{code}</pre>)
end
end
end

def exec_with_timeout(cmd, timeout)
pid = Process.spawn(cmd, { %i[err out] => :close, :pgroup => true })
begin
Timeout.timeout(timeout) do
Process.waitpid(pid, 0)
$CHILD_STATUS.exitstatus.zero?
end
rescue Timeout::Error
Process.kill(15, -Process.getpgid(pid))
false
end
end
end
end
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"lint": "standard"
},
"dependencies": {
"govuk-frontend": "~5.7.1"
"govuk-frontend": "~5.7.1",
"@mermaid-js/mermaid-cli": "^8.4.8"
},
"devDependencies": {
"standard": "^14.3.4"
Expand Down
26 changes: 26 additions & 0 deletions spec/features/diagrams_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rack/file"
require "capybara/rspec"

Capybara.app = Rack::File.new("example/build")

RSpec.describe "Diagrams in code blocks" do
include Capybara::DSL

it "generates static SVG from mermaid code blocks" do
when_the_site_is_created
and_i_visit_the_code_page
then_there_is_an_svg_diagram
end

def when_the_site_is_created
rebuild_site!
end

def and_i_visit_the_code_page
visit "/code.html"
end

def then_there_is_an_svg_diagram
expect(page.body).to match '<svg id="mermaid-'
end
end