Update public-api.js via upload script - 2026-10-27 15:55:49

This commit is contained in:
mike 2026-10-27 15:55:48 +08:00
parent 877113e211
commit 7786aed53e
1 changed files with 38 additions and 1 deletions

View File

@ -228,12 +228,43 @@ function htmlSegments(h) {
return out;
}
const tplHtmlOf = (t) => (t && String(t.html || '').trim() ? cleanTplHtml(t.html) : '');
// 详情图片里的 HTML 片段(原样显示,网站上放在隔离的 iframe 里):detailHtml = [{ id, html, pos }],pos = 排在第几张图后面(0 = 最上面)
const tplDetailBlocks = (t) => arr(t && t.detailHtml).filter((b) => b && String(b.html || '').trim());
function tplDetailSeq(t) {
const imgs = tplImagesOf(t), blocks = tplDetailBlocks(t);
const out = [];
for (let i = 0; i <= imgs.length; i++) {
for (const b of blocks) if (Math.min(Math.max(0, Math.round(+b.pos || 0)), imgs.length) === i) out.push({ html: String(b.html).replace(/\s*\[cite:[^\]]*\]/gi, '') });
if (i < imgs.length) out.push({ img: imgs[i] });
}
return out;
}
// 原样 HTML 的翻译:只翻译正文文字,<style> / <script> / <head> 里的内容原样保留
function rawHtmlParts(h) {
return String(h).split(/(<(?:style|script|head|svg)[\s\S]*?<\/(?:style|script|head|svg)>)/i);
}
function rawHtmlSegments(h) {
const out = [];
rawHtmlParts(h).forEach((part, i) => { if (i % 2 === 0) out.push(...htmlSegments(part)); });
return out;
}
function translateRawHtml(h, dict) {
return rawHtmlParts(h).map((part, i) => (i % 2 ? part : part.replace(/>([^<]+)</g, (m, raw) => {
const x = raw.trim(); const v = x && dict[x];
return v ? '>' + raw.replace(x, v.replace(/</g, '‹').replace(/>/g, '›')) + '<' : m;
}))).join('');
}
async function translateTemplateHtml(ai, products) {
const segs = [];
for (const p of products) for (const t of p.templateHtml || []) segs.push(...htmlSegments(t.html.en));
for (const p of products) for (const x of p.templateDetail || []) if (x.html) segs.push(...rawHtmlSegments(x.html.en));
if (!segs.length) return;
const tr = ai ? await translateStrings(ai, segs) : {};
for (const p of products) {
for (const x of p.templateDetail || []) {
if (!x.html) continue;
for (const lang of Object.keys(WEB_TRANSLATE_LANGS)) x.html[lang] = translateRawHtml(x.html.en, tr[lang] || {});
}
for (const t of p.templateHtml || []) {
for (const lang of Object.keys(WEB_TRANSLATE_LANGS)) {
const dict = tr[lang] || {};
@ -257,7 +288,7 @@ function descFacts(p) {
if (TYPE_EN[p.type]) f.type = TYPE_EN[p.type];
if ((p.type === '机架式UPS' || p.type === '机架式锂电UPS') && p.machineVariant) f.variant = /长/.test(p.machineVariant) ? 'Long-run model: no internal batteries, connects to an external battery bank' : 'Standard model with internal batteries';
for (const [k, v] of Object.entries(p)) {
if (DESC_SKIP.has(k) || k.startsWith('_') || ['slug', 'images', 'detailImages', 'templateImages', 'templateHtml', 'specI18n'].includes(k)) continue;
if (DESC_SKIP.has(k) || k.startsWith('_') || ['slug', 'images', 'detailImages', 'templateImages', 'templateDetail', 'templateHtml', 'specI18n'].includes(k)) continue;
const en = p.specI18n && p.specI18n[k] && p.specI18n[k].en;
if (en) { f[k] = en; continue; }
if (typeof v === 'string' && v.trim() && !hasZh(v)) f[k] = v.trim();
@ -455,6 +486,12 @@ function toPublic(p, templates = [], packSpecs = []) {
const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t)
.map((f) => `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(f)}`));
if (tplUrls.length) out.templateImages = tplUrls;
// 有 HTML 片段时,额外给出"图片 + HTML"的完整顺序(网站按这个顺序显示;没有 HTML 片段的模板不输出,网站沿用 templateImages)
if (templatesOf(p, templates).some((t) => tplDetailBlocks(t).length)) {
out.templateDetail = templatesOf(p, templates).flatMap((t) => tplDetailSeq(t).map((x) => (x.img
? { img: `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(x.img)}` }
: { html: { en: x.html } })));
}
// HTML 模板(系列参数表等):显示在详情图下方
const tplHtml = templatesOf(p, templates).map((t) => ({ id: t.id, html: { en: tplHtmlOf(t) } })).filter((x) => x.html.en);
if (tplHtml.length) out.templateHtml = tplHtml;