Skip to content

Commit 2981b17

Browse files
NicolasPllr1DouweM
andauthored
Fix Gateway links in code blocks (#3444)
Co-authored-by: Douwe Maan <douwe@pydantic.dev>
1 parent be2929c commit 2981b17

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

docs/.hooks/main.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) -> str:
1818
"""Called on each file after it is read and before it is converted to HTML."""
19-
relative_path_root = (DOCS_ROOT / page.file.src_uri).parent
20-
markdown = inject_snippets(markdown, relative_path_root)
19+
relative_path = DOCS_ROOT / page.file.src_uri
20+
markdown = inject_snippets(markdown, relative_path.parent)
2121
markdown = replace_uv_python_run(markdown)
2222
markdown = render_examples(markdown)
2323
markdown = render_video(markdown)
24-
markdown = create_gateway_toggle(markdown, relative_path_root)
24+
markdown = create_gateway_toggle(markdown, relative_path)
2525
return markdown
2626

2727

@@ -120,13 +120,13 @@ def sub_cf_video(m: re.Match[str]) -> str:
120120
"""
121121

122122

123-
def create_gateway_toggle(markdown: str, relative_path_root: Path) -> str:
123+
def create_gateway_toggle(markdown: str, relative_path: Path) -> str:
124124
"""Transform Python code blocks with Agent() calls to show both Pydantic AI and Gateway versions."""
125125
# Pattern matches Python code blocks with or without attributes, and optional annotation definitions after
126126
# Annotation definitions are numbered list items like "1. Some text" that follow the code block
127127
return re.sub(
128128
r'```py(?:thon)?(?: *\{?([^}\n]*)\}?)?\n(.*?)\n```(\n\n(?:\d+\..+?\n)+?\n)?',
129-
lambda m: transform_gateway_code_block(m, relative_path_root),
129+
lambda m: transform_gateway_code_block(m, relative_path),
130130
markdown,
131131
flags=re.MULTILINE | re.DOTALL,
132132
)
@@ -136,7 +136,7 @@ def create_gateway_toggle(markdown: str, relative_path_root: Path) -> str:
136136
GATEWAY_MODELS = ('anthropic', 'openai', 'openai-responses', 'openai-chat', 'bedrock', 'google-vertex', 'groq')
137137

138138

139-
def transform_gateway_code_block(m: re.Match[str], relative_path_root: Path) -> str:
139+
def transform_gateway_code_block(m: re.Match[str], relative_path: Path) -> str:
140140
"""Transform a single code block to show both versions if it contains Agent() calls."""
141141
attrs = m.group(1) or ''
142142
code = m.group(2)
@@ -186,9 +186,9 @@ def replace_agent_model(match: re.Match[str]) -> str:
186186

187187
# Build attributes string
188188
docs_path = DOCS_ROOT / 'gateway'
189-
relative_path = docs_path.relative_to(relative_path_root, walk_up=True)
190-
link = f"<a href='{relative_path}' style='float: right;'>Learn about Gateway</a>"
191189

190+
relative_path_to_gateway = docs_path.relative_to(relative_path, walk_up=True)
191+
link = f"<a href='{relative_path_to_gateway}' style='float: right;'>Learn about Gateway</a>"
192192
attrs_str = f' {{{attrs}}}' if attrs else ''
193193

194194
if 'title="' in attrs:

0 commit comments

Comments
 (0)