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)
| 858 | /// The frontmatter is our own output (from materialization), so we |
| 859 | /// know the format. |
| 860 | fn yaml_frontmatter_to_json(yaml: &str) -> String { |
| 861 | let mut map = serde_json::Map::new(); |
| 862 | |
| 863 | for line in yaml.lines() { |
| 864 | let line = line.trim(); |
| 865 | if line.is_empty() || line.starts_with('#') { |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | // Split on first ': ' |
| 870 | let Some(colon_idx) = line.find(": ") else { |
| 871 | // Handle `key:` with no value (treated as empty string) |
| 872 | if let Some(key) = line.strip_suffix(':') { |
| 873 | map.insert( |
| 874 | key.trim().to_string(), |
| 875 | serde_json::Value::String(String::new()), |
| 876 | ); |
| 877 | } |
| 878 | continue; |
| 879 | }; |
| 880 | |
| 881 | let key = line[..colon_idx].trim().to_string(); |
| 882 | let value_str = line[colon_idx + 2..].trim(); |
| 883 | |
| 884 | // Skip system fields that we inject during materialization |
| 885 | // (they'll be reconstructed, not stored in frontmatter_json) |
| 886 | if key == "entry_type" || key == "content_hash" { |
| 887 | continue; |
| 888 | } |
| 889 | |
| 890 | let value = parse_yaml_value(value_str); |
| 891 | map.insert(key, value); |
| 892 | } |
| 893 | |
| 894 | serde_json::to_string(&map).unwrap_or_else(|_| "{}".to_string()) |
| 895 | } |
| 896 | |
| 897 | /// Parse a YAML value string into a serde_json::Value. |
| 898 | fn parse_yaml_value(s: &str) -> serde_json::Value { |
no test coverage detected