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
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:
"text"
is meant to be just text
given your expected output.title
will have leading newlines, so you'll need to strip those first before feeding title
into the f-string.