diff --git a/ConsoleMarkdownRenderer.Example/data/example.md b/ConsoleMarkdownRenderer.Example/data/example.md index fda0e92..ad542d7 100644 --- a/ConsoleMarkdownRenderer.Example/data/example.md +++ b/ConsoleMarkdownRenderer.Example/data/example.md @@ -147,6 +147,14 @@ HTML : HyperText Markup Language. : The standard language for creating web pages. +## Figures + +Figures group an image (or other content) with an optional caption. + +^^^ A diagram illustrating the Markdown figure syntax. +![diagram](http://example.com/diagram.png) +^^^ + ## Thematic Break (Horizontal Rule) --- @@ -208,6 +216,7 @@ Some text after the block - [x] Footnotes - [x] Custom containers (admonitions) - [x] Abbreviations +- [x] Figures - [ ] One to always leave unchecked And here is the end \ No newline at end of file diff --git a/ConsoleMarkdownRenderer.Tests/RendererTests.cs b/ConsoleMarkdownRenderer.Tests/RendererTests.cs index 406841e..c32f14c 100644 --- a/ConsoleMarkdownRenderer.Tests/RendererTests.cs +++ b/ConsoleMarkdownRenderer.Tests/RendererTests.cs @@ -457,6 +457,15 @@ public void RendererTests_QuoteBlockTest(string text, Decoration decoration) AssertMarkdownYieldsFormat("quoteBlock", text, new Style(decoration: decoration), useCrazy: true); } + [TestMethod] + [DataRow(false)] + [DataRow(true)] + public void RendererTests_FigureCaptionTest(bool useCrazy) + { + // The caption inline content should carry the FigureCaption style (italic by default) + AssertMarkdownYieldsFormat("figure", "A descriptive caption for the figure.", new Style(decoration: Decoration.Italic), useCrazy); + } + [TestMethod] [DataRow(false)] [DataRow(true)] @@ -779,6 +788,7 @@ private static Dictionary Counts(string text) DefinitionList = c_crazyFormat, DefinitionTerm = c_crazyFormat, FencedCodeBlockInfo = c_crazyFormat, + FigureCaption = c_crazyFormat, Footnote = c_crazyFormat, FootnoteGroup = c_crazyFormat, FootnoteLink = c_crazyFormat, diff --git a/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.md b/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.md index b25b3fc..0330cbb 100644 --- a/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.md +++ b/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.md @@ -42,5 +42,8 @@ Paragraph with [test21] reference[^bracketfn] embedded. Inline ::tag [test29]:: container. Paragraph mentioning testAbbr abbreviation usage. - *[testAbbr]: [test30] expansion with [test31] brackets + +^^^ [test30] figure caption with **[test31] bold** content. +![[test32] alt](http://example.com/img.png) +^^^ diff --git a/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.txt b/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.txt index 285d1cb..080ff0c 100644 --- a/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.txt +++ b/ConsoleMarkdownRenderer.Tests/resources/bracketEscaping.txt @@ -49,6 +49,10 @@ │ └───────────────────────────────────────────┘ │ │ Inline tag [test29] container. │ │ Paragraph mentioning testAbbr ([test30] expansion with [test31] brackets) abbreviation usage. │ +│ ┌─────────────────────────────────────────────────────┐ │ +│ │ [test30] figure caption with [test31] bold content. │ │ +│ │ ![[test32] alt](http://example.com/img.png) │ │ +│ └─────────────────────────────────────────────────────┘ │ │ ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ │ │ ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── │ │ │ │ ┌───────────────────────────────────────────────────────┐ │ │ diff --git a/ConsoleMarkdownRenderer.Tests/resources/figure.md b/ConsoleMarkdownRenderer.Tests/resources/figure.md new file mode 100644 index 0000000..5eeb599 --- /dev/null +++ b/ConsoleMarkdownRenderer.Tests/resources/figure.md @@ -0,0 +1,3 @@ +^^^ A descriptive caption for the figure. +![sample](http://example.com/img.png) +^^^ diff --git a/ConsoleMarkdownRenderer.Tests/resources/figure.txt b/ConsoleMarkdownRenderer.Tests/resources/figure.txt new file mode 100644 index 0000000..37b9a11 --- /dev/null +++ b/ConsoleMarkdownRenderer.Tests/resources/figure.txt @@ -0,0 +1,6 @@ +┌───────────────────────────────────────────┐ +│ ┌───────────────────────────────────────┐ │ +│ │ A descriptive caption for the figure. │ │ +│ │ ![sample](http://example.com/img.png) │ │ +│ └───────────────────────────────────────┘ │ +└───────────────────────────────────────────┘ diff --git a/DisplayOptions.cs b/DisplayOptions.cs index 3cf0718..07278fa 100644 --- a/DisplayOptions.cs +++ b/DisplayOptions.cs @@ -73,6 +73,13 @@ public sealed class DisplayOptions /// public TextStyle FencedCodeBlockInfo { get; set; } = new(foreground: TextColor.Green, background: TextColor.Blue); + /// + /// Style applied to the inline content of a + /// (the optional caption line of a Markdig block). + /// Italic by default to visually distinguish it from the figure's body content. + /// + public TextStyle FigureCaption { get; set; } = new(decoration: TextDecoration.Italic); + // List of Styles to use for headers the first will be used for #, the second for ## and so on // If the document referenced more than the length of the list, the Style in header will be used. // By default the first entry is a FigletTextStyle, so top-level (#) headings render as @@ -169,6 +176,7 @@ public sealed class DisplayOptions DefinitionTerm = this.DefinitionTerm, Emojis = this.Emojis, FencedCodeBlockInfo = this.FencedCodeBlockInfo, + FigureCaption = this.FigureCaption, Footnote = this.Footnote, FootnoteGroup = this.FootnoteGroup, FootnoteLink = this.FootnoteLink, diff --git a/ObjectRenderers/ConsoleObjectRenderers.cs b/ObjectRenderers/ConsoleObjectRenderers.cs index 80435ed..15b0f17 100644 --- a/ObjectRenderers/ConsoleObjectRenderers.cs +++ b/ObjectRenderers/ConsoleObjectRenderers.cs @@ -1,6 +1,7 @@ using BoxOfYellow.ConsoleMarkdownRenderer.Styling; using Markdig.Extensions.CustomContainers; using Markdig.Extensions.DefinitionLists; +using Markdig.Extensions.Figures; using Markdig.Extensions.Footnotes; using Markdig.Extensions.TaskLists; using Markdig.Renderers; @@ -99,6 +100,26 @@ protected override void Write(ConsoleRenderer renderer, DefinitionTerm obj) .EndInline(); } + internal class ConsoleFigureRenderer : ConsoleObjectRenderer
+ { + protected override void Write(ConsoleRenderer renderer, Figure obj) + => renderer + .NewFrame(borderStyle: Style.Plain) + .WriteChildrenChain(obj) + .CompleteFrame(); + } + + internal class ConsoleFigureCaptionRenderer : ConsoleObjectRenderer + { + protected override void Write(ConsoleRenderer renderer, FigureCaption obj) + => renderer + .StartInline() + .AddInLine($"[{renderer.Options.FigureCaption.ToSpectreStyle().ToMarkup()}]") + .WriteLeafInline(obj) + .AddInLine("[/]") + .EndInline(); + } + internal class ConsoleFootnoteRenderer : ConsoleObjectRenderer { protected override void Write(ConsoleRenderer renderer, Footnote obj) diff --git a/ObjectRenderers/ConsoleRenderer.cs b/ObjectRenderers/ConsoleRenderer.cs index a497917..1b1e946 100644 --- a/ObjectRenderers/ConsoleRenderer.cs +++ b/ObjectRenderers/ConsoleRenderer.cs @@ -28,6 +28,8 @@ internal ConsoleRenderer(DisplayOptions options, bool omitAutolinkInlineRenderer new ConsoleDocumentRenderer(), new ConsoleEmphasisInlineRenderer(), new ConsoleEmojiInlineRenderer(), + new ConsoleFigureCaptionRenderer(), + new ConsoleFigureRenderer(), new ConsoleFootnoteGroupRenderer(), new ConsoleFootnoteLinkRenderer(), new ConsoleFootnoteRenderer(), diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 78cd342..2a6f9d8 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -18,19 +18,20 @@ - After Image - [#133](https://github.com/boxofyellow/ConsoleMarkdownRenderer/pull/133): Emit OSC 8 terminal hyperlinks from WriteLink via Spectre Markup -- [#141](https://github.com/boxofyellow/ConsoleMarkdownRenderer/issues/141): Render Markdig AbbreviationInline nodes with their expansion title +- [#142](https://github.com/boxofyellow/ConsoleMarkdownRenderer/pull/142): Render Markdig Figure and FigureCaption blocks - ```markdown - The HTML standard is maintained by the W3C. - - *[HTML]: HyperText Markup Language - *[W3C]: World Wide Web Consortium + ^^^ A descriptive caption for the figure. + ![sample](http://example.com/img.png) + ^^^ ``` - Rendered - The HTML (HyperText Markup Language) standard is maintained by the W3C (World Wide Web Consortium). + ^^^ A descriptive caption for the figure. + ![sample](http://example.com/img.png) + ^^^ - Before - Image + Image - After - Image + Image ### :wrench: Internal Improvements :wrench: - [#129](https://github.com/boxofyellow/ConsoleMarkdownRenderer/pull/129): Use ConfigureAwait(false) on awaits in published library code