/**
* 公众号草稿统一 layout:阅读原文 + 底部关注区。
* 标准定义见 wechat-draft-publication-standard.mjs 与 docs/wechat-draft-publication-standard.md
*/
import fs from 'node:fs';
import path from 'node:path';
import { fetch as undiciFetch } from 'undici';
import { PLATFORM_BRAND_ICON_PATH } from './mindspace-og-tags.mjs';
import {
resolveMpFollowQrcodePath,
uploadWechatArticleContentImage,
} from './wechat-news-morning-draft.mjs';
import { WECHAT_DRAFT_PUBLICATION_STANDARD } from './wechat-draft-publication-standard.mjs';
const WECHAT_DRAFT_CONTENT_CHAR_LIMIT =
WECHAT_DRAFT_PUBLICATION_STANDARD.limits.maxContentChars;
export function clipWechatDraftBodyToFitFooter(
body,
footer,
limit = WECHAT_DRAFT_CONTENT_CHAR_LIMIT,
) {
const source = String(body ?? '');
const suffix = String(footer ?? '');
const separator = source && suffix ? '\n' : '';
const maxBody = Math.max(0, Number(limit) - suffix.length - separator.length);
if (source.length <= maxBody) return source;
let clipped = source.slice(0, maxBody);
const lastLt = clipped.lastIndexOf('<');
const lastGt = clipped.lastIndexOf('>');
if (lastLt > lastGt) clipped = clipped.slice(0, lastLt);
return clipped.trimEnd();
}
export function renderReadOriginalLink(publicUrl) {
if (!publicUrl) return '';
return [
'
',
`📖 阅读原文`,
'
',
].join('');
}
/** @deprecated 保留别名,避免外部引用断裂 */
export const renderReadOriginalTop = renderReadOriginalLink;
export function renderFollowTkMindSection({
qrcodeImageUrl = '',
} = {}) {
const qrcodeBlock = qrcodeImageUrl
? `
`
: '';
return [
'',
'欢迎关注 TKMind 智趣
',
qrcodeBlock,
qrcodeImageUrl
? '长按扫描关注
'
: '关注 TKMind 服务号
',
'',
].filter(Boolean).join('');
}
export function applyWechatDraftArticleLayout(
content,
{ publicUrl = '', qrcodeImageUrl = '' } = {},
) {
const footerParts = [];
if (publicUrl) {
footerParts.push(renderReadOriginalLink(publicUrl));
}
footerParts.push(renderFollowTkMindSection({ qrcodeImageUrl }));
const footer = footerParts.filter(Boolean).join('\n');
const body = clipWechatDraftBodyToFitFooter(String(content ?? '').trim(), footer);
return [body, footer].filter(Boolean).join(body && footer ? '\n' : '');
}
export function resolveTkMindBrandIconPath(memindLibRoot = process.cwd()) {
const candidates = [
path.join(memindLibRoot, 'public', PLATFORM_BRAND_ICON_PATH.replace(/^\//, '')),
path.join(memindLibRoot, 'dist', PLATFORM_BRAND_ICON_PATH.replace(/^\//, '')),
path.join(process.cwd(), 'public', PLATFORM_BRAND_ICON_PATH.replace(/^\//, '')),
];
return candidates.find((candidate) => fs.existsSync(candidate)) ?? null;
}
export async function uploadWechatDraftBrandAssets({
accessToken,
wechatFetch = undiciFetch,
memindLibRoot = process.cwd(),
} = {}) {
let qrcodeImageUrl = '';
if (!accessToken) {
return { qrcodeImageUrl };
}
const qrcodePath = resolveMpFollowQrcodePath(memindLibRoot);
if (qrcodePath) {
qrcodeImageUrl = await uploadWechatArticleContentImage(
accessToken,
fs.readFileSync(qrcodePath),
{ wechatFetch, filename: 'mp-follow-qrcode.png' },
);
}
return { qrcodeImageUrl };
}
export async function finalizeWechatDraftArticleForPush({
content,
publicUrl = '',
accessToken = null,
wechatFetch = undiciFetch,
memindLibRoot = process.cwd(),
} = {}) {
const brandAssets = await uploadWechatDraftBrandAssets({
accessToken,
wechatFetch,
memindLibRoot,
});
return {
content: applyWechatDraftArticleLayout(content, {
publicUrl,
...brandAssets,
}),
...brandAssets,
};
}