Skip to content

Converter

converter

HTML to Markdown conversion.

Converts HTML documentation to Markdown with YAML frontmatter. Provides SusMarkdownConverter (custom markdownify with alt text preservation) and ContentConverter (high-level orchestrator).

Classes

SusMarkdownConverter

Bases: MarkdownConverter

Custom Markdown converter with better handling for docs.

Overrides specific conversion methods for improved output quality.

Functions

convert_img
convert_img(el: Any, text: str, **kwargs: Any) -> str

Override image conversion for better alt text handling.

Preserves alt text when present; uses empty string when absent to avoid None formatting issues in markdown output.

Parameters:

Name Type Description Default
el Any

HTML image element

required
text str

Converted text content (unused for images)

required
**kwargs Any

Additional arguments from parent class (e.g., convert_as_inline)

{}

Returns:

Type Description
str

Markdown image syntax: alt

Examples:

Company LogoCompany Logo

convert_pre
convert_pre(el: Any, text: str, **kwargs: Any) -> str

Override code block conversion with language detection.

Detects language from class attribute (e.g., class="language-python") and formats as fenced code blocks with language specifier.

Parameters:

Name Type Description Default
el Any

HTML pre element

required
text str

Converted text content

required
**kwargs Any

Additional arguments from parent class (e.g., parent_tags)

{}

Returns:

Type Description
str

Markdown fenced code block with language

Examples:

print("hello")

python → print("hello") →

plain text

→ plain text →

ContentConverter

ContentConverter(config: MarkdownConfig)

Converts HTML to Markdown with frontmatter.

Handles HTML cleaning, markdown conversion, frontmatter generation, and markdown post-processing.

Attributes:

Name Type Description
config

MarkdownConfig containing conversion options

converter

SusMarkdownConverter instance for HTML→Markdown conversion

Initialize converter with markdown config.

Parameters:

Name Type Description Default
config MarkdownConfig

MarkdownConfig from SusConfig

required

Functions

convert
convert(html: str, url: str, title: str | None = None, metadata: dict[str, Any] | None = None) -> str

Convert HTML to Markdown with frontmatter.

Parameters:

Name Type Description Default
html str

HTML content to convert

required
url str

Source URL (for frontmatter)

required
title str | None

Page title (extracted from or provided)</p> </div> </td> <td> <code>None</code> </td> </tr> <tr class="doc-section-item"> <td> <code>metadata</code> </td> <td> <code><span title="dict">dict</span>[<span title="str">str</span>, <span title="typing.Any">Any</span>] | None</code> </td> <td> <div class="doc-md-description"> <p>Additional metadata for frontmatter</p> </div> </td> <td> <code>None</code> </td> </tr> </tbody> </table> <p><span class="doc-section-title">Returns:</span></p> <table> <thead> <tr> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr class="doc-section-item"> <td> <code><span title="str">str</span></code> </td> <td> <div class="doc-md-description"> <p>Markdown content with YAML frontmatter</p> </div> </td> </tr> </tbody> </table> <p><span class="doc-section-title">Examples:</span></p> <div class="language-pycon highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="gp">>>> </span><span class="n">converter</span> <span class="o">=</span> <span class="n">ContentConverter</span><span class="p">(</span><span class="n">MarkdownConfig</span><span class="p">())</span> </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="gp">>>> </span><span class="n">html</span> <span class="o">=</span> <span class="s1">'<html><head><title>Test</title></head><body><h1>Hello</h1></body></html>'</span> </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">converter</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="s2">"https://example.com/test"</span><span class="p">)</span> </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="gp">>>> </span><span class="s2">"# Hello"</span> <span class="ow">in</span> <span class="n">result</span> </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="go">True</span> </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="gp">>>> </span><span class="s2">"title: Test"</span> <span class="ow">in</span> <span class="n">result</span> </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="go">True</span> </span></code></pre></div> <p>Steps: 1. Extract title from HTML if not provided (from <title> tag) 2. Convert HTML to Markdown using SusMarkdownConverter 3. Clean markdown (remove excessive blank lines, fix spacing) 4. Add frontmatter if configured 5. Return final markdown</p> </div> </div> </div> </div> </div> </div> </div> </div> </article> </div> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type="button" class="md-top md-icon" data-md-component="top" hidden> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class="md-footer"> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class="md-copyright"> Made with <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener"> Material for MkDocs </a> </div> <div class="md-social"> <a href="https://github.com/UtsavBalar1231/sus" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> </div> </div> </div> </footer> </div> <div class="md-dialog" data-md-component="dialog"> <div class="md-dialog__inner md-typeset"></div> </div> <script id="__config" type="application/json">{"annotate": null, "base": "../..", "features": ["navigation.tabs", "navigation.sections", "navigation.expand", "navigation.top", "toc.follow", "search.suggest", "search.highlight", "content.code.copy", "content.code.annotate"], "search": "../../assets/javascripts/workers/search.7a47a382.min.js", "tags": null, "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}, "version": null}</script> <script src="../../assets/javascripts/bundle.e71a0d61.min.js"></script> </body> </html>