Skip to content

codehilite: ignores language classes on <code> elements in fenced code #1570

@DevTwilight

Description

@DevTwilight

Issue Explanation

When using the codehilite extension in Python-Markdown, no class is added to the <pre><code> element, even if a language is specified in a fenced code block. The code is wrapped in <div class="codehilite">, and the language prefix is ignored. As a result, styling or syntax highlighting applied to the <code> element cannot be used.

Test Script

(For reference)

import markdown

# 1. HTML template
HTML_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{title}</title>
</head>
<body>
{body}
</body>
</html>"""

# 2. Markdown code block
markdown_input = """\
# Python Code Example

```python
def greet(name):
    print(f"Hello, {name}!")

greet("World")
```
"""

# only fence code test
def md_to_html(md_text):
    html_body = markdown.markdown(md_text, extensions=["fenced_code"])
    return HTML_TEMPLATE.format(title="Markdown to HTML Test", body=html_body)

# only highligt test 
def md_to_html_highlight(md_text):
    html_body = markdown.markdown(md_text, extensions=["fenced_code", "codehilite"])
    return HTML_TEMPLATE.format(title="Markdown with Highlight", body=html_body)

def write_html(filename, html_content):
    with open(filename, "w", encoding="utf-8") as f:
        f.write(html_content)
        print(f"HTML file saved as {filename}")

def md_to_html_full(md_text):
    html_body = markdown.markdown(
        md_text,
        extensions=["fenced_code", "codehilite"],
extension_configs={
    "codehilite": {"guess_lang": True} # optional: auto-detect language
    }
    )
    return HTML_TEMPLATE.format(title="Markdown Full Conversion with Lang Prefix", body=html_body)

# Generate html
write_html("output_basic.html", md_to_html(markdown_input))
write_html("output_highlight.html", md_to_html_highlight(markdown_input))
write_html("output_full.html", md_to_html_full(markdown_input))

Test Results

1. Basic Test

Filename: output_basic.html

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown to HTML Test</title>
</head>
<body>
<h1>Python Code Example</h1>
<pre><code class="language-python">def greet(name):
    print(f&quot;Hello, {name}!&quot;)

greet(&quot;World&quot;)
</code></pre>
</body>
</html>

2. Highlight Test

Filename: output_highlight.html

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown with Highlight</title>
</head>
<body>
<h1>Python Code Example</h1>
<div class="codehilite"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Hello, </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>

<span class="n">greet</span><span class="p">(</span><span class="s2">&quot;World&quot;</span><span class="p">)</span>
</code></pre></div>
</body>
</html>

3. Full Test

Filename: output_full.html

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown Full Conversion with Lang Prefix</title>
</head>
<body>
<h1>Python Code Example</h1>
<div class="codehilite"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Hello, </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>

<span class="n">greet</span><span class="p">(</span><span class="s2">&quot;World&quot;</span><span class="p">)</span>
</code></pre></div>
</body>
</html>

Even if language is specified

No class is added to <pre><code> even if the fenced code block specifies a language and

"fenced_code": {"lang_prefix": "lang-"}, # adds lang- prefix to <code>

is set.

Suggestion

Allow codehilite to respect fenced_code’s lang prefix.

Why We Need This

Adding the language class to <code> is important because:

  • It allows using a single CSS file to apply different styles for different languages.
  • It enables syntax highlighters to detect the language automatically.
  • It supports documents containing multiple languages, ensuring each code block can be highlighted appropriately without extra processing.

Without this, users cannot reliably style or highlight code blocks in multi-language documents.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3rd-partyShould be implemented as a third party extension.invalidInvalid report (user error, upstream issue, etc).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions