Skip to content

Commit

Permalink
Add YamlFrontMatterRoundtripRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed May 17, 2022
1 parent 89a10ee commit 5e91b9b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/Markdig.Tests/RoundtripSpecs/TestYamlFrontMatterBlock.cs
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Markdig.Renderers.Roundtrip;
using Markdig.Syntax;
using NUnit.Framework;
using static Markdig.Tests.TestRoundtrip;

namespace Markdig.Tests.RoundtripSpecs
{
[TestFixture]
public class TestYamlFrontMatterBlock
{
[TestCase("---\nkey1: value1\nkey2: value2\n---\n\nContent\n")]
[TestCase("No front matter")]
[TestCase("Looks like front matter but actually is not\n---\nkey1: value1\nkey2: value2\n---")]
public void FrontMatterBlockIsPreserved(string value)
{
RoundTrip(value);
}
}
}
2 changes: 2 additions & 0 deletions src/Markdig.Tests/TestRoundtrip.cs
Expand Up @@ -16,10 +16,12 @@ internal static void RoundTrip(string markdown, string context = null)
{
var pipelineBuilder = new MarkdownPipelineBuilder();
pipelineBuilder.EnableTrackTrivia();
pipelineBuilder.UseYamlFrontMatter();
MarkdownPipeline pipeline = pipelineBuilder.Build();
MarkdownDocument markdownDocument = Markdown.Parse(markdown, pipeline);
var sw = new StringWriter();
var nr = new RoundtripRenderer(sw);
pipeline.Setup(nr);

nr.Write(markdownDocument);

Expand Down
8 changes: 4 additions & 4 deletions src/Markdig/Extensions/Yaml/YamlFrontMatterExtension.cs
@@ -1,10 +1,9 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Parsers;
using Markdig.Renderers;
using Markdig.Renderers.Html;

namespace Markdig.Extensions.Yaml
{
Expand All @@ -24,9 +23,10 @@ public void Setup(MarkdownPipelineBuilder pipeline)

public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (!renderer.ObjectRenderers.Contains<YamlFrontMatterRenderer>())
if (!renderer.ObjectRenderers.Contains<YamlFrontMatterHtmlRenderer>())
{
renderer.ObjectRenderers.InsertBefore<CodeBlockRenderer>(new YamlFrontMatterRenderer());
renderer.ObjectRenderers.InsertBefore<Renderers.Html.CodeBlockRenderer> (new YamlFrontMatterHtmlRenderer());
renderer.ObjectRenderers.InsertBefore<Renderers.Roundtrip.CodeBlockRenderer>(new YamlFrontMatterRoundtripRenderer());
}
}
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ namespace Markdig.Extensions.Yaml
/// Empty renderer for a <see cref="YamlFrontMatterBlock"/>
/// </summary>
/// <seealso cref="HtmlObjectRenderer{YamlFrontMatterBlock}" />
public class YamlFrontMatterRenderer : HtmlObjectRenderer<YamlFrontMatterBlock>
public class YamlFrontMatterHtmlRenderer : HtmlObjectRenderer<YamlFrontMatterBlock>
{
protected override void Write(HtmlRenderer renderer, YamlFrontMatterBlock obj)
{
Expand Down
27 changes: 27 additions & 0 deletions src/Markdig/Extensions/Yaml/YamlFrontMatterRoundtripRenderer.cs
@@ -0,0 +1,27 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
using Markdig.Renderers;
using Markdig.Renderers.Roundtrip;

namespace Markdig.Extensions.Yaml
{
public class YamlFrontMatterRoundtripRenderer : MarkdownObjectRenderer<RoundtripRenderer, YamlFrontMatterBlock>
{
private readonly CodeBlockRenderer _codeBlockRenderer;

public YamlFrontMatterRoundtripRenderer()
{
_codeBlockRenderer = new CodeBlockRenderer();
}

protected override void Write(RoundtripRenderer renderer, YamlFrontMatterBlock obj)
{
renderer.Writer.WriteLine("---");
_codeBlockRenderer.Write(renderer, obj);
renderer.Writer.WriteLine("---");
}
}
}

0 comments on commit 5e91b9b

Please sign in to comment.