MC/website/node/news-format.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

8 lines
2.2 KiB
JavaScript

const esc=v=>String(v).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
function inline(text){let result='',from=0;for(const m of text.matchAll(/\[([^\]\n]{1,200})\]\((https:\/\/[^\s)]+)\)/g)){result+=esc(text.slice(from,m.index));let url;try{url=new URL(m[2]);}catch{}result+=url?.protocol==='https:'?'<a class="dispatch-read-more" href="'+esc(url.href)+'" rel="noopener noreferrer">'+esc(m[1])+'</a>':esc(m[0]);from=m.index+m[0].length;}return result+esc(text.slice(from));}
export function renderNewsBody(value){let html='',paragraph=[],list=false;const flush=()=>{if(paragraph.length){html+='<p>'+inline(paragraph.join(' '))+'</p>';paragraph=[];}if(list){html+='</ul>';list=false;}};for(const raw of String(value).replace(/\r/g,'').split('\n')){const line=raw.trim();if(!line){flush();continue;}
const picture=line.match(/^!\[([^\]\n]{1,200})\]\((https:\/\/www\.dunerealms\.com\/assets\/(?:seals\/)?[a-z0-9-]+\.(?:png|webp|jpg))\)$/i);
if(picture){flush();const seal=picture[2].includes('/seals/');html+='<link rel="stylesheet" href="/news-images.css?v=20261108"><figure class="article-picture'+(seal?' article-seal':'')+'"><img loading="lazy" src="'+esc(picture[2])+'" alt="'+esc(picture[1])+'"><figcaption>'+esc(picture[1])+'</figcaption></figure>';continue;}
const video=line.match(/^\[([^\]\n]{1,200})\]\((https:\/\/www\.dunerealms\.com\/assets\/video\/[a-z0-9-]+\.mp4)\)$/i);
if(video){flush();html+='<link rel="stylesheet" href="/news-video.css?v=20261107"><figure class="article-video"><video controls autoplay muted loop playsinline preload="metadata" aria-label="'+esc(video[1])+'" poster="'+esc(video[2].replace(/\.mp4$/i,'-poster.png'))+'"><source src="'+esc(video[2])+'" type="video/mp4">Your browser does not support embedded video.</video><figcaption>Enable sound in the player for the original epic soundtrack.</figcaption></figure>';continue;}
const heading=line.match(/^#{1,3}\s+(.+)$/),bullet=line.match(/^[-*]\s+(.+)$/);if(heading){flush();html+='<h3>'+inline(heading[1])+'</h3>';}else if(bullet){if(paragraph.length)flush();if(!list){html+='<ul>';list=true;}html+='<li>'+inline(bullet[1])+'</li>';}else{if(list)flush();paragraph.push(line);}}flush();return html;}