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

RoundtripRenderer and YamlFrontMatterRenderer #579

Closed
mnaoumov opened this issue Dec 16, 2021 · 1 comment · Fixed by #638
Closed

RoundtripRenderer and YamlFrontMatterRenderer #579

mnaoumov opened this issue Dec 16, 2021 · 1 comment · Fixed by #638

Comments

@mnaoumov
Copy link
Contributor

If I have a markdown file my-awesome.md

---
title: Hello world
---

My awesome **markdown**

which I want to edit programatically

var pipeline
    = new MarkdownPipelineBuilder()
        .UseYamlFrontMatter()
        .EnableTrackTrivia()
        .Build();

var md = File.ReadAllText("my-awesome-md");
var document = Markdown.Parse(md, pipeline);

var sw = new StringWriter();
var rr = new RoundtripRenderer(sw);
rr.ObjectRenderers.Add(new YamlFrontMatterRenderer()); // this line doesn't change anything
rr.Write(document);
Console.WriteLine(sw.ToString());

it outputs

title: Hello world

My awesome **markdown**

with --- removed around yaml frontmatter

@mnaoumov
Copy link
Contributor Author

mnaoumov commented Dec 16, 2021

Got it fixed via

rr.ObjectRenderers.Insert(0, new MyYamlFrontMatterRenderer()); // it's important to insert it before CodeBlockRenderer
public class MyYamlFrontMatterRenderer : MarkdownObjectRenderer<RoundtripRenderer, YamlFrontMatterBlock>
{
    private readonly CodeBlockRenderer _codeBlockRenderer;

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants