/* The article body and its images come entirely from the published local revision. */ window.NewsArticleView = (() => { const make=(tag,text='')=>{const node=document.createElement(tag);node.textContent=text;return node}; function blocks(body){ const out=[];let lines=[],section=0; const flush=()=>{if(lines.length){out.push({tag:'p',text:lines.join('\n'),section});lines=[]}}; for(const line of (body||'').split('\n')){ if(line.startsWith('## ')){flush();section++;out.push({tag:'h2',text:line.slice(3).trim(),section})} else if(!line.trim())flush();else lines.push(line); } flush();return out; } function render(target,item,base){ const used=new Set(); if(item.attributes?.news_article_format!=='article-v2')return null; const article=make('div');article.className='news-article'; const specs=Array.isArray(item.attributes.news_article_images)?item.attributes.news_article_images:[]; const media=new Map((item.media||[]).map(asset=>[asset.id,asset]));const inserted=new Set(); function figures(section){ specs.forEach((spec,i)=>{ if(inserted.has(i)||Number(spec.section||0)!==section)return; inserted.add(i); if(!/^[a-f0-9-]{36}$/i.test(spec.asset_id||'')||!media.has(spec.asset_id))return; const asset=media.get(spec.asset_id);used.add(spec.asset_id); if(spec.asset_id===item.cover_asset_id)return; const figure=make('figure'),img=make('img'),caption=make('figcaption',spec.caption||''); img.src=base+'api/media/'+spec.asset_id;img.alt=spec.caption||'文章配图'; img.loading='lazy';img.decoding='async'; img.width=Number(asset.extraction?.width)||1200;img.height=Number(asset.extraction?.height)||675; img.addEventListener('error',()=>{img.hidden=true;caption.textContent=(spec.caption||'文章配图')+'(图片暂不可用)'},{once:true}); figure.append(img,caption);article.append(figure); }); } const sections=new Set(); for(const block of blocks(item.body)){ article.append(make(block.tag,block.text)); if(block.tag==='p'&&!sections.has(block.section)){figures(block.section);sections.add(block.section)} } specs.forEach(spec=>figures(Number(spec.section||0))); target.append(article);return used; } return {render,blocks}; })();