From 9d5a3caaa9e95456303fc5223ab5e81bcd368445 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:22:58 +0530 Subject: [PATCH 1/4] Serialize boolean fenced code attributes without codehilite Boolean options (e.g. linenums) set via attr_list were passed to the escape helper as bool objects, raising TypeError when codehilite is not enabled. Serialize them the way the serializer does. Fixes #1713. Co-Authored-By: Claude Sonnet 5.5 --- docs/changelog.md | 3 ++ markdown/extensions/fenced_code.py | 17 ++++++++- .../extensions/test_fenced_code.py | 37 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index dca67c464..992ee5fae 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -35,6 +35,9 @@ See the [Contributing Guide](contributing.md) for details. to many references to the same footnote (#1726). * Release the parsed tree and source lines in `reset()` so a reused `Markdown` instance no longer retains the last document in memory (#1725). +* The `fenced_code` extension no longer raises `TypeError` for boolean options (e.g. + `linenums=true`) set with `attr_list` when `codehilite` is not enabled. The values + are now serialized like any other attribute (#1713). * The `toc` extension no longer raises `TypeError` for a heading whose `data-toc-label` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 944b4e42b..9ff1b28d7 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -27,7 +27,7 @@ from .codehilite import CodeHilite, CodeHiliteExtension, parse_hl_lines from .attr_list import get_attrs_and_remainder, sanitize_name, AttrListExtension from ..util import parseBoolValue -from ..serializers import _escape_attrib_html +from ..serializers import _escape_attrib_html, BOOLEAN_ATTRIBUTES import re from typing import TYPE_CHECKING, Any, Iterable @@ -160,7 +160,7 @@ def run(self, lines: list[str]) -> list[str]: # 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. kv_pairs = ''.join( - f' {sanitize_name(k)}="{_escape_attrib_html(v)}"' + self._format_attr(k, v) for k, v in config.items() if k != 'use_pygments' ) code = self._escape(m.group('code')) @@ -175,6 +175,19 @@ def run(self, lines: list[str]) -> list[str]: pieces.append(text[last:]) return "".join(pieces).split("\n") + def _format_attr(self, key: str, value: Any) -> str: + """ Serialize a key/value pair for the `code` element as the serializer would. """ + key = sanitize_name(key) + if value is None: + return f' {key}' + if value is True: + if self.md.output_format == 'html' and key in BOOLEAN_ATTRIBUTES['html']: + return f' {key}' + return f' {key}="{key}"' + if value is False: + return '' + return f' {key}="{_escape_attrib_html(value)}"' + def handle_attrs( self, attrs: Iterable[tuple[str, str | None]] ) -> tuple[str, list[str], dict[str, Any]]: diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 43a691591..095f8af54 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -455,6 +455,43 @@ def testFencedCodeMismatchedCurlyInAttrs(self): extensions=['fenced_code', 'attr_list'] ) + def testFencedCodeBooleanAttrsWithoutCodehilite(self): + # Boolean options are serialized like `attr_list` attributes when `codehilite` is not enabled (#1713). + self.assertMarkdownRenders( + self.dedent( + ''' + ```{.py linenums=true} + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                
+ ''' + ), + extensions=['fenced_code', 'attr_list'] + ) + + def testFencedCodeFalseBooleanAttrWithoutCodehilite(self): + self.assertMarkdownRenders( + self.dedent( + ''' + ```{.py linenums=false} + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                
+ ''' + ), + extensions=['fenced_code', 'attr_list'] + ) + def testMultipleFencedBlocks(self): self.assertMarkdownRenders( self.dedent( From 40b359532ef62fcd3bb231db7d14df8b8d64edf8 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:02:10 +0530 Subject: [PATCH 2/4] Add more fenced_code boolean attribute tests Co-Authored-By: Claude Sonnet 5.5 --- .../extensions/test_fenced_code.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 095f8af54..0c5a64bed 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -492,6 +492,63 @@ def testFencedCodeFalseBooleanAttrWithoutCodehilite(self): extensions=['fenced_code', 'attr_list'] ) + def testFencedCodeBooleanAttrsHtmlOutput(self): + # Only `linenums` is parsed as a boolean; other values stay strings. + self.assertMarkdownRenders( + self.dedent( + ''' + ```{.py hidden=true linenums=true} + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
+ ''' + ), + extensions=['fenced_code', 'attr_list'], + output_format='html' + ) + + def testFencedCodeValuelessAttrWithoutCodehilite(self): + # A valueless attribute is written with its name as the value, like `attr_list` does. + self.assertMarkdownRenders( + self.dedent( + ''' + ```{.py data-foo} + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                
+ ''' + ), + extensions=['fenced_code', 'attr_list'] + ) + + def testFencedCodeBooleanAndStringAttrsMixed(self): + self.assertMarkdownRenders( + self.dedent( + ''' + ```{.py linenums=true title="a & b" nowrap=false} + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                
+ ''' + ), + extensions=['fenced_code', 'attr_list'] + ) + def testMultipleFencedBlocks(self): self.assertMarkdownRenders( self.dedent( From b13e45a6ca39317a9af45155ebf55bf4ff7e3616 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:16:48 +0530 Subject: [PATCH 3/4] Fix fenced_code attr tests: drop odd nowrap case, cover html/xhtml output --- .../extensions/test_fenced_code.py | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 0c5a64bed..6cbbef693 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -535,20 +535,53 @@ def testFencedCodeBooleanAndStringAttrsMixed(self): self.assertMarkdownRenders( self.dedent( ''' - ```{.py linenums=true title="a & b" nowrap=false} + ```{.py linenums=true title="a & b" guess_lang=false} # Some python code ``` ''' ), self.dedent( ''' -
# Some python code
+                
# Some python code
                 
