forked from unimatrix/web
41 lines
4.8 KiB
JavaScript
41 lines
4.8 KiB
JavaScript
// Shared safe news grammar for editorial preview, website and native delivery.
|
||
const esc=v=>String(v).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||
const bb=v=>String(v).replaceAll('<','<').replaceAll('>','>').replaceAll('[','[').replaceAll(']',']');
|
||
export function newsLink(value){try{const u=new URL(value);return u.protocol==='https:'&&!u.username&&!u.password?u.href:null;}catch{return null;}}
|
||
export function newsImage(value){const u=newsLink(value);return u&&/^https:\/\/www\.dunerealms\.com\/assets\/(?:[a-z0-9_-]+\/)*[a-z0-9_-]+\.(?:png|webp|jpe?g)$/i.test(u)?u:null;}
|
||
export function validateNewsContent(value){
|
||
if(typeof value!=='string'||value.trim().length<10||value.length>12000||/[\x00-\x08\x0b\x0c\x0e-\x1f]/.test(value))throw Error('Use article content of 10–12,000 characters.');
|
||
if(/<\s*\/?[a-z][^>]*>/i.test(value))throw Error('Use the formatting controls instead of HTML.');
|
||
for(const m of value.matchAll(/(!?)\[([^\]\n]*)\]\(([^\n)]*)\)/g))if(!m[2].trim()||m[2].length>200||!(m[1]?newsImage(m[3]):newsLink(m[3])))throw Error(m[1]?'Images need alt text and an HTTPS image from the DuneRealms assets folder.':'Links need a label and a valid HTTPS address without login details.');
|
||
return value.replace(/\r\n?/g,'\n').trim();
|
||
}
|
||
function inline(value,mode){
|
||
const text=mode==='html'?esc:mode==='bbcode'?bb:v=>v;let out='',from=0;
|
||
for(const m of value.matchAll(/\[([^\]\n]{1,200})\]\(([^\s)]+)\)|\*\*([^*\n]+)\*\*|\*([^*\n]+)\*|`([^`\n]+)`/g)){
|
||
out+=text(value.slice(from,m.index));const url=m[1]&&newsLink(m[2]);
|
||
if(m[1])out+=url?(mode==='html'?'<a class="dispatch-read-more" href="'+esc(url)+'" rel="noopener noreferrer">'+text(m[1])+'</a>':mode==='bbcode'?'[url='+bb(url)+']'+text(m[1])+'[/url]':m[1]):text(m[0]);
|
||
else{const content=m[3]||m[4]||m[5],tag=m[3]?'strong':m[4]?'em':'code',btag=m[3]?'b':m[4]?'i':'font=monospace';out+=mode==='html'?'<'+tag+'>'+text(content)+'</'+tag+'>':mode==='bbcode'?'['+btag+']'+text(content)+'[/'+btag.split('=')[0]+']':content;}
|
||
from=m.index+m[0].length;
|
||
}return out+text(value.slice(from));
|
||
}
|
||
export function newsBlocks(value){
|
||
const blocks=[];let paragraph=[],list=null;const flush=()=>{if(paragraph.length){blocks.push({kind:'paragraph',text:paragraph.join(' ')});paragraph=[];}list=null;};
|
||
for(const raw of String(value).replace(/\r/g,'').split('\n')){
|
||
const line=raw.trim();if(!line){flush();continue;}
|
||
const image=line.match(/^!\[([^\]\n]{1,200})\]\(([^\s)]+)\)$/),video=line.match(/^\[([^\]\n]{1,200})\]\((https:\/\/www\.dunerealms\.com\/assets\/video\/[a-z0-9-]+\.mp4)\)$/i);
|
||
if(image&&newsImage(image[2])){flush();blocks.push({kind:'image',alt:image[1],url:newsImage(image[2])});continue;}
|
||
if(video){flush();blocks.push({kind:'video',alt:video[1],url:video[2]});continue;}
|
||
const heading=line.match(/^#{1,4}\s+(.+)$/),bullet=line.match(/^(?:([-*•])|(\d+)[.)])\s+(.+)$/);
|
||
if(heading){flush();blocks.push({kind:'heading',text:heading[1]});}
|
||
else if(bullet){if(paragraph.length)flush();const kind=bullet[2]?'ordered':'unordered';if(!list||list.kind!==kind){list={kind,items:[]};blocks.push(list);}list.items.push(bullet[3]);}
|
||
else{list=null;paragraph.push(line);}
|
||
}flush();return blocks;
|
||
}
|
||
export function renderNewsBody(value){return newsBlocks(value).map(b=>{
|
||
if(b.kind==='image')return '<link rel="stylesheet" href="/news-images.css?v=20261108"><figure class="article-picture'+(b.url.includes('/seals/')?' article-seal':'')+'"><img loading="lazy" src="'+esc(b.url)+'" alt="'+esc(b.alt)+'"><figcaption>'+esc(b.alt)+'</figcaption></figure>';
|
||
if(b.kind==='video')return '<link rel="stylesheet" href="/news-video.css?v=20261107"><figure class="article-video"><video controls autoplay muted loop playsinline preload="metadata" aria-label="'+esc(b.alt)+'" poster="'+esc(b.url.replace(/\.mp4$/i,'-poster.png'))+'"><source src="'+esc(b.url)+'" type="video/mp4">Your browser does not support embedded video.</video><figcaption>Enable sound in the player for the original epic soundtrack.</figcaption></figure>';
|
||
if(b.items){const tag=b.kind==='ordered'?'ol':'ul';return '<'+tag+'>'+b.items.map(t=>'<li>'+inline(t,'html')+'</li>').join('')+'</'+tag+'>';}
|
||
const tag=b.kind==='heading'?'h3':'p';return '<'+tag+'>'+inline(b.text,'html')+'</'+tag+'>';
|
||
}).join('');}
|
||
export function newsBBCode(value){return newsBlocks(value).map(b=>b.kind==='image'?'[img]'+bb(b.url)+'[/img]\n'+bb(b.alt):b.kind==='video'?'[url='+bb(b.url)+']'+bb(b.alt)+'[/url]':b.items?'[list'+(b.kind==='ordered'?'=1':'')+']\n'+b.items.map(t=>'[*]'+inline(t,'bbcode')).join('\n')+'\n[/list]':b.kind==='heading'?'[b]'+inline(b.text,'bbcode')+'[/b]':inline(b.text,'bbcode')).join('\n\n');}
|
||
export function newsPlainText(value){return newsBlocks(value).map(b=>b.alt||b.items?.map(t=>inline(t,'text')).join(' ')||inline(b.text,'text')).join(' ').replace(/<[^>]*>/g,'').replace(/\s+/g,' ').trim();}
|

Staff