Build Markdown news site with local AI generation and web push
This commit is contained in:
1 parent
ad30f12116
commit
199dec8a1e
26 files changed
+2277
-1
No files matched your search
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><rect width="40" height="40" rx="9" fill="#183f34"/><text x="8" y="30" fill="#fff" font-family="Georgia" font-size="34">t</text><circle cx="30" cy="29" r="4" fill="#daff7b"/></svg>
|
||||
|
After Width: | Height: | Size: 241 B |
Binary file not shown.
|
After Width: | Height: | Size: 939 B |
Binary file not shown.
|
After Width: | Height: | Size: 4.4 KiB |
@@ -0,0 +1 @@
|
||||
{"id":"/","name":"TrueNews","short_name":"TrueNews","start_url":"/","scope":"/","display":"standalone","background_color":"#f8f8f3","theme_color":"#214d3e","icons":[{"src":"/assets/icon-192.png","sizes":"192x192","type":"image/png"},{"src":"/assets/icon-512.png","sizes":"512x512","type":"image/png"}]}
|
||||
@@ -0,0 +1,73 @@
|
||||
(async()=>{
|
||||
const panel=document.querySelector('#push-prompt');
|
||||
const toggle=document.querySelector('#push-toggle');
|
||||
const enable=document.querySelector('#push-enable');
|
||||
const dismiss=document.querySelector('#push-dismiss');
|
||||
const message=document.querySelector('#push-message');
|
||||
const status=document.querySelector('#push-status');
|
||||
let registration;
|
||||
let config;
|
||||
let subscription;
|
||||
const remember=()=>{try{localStorage.setItem('truenews-push-dismissed',String(Date.now()));}catch{}};
|
||||
const dismissed=()=>{try{return Date.now()-Number(localStorage.getItem('truenews-push-dismissed')||0)<30*86400000;}catch{return false;}};
|
||||
const api=async(action,value)=>{
|
||||
const response=await fetch(`/api/push/${action}`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(value)});
|
||||
if(!response.ok)throw new Error('Could not save your preference. Please try again.');
|
||||
};
|
||||
const show=()=>{panel.hidden=false;};
|
||||
const update=()=>{
|
||||
toggle.textContent=subscription?'Notifications on':'Get new stories';
|
||||
enable.textContent=subscription?'Turn off notifications':'Enable notifications';
|
||||
message.textContent=subscription?'You’ll get a notification when a new article is published.':'Get a browser notification when a new story is published. You can turn this off anytime.';
|
||||
};
|
||||
try {
|
||||
const response=await fetch('/api/push/config');
|
||||
if(!response.ok)return;
|
||||
config=await response.json();
|
||||
if(!config.enabled)return;
|
||||
toggle.hidden=false;
|
||||
const supported=window.isSecureContext && 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window;
|
||||
if(!supported){
|
||||
const explain=()=>{
|
||||
show();enable.hidden=true;
|
||||
const apple=/iPad|iPhone|iPod/.test(navigator.userAgent)||(navigator.platform==='MacIntel'&&navigator.maxTouchPoints>1);
|
||||
message.textContent=apple?'On iPhone or iPad, add TrueNews to your Home Screen, open it from there, then enable notifications.':'Notifications need a supported browser and a secure HTTPS connection.';
|
||||
};
|
||||
toggle.addEventListener('click',explain);
|
||||
if((/iPad|iPhone|iPod/.test(navigator.userAgent)||(navigator.platform==='MacIntel'&&navigator.maxTouchPoints>1))&&!dismissed())explain();
|
||||
dismiss.addEventListener('click',()=>{panel.hidden=true;remember();});
|
||||
return;
|
||||
}
|
||||
registration=await navigator.serviceWorker.register('/sw.js');
|
||||
await navigator.serviceWorker.ready;
|
||||
subscription=await registration.pushManager.getSubscription();
|
||||
if(subscription && Notification.permission==='granted') {
|
||||
// Restore the server record if a device revisits after a transient failure.
|
||||
await api('subscribe',subscription.toJSON()).catch(()=>{status.textContent='Could not sync notifications. You can retry or turn them off below.';});
|
||||
}
|
||||
update();
|
||||
toggle.addEventListener('click',()=>{status.textContent='';update();show();});
|
||||
dismiss.addEventListener('click',()=>{panel.hidden=true;remember();});
|
||||
enable.addEventListener('click',async()=>{
|
||||
enable.disabled=true;status.textContent='';
|
||||
try {
|
||||
if(subscription){
|
||||
await api('unsubscribe',subscription.toJSON());
|
||||
await subscription.unsubscribe();subscription=null;remember();update();status.textContent='Notifications turned off.';
|
||||
}else{
|
||||
// Permission must be requested directly from this user gesture.
|
||||
const permission=await Notification.requestPermission();
|
||||
if(permission!=='granted'){
|
||||
remember();status.textContent=permission==='denied'?'Notifications are blocked. You can change this in your browser’s site settings.':'No problem. You can enable notifications here later.';return;
|
||||
}
|
||||
const key=Uint8Array.from(atob(config.publicKey.replace(/-/g,'+').replace(/_/g,'/')),c=>c.charCodeAt(0));
|
||||
const created=await registration.pushManager.subscribe({userVisibleOnly:true,applicationServerKey:key});
|
||||
try {await api('subscribe',created.toJSON());}catch(error){await created.unsubscribe();throw error;}
|
||||
subscription=created;update();status.textContent='You’re subscribed. Watch for the next story.';
|
||||
}
|
||||
}catch(error){status.textContent=error.message || 'Could not update notifications. Please try again.';}
|
||||
finally{enable.disabled=false;}
|
||||
});
|
||||
if(!subscription && Notification.permission==='default' && !dismissed()) show();
|
||||
}catch{toggle.hidden=true;panel.hidden=true;}
|
||||
})();
|
||||
@@ -0,0 +1,22 @@
|
||||
const search = document.querySelector('#search');
|
||||
let category = 'all';
|
||||
function filter() {
|
||||
const term = search.value.trim().toLowerCase();
|
||||
let count = 0;
|
||||
document.querySelectorAll('.latest .story').forEach(card => {
|
||||
card.hidden = !(category === 'all' || card.dataset.category === category) || !card.dataset.search.includes(term);
|
||||
if (!card.hidden) count++;
|
||||
});
|
||||
document.querySelector('#count').textContent = `${count} ${count === 1 ? 'story' : 'stories'}`;
|
||||
document.querySelector('#results-status').textContent = `${count} stories found`;
|
||||
document.querySelector('#no-results').hidden = count !== 0;
|
||||
}
|
||||
if (search) {
|
||||
search.addEventListener('input', filter);
|
||||
document.querySelectorAll('[data-filter]').forEach(button => button.addEventListener('click', () => {
|
||||
category = button.dataset.filter;
|
||||
document.querySelectorAll('[data-filter]').forEach(b => {b.classList.toggle('selected', b === button);b.setAttribute('aria-pressed',String(b === button));});
|
||||
filter();
|
||||
}));
|
||||
document.addEventListener('keydown', e => {if(e.key === '/' && !['INPUT','TEXTAREA'].includes(document.activeElement.tagName)){e.preventDefault();search.focus();}});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Playfair+Display:ital,wght@0,500;0,600;0,700;1,500&display=swap');
|
||||
:root{--paper:#f8f8f3;--ink:#202c27;--green:#214d3e;--muted:#6b706b;--line:#dddfd5;--lime:#d9efae}*{box-sizing:border-box}html{scroll-behavior:smooth;scroll-padding-top:24px}body{margin:0;background:var(--paper);color:var(--ink);font-family:'DM Sans',Arial,sans-serif;font-size:15px}a{color:inherit;text-decoration:none}a:hover{color:var(--green)}button,input{font:inherit}button,a,input{-webkit-tap-highlight-color:transparent}a:focus-visible,button:focus-visible,input:focus-visible{outline:3px solid #709444;outline-offset:5px}.wrap{width:min(1240px,calc(100% - 96px));margin-inline:auto}.masthead{display:flex;align-items:center;gap:32px;padding-block:30px}.brand{font-size:39px;letter-spacing:-2px;font-weight:700;display:inline-flex;align-items:baseline}.brand span{font-weight:400}.brand i{width:8px;height:8px;background:#678b44;border-radius:50%;margin-left:3px}.masthead p{font-size:11px;line-height:1.65;border-left:1px solid var(--line);padding-left:28px;color:var(--muted)}.edition{margin-left:auto;font-size:12px}.edition span{margin-left:24px;font-size:20px}.nav{display:flex;align-items:center;gap:32px;border-block:1px solid var(--line);font-size:12px;height:56px}.nav a{height:100%;display:flex;align-items:center}.nav .active{border-bottom:2px solid var(--green);font-weight:700}.nav-note{margin-left:auto;color:var(--muted);font-size:11px}.nav-note b{display:inline-block;width:6px;height:6px;background:#739557;border-radius:50%;margin-right:8px}.intro{position:relative;padding:48px 0 38px}.eyebrow{font-size:10px;letter-spacing:1.7px;font-weight:700;color:var(--green);line-height:1.6}h1,h2,h3{font-family:'Playfair Display',Georgia,serif;font-weight:500;margin:0;letter-spacing:-1.5px}.intro h1{font-size:clamp(40px,4.6vw,62px);line-height:1.12;margin-top:14px}.intro h1 em{font-weight:500;color:var(--green)}.intro>p:last-child{position:absolute;right:0;bottom:39px;font-size:13px;line-height:1.9;color:var(--muted)}.lead{display:grid;grid-template-columns:1.13fr 1fr;background:#ecefe5;border:1px solid #e0e4d7;border-radius:5px;overflow:hidden}.visual{position:relative;aspect-ratio:1.65;background:#d2dbcd;overflow:hidden}.visual img{width:100%;height:100%;object-fit:cover;display:block;transition:transform .5s}.card-image:hover img,.lead-visual:hover img{transform:scale(1.035)}.hero-image{height:100%;min-height:390px}.image-label{position:absolute;left:18px;bottom:18px;padding:6px 10px;border-radius:3px;background:rgba(248,248,243,.9);font-size:9px;letter-spacing:1.2px;text-transform:uppercase}.lead-copy{padding:38px;display:flex;flex-direction:column;justify-content:center}.lead-copy .eyebrow{display:flex;align-items:center;gap:9px}.dot{width:6px;height:6px;background:var(--green);border-radius:50%}.tag{margin-left:auto;font-weight:400;letter-spacing:0;border:1px solid #cdd6c4;border-radius:30px;padding:3px 10px}.lead h2{font-size:clamp(30px,3vw,44px);line-height:1.16;margin:16px 0}.lead-copy>p:not(.eyebrow){font-size:13px;color:#61685e;line-height:1.8;margin:0}.lead-bottom{display:flex;align-items:center;justify-content:space-between;margin-top:25px}.meta{font-size:10px;line-height:1.8;color:var(--muted);letter-spacing:.15px}.meta span{margin:0 7px}.round-link{display:grid;place-items:center;width:39px;height:39px;border:1px solid #aab8a3;border-radius:50%;font-size:22px}.round-link:hover{background:var(--green);color:white}.latest{padding-top:56px}.section-top{display:flex;justify-content:space-between;align-items:center}.section-top h2,.related h2{font-size:34px;letter-spacing:-1px}.section-top h2 span{color:#779654}.search{display:flex;align-items:center;gap:10px;padding:10px 13px;border:1px solid var(--line);border-radius:4px;width:240px}.search>span{font-size:23px}.search input{width:100%;min-width:0;background:none;border:0;outline-offset:0;font-size:12px;color:var(--ink)}kbd{font-size:10px;border:1px solid var(--line);padding:2px 5px;color:var(--muted)}.filters{display:flex;flex-wrap:wrap;gap:8px;align-items:center;border-bottom:1px solid var(--line);padding:24px 0}.filters button{border:1px solid var(--line);background:transparent;color:#596353;font-size:11px;border-radius:30px;padding:9px 16px;cursor:pointer}.filters button.selected{background:var(--green);border-color:var(--green);color:white}.filters button:hover{border-color:var(--green)}#count{margin-left:auto;color:var(--muted);font-size:10px}.grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:34px 26px;padding-top:26px}.story[hidden]{display:none}.card-image{display:block;border-radius:4px;overflow:hidden}.story .eyebrow{margin-top:20px;margin-bottom:9px}.story h3{font-size:25px;letter-spacing:-.6px;line-height:1.25}.description{font-size:12px;line-height:1.85;color:var(--muted);margin-block:12px}.story .meta{margin-top:18px}.manifesto{display:flex;gap:32px;align-items:center;background:#e9eddf;border-block:1px solid #dbe1cc;margin-top:60px;padding:35px 40px;border-radius:4px}.asterisk{font-size:74px;color:#577447}.manifesto h2{fonLine truncated
|
||||
.push-toggle{background:none;border:1px solid var(--line);border-radius:20px;padding:8px 14px;color:var(--green);font-size:11px;cursor:pointer}.push-panel{position:fixed;z-index:20;right:24px;bottom:24px;width:min(370px,calc(100% - 36px));padding:24px;background:var(--paper);border:1px solid var(--line);border-radius:12px;box-shadow:0 10px 45px #20352926}.push-panel[hidden],.push-toggle[hidden]{display:none}.push-panel h2{font-size:26px;letter-spacing:-.5px}.push-panel p{font-size:13px;line-height:1.7;color:var(--muted)}.push-actions{display:flex;flex-wrap:wrap;gap:12px;align-items:center}.push-actions button{border:0;padding:10px 14px;border-radius:6px;cursor:pointer}.push-actions button:first-child{background:var(--green);color:white}.push-actions button:last-child{background:transparent;color:var(--muted)}.push-actions button:disabled{opacity:.6;cursor:wait}#push-status:empty{display:none}@media(max-width:600px){.push-panel{right:18px;bottom:18px}}
|
||||
@@ -0,0 +1,24 @@
|
||||
self.addEventListener('install', () => self.skipWaiting());
|
||||
self.addEventListener('activate', event => event.waitUntil(self.clients.claim()));
|
||||
self.addEventListener('push', event => {
|
||||
let message={};
|
||||
try {message=event.data?.json() || {};} catch { /* Show a generic notification for malformed payloads. */ }
|
||||
let url='/';
|
||||
try {const target=new URL(message.url || '/',self.location.origin);if(target.origin===self.location.origin)url=target.pathname+target.search;} catch {}
|
||||
event.waitUntil(self.registration.showNotification(message.title || 'A new story on TrueNews',{
|
||||
body:message.body || 'Read the latest story.',
|
||||
icon:'/assets/icon-192.png',badge:'/assets/icon-192.png',
|
||||
tag:message.tag || 'truenews-story',data:{url}
|
||||
}));
|
||||
});
|
||||
self.addEventListener('notificationclick', event => {
|
||||
event.notification.close();
|
||||
event.waitUntil((async()=>{
|
||||
const target=new URL(event.notification.data?.url || '/',self.location.origin);
|
||||
if(target.origin!==self.location.origin) return;
|
||||
const windows=await self.clients.matchAll({type:'window',includeUncontrolled:true});
|
||||
const existing=windows.find(client=>new URL(client.url).origin===target.origin);
|
||||
if(existing){await existing.navigate(target.href);await existing.focus();}
|
||||
else await self.clients.openWindow(target.href);
|
||||
})());
|
||||
});
|
||||
Reference in new issue
Block a user