一份 Markdown 两种输出:公众号发布 + Web 预览落地

0 阅读2分钟

ScreenShot_2026-03-04_180049_554.png 之前我在做多端分发时,最常见的翻车场景是:
公众号版改过一次标题层级,Web 版忘记同步,最后两边内容结构不一致。
后来我把流程收敛成一个原则:输入只保留一份 Markdown,输出拆成两个固定函数。

我现在的做法很简单:

  • markdownToHtml 给公众号
  • markdownToWebHtml 给浏览器

先安装:

npm i wechat-markdown-html

第一步,先拿到公众号输出:

import { readFileSync } from "node:fs";
import { markdownToHtml } from "wechat-markdown-html";

const md = readFileSync("article.md", "utf8");
const wechatHtml = markdownToHtml(md, {
  theme: "nature-green",
  codeTheme: "wechat",
  macStyle: true,
  sanitize: true
});

第二步,再拿到 Web 输出:

import { markdownToWebHtml } from "wechat-markdown-html";

const webHtml = markdownToWebHtml(md, {
  theme: "nature-green",
  codeTheme: "github",
  title: "Markdown Preview"
});

为了减少人工步骤,我最后直接合并成一个脚本,一次产出两个文件:

import { readFileSync, writeFileSync } from "node:fs";
import { markdownToHtml, markdownToWebHtml } from "wechat-markdown-html";

const md = readFileSync("article.md", "utf8");

const wechatHtml = markdownToHtml(md, {
  theme: "nature-green",
  codeTheme: "wechat",
  macStyle: true,
  sanitize: true
});

const webHtml = markdownToWebHtml(md, {
  theme: "nature-green",
  codeTheme: "github",
  title: "Markdown Preview"
});

writeFileSync("output-wechat.html", wechatHtml, "utf8");
writeFileSync("output-web.html", webHtml, "utf8");

我在实战里最常用的能力:

  • [TOC] 直接出目录
  • 外链自动归到文末脚注
  • Ruby 注音和横向图片组都能用

参数上别追求花哨,先固定一套:

  • 发布公众号:sanitize: true
  • 本地交互预览:sanitize: false
  • 主题别频繁切,文章视觉会更统一

这套流程跑通之后,改动成本会明显下降。
写作和发布彻底拆开,后面不管换主题还是换渠道,基本都只动输出环节。

我现在就按这四步发:

  • 写完 article.md
  • 跑脚本生成 output-wechat.htmloutput-web.html
  • 先看 output-web.html 检查结构与代码块
  • 最后把 output-wechat.html 粘贴到公众号

核心不是工具有多花,而是流程足够稳定。只要输入仍是 Markdown,这套输出链路就能长期复用。