8000 Build `llms.txt` in documentation by petyosi · Pull Request #11405 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (1)


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Build llms.txt
  • Loading branch information
petyosi committed Feb 6, 2025
commit 9299ae580c1844fc0c56ac98715d590f0f2ddde1
35 changes: 35 additions & 0 deletions docs/plugins/build_llms_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations as _annotations

import os

from bs4 import BeautifulSoup
from markdownify import MarkdownConverter
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page


def on_config(config: MkDocsConfig):
os.makedirs(config.site_dir, exist_ok=True)
llms_path = os.path.join(config.site_dir, 'llms.txt')
with open(llms_path, 'w') as f:
f.write('')


def on_page_content(html: str, page: Page, config: MkDocsConfig, files: Files) -> str:
soup = BeautifulSoup(html, 'html.parser')

# Clean up presentational and UI elements
for element in soup.find_all(
['a', 'div', 'img'], attrs={'class': ['headerlink', 'tabbed-labels', 'twemoji lg middle', 'twemoji']}
):
element.decompose()

# The API reference generates HTML tables with line numbers, this strips the line numbers cell and goes back to a code block
for extra in soup.find_all('table', attrs={'class': 'highlighttable'}):
extra.replace_with(BeautifulSoup(f'<pre>{extra.find("code").get_text()}</pre>', 'html.parser'))

with open(os.path.join(config.site_dir, 'llms.txt'), 'a', encoding='utf-8') as f:
f.write(MarkdownConverter().convert_soup(soup)) # type: ignore[reportUnknownMemberType]

return html
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ watch:

hooks:
- 'docs/plugins/main.py'
- 'docs/plugins/build_llms_txt.py'

plugins:
- social
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ dev = [
]
docs = [
'autoflake',
"bs4>=0.0.2",
"markdownify>=0.14.1",
'mkdocs',
'mkdocs-exclude',
'mkdocs-material[imaging]',
Expand Down
56 changes: 56 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0