关于
将 HTML 转换为 Markdown。
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文tomd
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 (&, —, <) 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
探索相关项目
加载中…