Skip to content

Commit 22f5fbd

Browse files
committed
Compile Markdown blog posts into the site's SQLite migrations
1 parent 4a61e42 commit 22f5fbd

12 files changed

Lines changed: 212 additions & 31 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ jobs:
6464
save-if: ${{ github.ref == 'refs/heads/main' }}
6565
- run: cargo fmt --all -- --check
6666
- run: cargo clippy --all-targets --all-features -- -D warnings
67-
- name: Prepare official website
68-
run: npm run site:build
69-
- name: Check prepared component documentation
67+
- name: Prepare and check official website
7068
run: npm run site:test
7169
- name: Upload prepared website
7270
uses: actions/upload-artifact@v7

‎CONTRIBUTING.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ built executable. Component suites use `fixtures/<suite>/{index.sql,test.ts}` an
6767
import the shared `fixture.ts` harness; root-level `*.spec.ts` files cover the
6868
official site.
6969

70-
For changes to component documentation, run `npm run site:test` after preparing
71-
the site with `npm run site:build`. This checks the same reference the site serves
72-
against the public components and their template properties.
70+
For changes to component documentation, run `npm run site:test`. It rebuilds
71+
the site and checks the prepared reference against public components and their
72+
template properties.
7373

7474
Run `npx playwright test --grep @accessibility` from `tests/end-to-end` to audit
7575
the official site's component documentation and its live examples in SQLPage's

