From f28d538289957a88f2adcdb3a0dc397a7f0c3399 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:30 +0000 Subject: [PATCH] Compile Markdown blog posts into the site's SQLite migrations --- examples/official-site/README.md | 10 ++- .../blog/come-and-see-us-in-athens.md} | 17 ++--- ...-build-twitter-live-on-stage-in-prague.md} | 16 ++-- package-lock.json | 19 ++++- package.json | 5 +- scripts/site-build.mjs | 6 ++ scripts/site/content.mjs | 46 ++++++++++++ scripts/site/documentation.mjs | 75 +++++++++++++++++++ tests/site/seed.test.mjs | 37 +++++++++ 9 files changed, 207 insertions(+), 24 deletions(-) rename examples/official-site/{sqlpage/migrations/53_blog_pgconf2024.sql => content/blog/come-and-see-us-in-athens.md} (63%) rename examples/official-site/{sqlpage/migrations/22_pgconf_eu.sql => content/blog/come-see-me-build-twitter-live-on-stage-in-prague.md} (75%) create mode 100644 scripts/site/content.mjs create mode 100644 scripts/site/documentation.mjs create mode 100644 tests/site/seed.test.mjs diff --git a/examples/official-site/README.md b/examples/official-site/README.md index 201f458af..e26e8af7d 100644 --- a/examples/official-site/README.md +++ b/examples/official-site/README.md @@ -16,8 +16,7 @@ Feel free to [open a pull request](https://github.com/lovasoa/sqlpage/pulls) if Run `npm ci --ignore-scripts` from the repository root, then `npm run site`. The preview uses the same build as CI and deployment. Rerun the command after editing the site. -After `npm run build`, run `npm run site:build` to prepare the site under -`target/official-site/`. +Run `npm run site:build` to prepare the site under `target/official-site/`. Run SQLPage from that directory, or run `docker compose -f examples/official-site/docker-compose.yml up --build` from the repository root. @@ -26,6 +25,13 @@ Add `theme=light` or `theme=dark` to a component reference URL to preview its live examples using SQLPage's stock theme. For example, visit `/component.sql?component=form&theme=light`. +## Blog + +Write posts in `content/blog/` as Markdown with YAML metadata: `title`, `date` +(quoted as `YYYY-MM-DD`), `icon`, and `summary`. For a link to a post elsewhere, +set `external_url` and omit the body. The title is the public URL key, so keep it +when editing an existing post. Older posts are still in the SQL migrations. + ## Landing page The home page is rendered by `index.sql` and the `landing-page` custom template. diff --git a/examples/official-site/sqlpage/migrations/53_blog_pgconf2024.sql b/examples/official-site/content/blog/come-and-see-us-in-athens.md similarity index 63% rename from examples/official-site/sqlpage/migrations/53_blog_pgconf2024.sql rename to examples/official-site/content/blog/come-and-see-us-in-athens.md index 1f859c282..a74383aa3 100644 --- a/examples/official-site/sqlpage/migrations/53_blog_pgconf2024.sql +++ b/examples/official-site/content/blog/come-and-see-us-in-athens.md @@ -1,12 +1,10 @@ +--- +title: Come and see us in Athens ! +date: '2024-10-15' +icon: microphone-2 +summary: PGConf 2024 is coming to Athens, Greece. And we will be there to talk about SQLPage & Archeology ! +--- -INSERT INTO blog_posts (title, description, icon, created_at, content) -VALUES - ( - 'Come and see us in Athens !', - 'PGConf 2024 is coming to Athens, Greece. And we will be there to talk about SQLPage & Archeology !', - 'microphone-2', - '2024-10-15', - ' # PostgreSQL & SQLPage at PGConf 2024 We have been invited to give a talk at PGConf 2024 in Athens, Greece. @@ -20,9 +18,8 @@ drastically reduce the time spent on data entry and management. There will be two presenters: - **Ophir Lojkine**, the creator and maintainer of SQLPage, will present the project and its capabilities. - - **Thomas Guillemard**, an archaeologist from France''s National Institute for Preventive Archaeological Research, will present the project''s use case and its benefits. + - **Thomas Guillemard**, an archaeologist from France's National Institute for Preventive Archaeological Research, will present the project's use case and its benefits. Come and see us in Athens ! https://www.postgresql.eu/events/pgconfeu2024/schedule/session/5707-unearthing-the-past-with-postgresql-how-open-source-is-revolutionizing-digital-archaeology/ -'); diff --git a/examples/official-site/sqlpage/migrations/22_pgconf_eu.sql b/examples/official-site/content/blog/come-see-me-build-twitter-live-on-stage-in-prague.md similarity index 75% rename from examples/official-site/sqlpage/migrations/22_pgconf_eu.sql rename to examples/official-site/content/blog/come-see-me-build-twitter-live-on-stage-in-prague.md index 2647d3b09..81b8eed4f 100644 --- a/examples/official-site/sqlpage/migrations/22_pgconf_eu.sql +++ b/examples/official-site/content/blog/come-see-me-build-twitter-live-on-stage-in-prague.md @@ -1,10 +1,10 @@ -INSERT INTO blog_posts (title, description, icon, created_at, content) -VALUES ( - 'Come see me build twitter live on stage in Prague', - 'I will speak about SQLPage at pgconf.eu in Prague on December 14th. Come see me !', - 'calendar-event', - '2023-11-11', - ' +--- +title: Come see me build twitter live on stage in Prague +date: '2023-11-11' +icon: calendar-event +summary: I will speak about SQLPage at pgconf.eu in Prague on December 14th. Come see me ! +--- + # SQLPage live on stage Hello everyone ! @@ -25,5 +25,3 @@ Come see me ! - [Information about the talk](https://www.postgresql.eu/events/pgconfeu2023/schedule/session/4687-sqlpage-building-a-full-web-application-with-nothing-but-postgres-and-sql-queries/) - When ? **December 14th, 2023**, **09:30 - 10:20** - Where ? Zenit Room, [Clarion Congress Hotel, Prague, Czech Republic](https://maps.app.goo.gl/qHCAaRFRjXmx2kQA7) - -'); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 67f1c7ef9..1db04f14c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,8 @@ "@types/leaflet": "^1.9.23", "@types/node": "^26.6.10", "rolldown": "^1.3.3", - "typescript": "^7.0.2" + "typescript": "^7.0.2", + "yaml": "2.9.1" } }, "node_modules/@axe-core/playwright": { @@ -1246,6 +1247,22 @@ "dev": true, "license": "MIT" }, + "node_modules/yaml": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.1.tgz", + "integrity": "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw==", + "dev": true, + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14.6" + }, + "funding": { + "url": "https://github.com/sponsors/eemeli" + } + }, "tests/end-to-end": { "version": "1.0.0", "license": "ISC", diff --git a/package.json b/package.json index 458c667e8..4a69bb15d 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "fix": "biome check --fix --unsafe .", "presite:test": "npm run site:build", "site:build": "node scripts/site-build.mjs", - "site:test": "node --test tests/js/component_documentation.checks.ts", + "site:test": "node --test tests/js/component_documentation.checks.ts \"tests/site/*.test.mjs\"", "site": "npm run build && npm run site:build && cd target/official-site && cargo run --locked --manifest-path ../../Cargo.toml" }, "repository": { @@ -29,7 +29,8 @@ "@types/leaflet": "^1.9.23", "@types/node": "^26.6.10", "rolldown": "^1.3.3", - "typescript": "^7.0.2" + "typescript": "^7.0.2", + "yaml": "2.9.1" }, "workspaces": [ "tests/end-to-end" diff --git a/scripts/site-build.mjs b/scripts/site-build.mjs index 96a3d5c90..dad38aff2 100644 --- a/scripts/site-build.mjs +++ b/scripts/site-build.mjs @@ -8,12 +8,14 @@ import { rmSync, } from "node:fs"; import { dirname, join, relative, resolve } from "node:path"; +import { writeDocumentation } from "./site/documentation.mjs"; const repository = resolve(import.meta.dirname, ".."); const siteDirectory = join(repository, "target", "official-site"); const source = join(repository, "examples", "official-site"); const excluded = new Set([ + "content", "README.md", "docker-compose.yml", "test.hurl", @@ -36,6 +38,10 @@ function buildSite() { join(repository, "configuration.md"), join(next, "configuration.md"), ); + writeDocumentation( + join(source, "content"), + join(next, "sqlpage", "migrations"), + ); // Stage the complete site before exchanging the previous preview. try { renameSync(siteDirectory, previous); diff --git a/scripts/site/content.mjs b/scripts/site/content.mjs new file mode 100644 index 000000000..3ffa13819 --- /dev/null +++ b/scripts/site/content.mjs @@ -0,0 +1,46 @@ +import { readdirSync, readFileSync } from "node:fs"; +import { basename, join } from "node:path"; +import { parse } from "yaml"; + +export function atFile(path, visit) { + try { + return visit(readFileSync(path, "utf8")); + } catch (error) { + throw new Error(`${path}: ${error.message}`, { cause: error }); + } +} + +export function pages(dir, visit) { + for (const file of readdirSync(dir) + .filter((f) => f.endsWith(".md")) + .sort()) { + atFile(join(dir, file), (source) => { + const text = source.replaceAll("\r\n", "\n"); + let metadata = new Map(); + let body = text; + if (text.startsWith("---\n")) { + const rest = text.slice(4); + const end = /^---[ \t]*$/m.exec(rest); + if (!end) throw new Error("Unclosed YAML front matter"); + metadata = + parse(rest.slice(0, end.index), { mapAsMap: true }) ?? metadata; + body = rest.slice(end.index + end[0].length); + } + visit(basename(file, ".md"), metadata, body.trim()); + }); + } +} + +export function fields(map, allowed) { + if (!(map instanceof Map)) throw new Error("Expected a YAML mapping"); + for (const key of map.keys()) { + if (!allowed.includes(key)) throw new Error(`Unknown field: ${key}`); + } + return Object.fromEntries(map); +} + +export function text(value, name) { + if (typeof value !== "string" || !value.trim()) + throw new Error(`Expected text in ${name}`); + return value; +} diff --git a/scripts/site/documentation.mjs b/scripts/site/documentation.mjs new file mode 100644 index 000000000..ad1f0631f --- /dev/null +++ b/scripts/site/documentation.mjs @@ -0,0 +1,75 @@ +import { readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { DatabaseSync } from "node:sqlite"; +import { fields, pages, text } from "./content.mjs"; + +// Let SQLite quote bound values for the migration exactly as it executed them. +export class Seed { + #sql = []; + constructor(db) { + this.db = db; + } + row(sql, ...values) { + if (values.some((v) => typeof v === "string" && v.includes("\0"))) + throw new Error("NUL byte in SQL text value"); + const statement = this.db.prepare(sql); + statement.run(...values); + this.#sql.push(`${statement.expandedSQL};\n`); + } + toString() { + return this.#sql.join(""); + } +} + +export function insertPosts(seed, dir) { + pages(dir, (_, meta, body) => { + const p = fields(meta, [ + "title", + "date", + "icon", + "summary", + "external_url", + ]); + const external = p.external_url ?? null; + if (external !== null) text(external, "external_url"); + if (Boolean(external) === Boolean(body)) + throw new Error( + "A post needs either Markdown content or an external_url", + ); + seed.row( + "INSERT INTO blog_posts (title, created_at, icon, description, external_url, content) VALUES (?, ?, ?, ?, ?, ?)", + text(p.title, "title"), + text(p.date, "date"), + text(p.icon, "icon"), + text(p.summary, "summary"), + external, + body || null, + ); + }); +} + +export function writeDocumentation(content, migrations) { + const db = new DatabaseSync(":memory:"); + try { + // The generated rows go after the old content tables and before the search index. + const version = 100; + const files = readdirSync(migrations) + .filter((f) => f.endsWith(".sql") && !f.endsWith(".down.sql")) + .sort((a, b) => Number.parseInt(a, 10) - Number.parseInt(b, 10)); + for (const file of files) { + const n = Number.parseInt(file, 10); + if (n === version) + throw new Error(`Migration ${version} is reserved: ${file}`); + if (n < version) db.exec(readFileSync(join(migrations, file), "utf8")); + } + const seed = new Seed(db); + insertPosts(seed, join(content, "blog")); + writeFileSync( + join(migrations, `${version}_documentation_content.sql`), + seed.toString(), + { flag: "wx" }, + ); + } finally { + db.close(); + } +} diff --git a/tests/site/seed.test.mjs b/tests/site/seed.test.mjs new file mode 100644 index 000000000..868825090 --- /dev/null +++ b/tests/site/seed.test.mjs @@ -0,0 +1,37 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { DatabaseSync } from "node:sqlite"; +import test from "node:test"; +import { insertPosts, Seed } from "../../scripts/site/documentation.mjs"; + +test("a Markdown post survives replay as a SQLite migration", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sqlpage-posts-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + const post = join(dir, "one.md"); + writeFileSync( + post, + "---\r\ntitle: SQLPage's news\r\ndate: '2026-11-01'\r\nicon: news\r\nsummary: An announcement\r\n---\r\nA reader's choice.\r\n", + ); + const db = new DatabaseSync(":memory:"); + t.after(() => db.close()); + db.exec( + "CREATE TABLE blog_posts (title PRIMARY KEY, created_at, icon, description, external_url, content)", + ); + const seed = new Seed(db); + insertPosts(seed, dir); + db.exec("DELETE FROM blog_posts"); + db.exec(seed.toString()); + const { title, content } = db + .prepare("SELECT title, content FROM blog_posts") + .get(); + assert.equal(title, "SQLPage's news"); + assert.equal(content, "A reader's choice."); + + writeFileSync(post, "---\ntitle: Unclosed\n"); + assert.throws( + () => insertPosts(seed, dir), + /one\.md: Unclosed YAML front matter/, + ); +});