Cloudflare worker to watch RSS feeds and post updates to Discord webhooks

Fix Discord embed: strip HTML from descriptions and convert dates to ISO 8601

+14 -3
+14 -3
worker.js
··· 7 7 }, 8 8 9 9 async fetch(request, env) { 10 - if (new URL(request.url).pathname === '/check') { 10 + const path = new URL(request.url).pathname; 11 + if (path === '/check') { 11 12 await checkAllFeeds(env); 12 13 return new Response('Checked all feeds'); 13 14 } ··· 79 80 })); 80 81 } 81 82 83 + function stripHtml(html) { 84 + return html 85 + .replace(/<br\s*\/?>/gi, '\n') 86 + .replace(/<\/li>/gi, '\n') 87 + .replace(/<\/p>/gi, '\n') 88 + .replace(/<[^>]+>/g, '') 89 + .replace(/\n{3,}/g, '\n\n') 90 + .trim(); 91 + } 92 + 82 93 async function postToDiscord(webhook, entry, feedUrl) { 83 94 const feedName = new URL(feedUrl).pathname.split('/')[1] || 'Feed'; 84 95 const res = await fetch(webhook, { ··· 88 99 embeds: [{ 89 100 title: entry.title || 'New activity', 90 101 url: entry.link || feedUrl, 91 - description: entry.summary?.slice(0, 300) || '', 102 + description: entry.summary ? stripHtml(entry.summary).slice(0, 300) : '', 92 103 color: 0x5865F2, 93 - timestamp: entry.date || new Date().toISOString(), 104 + timestamp: entry.date ? new Date(entry.date).toISOString() : new Date().toISOString(), 94 105 footer: { text: entry.author ? `${feedName} • ${entry.author}` : feedName } 95 106 }] 96 107 })