‎examples/official-site/README.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Feel free to [open a pull request](https://github.com/lovasoa/sqlpage/pulls) if
1616
Run `npm ci --ignore-scripts` from the repository root, then `npm run site`.
1717
The preview uses the same build as CI and deployment. Rerun the command after editing the site.
1818

19-
After `npm run build`, run `npm run site:build` to prepare the site under
20-
`target/official-site/`.
19+
Run `npm run site:build` to prepare the site under `target/official-site/`.
2120
Run SQLPage from that directory, or run
2221
`docker compose -f examples/official-site/docker-compose.yml up --build` from
2322
the repository root.
@@ -26,6 +25,13 @@ Add `theme=light` or `theme=dark` to a component reference URL to preview its
2625
live examples using SQLPage's stock theme. For example, visit
2726
`/component.sql?component=form&theme=light`.
2827

28+
## Blog
29+
30+
Write posts in `content/blog/` as Markdown with YAML metadata: `title`, `date`
31+
(quoted as `YYYY-MM-DD`), `icon`, and `summary`. For a link to a post elsewhere,
32+
set `external_url` and omit the body. The title is the public URL key, so keep it
33+
when editing an existing post. Older posts are still in the SQL migrations.
34+
2935
## Landing page
3036

3137
The home page is rendered by `index.sql` and the `landing-page` custom template.

examples/official-site/sqlpage/migrations/53_blog_pgconf2024.sql renamed to examples/official-site/content/blog/come-and-see-us-in-athens.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
---
2+
title: Come and see us in Athens !
3+
date: '2024-10-15'
4+
icon: microphone-2
5+
summary: PGConf 2024 is coming to Athens, Greece. And we will be there to talk about SQLPage & Archeology !
6+
---
17

2-
INSERT INTO blog_posts (title, description, icon, created_at, content)
3-
VALUES
4-
(
5-
'Come and see us in Athens !',
6-
'PGConf 2024 is coming to Athens, Greece. And we will be there to talk about SQLPage & Archeology !',
7-
'microphone-2',
8-
'2024-10-15',
9-
'
108
# PostgreSQL & SQLPage at PGConf 2024
119

1210
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.
2018

2119
There will be two presenters:
2220
- **Ophir Lojkine**, the creator and maintainer of SQLPage, will present the project and its capabilities.
23-
- **Thomas Guillemard**, an archaeologist from France''s National Institute for Preventive Archaeological Research, will present the project''s use case and its benefits.
21+
- **Thomas Guillemard**, an archaeologist from France's National Institute for Preventive Archaeological Research, will present the project's use case and its benefits.
2422

2523
Come and see us in Athens !
2624

2725
https://www.postgresql.eu/events/pgconfeu2024/schedule/session/5707-unearthing-the-past-with-postgresql-how-open-source-is-revolutionizing-digital-archaeology/
28-
');

examples/official-site/sqlpage/migrations/22_pgconf_eu.sql renamed to examples/official-site/content/blog/come-see-me-build-twitter-live-on-stage-in-prague.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
INSERT INTO blog_posts (title, description, icon, created_at, content)
2-
VALUES (
3-
'Come see me build twitter live on stage in Prague',
4-
'I will speak about SQLPage at pgconf.eu in Prague on December 14th. Come see me !',
5-
'calendar-event',
6-
'2023-11-11',
7-
'
1+
---
2+
title: Come see me build twitter live on stage in Prague
3+
date: '2023-11-11'
4+
icon: calendar-event
5+
summary: I will speak about SQLPage at pgconf.eu in Prague on December 14th. Come see me !
6+
---
7+
88
# SQLPage live on stage
99

1010
Hello everyone !
@@ -25,5 +25,3 @@ Come see me !
2525
- [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/)
2626
- When ? **December 14th, 2023**, **09:30 - 10:20**
2727
- Where ? Zenit Room, [Clarion Congress Hotel, Prague, Czech Republic](https://maps.app.goo.gl/qHCAaRFRjXmx2kQA7)
28-
29-
');

‎package-lock.json‎

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format": "biome format --write .",
1010
"fix": "biome check --fix --unsafe .",
1111
"site:build": "node scripts/site-build.mjs",
12-
"site:test": "node --test tests/js/component_documentation.checks.ts",
12+
"site:test": "npm run site:build && node --test tests/js/component_documentation.checks.ts \"tests/site/*.test.mjs\"",
1313
"site": "npm run build && npm run site:build && cd target/official-site && cargo run --locked --manifest-path ../../Cargo.toml"
1414
},
1515
"repository": {
@@ -28,7 +28,8 @@
2828
"@types/leaflet": "^1.9.23",
2929
"@types/node": "^26.6.10",
3030
"rolldown": "^1.3.3",
31-
"typescript": "^7.0.2"
31+
"typescript": "^7.0.2",
32+
"yaml": "2.9.1"
3233
},
3334
"workspaces": [
3435
"tests/end-to-end"

‎scripts/site-build.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import {
88
rmSync,
99
} from "node:fs";
1010
import { dirname, join, relative, resolve } from "node:path";
11+
import { writeDocumentation } from "./site/documentation.mjs";
1112

1213
const repository = resolve(import.meta.dirname, "..");
1314
const siteDirectory = join(repository, "target", "official-site");
1415
const source = join(repository, "examples", "official-site");
1516

1617
const excluded = new Set([
18+
"content",
1719
"README.md",
1820
"docker-compose.yml",
1921
"test.hurl",
@@ -36,6 +38,10 @@ function buildSite() {
3638
join(repository, "configuration.md"),
3739
join(next, "configuration.md"),
3840
);
41+
writeDocumentation(
42+
join(source, "content"),
43+
join(next, "sqlpage", "migrations"),
44+
);
3945
// Stage the complete site before exchanging the previous preview.
4046
try {
4147
renameSync(siteDirectory, previous);

‎scripts/site/content.mjs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { readdirSync, readFileSync } from "node:fs";
2+
import { basename, join } from "node:path";
3+
import { parse } from "yaml";
4+
5+
export function atFile(path, visit) {
6+
try {
7+
return visit(readFileSync(path, "utf8"));
8+
} catch (error) {
9+
throw new Error(`${path}: ${error.message}`, { cause: error });
10+
}
11+
}
12+
13+
export function pages(dir, visit) {
14+
for (const file of readdirSync(dir)
15+
.filter((f) => f.endsWith(".md"))
16+
.sort()) {
17+
atFile(join(dir, file), (source) => {
18+
const text = source.replaceAll("\r\n", "\n");
19+
let metadata = new Map();
20+
let body = text;
21+
if (text.startsWith("---\n")) {
22+
const rest = text.slice(4);
23+
const end = /^---[ \t]*$/m.exec(rest);
24+
if (!end) throw new Error("Unclosed YAML front matter");
25+
metadata =
26+
parse(rest.slice(0, end.index), { mapAsMap: true }) ?? metadata;
27+
body = rest.slice(end.index + end[0].length);
28+
}
29+
visit(basename(file, ".md"), metadata, body.trim());
30+
});
31+
}
32+
}
33+
34+
export function fields(map, allowed) {
35+
if (!(map instanceof Map)) throw new Error("Expected a YAML mapping");
36+
for (const key of map.keys()) {
37+
if (!allowed.includes(key)) throw new Error(`Unknown field: ${key}`);
38+
}
39+
return Object.fromEntries(map);
40+
}
41+
42+
export function text(value, name) {
43+
if (typeof value !== "string" || !value.trim())
44+
throw new Error(`Expected text in ${name}`);
45+
return value;
46+
}

‎scripts/site/documentation.mjs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
2+
import { join } from "node:path";
3+
import { DatabaseSync } from "node:sqlite";
4+
import { fields, pages, text } from "./content.mjs";
5+
6+
// Let SQLite quote bound values for the migration exactly as it executed them.
7+
export class Seed {
8+
#sql = [];
9+
constructor(db) {
10+
this.db = db;
11+
}
12+
row(sql, ...values) {
13+
if (values.some((v) => typeof v === "string" && v.includes("\0")))
14+
throw new Error("NUL byte in SQL text value");
15+
const statement = this.db.prepare(sql);
16+
statement.run(...values);
17+
this.#sql.push(`${statement.expandedSQL};\n`);
18+
}
19+
toString() {
20+
return this.#sql.join("");
21+
}
22+
}
23+
24+
export function insertPosts(seed, dir) {
25+
pages(dir, (_, meta, body) => {
26+
const p = fields(meta, [
27+
"title",
28+
"date",
29+
"icon",
30+
"summary",
31+
"external_url",
32+
]);
33+
const external = p.external_url ?? null;
34+
if (external !== null) text(external, "external_url");
35+
if (Boolean(external) === Boolean(body))
36+
throw new Error(
37+
"A post needs either Markdown content or an external_url",
38+
);
39+
seed.row(
40+
"INSERT INTO blog_posts (title, created_at, icon, description, external_url, content) VALUES (?, ?, ?, ?, ?, ?)",
41+
text(p.title, "title"),
42+
text(p.date, "date"),
43+
text(p.icon, "icon"),
44+
text(p.summary, "summary"),
45+
external,
46+
body || null,
47+
);
48+
});
49+
}
50+
51+
export function writeDocumentation(content, migrations) {
52+
const db = new DatabaseSync(":memory:");
53+
try {
54+
// The generated rows go after the old content tables and before the search index.
55+
const version = 100;
56+
const files = readdirSync(migrations)
57+
.filter((f) => f.endsWith(".sql") && !f.endsWith(".down.sql"))
58+
.sort((a, b) => Number.parseInt(a, 10) - Number.parseInt(b, 10));
59+
for (const file of files) {
60+
const n = Number.parseInt(file, 10);
61+
if (n === version)
62+
throw new Error(`Migration ${version} is reserved: ${file}`);
63+
if (n < version) db.exec(readFileSync(join(migrations, file), "utf8"));
64+
}
65+
const seed = new Seed(db);
66+
insertPosts(seed, join(content, "blog"));
67+
writeFileSync(
68+
join(migrations, `${version}_documentation_content.sql`),
69+
seed.toString(),
70+
{ flag: "wx" },
71+
);
72+
} finally {
73+
db.close();
74+
}
75+
}

0 commit comments

Comments
 (0)