43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using Markdig;
|
|
using Markdig.Extensions.Yaml;
|
|
using Markdig.Syntax;
|
|
|
|
namespace Elternbeirat.Web.Services;
|
|
|
|
/// <summary>
|
|
/// Reads simple <c>key: value</c> pairs from a Markdown file's YAML front
|
|
/// matter. Kept deliberately minimal (one value per line, no nesting) — this is
|
|
/// enough for titles and dates. YamlDotNet will take over once we need
|
|
/// structured metadata. Front-matter keys stay German because the Markdown
|
|
/// files are edited by German-speaking editors.
|
|
/// </summary>
|
|
public static class FrontMatter
|
|
{
|
|
/// <summary>
|
|
/// Returns the value of the given front-matter key, or <c>null</c> if the
|
|
/// file has no front matter or the key is missing. The key match is
|
|
/// case-insensitive; surrounding quotes on the value are removed.
|
|
/// </summary>
|
|
public static string? Read(string key, MarkdownDocument document, string source)
|
|
{
|
|
var block = document.Descendants<YamlFrontMatterBlock>().FirstOrDefault();
|
|
if (block is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var prefix = key + ":";
|
|
var frontMatter = source.Substring(block.Span.Start, block.Span.Length);
|
|
foreach (var line in frontMatter.Split('\n'))
|
|
{
|
|
var trimmed = line.Trim();
|
|
if (trimmed.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return trimmed[prefix.Length..].Trim().Trim('"');
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|