Skip to content

Commit

Permalink
Merge branch 'main' into uuid7
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki authored Jan 9, 2025
2 parents 685cec1 + 2155e44 commit f163cfc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
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.mkdir(config.site_dir)
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 @@ -308,4 +308,5 @@ plugins:
"get-started/traces.md": "concepts.md"
hooks:
- docs/plugins/main.py
- docs/plugins/build_llms_txt.py
- docs/plugins/build_index.py
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ docs = [
"griffe",
"bs4>=0.0.2",
"algoliasearch>=3,<4",
"markdownify>=0.14.1",
]

[tool.inline-snapshot]
Expand Down
21 changes: 18 additions & 3 deletions uv.lock

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

0 comments on commit f163cfc

Please sign in to comment.