Update public-api.js via upload script - 2026-11-10 17:46:42

This commit is contained in:
mike 2026-11-10 17:46:54 +08:00
parent 8b103f7c26
commit 586553713c
1 changed files with 10 additions and 4 deletions

View File

@ -660,7 +660,9 @@ const attrsOf = (s) => { const a = {}; String(s || '').replace(/([\w:-]+)\s*(?:=
function parseHtml(html) { function parseHtml(html) {
const root = { tag: '#root', attrs: {}, kids: [] }; const root = { tag: '#root', attrs: {}, kids: [] };
const stack = [root]; const stack = [root];
const re = /<!--[\s\S]*?-->|<(script|style)\b[^>]*>[\s\S]*?<\/\1>|<\/([a-zA-Z][\w-]*)\s*>|<([a-zA-Z][\w-]*)([^>]*?)(\/?)>|([^<]+)/g; // 标签名允许带冒号:从 Word 粘贴到公众号的文章里有 <o:p></o:p>、<w:sdt> 这类标签,以前没认出来,"o:p>" 会漏进正文
// <!DOCTYPE>、<?xml:namespace …> 直接跳过;落单的 "<"(比如正文里的 "电压<10V")按普通文字保留
const re = /<!--[\s\S]*?-->|<(script|style)\b[^>]*>[\s\S]*?<\/\1>|<[!?][^>]*>|<\/([a-zA-Z][\w:.-]*)\s*>|<([a-zA-Z][\w:.-]*)([^>]*?)(\/?)>|([^<]+)|</g;
let m; let m;
while ((m = re.exec(html))) { while ((m = re.exec(html))) {
const top = stack[stack.length - 1]; const top = stack[stack.length - 1];
@ -673,6 +675,7 @@ function parseHtml(html) {
top.kids.push(node); top.kids.push(node);
if (!VOID_TAGS.has(t) && !m[5]) stack.push(node); if (!VOID_TAGS.has(t) && !m[5]) stack.push(node);
} else if (m[6]) top.kids.push({ tag: '#text', text: decodeEnt(m[6]) }); } else if (m[6]) top.kids.push({ tag: '#text', text: decodeEnt(m[6]) });
else if (m[0] === '<') top.kids.push({ tag: '#text', text: '<' });
} }
return root; return root;
} }
@ -689,13 +692,16 @@ function sliceById(html, id) {
const jsVar = (html, name) => { const m = html.match(new RegExp(`var\\s+${name}\\s*=\\s*(?:htmlDecode\\()?["']([^"']*)["']`)); return m ? decodeEnt(m[1]) : ''; }; const jsVar = (html, name) => { const m = html.match(new RegExp(`var\\s+${name}\\s*=\\s*(?:htmlDecode\\()?["']([^"']*)["']`)); return m ? decodeEnt(m[1]) : ''; };
const metaOf = (html, prop) => { const m = html.match(new RegExp(`<meta[^>]+(?:property|name)=["']${prop}["'][^>]+content=["']([^"']*)["']`, 'i')) || html.match(new RegExp(`<meta[^>]+content=["']([^"']*)["'][^>]+(?:property|name)=["']${prop}["']`, 'i')); return m ? decodeEnt(m[1]) : ''; }; const metaOf = (html, prop) => { const m = html.match(new RegExp(`<meta[^>]+(?:property|name)=["']${prop}["'][^>]+content=["']([^"']*)["']`, 'i')) || html.match(new RegExp(`<meta[^>]+content=["']([^"']*)["'][^>]+(?:property|name)=["']${prop}["']`, 'i')); return m ? decodeEnt(m[1]) : ''; };
// Office 标签残留(o:p> /o:p> <w:…> 等):解析器已修好;这里再兜底清理一次,旧数据发布时也会被清掉
const OFFICE_JUNK = /<?\/?(?:o|w|v|m|st\d+):[a-zA-Z]+[^>\n]{0,80}?>/g;
const cleanText = (s) => String(s || '').replace(OFFICE_JUNK, '').replace(/[ \t]{2,}/g, ' ').trim();
// 正文树 → 块。文字按块级元素分段;图片单独成块;连续图片合并成两图 / 图集 // 正文树 → 块。文字按块级元素分段;图片单独成块;连续图片合并成两图 / 图集
const NOISE = /^(点击|长按|扫码|识别二维码|关注我们|欢迎关注|点个在看|阅读原文|▼|▲|↓|END$|end$)|^[—\-·•\s]+$|^(公众号|微信号|往期推荐|往期回顾|推荐阅读|分享|收藏|点赞|在看)[::]?$|关注我们$|二维码/; const NOISE = /^(点击|长按|扫码|识别二维码|关注我们|欢迎关注|点个在看|阅读原文|▼|▲|↓|END$|end$)|^[—\-·•\s]+$|^(公众号|微信号|往期推荐|往期回顾|推荐阅读|分享|收藏|点赞|在看)[::]?$|关注我们$|二维码/;
function toBlocks(root) { function toBlocks(root) {
const out = []; const out = [];
let buf = '', bold = 0, all = 0, inQuote = false, center = false; let buf = '', bold = 0, all = 0, inQuote = false, center = false;
const flush = (forceTag) => { const flush = (forceTag) => {
const text = buf.replace(/[ \t\r\f\v]+/g, ' ').replace(/\s*\n\s*/g, '\n').trim(); const text = buf.replace(OFFICE_JUNK, '').replace(/[ \t\r\f\v]+/g, ' ').replace(/\s*\n\s*/g, '\n').trim();
if (text && !NOISE.test(text)) { if (text && !NOISE.test(text)) {
const isHead = forceTag === 'h' || (all > 0 && bold / all > 0.85 && text.length <= 40 && !/[。!?.!?]$/.test(text)); const isHead = forceTag === 'h' || (all > 0 && bold / all > 0.85 && text.length <= 40 && !/[。!?.!?]$/.test(text));
out.push({ t: forceTag === 'quote' || inQuote ? 'quote' : isHead ? 'h' : 'p', zh: text, ...(center && text.length <= 50 ? { center: true } : {}) }); out.push({ t: forceTag === 'quote' || inQuote ? 'quote' : isHead ? 'h' : 'p', zh: text, ...(center && text.length <= 50 ? { center: true } : {}) });
@ -821,7 +827,7 @@ async function translateAny(ai, strings) {
} }
const NEWS_CATS = ['projects', 'products', 'events', 'company']; const NEWS_CATS = ['projects', 'products', 'events', 'company'];
const newsSlugify = (s) => String(s || '').toLowerCase().normalize('NFKD').replace(/[^\w\s-]/g, '').trim().replace(/[\s_]+/g, '-').replace(/-+/g, '-').slice(0, 70).replace(/-$/, ''); const newsSlugify = (s) => String(s || '').toLowerCase().normalize('NFKD').replace(/[^\w\s-]/g, '').trim().replace(/[\s_]+/g, '-').replace(/-+/g, '-').slice(0, 70).replace(/-$/, '');
const srcText = (o) => String((o && (o.zh || o.en)) || '').trim(); const srcText = (o) => cleanText((o && (o.zh || o.en)) || '');
const articleTexts = (n) => [srcText(n.title), srcText(n.excerpt), srcText(n.place), const articleTexts = (n) => [srcText(n.title), srcText(n.excerpt), srcText(n.place),
...(Array.isArray(n.blocks) ? n.blocks : []).flatMap((b) => [srcText(b), srcText(b.cap), ...(Array.isArray(b.caps) ? b.caps.map(srcText) : [])])]; ...(Array.isArray(n.blocks) ? n.blocks : []).flatMap((b) => [srcText(b), srcText(b.cap), ...(Array.isArray(b.caps) ? b.caps.map(srcText) : [])])];
const articleFiles = (n) => [n.cover, ...(Array.isArray(n.blocks) ? n.blocks : []).flatMap((b) => (b.file ? [b.file] : Array.isArray(b.files) ? b.files : []))].filter(Boolean).map(String); const articleFiles = (n) => [n.cover, ...(Array.isArray(n.blocks) ? n.blocks : []).flatMap((b) => (b.file ? [b.file] : Array.isArray(b.files) ? b.files : []))].filter(Boolean).map(String);
@ -953,7 +959,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
const at = await translateAny(ai, arts.flatMap(articleTexts)); const at = await translateAny(ai, arts.flatMap(articleTexts));
const L = (o) => { const L = (o) => {
const src = srcText(o); const r = {}; const src = srcText(o); const r = {};
for (const lang of Object.keys(ZH_TARGETS)) r[lang] = String((o && lang !== 'zh' && o[lang]) || '').trim() || (src && at[lang][src]) || (hasZh(src) ? '' : src); for (const lang of Object.keys(ZH_TARGETS)) r[lang] = cleanText((o && lang !== 'zh' && o[lang]) || '') || (src && at[lang][src]) || (hasZh(src) ? '' : src);
return r; return r;
}; };
const usedSlugs = new Set(); const usedSlugs = new Set();