using Markdig;
using Markdig.Extensions.Yaml;
using Markdig.Syntax;
namespace Elternbeirat.Web.Services;
///
/// Reads simple key: value 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.
///
public static class FrontMatter
{
///
/// Returns the value of the given front-matter key, or null 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.
///
public static string? Read(string key, MarkdownDocument document, string source)
{
var block = document.Descendants().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;
}
}