wechat-md
Markdown → 微信公众号 HTML 渲染器。
输出全部行内 style、零 class、零外部 CSS 的 HTML,粘贴进微信公众号编辑器即为最终排版——标题、代码块、表格、引用、列表样式完整保留,不依赖任何外部资源。
为什么需要它
微信公众号编辑器会过滤 class 属性与外部样式表。通用 Markdown 渲染器输出的高亮 class 会被 strip,导致代码块排版错乱;依赖外部 CSS 的主题在公众号里整体失效。
wechat-md 针对公众号编辑器逐项适配,所有样式内联在元素上,输出即终稿。
安装
pip install wechat-md
生产环境锁定版本(升级是显式决策,不隐性漂移):
pip install wechat-md==0.2.1
引用 GitHub 源时锁 tag(.../archive/refs/tags/vX.Y.Z.tar.gz),不要引滚动的 main。
快速开始
from wechat_md import markdown_to_html
html = markdown_to_html("# 标题\n\n正文…")
返回的 HTML 可直接作为公众号草稿正文提交,或粘贴进编辑器。
排版规则
| Markdown 元素 | 公众号渲染效果 |
|---|---|
| 文首 H1 | 移除(标题由草稿字段承载,正文不重复) |
| H1 / H2 | 加粗 + 左侧红色竖条(border-left,复制时不产生多余换行) |
| H3 | ▪ 前缀加粗 |
| 代码块 | 灰底圆角卡片、等宽字体、缩进以 保留 |
| 引用块 | 灰底 + 左红边卡片 |
| 表格 | 全边框、表头灰底 |
| 链接 | 公众号蓝(#576b95)、可点击 |
| 列表 | 行内样式,并修复渲染器把 - 行并进段落的伪列表问题 |
--- 分隔线 |
移除(编辑器渲染为冗余灰线) |
| 连续「角色名:」行 | 自动聚合成对话卡片 |
| 标题后紧跟加粗小节 | 自动聚合成结论区卡片 |
设计约束
- 纯函数、无网络、无配置——
markdown_to_html(text: str) -> str一个入口。 - 唯一运行时依赖
markdown2(未安装时自动回退markdown)。 - 兼容 Python 3.9+。
开发
pip install -e ".[dev]"
pytest
质量门:ruff(E,W,F,I,B,C4,UP,SIM)+ mypy --strict + pytest 全绿后才发版。
License
MIT
Metadata
Release files for wechat-md 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wechat_md-0.2.1.tar.gz | 8.9 kB | With provenance | Details for wechat_md-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wechat_md-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for wechat_md-0.2.1-py3-none-any.whl |
Total release size: 17.4 kB
Release files / wechat_md-0.2.1.tar.gz
| Download URL | wechat_md-0.2.1.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
20834c91cf17e748a380ed977a56f61145b99e3369ae1f2fdaf449aceb787f6e
|
|
BLAKE2b-256 checksum How to use checksums |
6c5ed64d1a9e330743c2f5ca71deafa3ecfda82f2747588fc57ef3925e8468b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 3, 2026.
Transparency logRelease files / wechat_md-0.2.1-py3-none-any.whl
| Download URL | wechat_md-0.2.1-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ca01560232d7abee8339f6325196d3106c7b54c6c74b0d1df2bf3dc8de628af
|
|
BLAKE2b-256 checksum How to use checksums |
0487b9b0a39e7838322d6db3589f83b2e731ea97b4f8a202c298151cd3c54f90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 3, 2026.
Transparency logSecurity for wechat-md 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for wechat_md-0.2.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.