Skip to content

Commit a950dcb

Browse files
committed
docs: tighten llms_txt failure modes and name embedded snippet sources
Unresolvable relative .md links and pages that do not start with an H1 now fail the build instead of producing broken or malformed renditions. Embedded .py snippets gain a leading comment naming the source file, so the rendition still points at the file on disk.
1 parent 7c7aedc commit a950dcb

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

docs/hooks/llms_txt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def include(match: re.Match[str]) -> str:
9090
content = resolved_path.read_text(encoding="utf-8").rstrip("\n")
9191
except OSError as exc:
9292
raise PluginError(f"llms_txt: cannot read snippet {path!r} in {page.file.src_uri}") from exc
93+
# Keep a pointer to the embedded file so readers can find it on disk.
94+
if path.endswith(".py"):
95+
content = f"# {path}\n{content}"
9396
if indent:
9497
content = "\n".join(indent + line if line else line for line in content.split("\n"))
9598
return content
@@ -107,7 +110,7 @@ def rewrite(match: re.Match[str]) -> str:
107110
return match.group(0)
108111
linked = files.get_file_from_path(posixpath.normpath(posixpath.join(src_dir, target)))
109112
if linked is None:
110-
return match.group(0)
113+
raise PluginError(f"llms_txt: cannot resolve link target {target!r} in {page.file.src_uri}")
111114
# Pages without a markdown rendition (the api.md stub) link to their HTML instead.
112115
url = _md_uri(linked) if linked.src_uri in _state.rendition_uris else linked.url
113116
return f"{opening}{site_url}{url}{anchor or ''}{title or ''}{closing}"
@@ -157,7 +160,9 @@ def on_post_build(config: MkDocsConfig) -> None:
157160
tail = f": {description}" if description else ""
158161
index.append(f"- [{page.title}]({site_url}{_md_uri(page.file)}){tail}")
159162

160-
body = re.sub(r"\A\s*# .+\n", "", markdown)
163+
body, h1_found = re.subn(r"\A\s*# .+\n", "", markdown)
164+
if not h1_found:
165+
raise PluginError(f"llms_txt: page {page.file.src_uri} does not start with an H1")
161166
full += [f"# {page.title}", "", f"Source: {page.canonical_url}", "", body.strip(), ""]
162167
index.append("")
163168

0 commit comments

Comments
 (0)