Add html_baseurl to llms.txt docs links (#12)

This commit is contained in:
Jared Dillard
2025-05-18 13:37:34 -07:00
committed by GitHub
parent 77999f0923
commit cffac5615d
3 changed files with 61 additions and 1 deletions
+1
View File
@@ -5,6 +5,7 @@ Changelog
----- -----
- Refactor LLMSFullManager with clearer class structure - Refactor LLMSFullManager with clearer class structure
- Add ``html_baseurl`` to **llms.txt** docs links
0.2.1 0.2.1
----- -----
+7 -1
View File
@@ -89,9 +89,15 @@ class FileWriter:
f.write(f"> {description}\n\n") f.write(f"> {description}\n\n")
f.write("## Docs\n\n") f.write("## Docs\n\n")
# Get base URL from config
base_url = self.config.get("html_baseurl", "/")
# Ensure base_url ends with a trailing slash
if not base_url.endswith("/"):
base_url += "/"
for docname in page_order: for docname in page_order:
title = page_titles.get(docname, docname) title = page_titles.get(docname, docname)
f.write(f"- [{title}](/{docname}.html)\n") f.write(f"- [{title}]({base_url}{docname}.html)\n")
logger.info(f"sphinx-llms-txt: created {output_path}") logger.info(f"sphinx-llms-txt: created {output_path}")
return True return True
+53
View File
@@ -279,5 +279,58 @@ def test_write_verbose_info_to_file(tmp_path):
# Check that the content contains expected information # Check that the content contains expected information
assert "## Docs" in content assert "## Docs" in content
# Without html_baseurl, URLs should start with /
assert "- [Home Page](/index.html)" in content assert "- [Home Page](/index.html)" in content
assert "- [About Us](/about.html)" in content assert "- [About Us](/about.html)" in content
def test_write_verbose_info_with_baseurl(tmp_path):
"""Test writing verbose info to a file with html_baseurl set."""
# Create a build directory
build_dir = tmp_path / "build"
build_dir.mkdir()
# Create writer with configuration including html_baseurl
config = {
"llms_txt_file": True,
"llms_txt_full_max_size": 1000,
"llms_txt_filename": "llms.txt",
"html_baseurl": "https://example.com",
}
writer = FileWriter(config, str(build_dir))
# Create page titles
page_titles = {
"index": "Home Page",
"about": "About Us",
}
# Create a page order
page_order = ["index", "about"]
# Call the method to write verbose info to file
writer.write_verbose_info_to_file(page_order, page_titles)
# Check that the file was created
verbose_file = build_dir / "llms.txt"
assert verbose_file.exists()
# Read the file content
with open(verbose_file, "r", encoding="utf-8") as f:
content = f.read()
# Check that the content contains expected information with baseurl
assert "## Docs" in content
assert "- [Home Page](https://example.com/index.html)" in content
assert "- [About Us](https://example.com/about.html)" in content
# Test with baseurl without trailing slash
config["html_baseurl"] = "https://example.org"
writer = FileWriter(config, str(build_dir))
writer.write_verbose_info_to_file(page_order, page_titles)
with open(verbose_file, "r", encoding="utf-8") as f:
content = f.read()
assert "- [Home Page](https://example.org/index.html)" in content
assert "- [About Us](https://example.org/about.html)" in content