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 @@ -35,6 +35,9 @@ See the [Contributing Guide](contributing.md) for details.
and closing backticks did not match (#1668).
* Escape the language name in the `codehilite` extension's non-Pygments output to
maintain consistency with Pygments output.
* Ignore a `pygments_formatter` key in a fenced code block's attribute list so
the formatter is always the one set in the `codehilite` configuration. A
formatter such as `text` would otherwise leave the code unescaped.
* Preserve attributes without a value in the `attr_list` extension exactly as
provided, rather than rendering them as `key="key"`, so the same source
renders consistently under both the `html` and `xhtml` output formats
Expand Down
3 changes: 3 additions & 0 deletions markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def run(self, lines: list[str]) -> list[str]:
# is enabled, so we call it to highlight the code
if self.codehilite_conf and self.codehilite_conf['use_pygments'] and config.get('use_pygments', True):
local_config = self.codehilite_conf.copy()
# Only the `codehilite` configuration may set the formatter, as some
# formatters (such as `text`) do not escape the code.
config.pop('pygments_formatter', None)
local_config.update(config)
# Combine classes with `cssclass`. Ensure `cssclass` is at end
# as Pygments appends a suffix under certain circumstances.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_syntax/extensions/test_fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,29 @@ def testInvalidCustomPygmentsFormatter(self):
'fenced_code'
]
)

def testFencedLanguageAttrPygmentsFormatterIgnored(self):
# The formatter can only be set in the `codehilite` configuration.
if has_pygments:
expected = self.dedent(
'''
<div class="codehilite"><pre><span></span><code>&lt;b&gt;Some HTML&lt;/b&gt;
</code></pre></div>
'''
)
else:
expected = (
'<pre class="codehilite"><code class="language-text">&lt;b&gt;Some HTML&lt;/b&gt;\n'
'</code></pre>'
)
self.assertMarkdownRenders(
self.dedent(
'''
``` { .text pygments_formatter='text' }
<b>Some HTML</b>
```
'''
),
expected,
extensions=['codehilite', 'fenced_code']
)
Loading