Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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/
');
Original file line number Diff line number Diff line change
@@ -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 !
Expand All @@ -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)

');
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions scripts/site-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down
46 changes: 46 additions & 0 deletions scripts/site/content.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
75 changes: 75 additions & 0 deletions scripts/site/documentation.mjs
Original file line number Diff line number Diff line change
@@ -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();
}
}
37 changes: 37 additions & 0 deletions tests/site/seed.test.mjs
Original file line number Diff line number Diff line change
@@ -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/,
);
});
Loading