From 84b38f397e7d78213abe1b4e004561df3fa7a439 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 10 Sep 2026 09:28:04 +0800 Subject: [PATCH] fix(wechat): add fallback trim profiles for 20k content limit --- wechat-daily-news-inline.mjs | 41 ++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/wechat-daily-news-inline.mjs b/wechat-daily-news-inline.mjs index ce1dd5e..f3c9b96 100644 --- a/wechat-daily-news-inline.mjs +++ b/wechat-daily-news-inline.mjs @@ -1,7 +1,7 @@ import { extractPageTitle } from './wechat/verify/share-preview-repair.mjs'; const MAX_WECHAT_CONTENT_CHARS = 20000; -const MAX_WECHAT_CONTENT_TARGET_CHARS = 19800; +const MAX_WECHAT_CONTENT_TARGET_CHARS = 19600; const CARD_VARIANTS = { 'highlight-box': { @@ -221,6 +221,33 @@ const TRIM_PROFILES = [ }, ]; +function buildFallbackTrimProfiles() { + const profiles = []; + for (let headlines = 3; headlines >= 2; headlines -= 1) { + for (let maxBodyChars = 56; maxBodyChars >= 24; maxBodyChars -= 8) { + for (const skipWeather of [false, true]) { + profiles.push({ + skipHistory: true, + skipWeather, + sectionCardLimits: { + headlines, + world: 2, + domestic: 2, + google: 1, + finance: 2, + tech: 2, + sports: 1, + technews: 0, + hot: 0, + }, + maxBodyChars, + }); + } + } + } + return profiles; +} + function resolveSectionCardLimit(section, sectionCardLimits) { if (!sectionCardLimits) return null; return sectionCardLimits[section.id] ?? 3; @@ -238,7 +265,9 @@ function buildWechatInlineContent(html, { publicUrl = '', trimProfile = TRIM_PRO continue; } if (section.id === 'weather') { - parts.push(renderWeatherSection(section.html)); + if (!trimProfile.skipWeather) { + parts.push(renderWeatherSection(section.html)); + } continue; } parts.push(renderGenericSection(section.html, { @@ -318,10 +347,13 @@ export function convertDailyNewsHtmlToWechatInlineArticle(html, { publicUrl = '' const digest = (extractMetaDescription(html) || title).slice(0, 120); let built = null; let trimLevel = 0; + let usedFallback = false; + const allProfiles = [...TRIM_PROFILES, ...buildFallbackTrimProfiles()]; - for (const trimProfile of TRIM_PROFILES) { + for (const [index, trimProfile] of allProfiles.entries()) { built = buildWechatInlineContent(html, { publicUrl, trimProfile }); - trimLevel = TRIM_PROFILES.indexOf(trimProfile); + trimLevel = index; + usedFallback = index >= TRIM_PROFILES.length; if (built.content.length <= MAX_WECHAT_CONTENT_TARGET_CHARS) { break; } @@ -344,6 +376,7 @@ export function convertDailyNewsHtmlToWechatInlineArticle(html, { publicUrl = '' renderedSectionCount: built.renderedSectionCount, trimLevel, trimmed: trimLevel > 0, + usedFallbackTrim: usedFallback, }; }