MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / yaml_frontmatter_to_json

Function yaml_frontmatter_to_json

atomic-repository/src/repository/vault.rs:1165–1203  ·  view source on GitHub ↗

Convert simple YAML frontmatter to a JSON string. Handles common YAML patterns: - `key: value` -> `{"key": "value"}` - `key: 123` -> `{"key": 123}` - `key: true/false` -> `{"key": true/false}` - `key: [a, b, c]` -> `{"key": ["a", "b", "c"]}` - `key: {json}` -> `{"key": {json}}` This is intentionally simple — we don't need a full YAML parser. The frontmatter is our own output (from materializatio

(yaml: &str)

Source from the content-addressed store, hash-verified

1163/// The frontmatter is our own output (from materialization), so we
1164/// know the format.
1165fn yaml_frontmatter_to_json(yaml: &str) -> String {
1166 let mut map = serde_json::Map::new();
1167
1168 for line in yaml.lines() {
1169 let line = line.trim();
1170 if line.is_empty() || line.starts_with('#') {
1171 continue;
1172 }
1173
1174 // Split on first ': '
1175 let Some(colon_idx) = line.find(": ") else {
1176 // Handle `key:` with no value (treated as empty string)
1177 if let Some(key) = line.strip_suffix(':') {
1178 map.insert(
1179 key.trim().to_string(),
1180 serde_json::Value::String(String::new()),
1181 );
1182 }
1183 continue;
1184 };
1185
1186 let key = line[..colon_idx].trim().to_string();
1187 let value_str = line[colon_idx + 2..].trim();
1188
1189 // Skip system fields that we inject during materialization
1190 // (they'll be reconstructed, not stored in frontmatter_json)
1191 if matches!(
1192 key.as_str(),
1193 "entry_type" | "content_hash" | "created_at" | "updated_at"
1194 ) {
1195 continue;
1196 }
1197
1198 let value = parse_yaml_value(value_str);
1199 map.insert(key, value);
1200 }
1201
1202 serde_json::to_string(&map).unwrap_or_else(|_| "{}".to_string())
1203}
1204
1205/// Parse a YAML value string into a serde_json::Value.
1206fn parse_yaml_value(s: &str) -> serde_json::Value {

Callers 1

Calls 4

parse_yaml_valueFunction · 0.85
linesMethod · 0.45
is_emptyMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected