Return (front_matter_block, body) for a Jekyll markdown file. The front matter block includes the leading and trailing `---` lines and any whitespace immediately following the closing delimiter so that the returned body starts cleanly. If no front matter is present, an empty front m
(text: str)
| 29 | |
| 30 | |
| 31 | def split_front_matter(text: str) -> tuple[str, str]: |
| 32 | """Return (front_matter_block, body) for a Jekyll markdown file. |
| 33 | |
| 34 | The front matter block includes the leading and trailing `---` lines and |
| 35 | any whitespace immediately following the closing delimiter so that the |
| 36 | returned body starts cleanly. If no front matter is present, an empty |
| 37 | front matter block is returned and the entire text is treated as body. |
| 38 | """ |
| 39 | |
| 40 | lines = text.splitlines(keepends=True) |
| 41 | if not lines or lines[0].rstrip("\r\n") != FRONT_MATTER_DELIMITER: |
| 42 | return "", text |
| 43 | |
| 44 | closing_index = None |
| 45 | for index in range(1, len(lines)): |
| 46 | if lines[index].rstrip("\r\n") == FRONT_MATTER_DELIMITER: |
| 47 | closing_index = index |
| 48 | break |
| 49 | |
| 50 | if closing_index is None: |
| 51 | # Malformed front matter (no closing delimiter). Bail out and treat |
| 52 | # the file as bodyless rather than silently corrupting it. |
| 53 | raise ValueError( |
| 54 | f"Malformed Jekyll front matter in index.md: no closing '{FRONT_MATTER_DELIMITER}' " |
| 55 | "delimiter found." |
| 56 | ) |
| 57 | |
| 58 | front_matter = "".join(lines[: closing_index + 1]) |
| 59 | |
| 60 | # Skip a single blank line directly after the closing delimiter so the |
| 61 | # body we return doesn't start with a stray newline. Anything beyond that |
| 62 | # is body content. |
| 63 | body_start = closing_index + 1 |
| 64 | if body_start < len(lines) and lines[body_start].strip() == "": |
| 65 | body_start += 1 |
| 66 | |
| 67 | body = "".join(lines[body_start:]) |
| 68 | return front_matter, body |
| 69 | |
| 70 | |
| 71 | def build_index(front_matter: str, readme_text: str) -> str: |