Skip to content

Commit becc741

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 under docs_src/, so the rendition still points at the file on disk.
1 parent 6af8e07 commit becc741

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
@@ -96,6 +96,9 @@ def include(match: re.Match[str]) -> str:
9696
content = resolved_path.read_text(encoding="utf-8").rstrip("\n")
9797
except OSError as exc:
9898
raise PluginError(f"llms_txt: cannot read snippet {path!r} in {page.file.src_uri}") from exc
99+
# Keep a pointer to the embedded file so readers can find it on disk.
100+
if path.endswith(".py"):
101+
content = f"# {path}\n{content}"
99102
if indent:
100103
content = "\n".join(indent + line if line else line for line in content.split("\n"))
101104
return content
@@ -113,7 +116,7 @@ def rewrite(match: re.Match[str]) -> str:
113116
return match.group(0)
114117
linked = files.get_file_from_path(posixpath.normpath(posixpath.join(src_dir, target)))
115118
if linked is None:
116-
return match.group(0)
119+
raise PluginError(f"llms_txt: cannot resolve link target {target!r} in {page.file.src_uri}")
117120
# Pages without a markdown rendition (the api/ stubs) link to their HTML instead.
118121
url = _md_uri(linked) if linked.src_uri in _state.rendition_uris else linked.url
119122
return f"{opening}{site_url}{url}{anchor or ''}{title or ''}{closing}"
@@ -163,7 +166,9 @@ def on_post_build(config: MkDocsConfig) -> None:
163166
tail = f": {description}" if description else ""
164167
index.append(f"- [{page.title}]({site_url}{_md_uri(page.file)}){tail}")
165168

166-
body = re.sub(r"\A\s*# .+\n", "", markdown)
169+
body, h1_found = re.subn(r"\A\s*# .+\n", "", markdown)
170+
if not h1_found:
171+
raise PluginError(f"llms_txt: page {page.file.src_uri} does not start with an H1")
167172
full += [f"# {page.title}", "", f"Source: {page.canonical_url}", "", body.strip(), ""]
168173
index.append("")
169174

0 commit comments

Comments
 (0)