news-page.js (2068B)
1 /** 2 * Carrega notícias de data/news.json (gerado por site/news/publish_news.py). 3 */ 4 (function () { 5 async function run() { 6 const root = document.getElementById("news-feed"); 7 const empty = document.getElementById("news-empty"); 8 if (!root) return; 9 10 try { 11 const r = await fetch("data/news.json", { cache: "no-store" }); 12 if (!r.ok) throw new Error("news.json indisponível"); 13 const data = await r.json(); 14 const articles = Array.isArray(data.articles) ? data.articles : []; 15 if (articles.length === 0) { 16 if (empty) { 17 empty.hidden = false; 18 empty.textContent = 19 "Ainda não há entradas publicadas. Quando houver, aparecem aqui em destaque."; 20 } 21 return; 22 } 23 if (empty) empty.hidden = true; 24 25 const frag = document.createDocumentFragment(); 26 for (const a of articles) { 27 const art = document.createElement("article"); 28 art.className = "news-card"; 29 if (a.id) art.id = "post-" + a.id; 30 31 const head = document.createElement("header"); 32 head.className = "news-card__head"; 33 34 const time = document.createElement("time"); 35 time.className = "news-card__date"; 36 if (a.w3c_published) time.dateTime = a.w3c_published; 37 time.textContent = a.date || ""; 38 39 const h2 = document.createElement("h2"); 40 h2.className = "news-card__title"; 41 h2.textContent = a.title || ""; 42 43 head.appendChild(time); 44 head.appendChild(h2); 45 46 const body = document.createElement("div"); 47 body.className = "news-card__body prose-news"; 48 body.innerHTML = a.body_html || ""; 49 50 art.appendChild(head); 51 art.appendChild(body); 52 frag.appendChild(art); 53 } 54 root.appendChild(frag); 55 } catch (_e) { 56 if (empty) { 57 empty.hidden = false; 58 empty.textContent = 59 "Não foi possível carregar a lista (ficheiro data/news.json ausente ou indisponível). Use o feed RSS ou tente mais tarde."; 60 } 61 } 62 } 63 64 run(); 65 })();