-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownExtensions.cs
39 lines (33 loc) · 1.11 KB
/
MarkdownExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using C__Yaml_Parser;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Markdig;
using Markdig.Extensions.Yaml;
using Markdig.Syntax;
namespace C__Yaml_Parser
{
public static class MarkdownExtensions
{
private static readonly IDeserializer YamlDeserializer =
new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();
// Pipeline
private static readonly MarkdownPipeline Pipeline =
new MarkdownPipelineBuilder()
.UseYamlFrontMatter()
.Build();
public static T GetFrontMatter<T>(this string markdown)
{
var document = Markdown.Parse(markdown, Pipeline);
var block = document
.Descendants<YamlFrontMatterBlock>()
.FirstOrDefault();
if (block == null)
return default;
var yaml = block.Lines.ToString();
return YamlDeserializer.Deserialize<T>(yaml);
}
}
}