From ec7d843dfe4e5588138cf393ce2e8f3adadadac6 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Mon, 21 Jul 2025 22:50:35 +0200 Subject: [PATCH 1/4] Set interface ITitledBlock on blocks that have a Title property and use that in LlmDirectiveRenderer --- .../Directives/Admonition/AdmonitionBlock.cs | 4 +--- .../Myst/Directives/DirectiveBlock.cs | 5 +++++ .../Myst/Directives/Stepper/StepperBlock.cs | 2 +- .../Myst/Directives/Tabs/TabSetBlock.cs | 2 +- .../Renderers/LlmMarkdown/LlmBlockRenderers.cs | 16 ++-------------- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs b/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs index 04c50ec60..d213bb361 100644 --- a/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs @@ -8,7 +8,7 @@ namespace Elastic.Markdown.Myst.Directives.Admonition; public class DropdownBlock(DirectiveBlockParser parser, ParserContext context) : AdmonitionBlock(parser, "dropdown", context); -public class AdmonitionBlock : DirectiveBlock +public class AdmonitionBlock : DirectiveBlock, ITitledBlock { public AdmonitionBlock(DirectiveBlockParser parser, string admonition, ParserContext context) : base(parser, context) { @@ -45,5 +45,3 @@ public override void FinalizeAndValidate(ParserContext context) Title = Title.ReplaceSubstitutions(context); } } - - diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs index f1e588d4e..325c46b12 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs @@ -13,6 +13,11 @@ namespace Elastic.Markdown.Myst.Directives; +public interface ITitledBlock +{ + string Title { get; } +} + public interface IBlockExtension : IBlock { BuildContext Build { get; } diff --git a/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs b/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs index a0ad9e509..d0909bb46 100644 --- a/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs @@ -16,7 +16,7 @@ public override void FinalizeAndValidate(ParserContext context) } } -public class StepBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context) +public class StepBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context), ITitledBlock { public override string Directive => "step"; public string Title { get; private set; } = string.Empty; diff --git a/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs b/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs index c491d4dff..b08cf3d37 100644 --- a/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs @@ -33,7 +33,7 @@ public int FindIndex() } public class TabItemBlock(DirectiveBlockParser parser, ParserContext context) - : DirectiveBlock(parser, context) + : DirectiveBlock(parser, context), ITitledBlock { public override string Directive => "tab-item"; diff --git a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs index 2fca4e4b7..df5f9c2de 100644 --- a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs +++ b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs @@ -5,10 +5,8 @@ using Elastic.Markdown.Helpers; using Elastic.Markdown.Myst.CodeBlocks; using Elastic.Markdown.Myst.Directives; -using Elastic.Markdown.Myst.Directives.Admonition; using Elastic.Markdown.Myst.Directives.Image; using Elastic.Markdown.Myst.Directives.Include; -using Elastic.Markdown.Myst.Directives.Tabs; using Markdig.Extensions.DefinitionLists; using Markdig.Extensions.Tables; using Markdig.Extensions.Yaml; @@ -382,18 +380,8 @@ protected override void Write(LlmMarkdownRenderer renderer, DirectiveBlock obj) renderer.Writer.Write("<"); renderer.Writer.Write(obj.Directive); - switch (obj) - { - case DropdownBlock dropdown: - renderer.Writer.Write($" title=\"{dropdown.Title}\""); - break; - case TabItemBlock tabItem: - renderer.Writer.Write($" title=\"{tabItem.Title}\""); - break; - case AdmonitionBlock admonition when obj.Directive is "admonition": - renderer.Writer.Write($" title=\"{admonition.Title}\""); - break; - } + if (obj is ITitledBlock titledBlock) + renderer.Writer.Write($" title=\"{titledBlock.Title}\""); renderer.WriteLine(">"); renderer.EnsureLine(); From c7f0dbcd86f46406761329b57cd5fea2a2676c32 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Mon, 21 Jul 2025 23:18:20 +0200 Subject: [PATCH 2/4] Skip rendering the title for note, tip, warning and important admonitions --- .../LlmMarkdown/LlmBlockRenderers.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs index df5f9c2de..82bf39b0f 100644 --- a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs +++ b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs @@ -5,6 +5,7 @@ using Elastic.Markdown.Helpers; using Elastic.Markdown.Myst.CodeBlocks; using Elastic.Markdown.Myst.Directives; +using Elastic.Markdown.Myst.Directives.Admonition; using Elastic.Markdown.Myst.Directives.Image; using Elastic.Markdown.Myst.Directives.Include; using Markdig.Extensions.DefinitionLists; @@ -380,8 +381,25 @@ protected override void Write(LlmMarkdownRenderer renderer, DirectiveBlock obj) renderer.Writer.Write("<"); renderer.Writer.Write(obj.Directive); - if (obj is ITitledBlock titledBlock) - renderer.Writer.Write($" title=\"{titledBlock.Title}\""); + switch (obj) + { + case AdmonitionBlock when obj.Directive + is "note" or "tip" or "warning" or "important": + // skip for these directives + // otherwise it will render as + break; + case ITitledBlock titledBlock: + renderer.Writer.Write($" title=\"{titledBlock.Title}\""); + break; + } + + // if (obj is ITitledBlock titledBlock + // && obj.Directive != "note" + // && obj.Directive != "warning" + // && obj.Directive != "tip" + // && obj.Directive != "important" + // ) + // renderer.Writer.Write($" title=\"{titledBlock.Title}\""); renderer.WriteLine(">"); renderer.EnsureLine(); From 91ceb096f603b9fb912d0f7065cebe9856a90e77 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Mon, 21 Jul 2025 23:19:19 +0200 Subject: [PATCH 3/4] Remove commented code --- .../Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs index 82bf39b0f..03835c9fd 100644 --- a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs +++ b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs @@ -393,14 +393,6 @@ protected override void Write(LlmMarkdownRenderer renderer, DirectiveBlock obj) break; } - // if (obj is ITitledBlock titledBlock - // && obj.Directive != "note" - // && obj.Directive != "warning" - // && obj.Directive != "tip" - // && obj.Directive != "important" - // ) - // renderer.Writer.Write($" title=\"{titledBlock.Title}\""); - renderer.WriteLine(">"); renderer.EnsureLine(); From 72250cb924fd9ef25a979c325aeeefecb9214c49 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Mon, 21 Jul 2025 23:21:34 +0200 Subject: [PATCH 4/4] Rename to IBlockTitle --- .../Myst/Directives/Admonition/AdmonitionBlock.cs | 2 +- src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs | 2 +- src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs | 2 +- src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs | 2 +- .../Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs b/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs index d213bb361..ba676590f 100644 --- a/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs @@ -8,7 +8,7 @@ namespace Elastic.Markdown.Myst.Directives.Admonition; public class DropdownBlock(DirectiveBlockParser parser, ParserContext context) : AdmonitionBlock(parser, "dropdown", context); -public class AdmonitionBlock : DirectiveBlock, ITitledBlock +public class AdmonitionBlock : DirectiveBlock, IBlockTitle { public AdmonitionBlock(DirectiveBlockParser parser, string admonition, ParserContext context) : base(parser, context) { diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs index 325c46b12..4cda54a91 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs @@ -13,7 +13,7 @@ namespace Elastic.Markdown.Myst.Directives; -public interface ITitledBlock +public interface IBlockTitle { string Title { get; } } diff --git a/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs b/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs index d0909bb46..4f8645ff0 100644 --- a/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs @@ -16,7 +16,7 @@ public override void FinalizeAndValidate(ParserContext context) } } -public class StepBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context), ITitledBlock +public class StepBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context), IBlockTitle { public override string Directive => "step"; public string Title { get; private set; } = string.Empty; diff --git a/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs b/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs index b08cf3d37..e91744028 100644 --- a/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/Tabs/TabSetBlock.cs @@ -33,7 +33,7 @@ public int FindIndex() } public class TabItemBlock(DirectiveBlockParser parser, ParserContext context) - : DirectiveBlock(parser, context), ITitledBlock + : DirectiveBlock(parser, context), IBlockTitle { public override string Directive => "tab-item"; diff --git a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs index 03835c9fd..516f33a81 100644 --- a/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs +++ b/src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs @@ -388,7 +388,7 @@ protected override void Write(LlmMarkdownRenderer renderer, DirectiveBlock obj) // skip for these directives // otherwise it will render as break; - case ITitledBlock titledBlock: + case IBlockTitle titledBlock: renderer.Writer.Write($" title=\"{titledBlock.Title}\""); break; }