Skip to content

LeoYeAI/agent-master-skills / skills/baoyu-post-to-wechat

baoyu-post-to-wechat

Flagged

Posts content to WeChat Official Account (微信公众号) via API or Chrome CDP. Supports article posting (文章) with HTML, markdown, or plain text input, and image-text posting (贴图, formerly 图文) with multiple images. Markdown article workflows default to converting ordinary external links into bottom citations for WeChat-friendly output. Use when user mentions "发布公众号", "post to wechat", "微信公众号", or "贴图/图文/文章".

FileKindSize
SKILL.mdskill md19.7 KB
_meta.jsonconfig826 B
references/article-posting.mdmarkdown2.5 KB
references/config/first-time-setup.mdmarkdown5.2 KB
references/image-text-posting.mdmarkdown2.8 KB
scripts/bun.lockother38.7 KB
scripts/cdp.tsscript8.2 KB
scripts/check-permissions.tsscript8.6 KB
scripts/copy-to-clipboard.tsscript11.9 KB
scripts/md-to-wechat.tsscript4.7 KB
scripts/md/LICENSEother650 B
scripts/md/cli.tsscript5.7 KB
scripts/md/code-themes/1c-light.min.cssother1.1 KB
scripts/md/code-themes/a11y-dark.min.cssother1.1 KB
scripts/md/code-themes/a11y-light.min.cssother1.1 KB
scripts/md/code-themes/agate.min.cssother1.3 KB
scripts/md/code-themes/an-old-hope.min.cssother961 B
scripts/md/code-themes/androidstudio.min.cssother611 B
scripts/md/code-themes/arduino-light.min.cssother844 B
scripts/md/code-themes/arta.min.cssother673 B
scripts/md/code-themes/ascetic.min.cssother454 B
scripts/md/code-themes/atom-one-dark-reasonable.min.cssother1.2 KB
scripts/md/code-themes/atom-one-dark.min.cssother856 B
scripts/md/code-themes/atom-one-light.min.cssother856 B
scripts/md/code-themes/brown-paper.min.cssother656 B
scripts/md/code-themes/codepen-embed.min.cssother600 B
scripts/md/code-themes/color-brewer.min.cssother631 B
scripts/md/code-themes/dark.min.cssother625 B
scripts/md/code-themes/default.min.cssother1.1 KB
scripts/md/code-themes/devibeans.min.cssother1.1 KB
scripts/md/code-themes/docco.min.cssother837 B
scripts/md/code-themes/far.min.cssother669 B
scripts/md/code-themes/felipec.min.cssother1.2 KB
scripts/md/code-themes/foundation.min.cssother779 B
scripts/md/code-themes/github-dark-dimmed.min.cssother1.2 KB
scripts/md/code-themes/github-dark.min.cssother1.3 KB
scripts/md/code-themes/github.min.cssother1.3 KB
scripts/md/code-themes/gml.min.cssother787 B
scripts/md/code-themes/googlecode.min.cssother835 B
scripts/md/code-themes/gradient-dark.min.cssother1.1 KB
scripts/md/code-themes/gradient-light.min.cssother1.1 KB
scripts/md/code-themes/grayscale.min.cssother1.6 KB
scripts/md/code-themes/hybrid.min.cssother897 B
scripts/md/code-themes/idea.min.cssother906 B
scripts/md/code-themes/intellij-light.min.cssother1.0 KB
scripts/md/code-themes/ir-black.min.cssother694 B
scripts/md/code-themes/isbl-editor-dark.min.cssother971 B
scripts/md/code-themes/isbl-editor-light.min.cssother952 B
scripts/md/code-themes/kimbie-dark.min.cssother652 B
scripts/md/code-themes/kimbie-light.min.cssother652 B
scripts/md/code-themes/lightfair.min.cssother831 B
scripts/md/code-themes/lioshi.min.cssother715 B
scripts/md/code-themes/magula.min.cssother642 B
scripts/md/code-themes/mono-blue.min.cssother631 B
scripts/md/code-themes/monokai-sublime.min.cssother826 B
scripts/md/code-themes/monokai.min.cssother814 B
scripts/md/code-themes/night-owl.min.cssother1.4 KB
scripts/md/code-themes/nnfx-dark.min.cssother1.4 KB
scripts/md/code-themes/nnfx-light.min.cssother1.4 KB
scripts/md/code-themes/nord.min.cssother2.6 KB
scripts/md/code-themes/obsidian.min.cssother882 B
scripts/md/code-themes/panda-syntax-dark.min.cssother1.1 KB
scripts/md/code-themes/panda-syntax-light.min.cssother1.0 KB
scripts/md/code-themes/paraiso-dark.min.cssother637 B
scripts/md/code-themes/paraiso-light.min.cssother637 B
scripts/md/code-themes/pojoaque.min.cssother814 B
scripts/md/code-themes/purebasic.min.cssother734 B
scripts/md/code-themes/qtcreator-dark.min.cssother815 B
scripts/md/code-themes/qtcreator-light.min.cssother810 B
scripts/md/code-themes/rainbow.min.cssother826 B
scripts/md/code-themes/routeros.min.cssother862 B
scripts/md/code-themes/school-book.min.cssother664 B
scripts/md/code-themes/shades-of-purple.min.cssother854 B
scripts/md/code-themes/srcery.min.cssother795 B
scripts/md/code-themes/stackoverflow-dark.min.cssother1.2 KB
scripts/md/code-themes/stackoverflow-light.min.cssother1.3 KB
scripts/md/code-themes/sunburst.min.cssother950 B
scripts/md/code-themes/tokyo-night-dark.min.cssother1.2 KB
scripts/md/code-themes/tokyo-night-light.min.cssother1.2 KB
scripts/md/code-themes/tomorrow-night-blue.min.cssother648 B
scripts/md/code-themes/tomorrow-night-bright.min.cssother648 B
scripts/md/code-themes/vs.min.cssother640 B
scripts/md/code-themes/vs2015.min.cssother1.1 KB
scripts/md/code-themes/xcode.min.cssother945 B
scripts/md/code-themes/xt256.min.cssother765 B
scripts/md/constants.tsscript3.3 KB
scripts/md/extend-config.tsscript2.3 KB
scripts/md/extensions/alert.tsscript19.3 KB
scripts/md/extensions/footnotes.tsscript2.4 KB
scripts/md/extensions/index.tsscript280 B
scripts/md/extensions/infographic.tsscript3.6 KB
scripts/md/extensions/katex.tsscript4.6 KB
scripts/md/extensions/markup.tsscript2.3 KB
scripts/md/extensions/plantuml.tsscript8.1 KB
scripts/md/extensions/ruby.tsscript3.9 KB
scripts/md/extensions/slider.tsscript2.9 KB
scripts/md/extensions/toc.tsscript2.1 KB
scripts/md/html-builder.tsscript4.4 KB
scripts/md/package-lock.jsonmanifest50.2 KB
scripts/md/package.jsonmanifest321 B
scripts/md/render.tsscript3.8 KB
scripts/md/renderer.tsscript13.0 KB
scripts/md/themes.tsscript1.8 KB
scripts/md/themes/base.cssother816 B
scripts/md/themes/default.cssother7.7 KB
scripts/md/themes/grace.cssother2.5 KB
scripts/md/themes/modern.cssother8.2 KB
scripts/md/themes/simple.cssother2.7 KB
scripts/md/types.tsscript1.8 KB
scripts/md/utils/languages.tsscript8.1 KB
scripts/package.jsonmanifest261 B
scripts/paste-from-clipboard.tsscript5.5 KB
scripts/wechat-agent-browser.tsscript11.1 KB
scripts/wechat-api.tsscript25.3 KB
scripts/wechat-article.tsscript32.0 KB
scripts/wechat-browser.tsscript27.7 KB
scripts/wechat-extend-config.test.tsscript4.3 KB
scripts/wechat-extend-config.tsscript9.8 KB
scripts/wechat-image-processor.tsscript7.1 KB

