| 811 | } |
| 812 | |
| 813 | fn parse_markdown_frontmatter(content: &str) -> (String, String) { |
| 814 | // Check for frontmatter delimiter |
| 815 | if !content.starts_with("---\n") && !content.starts_with("---\r\n") { |
| 816 | return ("{}".to_string(), content.to_string()); |
| 817 | } |
| 818 | |
| 819 | // Find closing delimiter |
| 820 | let after_first = if content.starts_with("---\r\n") { 5 } else { 4 }; |
| 821 | let rest = &content[after_first..]; |
| 822 | |
| 823 | let (close_idx, skip_len) = if let Some(idx) = rest.find("\n---\n") { |
| 824 | (idx, 5) // "\n---\n" is 5 bytes |
| 825 | } else if let Some(idx) = rest.find("\n---\r\n") { |
| 826 | (idx, 6) // "\n---\r\n" is 6 bytes |
| 827 | } else if rest.ends_with("\n---") { |
| 828 | (rest.len() - 3, 3) |
| 829 | } else { |
| 830 | // No closing delimiter — treat entire content as body |
| 831 | return ("{}".to_string(), content.to_string()); |
| 832 | }; |
| 833 | |
| 834 | let yaml_str = &rest[..close_idx]; |
| 835 | let body_start = after_first + close_idx + skip_len; |
| 836 | let body = if body_start <= content.len() { |
| 837 | content[body_start..].to_string() |
| 838 | } else { |
| 839 | String::new() |
| 840 | }; |
| 841 | |
| 842 | // Convert YAML frontmatter to JSON |
| 843 | let frontmatter_json = yaml_frontmatter_to_json(yaml_str); |
| 844 | |
| 845 | (frontmatter_json, body) |
| 846 | } |
| 847 | |
| 848 | /// Convert simple YAML frontmatter to a JSON string. |
| 849 | /// |