Skip to content

Commit

Permalink
Ensure codeblocks support substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Dec 3, 2024
1 parent 18b4840 commit ccbcfcd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
15 changes: 13 additions & 2 deletions docs/source/markup/substitutions.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
---
title: Substitutions
sub:
'frontmatter_key': "Front Matter Value"
frontmatter_key: "Front Matter Value"
version: 7.17.0
---

Here are some variable substitutions:

| Value | Source |
| ------------------- | ------------ |
| {{frontmatter_key}} | Front Matter |
| {{frontmatter_key}} | Front Matter |

Substitutions should work in code blocks too.

```{code} sh
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512 <1>
tar -xzf elasticsearch-{{version}}-linux-x86_64.tar.gz
cd elasticsearch-{{version}}/ <2>
```
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Elastic.Markdown.Myst.Substitution;
using Elastic.Markdown.Slices;
using Elastic.Markdown.Slices.Directives;
using Markdig;
Expand Down Expand Up @@ -255,6 +256,9 @@ void RenderLeaf(LeafBlock p)
renderer.EnableHtmlForInline = false;
foreach (var oo in p.Inline ?? [])
{

if (oo is SubstitutionLeaf sl)
renderer.Write(sl.Replacement);
if (oo is LiteralInline li)
renderer.Write(li);
if (oo is LineBreakInline)
Expand Down
7 changes: 1 addition & 6 deletions tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ public override async Task InitializeAsync()
{
await base.InitializeAsync();
Block = Document
.Where(block => block is ParagraphBlock)
.Cast<ParagraphBlock>()
.FirstOrDefault()?
.Inline?
.Where(block => block is TDirective)
.Cast<TDirective>()
.Descendants<TDirective>()
.FirstOrDefault();
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,26 @@ public void GeneratesAttributesInHtml() =>
"""{{valid-key}}"""
);
}

public class SubstitutionInCodeBlockTest(ITestOutputHelper output) : LeafTest<SubstitutionLeaf>(output,
"""
---
sub:
version: "7.17.0"
---
```{code} sh
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-{{version}}-linux-x86_64.tar.gz.sha512 <1>
tar -xzf elasticsearch-{{version}}-linux-x86_64.tar.gz
cd elasticsearch-{{version}}/ <2>
```
"""
)
{

[Fact]
public void GeneratesAttributesInHtml() =>
Html.Should().Contain("7.17.0");
}

0 comments on commit ccbcfcd

Please sign in to comment.