''' ), extensions=['fenced_code', 'attr_list'] ) + def testFencedCodeBooleanAttrsXhtmlOutput(self): + # Explicit `xhtml` output: the same as the default. + self.assertMarkdownRenders( + '```{.py hidden=true linenums=true}\n# Some python code\n```', + '
', + extensions=['fenced_code', 'attr_list'], + output_format='xhtml' + ) + + def testFencedCodeFalseBooleanAttrHtmlOutput(self): + self.assertMarkdownRenders( + '```{.py linenums=false}\n# Some python code\n```', + '
# Some python code\n
', + extensions=['fenced_code', 'attr_list'], + output_format='html' + ) + + def testFencedCodeValuelessAttrHtmlOutput(self): + self.assertMarkdownRenders( + '```{.py data-foo}\n# Some python code\n```', + '
# Some python code\n
', + extensions=['fenced_code', 'attr_list'], + output_format='html' + ) + + def testFencedCodeBooleanAndStringAttrsMixedHtmlOutput(self): + self.assertMarkdownRenders( + '```{.py linenums=true title="a & b" guess_lang=false}\n# Some python code\n```', + '
# Some python code\n
', + extensions=['fenced_code', 'attr_list'], + output_format='html' + ) + def testMultipleFencedBlocks(self): self.assertMarkdownRenders( self.dedent( From 48a3f601ce81c104fe6f7280bef85936aeb5883f Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:18:24 +0530 Subject: [PATCH 4/4] Move changelog entry to Unreleased Co-Authored-By: Claude Sonnet 5.5 --- docs/changelog.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 992ee5fae..51fb99359 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -24,6 +24,9 @@ See the [Contributing Guide](contributing.md) for details. reference definitions can no longer use an identifier starting with `^`, which is footnote definition syntax; without the extension the core behavior is unchanged. +* The `fenced_code` extension no longer raises `TypeError` for boolean options (e.g. + `linenums=true`) set with `attr_list` when `codehilite` is not enabled. The values + are now serialized like any other attribute (#1713). ## [3.11.4] - 2026-11-25 @@ -35,9 +38,6 @@ See the [Contributing Guide](contributing.md) for details. to many references to the same footnote (#1726). * Release the parsed tree and source lines in `reset()` so a reused `Markdown` instance no longer retains the last document in memory (#1725). -* The `fenced_code` extension no longer raises `TypeError` for boolean options (e.g. - `linenums=true`) set with `attr_list` when `codehilite` is not enabled. The values - are now serialized like any other attribute (#1713). * The `toc` extension no longer raises `TypeError` for a heading whose `data-toc-label` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a