Skip to content

Commit ccbcfcd

Browse files
committed
Ensure codeblocks support substitutions
1 parent 18b4840 commit ccbcfcd

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
---
22
title: Substitutions
33
sub:
4-
'frontmatter_key': "Front Matter Value"
4+
frontmatter_key: "Front Matter Value"
5+
version: 7.17.0
56
---
67

78
Here are some variable substitutions:
89

910
| Value | Source |
1011
| ------------------- | ------------ |
11-
| {{frontmatter_key}} | Front Matter |
12+
| {{frontmatter_key}} | Front Matter |
13+
14+
Substitutions should work in code blocks too.
15+
16+
```{code} sh
17+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz
18+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512
19+
shasum -a 512 -c elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512 <1>
20+
tar -xzf elasticsearch-{{version}}-linux-x86_64.tar.gz
21+
cd elasticsearch-{{version}}/ <2>
22+
```

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file is licensed under the BSD-Clause 2 license.
66
// See the license.txt file in the project root for more information.
77

8+
using Elastic.Markdown.Myst.Substitution;
89
using Elastic.Markdown.Slices;
910
using Elastic.Markdown.Slices.Directives;
1011
using Markdig;
@@ -255,6 +256,9 @@ void RenderLeaf(LeafBlock p)
255256
renderer.EnableHtmlForInline = false;
256257
foreach (var oo in p.Inline ?? [])
257258
{
259+
260+
if (oo is SubstitutionLeaf sl)
261+
renderer.Write(sl.Replacement);
258262
if (oo is LiteralInline li)
259263
renderer.Write(li);
260264
if (oo is LineBreakInline)

tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public override async Task InitializeAsync()
2222
{
2323
await base.InitializeAsync();
2424
Block = Document
25-
.Where(block => block is ParagraphBlock)
26-
.Cast<ParagraphBlock>()
27-
.FirstOrDefault()?
28-
.Inline?
29-
.Where(block => block is TDirective)
30-
.Cast<TDirective>()
25+
.Descendants<TDirective>()
3126
.FirstOrDefault();
3227
}
3328

tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,26 @@ public void GeneratesAttributesInHtml() =>
6262
"""{{valid-key}}"""
6363
);
6464
}
65+
66+
public class SubstitutionInCodeBlockTest(ITestOutputHelper output) : LeafTest<SubstitutionLeaf>(output,
67+
"""
68+
---
69+
sub:
70+
version: "7.17.0"
71+
---
72+
```{code} sh
73+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz
74+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512
75+
shasum -a 512 -c elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512 <1>
76+
tar -xzf elasticsearch-{{version}}-linux-x86_64.tar.gz
77+
cd elasticsearch-{{version}}/ <2>
78+
```
79+
"""
80+
)
81+
{
82+
83+
[Fact]
84+
public void GeneratesAttributesInHtml() =>
85+
Html.Should().Contain("7.17.0");
86+
}
87+

0 commit comments

Comments
 (0)