Create custom converters by subclassing MarkdownConverter
developTo implement custom conversion logic, inherit from MarkdownConverter and override specific tag methods. The method naming convention is convert_{tag_name}(self, el, text, parent_tags).
For example, to add extra newlines after images or to ignore specific tags like paragraphs, you can override convert_img or convert_p respectively.
from markdownify import MarkdownConverter
class ImageBlockConverter(MarkdownConverter):
"""
Create a custom MarkdownConverter that adds two newlines after an image
"""
def convert_img(self, el, text, parent_tags):
return super().convert_img(el, text, parent_tags) + '\n\n'
# Usage
def md(html, **options):
return ImageBlockConverter(**options).convert(html)
class IgnoreParagraphsConverter(MarkdownConverter):
"""
Create a custom MarkdownConverter that ignores paragraphs
"""
def convert_p(self, el, text, parent_tags):
return ''
# Usage
def md(html, **options):
return IgnoreParagraphsConverter(**options).convert(html)