From bfe8bac00c73a74d0e1fda464183b0896cf30bb0 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 10 Sep 2026 09:43:16 +0800 Subject: [PATCH] fix(wechat): reserve space for uploaded follow QR in draft body Co-authored-by: Cursor --- wechat-daily-news-inline.mjs | 53 +++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/wechat-daily-news-inline.mjs b/wechat-daily-news-inline.mjs index b8e431d..17a0fa2 100644 --- a/wechat-daily-news-inline.mjs +++ b/wechat-daily-news-inline.mjs @@ -287,10 +287,18 @@ function resolveSectionCardLimit(section, sectionCardLimits) { return sectionCardLimits[section.id] ?? 3; } +function estimateQrcodeUrlForLayout(qrcodeImageUrl = '') { + const url = String(qrcodeImageUrl ?? ''); + if (!url) return ''; + if (url.length >= 180) return url; + return `${url}${'x'.repeat(180 - url.length)}`; +} + function estimateFixedLayoutLength({ publicUrl = '', qrcodeImageUrl = '', portalUrl = DEFAULT_PORTAL_URL } = {}) { + const qrcodeForEstimate = estimateQrcodeUrlForLayout(qrcodeImageUrl); return ( (publicUrl ? renderReadOriginalTop(publicUrl).length : 0) - + renderFollowMpSection(qrcodeImageUrl).length + + renderFollowMpSection(qrcodeForEstimate).length + renderCreatePortalCTA(portalUrl).length ); } @@ -405,7 +413,7 @@ export function convertDailyNewsHtmlToWechatInlineArticle(html, { const allProfiles = [...TRIM_PROFILES, ...buildFallbackTrimProfiles()]; const fixedLayoutLength = estimateFixedLayoutLength({ publicUrl, qrcodeImageUrl, portalUrl }); - const trimTargetChars = Math.max(12000, MAX_WECHAT_CONTENT_CHARS - fixedLayoutLength - 600); + const trimTargetChars = Math.max(12000, MAX_WECHAT_CONTENT_CHARS - fixedLayoutLength - 800); for (const [index, trimProfile] of allProfiles.entries()) { built = buildWechatInlineContent(html, { @@ -431,23 +439,36 @@ export function convertDailyNewsHtmlToWechatInlineArticle(html, { trimProfile: selectedProfile, includeFixedLayout: true, }); - while (built.content.length > MAX_WECHAT_CONTENT_CHARS && (selectedProfile.maxBodyChars ?? 120) > 20) { + let tightenGuard = 0; + while (built.content.length > MAX_WECHAT_CONTENT_CHARS && tightenGuard < 24) { + tightenGuard += 1; + const limits = { + world: 2, + domestic: 2, + google: 1, + tech: 2, + sports: 1, + technews: 0, + hot: 0, + headlines: selectedProfile.sectionCardLimits?.headlines ?? 3, + finance: selectedProfile.sectionCardLimits?.finance ?? 2, + }; + const maxBody = selectedProfile.maxBodyChars ?? 64; + if (maxBody > 16) { + selectedProfile.maxBodyChars = maxBody - 4; + } else if (limits.headlines > 2) { + limits.headlines -= 1; + } else if (limits.finance > 1) { + limits.finance -= 1; + } else { + break; + } selectedProfile = { ...selectedProfile, + skipHistory: true, skipWeather: true, - maxBodyChars: Math.max(20, (selectedProfile.maxBodyChars ?? 64) - 8), - sectionCardLimits: { - ...(selectedProfile.sectionCardLimits ?? {}), - headlines: Math.max(2, selectedProfile.sectionCardLimits?.headlines ?? 3), - world: Math.min(2, selectedProfile.sectionCardLimits?.world ?? 2), - domestic: Math.min(2, selectedProfile.sectionCardLimits?.domestic ?? 2), - google: 1, - finance: 2, - tech: 2, - sports: 1, - technews: 0, - hot: 0, - }, + maxBodyChars: selectedProfile.maxBodyChars ?? maxBody, + sectionCardLimits: limits, }; built = buildWechatInlineContent(html, { publicUrl,