Extract frontmatter as a dict from YAML frontmatter block.
(content: str)
| 1077 | |
| 1078 | @staticmethod |
| 1079 | def _extract_frontmatter(content: str) -> dict[str, Any]: |
| 1080 | """Extract frontmatter as a dict from YAML frontmatter block.""" |
| 1081 | |
| 1082 | if not content.startswith("---"): |
| 1083 | return {} |
| 1084 | |
| 1085 | lines = content.splitlines(keepends=True) |
| 1086 | if not lines or lines[0].rstrip("\r\n") != "---": |
| 1087 | return {} |
| 1088 | |
| 1089 | frontmatter_end = -1 |
| 1090 | for i, line in enumerate(lines[1:], start=1): |
| 1091 | if line.rstrip("\r\n") == "---": |
| 1092 | frontmatter_end = i |
| 1093 | break |
| 1094 | |
| 1095 | if frontmatter_end == -1: |
| 1096 | return {} |
| 1097 | |
| 1098 | frontmatter_text = "".join(lines[1:frontmatter_end]) |
| 1099 | try: |
| 1100 | fm = yaml.safe_load(frontmatter_text) or {} |
| 1101 | except yaml.YAMLError: |
| 1102 | return {} |
| 1103 | |
| 1104 | return fm if isinstance(fm, dict) else {} |
| 1105 | |
| 1106 | @staticmethod |
| 1107 | def _split_frontmatter(content: str) -> tuple[str, str]: |