How to get YAML front matter? #295

Closed
opened 2026-01-29 14:33:03 +00:00 by claunia · 1 comment
Owner

Originally created by @lloydjatkinson on GitHub (May 26, 2019).

Given a markdown file, how do we get access to the YAML front matter?

---
title: "First"
date: 2019-05-26T14:30:08.8803451+00:00
tags: ["test1", "test2"]
---

# Hello World

For example I have:

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

When I parse a markdown file I'd like to be able to also get the YAML front matter, probably as a dictionary (or even a plain string that we could use with a YAML parser...).

var someString = Markdown.ToHtml(await streamReader.ReadToEndAsync(), pipeline);
// <h1>Hello World</h1>
// But how do we programmatically access the YAML?
Originally created by @lloydjatkinson on GitHub (May 26, 2019). Given a markdown file, how do we get access to the YAML front matter? ``` --- title: "First" date: 2019-05-26T14:30:08.8803451+00:00 tags: ["test1", "test2"] --- # Hello World ``` For example I have: var pipeline = new MarkdownPipelineBuilder() .UseAdvancedExtensions() .UseYamlFrontMatter() .Build(); When I parse a markdown file I'd like to be able to also get the YAML front matter, probably as a dictionary (or even a plain string that we could use with a YAML parser...). var someString = Markdown.ToHtml(await streamReader.ReadToEndAsync(), pipeline); // <h1>Hello World</h1> // But how do we programmatically access the YAML?
claunia added the question label 2026-01-29 14:33:03 +00:00
Author
Owner

@MihaZupan commented on GitHub (May 26, 2019):

The Yaml extension does not do any yaml parsing, only detects its start and end. It does emit a YamlFrontMatterBlock that you can then access in the AST.

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

StringWriter writer = new StringWriter();
var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);

MarkdownDocument document = Markdown.Parse(markdown, pipeline);
var yamlBlock = document.Descendants<YamlFrontMatterBlock>().FirstOrDefault();

if (yamlBlock != null)
{
    string yaml = markdown.Substring(yamlBlock.Span.Start, yamlBlock.Span.Length);

    Console.WriteLine(yaml);
}

renderer.Render(document);
writer.Flush();
string html = writer.ToString();
@MihaZupan commented on GitHub (May 26, 2019): The Yaml extension does not do any yaml parsing, only detects its start and end. It does emit a `YamlFrontMatterBlock` that you can then access in the AST. ```c# var pipeline = new MarkdownPipelineBuilder() .UseYamlFrontMatter() .Build(); StringWriter writer = new StringWriter(); var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); MarkdownDocument document = Markdown.Parse(markdown, pipeline); var yamlBlock = document.Descendants<YamlFrontMatterBlock>().FirstOrDefault(); if (yamlBlock != null) { string yaml = markdown.Substring(yamlBlock.Span.Start, yamlBlock.Span.Length); Console.WriteLine(yaml); } renderer.Render(document); writer.Flush(); string html = writer.ToString(); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#295