htmlpython-3.xmarkdown

`markdownify.MarkdownConverter` - Impossible to change the behavior of `convert_hN`


Here is a M(not)WE where changing convert_hN seems broken. Why?

from markdownify import MarkdownConverter

class CustomMarkdownConverter(MarkdownConverter):
    def convert_hN(self, n, el, text, parent_tags):
        title = super().convert_hN(n, el, "text", parent_tags)

        return f"BUG: {title}"

def custom_markdownify(html):
    return CustomMarkdownConverter().convert(html)

html = """
<h1>Section 1</h1>
<h2>Section 2</h2>
<h3>Sub Section 3.1</h3>
"""

print(custom_markdownify(html))

This prints

Section 1
=========

Section 2
---------

### Sub Section 3.1

instead of

BUG: Section 1
==============

BUG: Section 2
--------------

### BUG: Sub Section 3.1

Solution

  • It looks like that method was private and called _convert_hn, and was recently made public. The reason your code doesn't work, presumably, is because that method override isn't an override at all because the name is incorrect, and isn't being called.

    You'll need to wait for PyPi to be updated, use the version from Github directly, or just use the old "private" method name.


    Two other notes: