Skip to content

Commit a784264

Browse files
authored
- Add handling for mermaid code blocks
2 parents dda2d83 + 663de11 commit a784264

10 files changed

Lines changed: 77 additions & 16 deletions

File tree

lib/madness/rendering/pandoc.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Pandoc
66
include ServerHelper
77

88
def render(text)
9+
text = process_mermaid_blocks text if config.mermaid
910
PandocRuby.new(text, [{ from: :gfm, to: :html }], *options).convert
1011
end
1112

@@ -14,6 +15,12 @@ def render(text)
1415
def options
1516
@options ||= config.highlighter ? [] : :no_highlight
1617
end
18+
19+
def process_mermaid_blocks(text)
20+
text.gsub(/```mermaid\s+(.+?)\s+```/m) do
21+
"<pre class='mermaid'>#{$1.strip}</pre>"
22+
end
23+
end
1724
end
1825
end
1926
end

lib/madness/rendering/redcarpet.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def renderer
3636
end
3737

3838
def handler_class
39-
config.highlighter ? HighlightRenderer : ::Redcarpet::Render::HTML
39+
if config.mermaid || config.highlighter
40+
CustomRenderer
41+
else
42+
::Redcarpet::Render::HTML
43+
end
4044
end
4145
end
4246
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'redcarpet'
2+
require 'rouge'
3+
require 'rouge/plugins/redcarpet'
4+
5+
module Madness
6+
# Renderer with syntax highlighting support
7+
class CustomRenderer < Redcarpet::Render::HTML
8+
include Rouge::Plugins::Redcarpet
9+
10+
def block_code(code, language)
11+
if language == 'mermaid'
12+
"<pre class='mermaid'>#{code}</pre>"
13+
else
14+
super
15+
end
16+
end
17+
end
18+
end

lib/madness/rendering/redcarpet_highlighter.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

spec/approvals/render/all-disabled

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656
<li>List
5757

5858
<ol>
59-
<li>with </li>
59+
<li>with</li>
6060
<li>nesting</li>
6161
</ol></li>
6262
</ol>
@@ -111,6 +111,15 @@ graph TD;
111111
C-->D;
112112
</div>
113113

114+
<p>Or using the <code>mermaid</code> code block:</p>
115+
116+
<pre><code class="mermaid">graph TD;
117+
A--&gt;B;
118+
A--&gt;C;
119+
B--&gt;D;
120+
C--&gt;D;
121+
</code></pre>
122+
114123
<h2 id="unsafe-html">Unsafe HTML</h2>
115124

116125
<p>This is calculated by Javascript: <span id='demo'></div></p>

spec/approvals/render/all-enabled

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ with <strong>markdown support</strong> in it</p>
6767
<li>List
6868

6969
<ol>
70-
<li>with </li>
70+
<li>with</li>
7171
<li>nesting</li>
7272
</ol></li>
7373
</ol>
@@ -122,6 +122,13 @@ graph TD;
122122
C-->D;
123123
</div>
124124

125+
<p>Or using the <code>mermaid</code> code block:</p>
126+
<pre class='mermaid'>graph TD;
127+
A-->B;
128+
A-->C;
129+
B-->D;
130+
C-->D;
131+
</pre>
125132
<h2 id="unsafe-html">Unsafe HTML</h2>
126133

127134
<p>This is calculated by Javascript: <span id='demo'></div></p>

spec/approvals/render/pandoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ graph TD;
8787
C-->D;
8888
</div>
8989

90+
<p>Or using the <code>mermaid</code> code block:</p>
91+
<pre class='mermaid'>graph TD;
92+
A-->B;
93+
A-->C;
94+
B-->D;
95+
C-->D;</pre>
96+
9097
<h2 id="unsafe-html">Unsafe HTML</h2>
9198
<p>This is calculated by Javascript: <span id='demo'></div></p>
9299
<script>document.getElementById("demo").innerHTML = 5 + 6;</script>

spec/fixtures/docroot/Style Guide/All.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This needs a footnote explanation[^1]
4545

4646
1. Ordered
4747
2. List
48-
1. with
48+
1. with
4949
2. nesting
5050

5151
- [x] This is done
@@ -86,6 +86,16 @@ graph TD;
8686
C-->D;
8787
</div>
8888

89+
Or using the `mermaid` code block:
90+
91+
```mermaid
92+
graph TD;
93+
A-->B;
94+
A-->C;
95+
B-->D;
96+
C-->D;
97+
```
98+
8999
## Unsafe HTML
90100

91101
This is calculated by Javascript: <span id='demo'></div>
@@ -95,5 +105,3 @@ This is calculated by Javascript: <span id='demo'></div>
95105

96106
[^1]: Footnote explanation
97107
[Reference link]: http://example.com
98-
99-

spec/fixtures/docroot/Style Guide/Mermaid Diagrams.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ graph TD;
88
B-->D;
99
C-->D;
1010
</div>
11+
12+
Or using the `mermaid` code block:
13+
14+
```mermaid
15+
graph TD;
16+
A-->B;
17+
A-->C;
18+
B-->D;
19+
C-->D;
20+
```

spec/madness/markdown_document_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
before do
4848
config.renderer = 'pandoc'
4949
config.highlighter = true
50+
config.mermaid = true
5051
end
5152

5253
# In CI we have a different pandoc version, so we allow

0 commit comments

Comments
 (0)