|
| 1 | +#!/usr/bin/env bun |
| 2 | +// Sends a Resend broadcast for each newly added blog post listed in new_posts.txt |
| 3 | +// (paths relative to repo root, one per line). Invoked by the GitHub Action |
| 4 | +// .github/workflows/notify-new-post.yml on push to main. |
| 5 | + |
| 6 | +import { readFileSync } from "node:fs"; |
| 7 | +import { basename } from "node:path"; |
| 8 | + |
| 9 | +const SITE = "https://moq.dev"; |
| 10 | +const FROM = "Media over QUIC <blog@moq.dev>"; |
| 11 | + |
| 12 | +const apiKey = requireEnv("RESEND_API_KEY"); |
| 13 | +const segmentId = requireEnv("RESEND_SEGMENT_ID"); |
| 14 | + |
| 15 | +const newPostsList = readFileSync("new_posts.txt", "utf8").trim(); |
| 16 | +if (!newPostsList) { |
| 17 | + console.log("No new posts. Exiting."); |
| 18 | + process.exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +const paths = newPostsList.split("\n").filter(Boolean); |
| 22 | +console.log(`Found ${paths.length} new post(s): ${paths.join(", ")}`); |
| 23 | + |
| 24 | +for (const path of paths) { |
| 25 | + const rawSlug = basename(path, ".mdx"); |
| 26 | + const slug = encodeURIComponent(rawSlug); |
| 27 | + const fm = parseFrontmatter(readFileSync(path, "utf8")); |
| 28 | + const title = fm.title ?? rawSlug; |
| 29 | + const description = fm.description ?? ""; |
| 30 | + const url = `${SITE}/blog/${slug}`; |
| 31 | + |
| 32 | + console.log(`Creating broadcast for "${title}" → ${url}`); |
| 33 | + |
| 34 | + const create = await fetch("https://api.resend.com/broadcasts", { |
| 35 | + signal: AbortSignal.timeout(15000), |
| 36 | + method: "POST", |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${apiKey}`, |
| 39 | + "Content-Type": "application/json", |
| 40 | + }, |
| 41 | + body: JSON.stringify({ |
| 42 | + segment_id: segmentId, |
| 43 | + from: FROM, |
| 44 | + subject: title, |
| 45 | + html: renderHtml({ title, description, url }), |
| 46 | + }), |
| 47 | + }); |
| 48 | + |
| 49 | + if (!create.ok) { |
| 50 | + const err = await create.text(); |
| 51 | + throw new Error(`Resend broadcast create failed (${create.status}): ${err}`); |
| 52 | + } |
| 53 | + |
| 54 | + const { id } = (await create.json()) as { id: string }; |
| 55 | + |
| 56 | + const send = await fetch(`https://api.resend.com/broadcasts/${id}/send`, { |
| 57 | + signal: AbortSignal.timeout(15000), |
| 58 | + method: "POST", |
| 59 | + headers: { Authorization: `Bearer ${apiKey}` }, |
| 60 | + }); |
| 61 | + |
| 62 | + if (!send.ok) { |
| 63 | + const err = await send.text(); |
| 64 | + throw new Error(`Resend broadcast send failed (${send.status}): ${err}`); |
| 65 | + } |
| 66 | + |
| 67 | + console.log(`✓ Sent broadcast ${id} for "${title}"`); |
| 68 | +} |
| 69 | + |
| 70 | +function requireEnv(name: string): string { |
| 71 | + const v = process.env[name]; |
| 72 | + if (!v) throw new Error(`Missing env var: ${name}`); |
| 73 | + return v; |
| 74 | +} |
| 75 | + |
| 76 | +function parseFrontmatter(source: string): Record<string, string> { |
| 77 | + const match = source.match(/^---\r?\n([\s\S]*?)\r?\n---/); |
| 78 | + if (!match) return {}; |
| 79 | + const out: Record<string, string> = {}; |
| 80 | + for (const line of match[1].split(/\r?\n/)) { |
| 81 | + const m = line.match(/^([A-Za-z_][\w-]*):\s*(.*)$/); |
| 82 | + if (m) out[m[1]] = m[2].trim().replace(/^["']|["']$/g, ""); |
| 83 | + } |
| 84 | + return out; |
| 85 | +} |
| 86 | + |
| 87 | +function renderHtml({ title, description, url }: { title: string; description: string; url: string }): string { |
| 88 | + const safeTitle = escapeHtml(title); |
| 89 | + const safeDescription = escapeHtml(description); |
| 90 | + const safeUrl = escapeHtml(url); |
| 91 | + return `<!doctype html> |
| 92 | +<html><body style="font-family: -apple-system, system-ui, sans-serif; line-height: 1.5; color: #1f2937;"> |
| 93 | + <h1 style="margin: 0 0 16px;">${safeTitle}</h1> |
| 94 | + ${safeDescription ? `<p style="font-size: 16px; color: #4b5563;">${safeDescription}</p>` : ""} |
| 95 | + <p style="margin: 24px 0;"> |
| 96 | + <a href="${safeUrl}" style="display: inline-block; background: #2563eb; color: #fff; text-decoration: none; padding: 10px 20px; border-radius: 6px;">Read it on moq.dev →</a> |
| 97 | + </p> |
| 98 | + <p style="font-size: 13px; color: #6b7280;">Or open it directly: <a href="${safeUrl}">${safeUrl}</a></p> |
| 99 | +</body></html>`; |
| 100 | +} |
| 101 | + |
| 102 | +function escapeHtml(s: string): string { |
| 103 | + return s.replace(/[&<>"']/g, (c) => { |
| 104 | + switch (c) { |
| 105 | + case "&": |
| 106 | + return "&"; |
| 107 | + case "<": |
| 108 | + return "<"; |
| 109 | + case ">": |
| 110 | + return ">"; |
| 111 | + case '"': |
| 112 | + return """; |
| 113 | + default: |
| 114 | + return "'"; |
| 115 | + } |
| 116 | + }); |
| 117 | +} |
0 commit comments