pub fn extract_json_frontmatter(content: &str) -> Result<&str, Error>
Expand description
Extracts JSON frontmatter from the content by detecting balanced curly braces ({}
).
This function attempts to locate a valid JSON object starting with {
and checks for balanced
curly braces to identify the end of the frontmatter. If the JSON object is found, it returns
the frontmatter as a string slice. A maximum nesting depth is enforced to prevent deeply nested
JSON from causing stack overflow.
§Arguments
content
- The full content string that may contain JSON frontmatter.
§Returns
A Result
containing the extracted JSON frontmatter string slice.
If no valid JSON frontmatter is detected, it returns an Error
.
§Errors
Error::InvalidJson
: If the content does not start with{
or contains unbalanced braces.Error::JsonDepthLimitExceeded
: If the JSON object exceeds the allowed nesting depth.
§Example
use frontmatter_gen::extractor::extract_json_frontmatter;
let content = "{ \"title\": \"Example\" }\nContent";
let frontmatter = extract_json_frontmatter(content).unwrap();
assert_eq!(frontmatter, "{ \"title\": \"Example\" }");