Skip to content

fenced_code + attr_list: boolean options like linenums crash with "cannot serialize" when codehilite is not enabled #1713

Description

@Aryan7280

With fenced_code and attr_list enabled but codehilite not enabled, any of the boolean options on a fenced block crashes:

import markdown
src = "``` { .python linenums }\nprint(1)\n```"
markdown.markdown(src, extensions=["fenced_code", "attr_list"])
TypeError: cannot serialize None (type NoneType)

Same thing with the explicit forms:

Attribute Result
linenums cannot serialize None
linenums=true cannot serialize True (type bool)
linenums="1" cannot serialize True (type bool)
guess_lang, noclasses cannot serialize None
data-x (not a boolean option) works

With codehilite also enabled, all of these work fine.

From what I can tell, handle_attrs() runs every key in bool_options (linenums, guess_lang, noclasses, use_pygments) through parseBoolValue(), so the config value becomes True, False or None. When Pygments isn't used, those values are written straight into the <code> tag:

f' {sanitize_name(k)}="{_escape_attrib_html(v)}"'

and _escape_attrib_html() only handles strings.

I'm not sure which output you'd prefer (drop the codehilite-only options, or write them as strings), but it probably shouldn't raise. This isn't new: 3.11.2 crashes the same way.

Versions: Markdown 3.11.3 and master (4177c1b), Python 3.14.2, Windows 11.

Activity

  1. waylan commented on Nov 17, 2026

    @waylan
    Member

    If attributes are defined for a code block and codehilite is not enabled, then we should assume that the user is passing attributes to a Javascript highlighting library. We do not want to remove or alter those attributes differently than normal attr_list behavior.

    The problem is that the extension is not using the serializer, which properly handles these values . Therefore, these should be treated in the same manner as the serializer would handle them (as implemented in #1674). We will need to reimplement the same behavior within the fenced_code extension as outlined below:

    • value == None => key
    • value == True and output_format == 'html' and key in markdown.serializers.BOOLEAN_ATTRIBUTES['html'] => key
    • value == True => key="key"
    • value == False => (an empty string)

    By way of explanation, the default behavior for the attr_list extension is to use key=key internally for truthy values. However, that doesn't preserve explicitly defined falsey values. Therefore, within the fenced_code extension we are preserving boolean values so that they can be passed on to codehilite. However, when codehilite is not enabled, we now need to revert to normal behavior in our x/html output.

  2. added a commit that references this issue on Nov 18, 2026
    f229286
  3. added 2 commits that reference this issue on Nov 25, 2026
    3143b71
    e8b60cb
  4. added a commit that references this issue on Nov 25, 2026
    e30c04a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions