parseFrontmatter function

({String content, Map<String, String> frontmatter}) parseFrontmatter(
  1. String content
)

Parse YAML frontmatter from skill content. Returns the body content without frontmatter.

Implementation

({String content, Map<String, String> frontmatter}) parseFrontmatter(
  String content,
) {
  final trimmed = content.trimLeft();
  if (!trimmed.startsWith('---')) {
    return (content: content, frontmatter: const {});
  }

  final endIndex = trimmed.indexOf('\n---', 3);
  if (endIndex == -1) {
    return (content: content, frontmatter: const {});
  }

  final frontmatterBlock = trimmed.substring(3, endIndex).trim();
  final body = trimmed.substring(endIndex + 4).trimLeft();

  final frontmatter = <String, String>{};
  for (final line in frontmatterBlock.split('\n')) {
    final colonIdx = line.indexOf(':');
    if (colonIdx > 0) {
      final key = line.substring(0, colonIdx).trim();
      final value = line.substring(colonIdx + 1).trim();
      frontmatter[key] = value;
    }
  }

  return (content: body, frontmatter: frontmatter);
}