scripts/wechat-image-processor.ts

View on GitHub
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { Jimp, JimpMime } from "jimp";
import decodeWebp, { init as initWebpDecode } from "@jsquash/webp/decode.js";

export interface WechatUploadAsset {
  buffer: Buffer;
  filename: string;
  contentType: string;
  fileExt: string;
  fileSize: number;
}

export interface PreparedWechatUploadAsset {
  buffer: Buffer;
  filename: string;
  contentType: string;
  wasProcessed: boolean;
  processingNotes: string[];
}

export const WECHAT_BODY_IMAGE_MAX_SIZE = 1024 * 1024; // 1MB
export const WECHAT_BODY_IMAGE_UNSUPPORTED_FORMATS = new Set([
  ".gif",
  ".webp",
  ".bmp",
  ".tiff",
  ".tif",
  ".svg",
  ".ico",
]);

const BODY_UPLOAD_ALLOWED_MIME_TYPES = new Set([
  JimpMime.jpeg,
  JimpMime.png,
]);

const MIME_TO_EXT: Record<string, string> = {
  "image/jpeg": ".jpg",
  "image/png": ".png",
  "image/gif": ".gif",
  "image/webp": ".webp",
  "image/bmp": ".bmp",
  "image/x-ms-bmp": ".bmp",
  "image/tiff": ".tiff",
  "image/svg+xml": ".svg",
  "image/x-icon": ".ico",
  "image/vnd.microsoft.icon": ".ico",
};

