Markdown to WeChat Official Accounts: styles, code and links

Writing in Markdown6 min read

Pasting raw Markdown or a standard HTML export into the WeChat Official Accounts editor (mp.weixin.qq.com) strips stylesheets, collapses code indentation, breaks blockquotes, and leaves external links unclickable. The reliable fix is to inline computed CSS rules and rewrite unsupported DOM nodes before copying, which MiaoYan's free browser Markdown viewer handles in one click (open /zh/tools/markdown-viewer/ and click 复制到公众号 when you also want external URLs turned into numbered end-references). Text, tables, code highlighting, task lists, and Mermaid diagrams arrive intact, while LaTeX formulas and local images need one extra check below.

Why strict rich-text editors break standard Markdown HTML

Web pages normally separate structure from presentation: a Markdown parser outputs <h2>, <blockquote>, and <pre><code class="hljs">, while a .css file supplies fonts and colors (covered in Markdown to HTML). WeChat's article editor runs a strict HTML sanitizer on ⌘V so pasted markup cannot break WeChat's mobile webview. Five sanitizer rules trip ordinary pastes:

  1. Every <style> tag, <link rel="stylesheet">, class, and id attribute disappears, turning <span class="hljs-keyword"> code tokens plain black unless RGB colors sit inside style="color: ...".
  2. CSS pseudo-elements such as blockquote::before cannot travel on the clipboard, and WeChat applies its own grey border to <blockquote> tags unless they are retagged into <section> elements.
  3. Arbitrary external https:// links do not open inside standard Official Account articles; only mp.weixin.qq.com article links remain tappable.
  4. Rich-text editors collapse runs of ASCII spaces and newlines inside <pre><code> unless spaces become &nbsp; and line breaks become <br>.
  5. Inline <svg> diagrams and <input type="checkbox"> controls get stripped, and KaTeX HTML spans cannot load external .woff2 math fonts inside WeChat.

How MiaoYan's browser converter rewrites the DOM

MiaoYan's Markdown viewer renders your draft inside a sandboxed frame using the stylesheets, highlight.js grammars, and Mermaid theme from the Mac app. Clicking 复制到公众号 on /zh/tools/markdown-viewer/ (or Copy formatted on /tools/markdown-viewer/ for email and CMS editors that keep external links) clones the preview offscreen and runs six DOM passes before writing to your clipboard:

  • It reads getComputedStyle on every node, writes inherited typography (color, font-family, font-size, line-height, letter-spacing) where it differs from the parent alongside explicit box margin, padding, border, and background-color, then strips class and id attributes.
  • It measures MiaoYan's blockquote::before bar, converts it into an inline border-left and padding-left, and retags <blockquote> and the root container to <section> so WeChat's quote style never overrides it.
  • Inside <pre>, tabs become four spaces, spaces turn into \u00a0 (&nbsp;), newlines become <br> with trailing empty breaks trimmed, and white-space: nowrap; overflow-x: auto keeps code horizontally scrollable on phones.
  • Every .mermaid-image-container svg is drawn onto a <canvas> at 2x resolution and replaced with a data:image/png <img> locked to its display width for Retina clarity.
  • Task checkboxes become `☑` and `☐` text spans, and nested <ul> or <ol> lists inside <li> move right after their parent item so indentation survives.
  • In WeChat mode (/zh/tools/markdown-viewer/), https://mp.weixin.qq.com/ links stay clickable, bare URLs stay plain text, and other external http or https links become inline text plus a superscript [n] marker with a 14px 参考链接 section appended at the end.

A step-by-step publishing workflow

  1. Draft your post in MiaoYan on Mac (⌘\ for split preview) or directly inside the browser editor, which supports ⌘B, ⌘I, ⌘K, ⌘E, ⇧⌘E, and list continuation.
  2. Run Edit > Clean Typography in MiaoYan on Mac, or paste into the Chinese typography cleaner to space Chinese and Latin words and fix half-width punctuation without touching code, URLs, or math; see Chinese-English spacing in Markdown notes.
  3. Open /zh/tools/markdown-viewer/, load your .md file, and toggle 手机宽度 (Phone width) to check heading wraps and table widths at 375px.
  4. Click 复制到公众号, note how many external links moved to 参考链接 in the status line, and press ⌘V inside mp.weixin.qq.com.

What goes wrong and how to fix it

LaTeX math shows as $...$ source text

Because clipboard HTML cannot embed KaTeX web fonts into WeChat, 复制到公众号 converts inline and display math back into <code>$...$</code> and <code>$$...$$</code> source text and notes that in the status bar rather than pasting broken boxes. For equation-heavy posts, export the section or full note as a 3x mobile PNG in Markdown to image; sizing details are in Markdown to image: export a note as a long PNG.

Local images are missing after paste

Browser tabs cannot read /i/screenshot.png from local disk, and WeChat blocks many hotlinked hosts on save. Either set up Settings > Editor > Upload to S3 or R2 in MiaoYan on Mac so screenshots get public https:// URLs WeChat can ingest on paste, or drag local PNGs from Finder straight into the WeChat editor afterward.

Dark-mode contrast and async diagrams

Always send a test preview (预览) to your phone with iOS or Android Dark Mode enabled. WeChat automatically inverts inline colors on dark screens; MiaoYan's warm paper palette (#403E3C ink with subtle quote and code fills) survives WeChat's inversion cleanly, whereas raw #000000 blocks turn harsh. Drafts stay in your browser's localStorage, or click 存进妙言 (Save to MiaoYan) on Mac to store the note in the GitHub or App Store app.

FAQ

WeChat Official Accounts permit inline links to https://mp.weixin.qq.com/ articles, so WeChat mode preserves those <a> tags and turns only external domains into numbered [1] end-references.

Does the browser tool upload my draft or images to a server?

No. Parsing, syntax highlighting, Mermaid rasterization, and CSS inlining run locally in your browser, and your draft stays only in localStorage on your device.

Can I use the same copy button for Substack, Ghost, or HTML email?

Use the English page at Markdown viewer and click Copy formatted. It runs the same inline CSS, <section> quote border, &nbsp; code spacing, and 2x PNG Mermaid steps while keeping external <a> links clickable.

Why did a Mermaid diagram fail to appear when I pasted immediately?

Mermaid loads on demand and renders asynchronously. Wait a second until the diagram finishes drawing in the preview pane before clicking 复制到公众号 so the canvas exporter can convert the SVG to PNG.