Skip to content
Closed
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 @@ -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
Expand Down
4 changes: 3 additions & 1 deletion markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<pre{id_attr}{class_attr}><code{lang_attr}{kv_pairs}>{code}</code></pre>'
Expand Down
10 changes: 10 additions & 0 deletions tests/test_syntax/extensions/test_fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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```',
'<pre><code class="language-python" foo="bar"># Some python code\n</code></pre>',
extensions=['fenced_code', 'attr_list']
)

def testFencedLanguagePygmentsDisabledInAttrNoCodehiliteWithAttrList(self):
self.assertMarkdownRenders(
self.dedent(
Expand Down