const JPEG_QUALITY_STEPS = [82, 74, 66, 58, 50, 42, 34];
const MAX_WIDTH_STEPS = [2560, 2048, 1600, 1280, 1024, 800, 640, 480];

let webpDecoderReady: Promise<void> | undefined;

type JimpImage = Awaited<ReturnType<typeof Jimp.read>>;

function normalizeMimeType(contentType: string): string {
  return contentType.split(";")[0]!.trim().toLowerCase();
}

function extFromMimeType(contentType: string): string {
  return MIME_TO_EXT[normalizeMimeType(contentType)] || "";
}

function ensureFileExt(asset: WechatUploadAsset): string {
  return asset.fileExt || extFromMimeType(asset.contentType);
}

function basenameWithoutExt(filename: string): string {
  const base = path.basename(filename, path.extname(filename));
  return base || "image";
}

function renameWithExt(filename: string, ext: string): string {
  return `${basenameWithoutExt(filename)}${ext}`;
}

export function needsWechatBodyImageProcessing(asset: WechatUploadAsset): boolean {
  if (asset.fileSize > WECHAT_BODY_IMAGE_MAX_SIZE) {
    return true;
  }

  const normalizedMimeType = normalizeMimeType(asset.contentType);
  if (BODY_UPLOAD_ALLOWED_MIME_TYPES.has(normalizedMimeType)) {
    return false;
  }

  const fileExt = ensureFileExt(asset);
  return WECHAT_BODY_IMAGE_UNSUPPORTED_FORMATS.has(fileExt) || !fileExt;
}

async function ensureWebpDecoder(): Promise<void> {
  if (!webpDecoderReady) {
    webpDecoderReady = (async () => {
      const __filename = fileURLToPath(import.meta.url);
      const __dirname = path.dirname(__filename);
      const wasmPath = path.resolve(__dirname, "node_modules/@jsquash/webp/codec/dec/webp_dec.wasm");
      const wasmModule = await WebAssembly.compile(await fs.readFile(wasmPath));
      await initWebpDecode(wasmModule, {});
    })();
  }

  await webpDecoderReady;
}

async function loadImageForProcessing(asset: WechatUploadAsset): Promise<JimpImage> {
  const fileExt = ensureFileExt(asset);
  const normalizedMimeType = normalizeMimeType(asset.contentType);

  if (fileExt === ".webp" || normalizedMimeType === "image/webp") {
    await ensureWebpDecoder();
    const decoded = await decodeWebp(asset.buffer);
    return new Jimp({
      data: Buffer.from(decoded.data.buffer, decoded.data.byteOffset, decoded.data.byteLength),
      width: decoded.width,
      height: decoded.height,
    });
  }

  if (fileExt === ".svg" || fileExt === ".ico") {
    throw new Error(`Cannot convert ${fileExt} image for WeChat body upload; provide a PNG or JPG instead.`);
  }

  return Jimp.read(asset.buffer);
}

