Skip to content

Commit 04f596e

Browse files
authored
Extracted Markdown converter to a separate package (#3292)
1 parent 2967742 commit 04f596e

15 files changed

Lines changed: 811 additions & 770 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ lychee.toml
1919
lychee-report.md
2020
.phpunit.result.cache
2121
code_samples/_inline_php/
22+
/build/
23+
*.egg-info/

hooks.py

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,5 @@
1-
"""
2-
MkDocs hooks for Ibexa developer documentation.
1+
"""MkDocs hooks entry point — delegates to the installable llms_txt package."""
32

4-
- Keeps the llmstxt plugin's ``sections`` config in sync with the ``nav``
5-
defined in ``mkdocs.yml``.
6-
- Post-processes the Markdown generated by the llmstxt plugin.
3+
from llms_txt.hooks import on_config, on_page_content
74

8-
All content transformations live in ``llmstxt_preprocess.py``; this module is
9-
only MkDocs event glue.
10-
"""
11-
12-
from __future__ import annotations
13-
14-
import sys
15-
from pathlib import Path, PurePosixPath
16-
from typing import TYPE_CHECKING
17-
18-
_here = Path(__file__).parent
19-
if str(_here) not in sys.path:
20-
sys.path.insert(0, str(_here))
21-
22-
from llmstxt_preprocess import (
23-
absolutize_image_urls,
24-
editions_from_frontmatter,
25-
expand_macros,
26-
inject_page_metadata,
27-
renumber_ordered_lists,
28-
)
29-
from update_llmstxt_config import convert_nav_to_llmstxt_sections
30-
31-
if TYPE_CHECKING:
32-
from mkdocs.config.defaults import MkDocsConfig
33-
from mkdocs.structure.pages import Page
34-
35-
36-
def on_config(config: "MkDocsConfig") -> None:
37-
"""Populate llmstxt sections from nav before the build starts.
38-
39-
The llmstxt plugin reads ``config.sections`` in its ``on_files`` event,
40-
which fires after ``on_config``, so injecting here is the right place.
41-
"""
42-
nav = config.get("nav")
43-
if not nav:
44-
return
45-
46-
llmstxt = config["plugins"].get("llmstxt")
47-
if llmstxt is None:
48-
return
49-
50-
docs_dir = Path(config["docs_dir"])
51-
llmstxt.config.sections = convert_nav_to_llmstxt_sections(nav, docs_dir)
52-
53-
54-
def on_page_content(html: str, *, page: "Page", config: "MkDocsConfig", **kwargs) -> None:
55-
"""Reformat the Markdown content generated by the llmstxt plugin for this page.
56-
57-
Hooks run after plugins for every event, so at this point the llmstxt
58-
plugin has already generated Markdown and stored it in ``_md_pages``.
59-
We reformat here so the plugin writes the corrected content in its own
60-
``on_post_build`` (which also runs before ours, for the same reason).
61-
"""
62-
llmstxt = config["plugins"].get("llmstxt")
63-
if llmstxt is None:
64-
return
65-
66-
src_uri = page.file.src_uri
67-
page_info = llmstxt._md_pages.get(src_uri)
68-
if page_info is None:
69-
return
70-
71-
frontmatter = _read_frontmatter(page, config)
72-
editions = editions_from_frontmatter(frontmatter)
73-
description = expand_macros(str(frontmatter.get("description") or ""), config.get("extra") or {})
74-
if "[[=" in description:
75-
# Unresolved macros must not leak into the output.
76-
description = ""
77-
content = inject_page_metadata(page_info.content, description, editions)
78-
content = renumber_ordered_lists(content)
79-
# Same base URL and page directory the plugin uses for making link hrefs absolute.
80-
page_dir = PurePosixPath(page.file.dest_uri).parent.as_posix()
81-
content = absolutize_image_urls(content, llmstxt._base_url, page_dir)
82-
if content != page_info.content:
83-
llmstxt._md_pages[src_uri] = page_info._replace(content=content)
84-
85-
86-
def _read_frontmatter(page: "Page", config: "MkDocsConfig") -> dict:
87-
"""Read the page's YAML frontmatter from its source file."""
88-
src_path = Path(config["docs_dir"]) / page.file.src_path
89-
try:
90-
from mkdocs.utils import meta as mkdocs_meta
91-
with open(src_path, encoding="utf-8") as f:
92-
raw = f.read()
93-
_, frontmatter = mkdocs_meta.get_data(raw)
94-
return frontmatter
95-
except Exception:
96-
return {}
5+
__all__ = ["on_config", "on_page_content"]

0 commit comments

Comments
 (0)