Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'))
Expand All @@ -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]]:
Expand Down
127 changes: 127 additions & 0 deletions tests/test_syntax/extensions/test_fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,133 @@ 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(
'''
<pre><code class="language-py" linenums="linenums"># Some python code
</code></pre>
'''
),
extensions=['fenced_code', 'attr_list']
)

def testFencedCodeFalseBooleanAttrWithoutCodehilite(self):
self.assertMarkdownRenders(
self.dedent(
'''
```{.py linenums=false}
# Some python code
```
'''
),
self.dedent(
'''
<pre><code class="language-py"># Some python code
</code></pre>
'''
),
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(
'''
<pre><code class="language-py" hidden="true" linenums="linenums"># Some python code
</code></pre>
'''
),
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(
'''
<pre><code class="language-py" data-foo="data-foo"># Some python code
</code></pre>
'''
),
extensions=['fenced_code', 'attr_list']
)

def testFencedCodeBooleanAndStringAttrsMixed(self):
self.assertMarkdownRenders(
self.dedent(
'''
```{.py linenums=true title="a & b" guess_lang=false}
# Some python code
```
'''
),
self.dedent(
'''
<pre><code class="language-py" linenums="linenums" title="a &amp; b"># Some python code
</code></pre>
'''
),
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```',
'<pre><code class="language-py" hidden="true" linenums="linenums"># Some python code\n</code></pre>',
extensions=['fenced_code', 'attr_list'],
output_format='xhtml'
)

def testFencedCodeFalseBooleanAttrHtmlOutput(self):
self.assertMarkdownRenders(
'```{.py linenums=false}\n# Some python code\n```',
'<pre><code class="language-py"># Some python code\n</code></pre>',
extensions=['fenced_code', 'attr_list'],
output_format='html'
)

def testFencedCodeValuelessAttrHtmlOutput(self):
self.assertMarkdownRenders(
'```{.py data-foo}\n# Some python code\n```',
'<pre><code class="language-py" data-foo="data-foo"># Some python code\n</code></pre>',
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```',
'<pre><code class="language-py" linenums="linenums" title="a &amp; b"># Some python code\n</code></pre>',
extensions=['fenced_code', 'attr_list'],
output_format='html'
)

def testMultipleFencedBlocks(self):
self.assertMarkdownRenders(
self.dedent(
Expand Down
Loading