Skip to content

Commit 2fd4675

Browse files
committed
ensure we reapply mermaid on htmx navigation too
1 parent acae71d commit 2fd4675

File tree

47 files changed

+669
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+669
-355
lines changed

docs/syntax/diagrams.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can embedd diagrams using [mermaid.js](https://mermaid.js.org)
88

99
::::{tab-item} Output
1010

11-
:::{mermaid}
11+
```mermaid
1212
flowchart LR
1313
A[Jupyter Notebook] --> C
1414
B[MyST Markdown] --> C
@@ -19,13 +19,13 @@ D --> G[Word]
1919
D --> H[React]
2020
D --> I[HTML]
2121
D <--> J[JATS]
22-
:::
22+
```
2323

2424
::::
2525

2626
::::{tab-item} Markdown
27-
```markdown
28-
:::{mermaid}
27+
````markdown
28+
```mermaid
2929
flowchart LR
3030
A[Jupyter Notebook] --> C
3131
B[MyST Markdown] --> C
@@ -36,8 +36,8 @@ D --> G[Word]
3636
D --> H[React]
3737
D --> I[HTML]
3838
D <--> J[JATS]
39-
:::
4039
```
40+
````
4141
::::
4242

4343
:::::

src/Elastic.Markdown/Assets/main.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@ import mermaid from 'mermaid/dist/mermaid.esm.min.mjs'
1515

1616
const {getOS} = new UAParser()
1717

18-
document.addEventListener('htmx:load', function () {
19-
initTocNav()
20-
initHighlight()
21-
initCopyButton()
22-
initTabs()
23-
initNav()
24-
initSmoothScroll()
25-
openDetailsWithAnchor()
26-
initDismissibleBanner()
18+
var mermaidInitialize = function () {
2719
mermaid.initialize({
28-
startOnLoad: true, theme: 'base',
20+
startOnLoad: false, theme: 'base',
2921
themeVariables: {
3022
fontFamily: 'inherit',
3123
altFontFamily: 'inherit',
@@ -40,7 +32,22 @@ document.addEventListener('htmx:load', function () {
4032
"journey": {
4133
"taskFontFamily": "inherit"
4234
}
43-
})
35+
});
36+
mermaid.run({
37+
nodes: document.querySelectorAll('.mermaid'),
38+
});
39+
}
40+
41+
document.addEventListener('htmx:load', function () {
42+
initTocNav()
43+
initHighlight()
44+
initCopyButton()
45+
initTabs()
46+
initNav()
47+
initSmoothScroll()
48+
openDetailsWithAnchor()
49+
initDismissibleBanner()
50+
mermaidInitialize()
4451
})
4552

4653
// Don't remove style tags because they are used by the elastic global nav.
@@ -70,9 +77,9 @@ document.addEventListener('htmx:beforeRequest', function (event) {
7077
}
7178
}
7279
})
73-
7480
document.body.addEventListener('htmx:oobBeforeSwap', function (event) {
7581
// This is needed to scroll to the top of the page when the content is swapped
82+
7683
if (
7784
event.target.id === 'main-container' ||
7885
event.target.id === 'markdown-content' ||

src/Elastic.Markdown/Assets/markdown/mermaid.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.mermaid {
2-
@apply bg-grey-10 border-grey-20 rounded-xs border-1 font-mono;
2+
@apply border-grey-10 rounded-md border-2 font-sans mt-4;
33

44
font-size: 0.875em !important;
55

@@ -84,4 +84,8 @@
8484
stroke: var(--color-yellow-50) !important;
8585
font-size: 1em !important;
8686
}
87+
88+
rect.background {
89+
display: none;
90+
}
8791
}

src/Elastic.Markdown/Myst/Directives/AppliesToDirective.cs renamed to src/Elastic.Markdown/Myst/CodeBlocks/AppliesToCodeBlock.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
using Elastic.Markdown.Myst.CodeBlocks;
65
using Elastic.Markdown.Myst.FrontMatter;
76
using Markdig.Parsers;
87

9-
namespace Elastic.Markdown.Myst.Directives;
8+
namespace Elastic.Markdown.Myst.CodeBlocks;
109

11-
12-
public class AppliesToDirective(BlockParser parser, ParserContext context)
10+
public class AppliesToCodeBlock(BlockParser parser, ParserContext context)
1311
: EnhancedCodeBlock(parser, context), IApplicableToElement
1412
{
1513
public ApplicableTo? AppliesTo { get; set; }

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private static int CountIndentation(StringSlice slice)
115115

116116
protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
117117
{
118-
if (block is AppliesToDirective appliesToDirective)
118+
if (block is AppliesToCodeBlock appliesToDirective)
119119
{
120120
RenderAppliesToHtml(renderer, appliesToDirective);
121121
return;
@@ -229,9 +229,9 @@ protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
229229
}
230230

231231
[SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")]
232-
private static void RenderAppliesToHtml(HtmlRenderer renderer, AppliesToDirective appliesToDirective)
232+
private static void RenderAppliesToHtml(HtmlRenderer renderer, AppliesToCodeBlock appliesToCodeBlock)
233233
{
234-
var appliesTo = appliesToDirective.AppliesTo;
234+
var appliesTo = appliesToCodeBlock.AppliesTo;
235235
var slice = ApplicableToDirective.Create(appliesTo);
236236
if (appliesTo is null || appliesTo == FrontMatter.ApplicableTo.All)
237237
return;

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected override EnhancedCodeBlock CreateFencedBlock(BlockProcessor processor)
3434

3535
var lineSpan = processor.Line.AsSpan();
3636
var codeBlock = lineSpan.IndexOf("{applies_to}") > -1
37-
? new AppliesToDirective(this, context)
37+
? new AppliesToCodeBlock(this, context)
3838
{
3939
IndentCount = processor.Indent
4040
}
@@ -110,31 +110,31 @@ public override bool Close(BlockProcessor processor, Block block)
110110
if (lines.Lines is null)
111111
return base.Close(processor, block);
112112

113-
if (codeBlock is not AppliesToDirective appliesToDirective)
113+
if (codeBlock is not AppliesToCodeBlock appliesToDirective)
114114
ProcessCodeBlock(lines, language, codeBlock, context);
115115
else
116116
ProcessAppliesToDirective(appliesToDirective, lines);
117117

118118
return base.Close(processor, block);
119119
}
120120

121-
private static void ProcessAppliesToDirective(AppliesToDirective appliesToDirective, StringLineGroup lines)
121+
private static void ProcessAppliesToDirective(AppliesToCodeBlock appliesToCodeBlock, StringLineGroup lines)
122122
{
123123
var yaml = lines.ToSlice().AsSpan().ToString();
124124

125125
try
126126
{
127127
var applicableTo = YamlSerialization.Deserialize<ApplicableTo>(yaml);
128-
appliesToDirective.AppliesTo = applicableTo;
129-
if (appliesToDirective.AppliesTo.Warnings is null)
128+
appliesToCodeBlock.AppliesTo = applicableTo;
129+
if (appliesToCodeBlock.AppliesTo.Warnings is null)
130130
return;
131-
foreach (var warning in appliesToDirective.AppliesTo.Warnings)
132-
appliesToDirective.EmitWarning(warning);
131+
foreach (var warning in appliesToCodeBlock.AppliesTo.Warnings)
132+
appliesToCodeBlock.EmitWarning(warning);
133133
applicableTo.Warnings = null;
134134
}
135135
catch (Exception e)
136136
{
137-
appliesToDirective.EmitError($"Unable to parse applies_to directive: {yaml}", e);
137+
appliesToCodeBlock.EmitError($"Unable to parse applies_to directive: {yaml}", e);
138138
}
139139
}
140140

src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
9191
if (info.IndexOf("{figure-md}") > 0)
9292
return new FigureBlock(this, context);
9393

94-
// this is currently listed as unsupported
95-
// leaving the parsing in until we are confident we don't want this
96-
// for dev-docs
97-
if (info.IndexOf("{mermaid}") > 0)
98-
return new MermaidBlock(this, context);
99-
10094
if (info.IndexOf("{include}") > 0)
10195
return new IncludeBlock(this, context);
10296

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo
2929

3030
switch (directiveBlock)
3131
{
32-
case MermaidBlock mermaidBlock:
33-
WriteMermaid(renderer, mermaidBlock);
34-
return;
3532
case FigureBlock imageBlock:
3633
WriteFigure(renderer, imageBlock);
3734
return;
@@ -206,12 +203,6 @@ private static void WriteTabItem(HtmlRenderer renderer, TabItemBlock block)
206203
RenderRazorSlice(slice, renderer);
207204
}
208205

209-
private static void WriteMermaid(HtmlRenderer renderer, MermaidBlock block)
210-
{
211-
var slice = Mermaid.Create(new MermaidViewModel { DirectiveBlock = block });
212-
RenderRazorSliceRawContent(slice, renderer, block);
213-
}
214-
215206
private static void WriteLiteralIncludeBlock(HtmlRenderer renderer, IncludeBlock block)
216207
{
217208
if (!block.Found || block.IncludePath is null)
@@ -289,71 +280,7 @@ private static void WriteSettingsBlock(HtmlRenderer renderer, SettingsBlock bloc
289280
}
290281

291282
[SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")]
292-
private static void RenderRazorSlice<T>(RazorSlice<T> slice, HtmlRenderer renderer) => slice.RenderAsync(renderer.Writer).GetAwaiter().GetResult();
293-
294-
[SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")]
295-
private static void RenderRazorSliceRawContent<T>(RazorSlice<T> slice, HtmlRenderer renderer, DirectiveBlock obj)
296-
where T : DirectiveViewModel
297-
{
298-
var html = slice.RenderAsync().GetAwaiter().GetResult();
299-
var blocks = html.Split("[CONTENT]", 2, StringSplitOptions.RemoveEmptyEntries);
300-
_ = renderer.Write(blocks[0]);
301-
foreach (var o in obj)
302-
Render(o);
303-
304-
_ = renderer.Write(blocks[1]);
305-
306-
void RenderLeaf(LeafBlock p)
307-
{
308-
_ = renderer.WriteLeafRawLines(p, true, false, false);
309-
renderer.EnableHtmlForInline = false;
310-
foreach (var oo in p.Inline ?? [])
311-
{
312-
if (oo is SubstitutionLeaf sl)
313-
_ = renderer.Write(sl.Replacement);
314-
else if (oo is LiteralInline li)
315-
renderer.Write(li);
316-
else if (oo is LineBreakInline)
317-
_ = renderer.WriteLine();
318-
else if (oo is Role r)
319-
{
320-
_ = renderer.Write(new string(r.DelimiterChar, r.DelimiterCount));
321-
renderer.WriteChildren(r);
322-
}
323-
324-
else
325-
_ = renderer.Write($"(LeafBlock: {oo.GetType().Name}");
326-
}
327-
328-
renderer.EnableHtmlForInline = true;
329-
}
283+
private static void RenderRazorSlice<T>(RazorSlice<T> slice, HtmlRenderer renderer) =>
284+
slice.RenderAsync(renderer.Writer).GetAwaiter().GetResult();
330285

331-
void RenderListBlock(ListBlock l)
332-
{
333-
foreach (var bb in l)
334-
{
335-
if (bb is LeafBlock lbi)
336-
RenderLeaf(lbi);
337-
else if (bb is ListItemBlock ll)
338-
{
339-
_ = renderer.Write(ll.TriviaBefore);
340-
_ = renderer.Write("-");
341-
foreach (var lll in ll)
342-
Render(lll);
343-
}
344-
else
345-
_ = renderer.Write($"(ListBlock: {l.GetType().Name}");
346-
}
347-
}
348-
349-
void Render(Block o)
350-
{
351-
if (o is LeafBlock p)
352-
RenderLeaf(p);
353-
else if (o is ListBlock l)
354-
RenderListBlock(l);
355-
else
356-
_ = renderer.Write($"(Block: {o.GetType().Name}");
357-
}
358-
}
359286
}

src/Elastic.Markdown/Myst/Directives/MermaidBlock.cs

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

src/Elastic.Markdown/Slices/Directives/Code.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
@if (Model.Language == "mermaid")
33
{
44
<pre class="mermaid">
5-
[CONTENT]
5+
%%{init: {'theme': 'base', 'themeVariables': { 'fontSize': '1em'}}}%%
6+
@(Model.RenderBlock())
67
</pre>
78
}
89
else {

0 commit comments

Comments
 (0)