diff --git a/server/src/admin-crud.js b/server/src/admin-crud.js
index a8ae9a7..d7afa70 100644
--- a/server/src/admin-crud.js
+++ b/server/src/admin-crud.js
@@ -189,8 +189,9 @@ export function readRow(db, entity, rawId) {
row[ext.key] = readExtension(db, ext, id);
}
+ // As text, for the reason readChildren gives its nested ids.
for (const child of entity.children ?? []) {
- row[child.key] = readChildren(db, child, id);
+ row[child.key] = readChildren(db, child, String(id));
}
return row;
@@ -513,7 +514,10 @@ function keyedDbErrors(prefix, fn) {
}
}
-function writeChildren(db, entity, id, payload, parentValues) {
+function writeChildren(db, entity, rawId, payload, parentValues) {
+ // As text: an auto id is a JS number, which binds as REAL and
+ // lands in a TEXT owner column (a newsletter's blocks) as '1.0'.
+ const id = String(rawId);
for (const child of entity.children ?? []) {
if (!applies(child.when, parentValues)) {
deleteChildren(db, child, id);
diff --git a/server/src/admin-schema.js b/server/src/admin-schema.js
index 7ed9db7..5bca3db 100644
--- a/server/src/admin-schema.js
+++ b/server/src/admin-schema.js
@@ -51,6 +51,7 @@ import { timeline } from "./admin-schema/timeline.js";
import { frontPage } from "./admin-schema/frontPage.js";
import { blogPosts } from "./admin-schema/blog.js";
import { siteSettings } from "./admin-schema/siteSettings.js";
+import { newsletters } from "./admin-schema/newsletters.js";
import {
communityPage,
retreatsPage,
@@ -74,6 +75,7 @@ export const ENTITIES = {
organization_page: organizationPage,
history_page: historyPage,
site_settings: siteSettings,
+ newsletters,
};
/* ── Options for the form's select inputs ────────────────────── */
diff --git a/server/src/admin-schema/newsletters.js b/server/src/admin-schema/newsletters.js
new file mode 100644
index 0000000..be6d58d
--- /dev/null
+++ b/server/src/admin-schema/newsletters.js
@@ -0,0 +1,27 @@
+import { text, blocksChild } from "./columns.js";
+
+/* ── Newsletter drafts (057_newsletters.sql) ─────────────────────
+
+ Saved from Tools → Newsletter (AdminNewsletter.tsx), which is
+ their only editor: there is no list or form for them in Data.
+ `picks` is the builder's JSON of what's ticked. Deleting a draft
+ takes its blocks with it (newsletters_cleanup). */
+
+export const newsletters = {
+ key: "newsletters",
+ table: "newsletters",
+ idColumn: "id",
+ idKind: "auto",
+ concurrency: "updated_at",
+
+ list: {
+ columns: ["id", "title", "updated_at"],
+ filters: [],
+ search: ["title"],
+ order: "updated_at DESC, id DESC",
+ },
+
+ columns: [text("title"), text("intro"), text("footer"), text("picks")],
+
+ children: [blocksChild("newsletter")],
+};
diff --git a/server/src/migrations/057_newsletters.sql b/server/src/migrations/057_newsletters.sql
new file mode 100644
index 0000000..708889d
--- /dev/null
+++ b/server/src/migrations/057_newsletters.sql
@@ -0,0 +1,111 @@
+-- ═══════════════════════════════════════════════════════════════
+-- 057 NEWSLETTER DRAFTS
+--
+-- Tools → Newsletter saves what it builds as a draft, to come back
+-- to, copy into Mailchimp again, or post as a blog post:
+--
+-- title, intro the email's heading and opening lines
+-- footer the closing lines (blank: the site's name and
+-- address); no unsubscribe link, since Mailchimp
+-- adds its own
+-- picks what's ticked, as JSON: event ids, blog post
+-- ids, and the front page cards by their link
+-- ({"events": [...], "posts": [...], "cards": [...]})
+-- blocks content_blocks owned 'newsletter': whatever is
+-- written for this issue, under the intro
+--
+-- content_blocks is rebuilt for its owner_kind CHECK (as 056 did). A
+-- draft deleted takes its blocks with it, by trigger.
+-- ═══════════════════════════════════════════════════════════════
+
+CREATE TABLE newsletters (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ title TEXT,
+ intro TEXT,
+ footer TEXT,
+ picks TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(picks)),
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
+) STRICT;
+
+
+-- ── Blocks: the new owner kind ─────────────────────────────────
+
+CREATE TABLE content_blocks_new (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ owner_kind TEXT NOT NULL
+ CHECK (owner_kind IN ('organization', 'event', 'person', 'team', 'blog_post', 'page_section',
+ 'newsletter')),
+ owner_id TEXT NOT NULL,
+ slot TEXT NOT NULL DEFAULT 'body' CHECK (slot IN ('card', 'body')),
+ sort_order INTEGER NOT NULL DEFAULT 0,
+
+ type TEXT NOT NULL
+ CHECK (type IN ('heading', 'subheading', 'paragraph',
+ 'list', 'links', 'quote', 'image', 'divider',
+ 'video', 'button', 'callout', 'schedule', 'table', 'cards',
+ 'people')),
+ text TEXT,
+ media TEXT,
+ href TEXT,
+ alt TEXT,
+ cite TEXT,
+ team_id TEXT REFERENCES teams (id) ON DELETE SET NULL
+) STRICT;
+
+INSERT INTO content_blocks_new
+ (id, owner_kind, owner_id, slot, sort_order, type, text, media, href, alt, cite, team_id)
+SELECT id, owner_kind, owner_id, slot, sort_order, type, text, media, href, alt, cite, team_id
+ FROM content_blocks;
+
+PRAGMA legacy_alter_table = ON;
+DROP TABLE content_blocks;
+ALTER TABLE content_blocks_new RENAME TO content_blocks;
+PRAGMA legacy_alter_table = OFF;
+
+CREATE INDEX content_blocks_owner_idx
+ ON content_blocks (owner_kind, owner_id, slot, sort_order);
+
+
+-- ── Triggers last (see CLAUDE.md, Migrations) ──
+
+CREATE TRIGGER newsletters_touch
+AFTER UPDATE ON newsletters
+FOR EACH ROW WHEN new.updated_at = old.updated_at
+BEGIN
+ UPDATE newsletters SET updated_at = datetime('now') WHERE id = new.id;
+END;
+
+CREATE TRIGGER newsletters_cleanup
+AFTER DELETE ON newsletters
+BEGIN
+ DELETE FROM content_blocks WHERE owner_kind = 'newsletter' AND owner_id = CAST(old.id AS TEXT);
+END;
+
+CREATE TRIGGER content_blocks_owner_exists
+BEFORE INSERT ON content_blocks
+BEGIN
+ SELECT CASE
+ WHEN new.owner_kind = 'event'
+ AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such event')
+ WHEN new.owner_kind = 'organization'
+ AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such organization')
+ WHEN new.owner_kind = 'person'
+ AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such person')
+ WHEN new.owner_kind = 'team'
+ AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such team')
+ WHEN new.owner_kind = 'blog_post'
+ AND NOT EXISTS (SELECT 1 FROM blog_posts WHERE id = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such blog post')
+ WHEN new.owner_kind = 'page_section'
+ AND NOT EXISTS (SELECT 1 FROM page_sections WHERE CAST(id AS TEXT) = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such page section')
+ WHEN new.owner_kind = 'newsletter'
+ AND NOT EXISTS (SELECT 1 FROM newsletters WHERE CAST(id AS TEXT) = new.owner_id)
+ THEN RAISE(ABORT, 'content_blocks: no such newsletter')
+ END;
+END;
diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx
index e5b42a6..f930891 100644
--- a/src/components/Layout.tsx
+++ b/src/components/Layout.tsx
@@ -166,10 +166,12 @@ export default function Layout() {
{/* Each page, and under it on hover or keyboard focus its
sections (navSections.ts): the sections live in these
- dropdowns rather than a strip of their own. */}
+ dropdowns rather than a strip of their own. Home has
+ none: it's the logo's link too, and its bands are one
+ scroll away. */}
+}
+
export type Newsletter = {
title: string
intro?: string | null
+ /** Written for this issue, under the intro. */
+ blocks?: NewsletterBlock[]
sections: NewsletterSection[]
+ /** The closing lines. Blank: the site's name and address. */
+ footer?: string | null
/** The site's address, for the header link and footer. */
origin: string
siteName: string
/** An absolute URL of the logo for the header. */
logo?: string | null
+ /** A site path or upload as an absolute address (blocks hold paths and bare filenames). */
+ absolute: (pathOrFile: string, kind: 'link' | 'image') => string
}
/* The palette as an inbox needs it. */
@@ -55,15 +80,148 @@ const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif"
const escape = (value: string) =>
value.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"')
-/* Paragraphs from text with blank lines between them. */
-const paragraphs = (text: string, style: string) =>
+/* A link an email can follow: the site's own paths made absolute,
+ anything but http, https and mailto refused. */
+function safeHref(href: string, n: Newsletter): string | null {
+ const value = href.trim()
+ if (/^(https?:|mailto:)/i.test(value)) return value
+ if (value.startsWith('/')) return n.absolute(value, 'link')
+ return null
+}
+
+/* Escaped text with the editor's **bold**, *italic* and [text](url). */
+function inline(text: string, n: Newsletter): string {
+ return escape(text)
+ .replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_whole, label, href) => {
+ const url = safeHref(href.replace(/&/g, '&'), n)
+ return url ? `${label}` : label
+ })
+ .replace(/\*\*([^*]+)\*\*/g, '$1')
+ .replace(/\*([^*]+)\*/g, '$1')
+}
+
+/* Paragraphs from text with blank lines between them, formatted
+ when given the newsletter (its links need the site's address). */
+const paragraphs = (text: string, style: string, n?: Newsletter) =>
text
.split(/\n\s*\n/)
.map((part) => part.trim())
.filter(Boolean)
- .map((part) => `
+ Default: the site's name and address. No unsubscribe link needed: Mailchimp adds its own.
+
+
@@ -228,8 +463,8 @@ export default function AdminNewsletter() {
{newsletter.count === 0
- ? 'Tick at least one item to build the email.'
- : `${newsletter.count} item${newsletter.count === 1 ? '' : 's'}. In Mailchimp, paste the HTML into a Code block or "Paste in code".`}
+ ? 'Tick an item or add a block to build the email.'
+ : `${newsletter.count} part${newsletter.count === 1 ? '' : 's'}. In Mailchimp, paste the HTML into a Code block or "Paste in code".`}