extractYAMLValueAtPath extracts the scalar value at a JSON path from raw YAML frontmatter. Supports top-level paths ("/field") and two-level nested paths ("/parent/child"). Deeper paths return an empty string.
(yamlContent, jsonPath string)
| 475 | // Supports top-level paths ("/field") and two-level nested paths ("/parent/child"). |
| 476 | // Deeper paths return an empty string. |
| 477 | func extractYAMLValueAtPath(yamlContent, jsonPath string) string { |
| 478 | if yamlContent == "" || jsonPath == "" { |
| 479 | return "" |
| 480 | } |
| 481 | segments := strings.SplitN(strings.TrimPrefix(jsonPath, "/"), "/", 3) |
| 482 | switch len(segments) { |
| 483 | case 1: |
| 484 | return extractTopLevelYAMLValue(yamlContent, segments[0]) |
| 485 | case 2: |
| 486 | return extractNestedYAMLValue(yamlContent, segments[0], segments[1]) |
| 487 | default: |
| 488 | return "" |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // extractTopLevelYAMLValue extracts the scalar value of a top-level key from raw YAML. |
| 493 | // Uses horizontal-only whitespace between the colon and value to avoid matching multi-line blocks. |