namespace MeetingAssistant.MeetingNotes; internal readonly record struct MarkdownDocument( bool HasFrontmatter, string Frontmatter, string Body); internal static class MarkdownDocumentParser { public static MarkdownDocument SplitOptional(string content) { using var reader = new StringReader(content); if (reader.ReadLine() != "---") { return new MarkdownDocument(false, "", content); } var yaml = new List(); string? line; while ((line = reader.ReadLine()) is not null) { if (line == "---") { return new MarkdownDocument( true, string.Join(Environment.NewLine, yaml), RemoveBodySeparator(reader.ReadToEnd())); } yaml.Add(line); } return new MarkdownDocument(false, "", content); } public static MarkdownDocument SplitRequired( string content, string missingFrontmatterMessage, string unclosedFrontmatterMessage) { using var reader = new StringReader(content); if (reader.ReadLine() != "---") { throw new InvalidDataException(missingFrontmatterMessage); } var yaml = new List(); string? line; while ((line = reader.ReadLine()) is not null) { if (line == "---") { return new MarkdownDocument( true, string.Join(Environment.NewLine, yaml), RemoveBodySeparator(reader.ReadToEnd())); } yaml.Add(line); } throw new InvalidDataException(unclosedFrontmatterMessage); } private static string RemoveBodySeparator(string body) { if (body.StartsWith("\r\n", StringComparison.Ordinal)) { return body[2..]; } return body.StartsWith('\n') ? body[1..] : body; } }