function imageHasTransparency(image: JimpImage): boolean {
  const { data } = image.bitmap;
  for (let i = 3; i < data.length; i += 4) {
    if (data[i] !== 255) {
      return true;
    }
  }
  return false;
}

function buildCandidateWidths(width: number): number[] {
  const candidates = new Set<number>([width]);

  for (const maxWidth of MAX_WIDTH_STEPS) {
    if (width > maxWidth) {
      candidates.add(maxWidth);
    }
  }

  return [...candidates].sort((a, b) => b - a);
}

function resizeToWidth(image: JimpImage, width: number): JimpImage {
  const cloned = image.clone();
  if (width < image.bitmap.width) {
    cloned.resize({ w: width });
  }
  return cloned;
}

function flattenOnWhite(image: JimpImage): JimpImage {
  const flattened = new Jimp({
    width: image.bitmap.width,
    height: image.bitmap.height,
    color: 0xffffffff,
  });
  flattened.composite(image, 0, 0);
  return flattened;
}

async function encodePng(image: JimpImage): Promise<Buffer> {
  return image.getBuffer(JimpMime.png);
}

async function encodeJpeg(image: JimpImage, quality: number): Promise<Buffer> {
  const jpegSource = imageHasTransparency(image) ? flattenOnWhite(image) : image;
  return jpegSource.getBuffer(JimpMime.jpeg, { quality });
}

function buildProcessingNotes(asset: WechatUploadAsset): string[] {
  const notes: string[] = [];
  const fileExt = ensureFileExt(asset);

  if (fileExt && WECHAT_BODY_IMAGE_UNSUPPORTED_FORMATS.has(fileExt)) {
    notes.push(`converted unsupported ${fileExt} source`);
  }

  if (asset.fileSize > WECHAT_BODY_IMAGE_MAX_SIZE) {
    notes.push(`compressed ${(asset.fileSize / 1024 / 1024).toFixed(2)}MB source below 1MB`);
  }

  if (notes.length === 0) {
    notes.push("re-encoded for WeChat body upload");
  }

  return notes;
}

export async function prepareWechatBodyImageUpload(
  asset: WechatUploadAsset,
): Promise<PreparedWechatUploadAsset> {
  if (!needsWechatBodyImageProcessing(asset)) {
    return {
      buffer: asset.buffer,
      filename: asset.filename,
      contentType: asset.contentType,
      wasProcessed: false,
      processingNotes: [],
    };
  }

  const image = await loadImageForProcessing(asset);
  const widths = buildCandidateWidths(image.bitmap.width);
  const preferPng = imageHasTransparency(image) || ensureFileExt(asset) === ".png";
  const processingNotes = buildProcessingNotes(asset);

  for (const width of widths) {
    const resized = resizeToWidth(image, width);

    if (preferPng) {
      const pngBuffer = await encodePng(resized);
      if (pngBuffer.length <= WECHAT_BODY_IMAGE_MAX_SIZE) {
        return {
          buffer: pngBuffer,
          filename: renameWithExt(asset.filename, ".png"),
          contentType: JimpMime.png,
          wasProcessed: true,
          processingNotes: width < image.bitmap.width
            ? [...processingNotes, `resized to ${width}px wide`]
            : processingNotes,
        };
      }
    }

    for (const quality of JPEG_QUALITY_STEPS) {
      const jpegBuffer = await encodeJpeg(resized, quality);
      if (jpegBuffer.length <= WECHAT_BODY_IMAGE_MAX_SIZE) {
        const notes = [...processingNotes, `encoded as JPEG (${quality} quality)`];
        if (width < image.bitmap.width) {
          notes.push(`resized to ${width}px wide`);
        }
        return {
          buffer: jpegBuffer,
          filename: renameWithExt(asset.filename, ".jpg"),
          contentType: JimpMime.jpeg,
          wasProcessed: true,
          processingNotes: notes,
        };
      }
    }
  }

  throw new Error(`Unable to reduce ${asset.filename} below 1MB for WeChat body upload.`);
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall