Skip to content

Commit 1087ff3

Browse files
committed
fix(docs): mkdocs build with Pygments 2.20 and pymdown-extensions
Register a small MkDocs hook that coerces BlockHtmlFormatter filename=None to an empty string before Pygments html.escape, fixing strict builds when mkdocstrings highlights signatures without a title. Made-with: Cursor
1 parent 2f795f9 commit 1087ff3

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ nav:
3232
- Getting started: index.md
3333
- API Reference: api.md
3434

35+
hooks:
36+
- mkdocs_pygments_hook.py
37+
3538
plugins:
3639
- search
3740
- mkdocstrings:

mkdocs_pygments_hook.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2025 MOSTLY AI
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""MkDocs hook: Pygments 2.20+ HtmlFormatter expects filename to be a string.
16+
17+
pymdown-extensions can pass filename=None when highlighting block code with no
18+
title (e.g. mkdocstrings signatures), which breaks html.escape().
19+
"""
20+
21+
22+
def on_config(config, **kwargs):
23+
import pymdownx.highlight as ph
24+
25+
if not getattr(ph, "pygments", False):
26+
return config
27+
28+
_orig = ph.BlockHtmlFormatter.__init__
29+
30+
def __init__(self, **options):
31+
if options.get("filename") is None:
32+
options = {**options, "filename": ""}
33+
_orig(self, **options)
34+
35+
ph.BlockHtmlFormatter.__init__ = __init__ # type: ignore[method-assign]
36+
return config

0 commit comments

Comments
 (0)