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

Ensure codeblocks support substitutions #92

Merged
merged 1 commit into from
Dec 3, 2024
Merged
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
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");
}

Loading