From ccbcfcd34bbe25d397c05c705213ced34dcd3609 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 3 Dec 2024 17:29:18 +0100 Subject: [PATCH] Ensure codeblocks support substitutions --- docs/source/markup/substitutions.md | 15 ++++++++++-- .../Myst/Directives/DirectiveHtmlRenderer.cs | 4 ++++ .../Inline/InlneBaseTests.cs | 7 +----- .../Inline/SubstitutionTest.cs | 23 +++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/source/markup/substitutions.md b/docs/source/markup/substitutions.md index 1b2ffeb..c38718b 100644 --- a/docs/source/markup/substitutions.md +++ b/docs/source/markup/substitutions.md @@ -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 | \ No newline at end of file +| {{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> +``` diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 9d1bc3c..e851982 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -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; @@ -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) diff --git a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs index 62ac18a..876b21a 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs @@ -22,12 +22,7 @@ public override async Task InitializeAsync() { await base.InitializeAsync(); Block = Document - .Where(block => block is ParagraphBlock) - .Cast() - .FirstOrDefault()? - .Inline? - .Where(block => block is TDirective) - .Cast() + .Descendants() .FirstOrDefault(); } diff --git a/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs b/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs index bf4bb56..7ea4b3c 100644 --- a/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs +++ b/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs @@ -62,3 +62,26 @@ public void GeneratesAttributesInHtml() => """{{valid-key}}""" ); } + +public class SubstitutionInCodeBlockTest(ITestOutputHelper output) : LeafTest(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"); +} +