From 1e8d9768f86125348822141496c74cad5398b2f9 Mon Sep 17 00:00:00 2001 From: jeej3000 <171628180+jeej3000@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:36:32 +0100 Subject: [PATCH] Fix TypeError for codehilite options in fenced_code attr_list Options such as `linenums` or `hl_lines` set in the attr_list of a fenced block are parsed into bool/None/list values. Without `codehilite` they were written into the `` tag as attributes and crashed serialization. Omit non-string values from the output, like `use_pygments` already is. Fixes #1713. Co-authored-by: jeej1k <351634202+jeej1k@users.noreply.github.com> --- docs/changelog.md | 3 +++ markdown/extensions/fenced_code.py | 4 +++- tests/test_syntax/extensions/test_fenced_code.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 043bfa2d7..612e7f796 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,9 @@ See the [Contributing Guide](contributing.md) for details. ### Fixed +* The `fenced_code` extension no longer raises `TypeError` for `codehilite` options such as + `linenums` set in the `attr_list` of a code block when `codehilite` is not enabled; + these options are now omitted from the output (#1713). * The `toc` extension no longer raises `TypeError` for a heading whose `id` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{id}`) writes it: like a heading with no `id` at diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 944b4e42b..aca379633 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -159,9 +159,11 @@ def run(self, lines: list[str]) -> list[str]: # Only assign key/value pairs to code element if `attr_list` extension is enabled, key/value # pairs were defined on the code block, and the `use_pygments` key was not set to `True`. The # `use_pygments` key could be either set to `False` or not defined. It is omitted from output. + # Options parsed into non-string values (`linenums`, `hl_lines`, ...) only apply to + # `codehilite` and are omitted as well. kv_pairs = ''.join( f' {sanitize_name(k)}="{_escape_attrib_html(v)}"' - for k, v in config.items() if k != 'use_pygments' + for k, v in config.items() if k != 'use_pygments' and isinstance(v, str) ) code = self._escape(m.group('code')) code = f'{code}' diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 43a691591..92dcc6941 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -323,6 +323,16 @@ def testFencedCodeAttrNameSanitizedNoCodehiliteWithAttrList(self): extensions=['fenced_code', 'attr_list'] ) + def testFencedCodeBoolOptionsOmittedNoCodehiliteWithAttrList(self): + # Options only used by `codehilite` must not crash serialization (see #1713). + for attrs in ('linenums', 'linenums=true', 'guess_lang noclasses=False', 'hl_lines="1"'): + with self.subTest(attrs=attrs): + self.assertMarkdownRenders( + f'``` {{ .python {attrs} foo=bar }}\n# Some python code\n```', + '
# Some python code\n
', + extensions=['fenced_code', 'attr_list'] + ) + def testFencedLanguagePygmentsDisabledInAttrNoCodehiliteWithAttrList(self): self.assertMarkdownRenders( self.dedent(