from __future__ import annotations import re GLFM_CHUNK_LIMIT = 65000 _FENCED_BLOCK_RE = re.compile(r"^```") def sanitize_glfm(text: str) -> str: return text.replace("\x00", "") def chunk_glfm_markdown(text: str, limit: int = GLFM_CHUNK_LIMIT) -> list[str]: if len(text) <= limit: return [text] chunks: list[str] = [] in_fenced_block = False current = "" for line in text.split("\n"): if _FENCED_BLOCK_RE.match(line): in_fenced_block = not in_fenced_block candidate = current + line + "\n" if len(candidate) > limit and not in_fenced_block: chunks.append(current.rstrip()) current = line + "\n" else: current = candidate if current.strip(): chunks.append(current.rstrip()) return chunks if chunks else [text] def safe_glfm_body(text: str, max_length: int = 950000) -> str: if len(text) > max_length: return text[: max_length - 3] + "..." return text