Skip to content
プロジェクト

概要

HTMLをMarkdownに変換します。

Web・アプリ開発

この数値の測り方

計測したスター増加

24時間
0
7日間
0
15日間
0
1か月
履歴を収集中

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

tomd

CI PyPI Python License Downloads Ruff uv

Convert real-world HTML to clean Markdown.

tomd is a small, fast Python library that turns an HTML string into Markdown — handy for archiving articles, normalising scraped content, piping rendered pages into LLM context, or anywhere you'd rather work with plain text than a DOM.

It walks the parsed tree with lxml, so it handles nested lists, formatted table cells, fenced code blocks with language hints, HTML entities, hard line breaks, and the messy markup you actually find in the wild.

Install

pip install tomd
# or
uv add tomd

Requires Python 3.10+.

Quickstart

import tomd

tomd.convert("<h1>Hello, world!</h1>")
# => '# Hello, world!\n'

Or via the class:

from tomd import Tomd

Tomd("<p><b>bold</b> and <i>italic</i></p>").markdown
# => '**bold** and *italic*\n'

What it converts

Markdown HTML
Headings <h1>–<h6>
Bold / italic <b>, <strong>, <i>, <em>
Strikethrough <del>, <s>, <strike>
Inline code <code>, <kbd>, <samp>
Links <a href="..."> incl. relative, mailto:, title
Images <img src="..." alt="..." title="..."/>
Lists (nested, mixed) <ul>, <ol start="N">, <li>
Blockquotes <blockquote> (incl. nested, multi-paragraph)
Horizontal rule <hr/>
Fenced code blocks <pre><code class="language-X">
Tables <table> with or without <thead>/<tbody>
Hard line breaks <br/> → \n
Definition lists <dl> / <dt> / <dd>

<script>, <style>, <iframe>, <head>, and friends are stripped. HTML entities (&amp;, &#8212;, &lt;) are decoded. Unknown tags fall through to their text content.

Example

from tomd import Tomd

html = """
<article>
  <h1>Heading</h1>
  <p><b>bold</b>, <i>italic</i>, and a <a href="https://example.com">link</a>.</p>
  <ul>
    <li>one</li>
    <li>two
      <ul><li>nested</li></ul>
    </li>
  </ul>
  <pre><code class="language-python">print("hi")</code></pre>
</article>
"""

print(Tomd(html).markdown)
# Heading

**bold**, *italic*, and a [link](https://example.com).

- one
- two
  - nested

```python
print("hi")
```

Development

git clone https://github.com/elliotgao2/tomd.git
cd tomd
uv sync                       # install deps into .venv
uv run pytest                 # run tests
uv run ruff check .           # lint
uv run ruff format --check .  # format check

Install the pre-commit hooks:

uv run pre-commit install

Contributing

Pull requests are welcome. For non-trivial changes, please open an issue first. Make sure uv run pytest, uv run ruff check ., and uv run ruff format --check . all pass before submitting.

License

MIT © Elliot Gao

関連プロジェクトを探す

読み込み中…