Skip to main content

wechat-md

PyPI Python License: MIT CI

Markdown → 微信公众号 HTML 渲染器。

输出全部行内 style、零 class、零外部 CSS 的 HTML,粘贴进微信公众号编辑器即为最终排版——标题、代码块、表格、引用、列表样式完整保留,不依赖任何外部资源。

为什么需要它

微信公众号编辑器会过滤 class 属性与外部样式表。通用 Markdown 渲染器输出的高亮 class 会被 strip,导致代码块排版错乱;依赖外部 CSS 的主题在公众号里整体失效。

wechat-md 针对公众号编辑器逐项适配,所有样式内联在元素上,输出即终稿。

安装

pip install wechat-md

生产环境锁定版本(升级是显式决策,不隐性漂移):

pip install wechat-md==0.5.0

引用 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 可直接作为公众号草稿正文提交,或粘贴进编辑器。

主题(可选)

传入 theme 即可按主题配色渲染(键与 wechat-publish-service 的 ThemeConfig 对齐):

html = markdown_to_html(
    md,
    theme={"h1_color": "#3498db", "h2_color": "#3498db", "strong_color": "#3498db"},
)

支持键:h1_color / h2_color / h3_color / strong_color / quote_bg / quote_border / code_bg / code_font_size / text_color。未提供或未知键保持默认; 不传 theme 时输出与历史版本逐字节一致。

排版规则

Markdown 元素 公众号渲染效果
文首 H1 移除(标题由草稿字段承载,正文不重复)
H1 / H2 加粗 + 左侧红色竖条(border-left,复制时不产生多余换行)
H3 ▪ 前缀加粗
代码块 灰底圆角卡片、等宽字体、缩进以   保留
引用块 灰底 + 左红边卡片
表格 全边框、表头灰底
链接 公众号蓝(#576b95)、可点击
列表 行内样式,并修复渲染器把 - 行并进段落的伪列表问题
--- 分隔线 移除(编辑器渲染为冗余灰线)
连续「角色名:」行 自动聚合成对话卡片
标题后紧跟加粗小节 自动聚合成结论区卡片

设计约束

  • 纯函数、无网络、无配置——markdown_to_html(text, theme=None) 一个入口;主题为可选配色字典,由调用方传入。
  • 唯一运行时依赖 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.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wechat-md 0.5.0
File Size Uploaded Provenance Actions
wechat_md-0.5.0.tar.gz 18.5 kB With provenance Details for wechat_md-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wechat-md 0.5.0
File Interpreter ABI Platform Actions
wechat_md-0.5.0-py3-none-any.whl Python 3 none any Details for wechat_md-0.5.0-py3-none-any.whl

Total release size: 32.5 kB

Release files / wechat_md-0.5.0.tar.gz

Download URL wechat_md-0.5.0.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
eff6c907e364b077017838e8e093324d8a8decef2354ba14545fd0801d9cabc8
BLAKE2b-256 checksum
How to use checksums
775eb96ba30144acfa6cd7d9cf3a09d813d4379da2e6a26e1333fff8aa08e6f5
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 Oct 4, 2026.

Transparency log

Release files / wechat_md-0.5.0-py3-none-any.whl

Download URL wechat_md-0.5.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f840e0b7a9f3ddbfca0a8bef6d6618428a0d477fb6f79760e9ea01251072fe4a
BLAKE2b-256 checksum
How to use checksums
f2d0e277f35fcd475a35b02b5a35396e0182b859863bdefbf39747ee38a44cbb
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 Oct 4, 2026.

Transparency log

Security for wechat-md 0.5.0

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.

Select from 2 files

Attestation for wechat_md-0.5.0.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 Oct 4, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page