@seip/bluebird-js 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/extensions.json +7 -7
- package/.vscode/settings.json +10 -10
- package/AGENTS.md +266 -116
- package/README.md +258 -245
- package/app/models/user.js +19 -0
- package/app/resources/html/home.html +137 -0
- package/app/resources/html/layouts/main.html +54 -0
- package/app/resources/html/notFound.html +14 -0
- package/app/resources/html/terms.html +46 -0
- package/app/resources/index.html +27 -0
- package/app/resources/layouts/AppLayout.jsx +89 -0
- package/app/resources/main.jsx +18 -0
- package/app/resources/pages/Dashboard.jsx +98 -0
- package/app/resources/pages/Home.jsx +140 -0
- package/app/resources/pages/Login.jsx +35 -0
- package/app/resources/pages/NotFound.jsx +16 -0
- package/app/resources/pages/Register.jsx +55 -0
- package/app/resources/routes.jsx +54 -0
- package/app/resources/spa.js +67 -0
- package/app/resources/styles.css +9 -0
- package/app/routes/api.js +85 -50
- package/app/routes/auth.js +209 -0
- package/app/routes/web.js +49 -37
- package/core/ai.d.ts +151 -151
- package/core/ai.js +1071 -1051
- package/core/app.d.ts +2 -18
- package/core/app.js +978 -792
- package/core/auth.d.ts +2 -13
- package/core/auth.js +444 -326
- package/core/cli/backup.js +167 -167
- package/core/cli/build.js +37 -0
- package/core/cli/dev.js +155 -0
- package/core/cli/doctor.js +37 -2
- package/core/cli/export.js +191 -0
- package/core/cli/init.js +101 -332
- package/core/cli/nginx.js +1 -1
- package/core/cli/templates.js +139 -0
- package/core/cli/view.js +73 -136
- package/core/config.js +44 -2
- package/core/csrf.js +156 -24
- package/core/database.js +737 -684
- package/core/debug.js +1 -1
- package/core/html.js +8 -1
- package/core/index.d.ts +1286 -1431
- package/core/paths.js +55 -0
- package/core/ratelimit.js +10 -1
- package/core/react/client/api.js +237 -0
- package/core/react/client/auth.js +236 -0
- package/core/react/client/components.js +1321 -0
- package/core/react/client/i18n.js +338 -0
- package/core/react/client/index.d.ts +249 -0
- package/core/react/client/index.js +49 -0
- package/core/react/client/router.js +26 -0
- package/core/react/client/theme.css +512 -0
- package/core/render-cache.js +250 -212
- package/core/render.d.ts +25 -21
- package/core/render.js +613 -603
- package/core/router.d.ts +2 -3
- package/core/router.js +171 -171
- package/core/seo.js +10 -0
- package/core/spa.js +599 -0
- package/core/view-loader.js +81 -455
- package/core/vite-plugin.d.ts +18 -0
- package/core/vite-plugin.js +162 -0
- package/core/vite.js +250 -145
- package/index.js +22 -18
- package/jsconfig.json +5 -3
- package/package.json +114 -92
- package/production.md +8 -3
- package/public/js/bluebird.js +1160 -0
- package/templates/_auth/app/database.js +19 -0
- package/templates/_auth/app/models/user.js +22 -0
- package/templates/_auth/app/routes/auth-router.js +304 -0
- package/templates/_base/app/resources/html/layouts/main.html +55 -0
- package/templates/_base/app/resources/html/notFound.html +14 -0
- package/templates/_base/app/resources/index.html +27 -0
- package/templates/_base/app/resources/styles.css +9 -0
- package/templates/api/app/models/note.js +18 -0
- package/templates/api/app/routes/api.js +82 -0
- package/templates/api/app/routes/auth.js +11 -0
- package/templates/api/index.js +19 -0
- package/templates/api/template.json +13 -0
- package/templates/auth/app/mail.js +11 -0
- package/templates/auth/app/models/password_reset.js +18 -0
- package/templates/auth/app/resources/html/home.html +21 -0
- package/templates/auth/app/resources/html/partials/nav.html +2 -0
- package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
- package/templates/auth/app/resources/main.jsx +18 -0
- package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
- package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
- package/templates/auth/app/resources/pages/Login.jsx +36 -0
- package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
- package/templates/auth/app/resources/pages/Profile.jsx +99 -0
- package/templates/auth/app/resources/pages/Register.jsx +50 -0
- package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
- package/templates/auth/app/resources/routes.jsx +54 -0
- package/templates/auth/app/resources/spa.js +29 -0
- package/templates/auth/app/routes/api.js +17 -0
- package/templates/auth/app/routes/auth.js +13 -0
- package/templates/auth/app/routes/web.js +21 -0
- package/templates/auth/index.js +19 -0
- package/templates/auth/template.json +15 -0
- package/templates/blank/app/models/.gitkeep +0 -0
- package/templates/blank/app/resources/html/home.html +27 -0
- package/templates/blank/app/resources/html/partials/nav.html +1 -0
- package/templates/blank/app/resources/main.jsx +16 -0
- package/templates/blank/app/resources/pages/Home.jsx +42 -0
- package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
- package/templates/blank/app/resources/routes.jsx +15 -0
- package/templates/blank/app/resources/spa.js +24 -0
- package/templates/blank/app/routes/api.js +11 -0
- package/templates/blank/app/routes/web.js +21 -0
- package/templates/blank/index.js +13 -0
- package/templates/blank/template.json +12 -0
- package/templates/blog/app/lib/blog.js +149 -0
- package/templates/blog/app/models/post.js +22 -0
- package/templates/blog/app/resources/html/home.html +28 -0
- package/templates/blog/app/resources/html/not-found.html +5 -0
- package/templates/blog/app/resources/html/partials/nav.html +2 -0
- package/templates/blog/app/resources/html/partials/post-card.html +9 -0
- package/templates/blog/app/resources/html/post.html +8 -0
- package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
- package/templates/blog/app/resources/main.jsx +18 -0
- package/templates/blog/app/resources/pages/Login.jsx +31 -0
- package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
- package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
- package/templates/blog/app/resources/pages/Posts.jsx +83 -0
- package/templates/blog/app/resources/pages/Register.jsx +43 -0
- package/templates/blog/app/resources/routes.jsx +51 -0
- package/templates/blog/app/resources/spa.js +25 -0
- package/templates/blog/app/resources/styles.css +40 -0
- package/templates/blog/app/routes/api.js +77 -0
- package/templates/blog/app/routes/auth.js +10 -0
- package/templates/blog/app/routes/web.js +114 -0
- package/templates/blog/index.js +21 -0
- package/templates/blog/template.json +14 -0
- package/vite.config.js +11 -0
- package/app/views/about.jsx +0 -98
- package/app/views/components/Header.jsx +0 -42
- package/app/views/components/LiveCounter.jsx +0 -66
- package/app/views/dashboard.jsx +0 -300
- package/app/views/index.jsx +0 -123
- package/app/views/layouts/main/view.jsx +0 -201
- package/app/views/legal/terms.html +0 -29
- package/app/views/login.jsx +0 -141
- package/core/actions.d.ts +0 -4
- package/core/actions.js +0 -329
- package/core/jsx.d.ts +0 -38
- package/core/jsx.js +0 -1432
- package/core/vite.d.ts +0 -3
- package/public/js/bluebird-client.js +0 -3161
- package/public/js/tailwind.js +0 -8
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { Button, Card, ThemeToggle, api, isExported, toast } from "@seip/bluebird-js/react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Starting page of the React SPA. Edit it and Vite hot-reloads the change.
|
|
6
|
+
*/
|
|
7
|
+
export default function Home() {
|
|
8
|
+
const [message, setMessage] = useState("");
|
|
9
|
+
|
|
10
|
+
const callApi = async () => {
|
|
11
|
+
try {
|
|
12
|
+
const data = await api.get("/api/hello");
|
|
13
|
+
setMessage(data.message);
|
|
14
|
+
toast.success("API responded", data.time);
|
|
15
|
+
} catch (err) {
|
|
16
|
+
toast.error("Request failed", err.message);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<div className="mx-auto flex min-h-screen max-w-3xl flex-col justify-center gap-6 px-4 py-16">
|
|
22
|
+
<div className="flex items-center justify-between">
|
|
23
|
+
{isExported() ? <span /> : (
|
|
24
|
+
<a href="/" className="text-sm font-medium text-muted-foreground hover:text-foreground">
|
|
25
|
+
← Back to HTML page
|
|
26
|
+
</a>
|
|
27
|
+
)}
|
|
28
|
+
<ThemeToggle />
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<Card
|
|
32
|
+
title="%%APP_TITLE%%"
|
|
33
|
+
description="React 19 + React Router + Tailwind CSS v4. This page lives in app/resources/pages/Home.jsx."
|
|
34
|
+
>
|
|
35
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
36
|
+
<Button onClick={callApi}>Call /api/hello</Button>
|
|
37
|
+
{message ? <span className="text-sm text-muted-foreground">{message}</span> : null}
|
|
38
|
+
</div>
|
|
39
|
+
</Card>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Link, isExported } from "@seip/bluebird-js/react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fallback page for unmatched routes inside the React SPA (`/app/*`).
|
|
5
|
+
*/
|
|
6
|
+
export default function NotFound() {
|
|
7
|
+
return (
|
|
8
|
+
<main className="mx-auto flex min-h-screen max-w-md flex-col items-center justify-center px-6 text-center">
|
|
9
|
+
<span className="badge">
|
|
10
|
+
<span className="badge-dot bg-warning" aria-hidden="true" />
|
|
11
|
+
404
|
|
12
|
+
</span>
|
|
13
|
+
<h1 className="mt-4 text-3xl font-semibold tracking-tight text-foreground">Page not found</h1>
|
|
14
|
+
<p className="mt-3 text-muted-foreground">This page does not exist in the app.</p>
|
|
15
|
+
<div className="mt-8 flex flex-wrap justify-center gap-2.5">
|
|
16
|
+
<Link to="/" className="btn btn-primary">
|
|
17
|
+
Back to the app
|
|
18
|
+
</Link>
|
|
19
|
+
{isExported() ? null : (
|
|
20
|
+
<a href="/" className="btn btn-outline">
|
|
21
|
+
Home page
|
|
22
|
+
</a>
|
|
23
|
+
)}
|
|
24
|
+
</div>
|
|
25
|
+
</main>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRouter } from "@seip/bluebird-js/react";
|
|
2
|
+
import Home from "./pages/Home.jsx";
|
|
3
|
+
import NotFound from "./pages/NotFound.jsx";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React Router configuration: browser router under `/app` on the server, hash router in static exports.
|
|
7
|
+
* Add pages to `app/resources/pages/` and register them here.
|
|
8
|
+
*/
|
|
9
|
+
export const router = createRouter(
|
|
10
|
+
[
|
|
11
|
+
{ path: "/", element: <Home /> },
|
|
12
|
+
{ path: "*", element: <NotFound /> },
|
|
13
|
+
],
|
|
14
|
+
{ basename: "/app" }
|
|
15
|
+
);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React SPA configuration shared by `routerWeb.spa(spa.mount, spa)` and `npx bluebird-js export`.
|
|
3
|
+
* @type {import('@seip/bluebird-js/core/index.js').SpaOptions & { mount: string }}
|
|
4
|
+
*/
|
|
5
|
+
const spa = {
|
|
6
|
+
mount: "/app",
|
|
7
|
+
lang: "en",
|
|
8
|
+
titleTemplate: "%s — %%APP_TITLE%%",
|
|
9
|
+
title: "App",
|
|
10
|
+
description: "%%APP_TITLE%% React application.",
|
|
11
|
+
canonical: true,
|
|
12
|
+
head: [{ tag: "link", rel: "icon", href: "/images/favicon.ico" }],
|
|
13
|
+
htmlAttrs: { class: "dark", "data-theme": "dark" },
|
|
14
|
+
bodyAttrs: {
|
|
15
|
+
class: "min-h-screen bg-app text-foreground antialiased",
|
|
16
|
+
},
|
|
17
|
+
skeleton: "page",
|
|
18
|
+
meta: {
|
|
19
|
+
"*": { title: "App", robots: "noindex, nofollow" },
|
|
20
|
+
"/": { title: "Home", robots: "index, follow" },
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default spa;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Router from "@seip/bluebird-js/core/router.js";
|
|
2
|
+
import Csrf from "@seip/bluebird-js/core/csrf.js";
|
|
3
|
+
|
|
4
|
+
const routerApi = new Router("/api");
|
|
5
|
+
routerApi.use(Csrf.middleware());
|
|
6
|
+
|
|
7
|
+
routerApi.get("/hello", (req, res) => {
|
|
8
|
+
res.ok({ message: "Hello from the Bluebird API", time: new Date().toISOString() });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export default routerApi;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Router from "@seip/bluebird-js/core/router.js";
|
|
2
|
+
import Render from "@seip/bluebird-js/core/render.js";
|
|
3
|
+
import spa from "../resources/spa.js";
|
|
4
|
+
|
|
5
|
+
const routerWeb = new Router("/", { seo: true });
|
|
6
|
+
|
|
7
|
+
routerWeb.get("/", (req, res) =>
|
|
8
|
+
Render.html(
|
|
9
|
+
res,
|
|
10
|
+
"home",
|
|
11
|
+
{
|
|
12
|
+
title: "%%APP_TITLE%%",
|
|
13
|
+
description: "Server-rendered HTML page powered by Bluebird.js.",
|
|
14
|
+
},
|
|
15
|
+
{ layout: "main", cache: 300 }
|
|
16
|
+
)
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
routerWeb.spa(spa.mount, spa);
|
|
20
|
+
|
|
21
|
+
export default routerWeb;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import App from "@seip/bluebird-js/core/app.js";
|
|
2
|
+
import routerApi from "./app/routes/api.js";
|
|
3
|
+
import routerWeb from "./app/routes/web.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Application entrypoint: HTML {{ }} pages from `app/resources/html/` and the React SPA from `app/resources/`.
|
|
7
|
+
*/
|
|
8
|
+
const app = new App({
|
|
9
|
+
routes: [routerWeb, routerApi],
|
|
10
|
+
port: Number(process.env.PORT || 3000),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.run();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "HTML {{ }} page on / and a one-page React SPA on /app/*",
|
|
3
|
+
"layers": [
|
|
4
|
+
"_base"
|
|
5
|
+
],
|
|
6
|
+
"client": true,
|
|
7
|
+
"database": "optional",
|
|
8
|
+
"routes": [
|
|
9
|
+
"/ HTML {{ }} page (app/resources/html/home.html)",
|
|
10
|
+
"/app/* React SPA with one page (app/resources/pages/Home.jsx)"
|
|
11
|
+
]
|
|
12
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import Render from "@seip/bluebird-js/core/render.js";
|
|
2
|
+
import Seo from "@seip/bluebird-js/core/seo.js";
|
|
3
|
+
import { escapeHtml } from "@seip/bluebird-js/core/html.js";
|
|
4
|
+
import Post from "../models/post.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Turns a title into a URL slug (`"Hello, World!"` → `"hello-world"`).
|
|
8
|
+
* @param {string} text
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
export function slugify(text) {
|
|
12
|
+
return String(text || "")
|
|
13
|
+
.normalize("NFKD")
|
|
14
|
+
.replace(/[̀-ͯ]/g, "")
|
|
15
|
+
.toLowerCase()
|
|
16
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
17
|
+
.replace(/^-+|-+$/g, "")
|
|
18
|
+
.slice(0, 200) || "post";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Returns a slug that no other post uses, appending `-2`, `-3`... when needed.
|
|
23
|
+
* @param {string} base
|
|
24
|
+
* @param {number|string|null} [ignoreId=null] - Post being edited.
|
|
25
|
+
* @returns {Promise<string>}
|
|
26
|
+
*/
|
|
27
|
+
export async function uniqueSlug(base, ignoreId = null) {
|
|
28
|
+
const root = slugify(base);
|
|
29
|
+
let candidate = root;
|
|
30
|
+
for (let n = 2; ; n++) {
|
|
31
|
+
const found = await Post.find({ slug: candidate }, { withDeleted: true });
|
|
32
|
+
if (!found || String(found.id) === String(ignoreId)) return candidate;
|
|
33
|
+
candidate = `${root}-${n}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Formats inline Markdown (`code`, **bold**, *italic*, [links](https://...)) on already escaped text.
|
|
39
|
+
* @param {string} escaped
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
function inline(escaped) {
|
|
43
|
+
return escaped
|
|
44
|
+
.replace(/`([^`]+)`/g, "<code>$1</code>")
|
|
45
|
+
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
|
|
46
|
+
.replace(/\*([^*]+)\*/g, "<em>$1</em>")
|
|
47
|
+
.replace(/\[([^\]]+)\]\(((?:https?:\/\/|\/|mailto:)[^)\s]+)\)/g, '<a href="$2" rel="noopener">$1</a>');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Renders a safe Markdown subset to HTML: `##`/`###` headings (`#` also maps to `<h2>`, the post title is the `<h1>`), paragraphs, `-` lists, `>` quotes,
|
|
52
|
+
* fenced code blocks, and inline formatting. Everything is HTML-escaped first, so raw HTML never passes through.
|
|
53
|
+
* @param {string} source
|
|
54
|
+
* @returns {string}
|
|
55
|
+
*/
|
|
56
|
+
export function renderMarkdown(source) {
|
|
57
|
+
const lines = String(source || "").replace(/\r\n/g, "\n").split("\n");
|
|
58
|
+
const html = [];
|
|
59
|
+
let paragraph = [];
|
|
60
|
+
let list = [];
|
|
61
|
+
let code = null;
|
|
62
|
+
|
|
63
|
+
const flush = () => {
|
|
64
|
+
if (paragraph.length) html.push(`<p>${inline(escapeHtml(paragraph.join(" ")))}</p>`);
|
|
65
|
+
if (list.length) html.push(`<ul>${list.map((item) => `<li>${inline(escapeHtml(item))}</li>`).join("")}</ul>`);
|
|
66
|
+
paragraph = [];
|
|
67
|
+
list = [];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
for (const line of lines) {
|
|
71
|
+
if (code !== null) {
|
|
72
|
+
if (line.startsWith("```")) {
|
|
73
|
+
html.push(`<pre><code>${escapeHtml(code.join("\n"))}</code></pre>`);
|
|
74
|
+
code = null;
|
|
75
|
+
} else {
|
|
76
|
+
code.push(line);
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (line.startsWith("```")) {
|
|
81
|
+
flush();
|
|
82
|
+
code = [];
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const heading = /^(#{1,3})\s+(.+)$/.exec(line);
|
|
86
|
+
if (heading) {
|
|
87
|
+
flush();
|
|
88
|
+
const level = Math.max(2, heading[1].length);
|
|
89
|
+
html.push(`<h${level}>${inline(escapeHtml(heading[2]))}</h${level}>`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (/^[-*]\s+/.test(line)) {
|
|
93
|
+
if (paragraph.length) flush();
|
|
94
|
+
list.push(line.replace(/^[-*]\s+/, ""));
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (line.startsWith(">")) {
|
|
98
|
+
flush();
|
|
99
|
+
html.push(`<blockquote>${inline(escapeHtml(line.replace(/^>\s?/, "")))}</blockquote>`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!line.trim()) {
|
|
103
|
+
flush();
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (list.length) flush();
|
|
107
|
+
paragraph.push(line.trim());
|
|
108
|
+
}
|
|
109
|
+
if (code !== null) html.push(`<pre><code>${escapeHtml(code.join("\n"))}</code></pre>`);
|
|
110
|
+
flush();
|
|
111
|
+
return html.join("\n");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Formats an ISO date for post listings.
|
|
116
|
+
* @param {string|null|undefined} iso
|
|
117
|
+
* @returns {string}
|
|
118
|
+
*/
|
|
119
|
+
export function formatDate(iso) {
|
|
120
|
+
const date = iso ? new Date(iso) : null;
|
|
121
|
+
if (!date || Number.isNaN(date.getTime())) return "";
|
|
122
|
+
return date.toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Registers every published post in `/sitemap.xml`.
|
|
127
|
+
* @returns {Promise<void>}
|
|
128
|
+
*/
|
|
129
|
+
export async function syncSitemap() {
|
|
130
|
+
const posts = await Post.all({ where: { status: "published" } });
|
|
131
|
+
for (const post of posts) Seo.registerRoute("/", `/blog/${post.slug}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Refreshes caches and the sitemap after a post changes.
|
|
136
|
+
* @param {{ slug: string, status: string }|null} post - Current state (null when deleted).
|
|
137
|
+
* @param {string|null} [previousSlug=null]
|
|
138
|
+
* @returns {Promise<void>}
|
|
139
|
+
*/
|
|
140
|
+
export async function postChanged(post, previousSlug = null) {
|
|
141
|
+
const paths = ["/", "/feed.xml"];
|
|
142
|
+
for (const slug of [previousSlug, post?.slug]) {
|
|
143
|
+
if (!slug) continue;
|
|
144
|
+
paths.push(`/blog/${slug}`);
|
|
145
|
+
Seo.unregisterRoute(`/blog/${slug}`);
|
|
146
|
+
}
|
|
147
|
+
if (post && post.status === "published") Seo.registerRoute("/", `/blog/${post.slug}`);
|
|
148
|
+
await Render.invalidate(paths);
|
|
149
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Model from "@seip/bluebird-js/core/model.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Blog post. `body` is written in a small Markdown subset (see `app/lib/blog.js`).
|
|
5
|
+
*/
|
|
6
|
+
export default class Post extends Model {
|
|
7
|
+
static table = "posts";
|
|
8
|
+
static primaryKey = "id";
|
|
9
|
+
static timestamps = true;
|
|
10
|
+
static softDelete = true;
|
|
11
|
+
|
|
12
|
+
static schema = {
|
|
13
|
+
id: { type: "int", primary: true, auto: true },
|
|
14
|
+
user_id: { type: "int", required: true },
|
|
15
|
+
title: { type: "string", length: 200, required: true },
|
|
16
|
+
slug: { type: "string", length: 220, unique: true, required: true },
|
|
17
|
+
excerpt: { type: "string", length: 300 },
|
|
18
|
+
body: { type: "text", required: true },
|
|
19
|
+
status: { type: "string", length: 20, default: "draft", in: ["draft", "published"] },
|
|
20
|
+
published_at: { type: "string", length: 40 },
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<section class="mb-10">
|
|
2
|
+
<h1 class="text-4xl font-semibold tracking-tight text-foreground md:text-5xl">%%APP_TITLE%%</h1>
|
|
3
|
+
<p class="mt-3 text-lg text-muted-foreground">{{ description }}</p>
|
|
4
|
+
</section>
|
|
5
|
+
|
|
6
|
+
<section class="space-y-4">
|
|
7
|
+
{{#each posts}}
|
|
8
|
+
{{> post-card }}
|
|
9
|
+
{{else}}
|
|
10
|
+
<div class="rounded-2xl border border-dashed border-border p-10 text-center">
|
|
11
|
+
<p class="text-muted-foreground">No posts yet.</p>
|
|
12
|
+
<a href="/app" class="mt-3 inline-block text-sm font-semibold text-foreground underline-offset-4 hover:underline">Write the first one</a>
|
|
13
|
+
</div>
|
|
14
|
+
{{/each}}
|
|
15
|
+
</section>
|
|
16
|
+
|
|
17
|
+
{{#if prevPage}}
|
|
18
|
+
<nav class="mt-10 flex justify-between">
|
|
19
|
+
<a href="/?page={{ prevPage }}" class="text-sm font-semibold text-foreground underline-offset-4 hover:underline">← Newer posts</a>
|
|
20
|
+
{{#if nextPage}}<a href="/?page={{ nextPage }}" class="text-sm font-semibold text-foreground underline-offset-4 hover:underline">Older posts →</a>{{/if}}
|
|
21
|
+
</nav>
|
|
22
|
+
{{else}}
|
|
23
|
+
{{#if nextPage}}
|
|
24
|
+
<nav class="mt-10 flex justify-end">
|
|
25
|
+
<a href="/?page={{ nextPage }}" class="text-sm font-semibold text-foreground underline-offset-4 hover:underline">Older posts →</a>
|
|
26
|
+
</nav>
|
|
27
|
+
{{/if}}
|
|
28
|
+
{{/if}}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<section class="mx-auto max-w-md py-16 text-center">
|
|
2
|
+
<h1 class="text-3xl font-semibold text-foreground">Post not found</h1>
|
|
3
|
+
<p class="mt-3 text-muted-foreground">It may have been moved or unpublished.</p>
|
|
4
|
+
<a href="/" class="btn btn-primary mt-6">Back to the blog</a>
|
|
5
|
+
</section>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<article class="card p-6 transition hover:border-ring/50">
|
|
2
|
+
<p class="text-xs font-medium uppercase tracking-wide text-muted-foreground">{{ date }}</p>
|
|
3
|
+
<h2 class="mt-1 text-xl font-bold text-foreground">
|
|
4
|
+
<a href="/blog/{{ slug }}" class="hover:text-foreground">{{ title }}</a>
|
|
5
|
+
</h2>
|
|
6
|
+
{{#if excerpt}}
|
|
7
|
+
<p class="mt-2 text-sm text-muted-foreground">{{ excerpt }}</p>
|
|
8
|
+
{{/if}}
|
|
9
|
+
</article>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<article class="mx-auto max-w-2xl">
|
|
2
|
+
<a href="/" class="text-sm font-medium text-muted-foreground hover:text-foreground">← All posts</a>
|
|
3
|
+
<h1 class="mt-4 text-4xl font-semibold tracking-tight text-foreground">{{ post.title }}</h1>
|
|
4
|
+
<p class="mt-2 text-sm text-muted-foreground">{{ post.date }}</p>
|
|
5
|
+
<div class="post-body mt-8">
|
|
6
|
+
{{{ bodyHtml }}}
|
|
7
|
+
</div>
|
|
8
|
+
</article>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Outlet } from "react-router";
|
|
2
|
+
import { Button, Link, ProgressBar, ThemeToggle, isExported, toast, useAuth } from "@seip/bluebird-js/react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shell of the blog admin: navigation, session actions, and the routed page.
|
|
6
|
+
*/
|
|
7
|
+
export default function AdminLayout() {
|
|
8
|
+
const { user, logout } = useAuth();
|
|
9
|
+
|
|
10
|
+
const handleLogout = async () => {
|
|
11
|
+
await logout();
|
|
12
|
+
toast.info("Signed out");
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<div className="flex min-h-screen flex-col">
|
|
17
|
+
<ProgressBar />
|
|
18
|
+
<header className="sticky top-0 z-30 border-b border-border/70 glass">
|
|
19
|
+
<nav className="mx-auto flex max-w-5xl items-center justify-between gap-4 px-4 py-3 md:px-6">
|
|
20
|
+
<span className="text-lg">
|
|
21
|
+
<span className="brand">%%APP_TITLE%%</span> <span className="text-muted-foreground">Admin</span>
|
|
22
|
+
</span>
|
|
23
|
+
<div className="flex items-center gap-1.5 overflow-x-auto">
|
|
24
|
+
{user ? (
|
|
25
|
+
<>
|
|
26
|
+
<Link to="/posts" exact className="nav-link">
|
|
27
|
+
Posts
|
|
28
|
+
</Link>
|
|
29
|
+
<Link to="/posts/new" className="nav-link">
|
|
30
|
+
New post
|
|
31
|
+
</Link>
|
|
32
|
+
</>
|
|
33
|
+
) : null}
|
|
34
|
+
{isExported() ? null : (
|
|
35
|
+
<a href="/" className="nav-link">
|
|
36
|
+
View blog
|
|
37
|
+
</a>
|
|
38
|
+
)}
|
|
39
|
+
{user ? (
|
|
40
|
+
<Button size="sm" variant="outline" onClick={handleLogout} className="ml-1">
|
|
41
|
+
Sign out
|
|
42
|
+
</Button>
|
|
43
|
+
) : null}
|
|
44
|
+
<ThemeToggle className="ml-1" />
|
|
45
|
+
</div>
|
|
46
|
+
</nav>
|
|
47
|
+
</header>
|
|
48
|
+
|
|
49
|
+
<main className="mx-auto w-full max-w-5xl flex-1 px-4 py-10 md:px-6">
|
|
50
|
+
<Outlet />
|
|
51
|
+
</main>
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { StrictMode } from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import { RouterProvider } from "react-router/dom";
|
|
4
|
+
import { AuthProvider, Toaster } from "@seip/bluebird-js/react";
|
|
5
|
+
import "./styles.css";
|
|
6
|
+
import { router } from "./routes.jsx";
|
|
7
|
+
|
|
8
|
+
const rootEl = document.getElementById("root");
|
|
9
|
+
if (rootEl) {
|
|
10
|
+
createRoot(rootEl).render(
|
|
11
|
+
<StrictMode>
|
|
12
|
+
<AuthProvider>
|
|
13
|
+
<RouterProvider router={router} />
|
|
14
|
+
<Toaster />
|
|
15
|
+
</AuthProvider>
|
|
16
|
+
</StrictMode>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Card, Form, Input, Link, SubmitButton, useAuth } from "@seip/bluebird-js/react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Admin sign-in page. While no account exists, it points to the one-time admin registration.
|
|
5
|
+
*/
|
|
6
|
+
export default function Login() {
|
|
7
|
+
const { login, registerEnabled } = useAuth();
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<div className="mx-auto max-w-md py-6">
|
|
11
|
+
<Card variant="glass" title="Sign in" description="Manage your blog posts.">
|
|
12
|
+
<Form onSubmitRequest={(values) => login(values)} successMessage="Signed in" className="space-y-4">
|
|
13
|
+
<Input name="email" type="email" label="Email" autoComplete="email" required />
|
|
14
|
+
<Input name="password" type="password" label="Password" autoComplete="current-password" required />
|
|
15
|
+
<SubmitButton className="w-full" loadingText="Signing in...">
|
|
16
|
+
Sign in
|
|
17
|
+
</SubmitButton>
|
|
18
|
+
</Form>
|
|
19
|
+
|
|
20
|
+
{registerEnabled ? (
|
|
21
|
+
<p className="mt-4 text-center text-sm text-muted-foreground">
|
|
22
|
+
First time here?{" "}
|
|
23
|
+
<Link to="/register" className="font-semibold text-foreground underline-offset-4 hover:underline">
|
|
24
|
+
Create the admin account
|
|
25
|
+
</Link>
|
|
26
|
+
</p>
|
|
27
|
+
) : null}
|
|
28
|
+
</Card>
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Card, Link } from "@seip/bluebird-js/react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fallback page for unknown routes inside `/app`.
|
|
5
|
+
*/
|
|
6
|
+
export default function NotFound() {
|
|
7
|
+
return (
|
|
8
|
+
<div className="mx-auto max-w-md py-12">
|
|
9
|
+
<Card title="Page not found" description="This page does not exist.">
|
|
10
|
+
<Link to="/" className="text-sm font-semibold text-foreground underline-offset-4 hover:underline">
|
|
11
|
+
Go home
|
|
12
|
+
</Link>
|
|
13
|
+
</Card>
|
|
14
|
+
</div>
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useNavigate, useParams } from "react-router";
|
|
3
|
+
import { Card, Form, Input, PageSkeleton, Select, SubmitButton, Textarea, api, toast } from "@seip/bluebird-js/react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates (`/posts/new`) or edits (`/posts/:id`) a post.
|
|
7
|
+
*/
|
|
8
|
+
export default function PostEditor() {
|
|
9
|
+
const { id } = useParams();
|
|
10
|
+
const navigate = useNavigate();
|
|
11
|
+
const [post, setPost] = useState(id ? null : {});
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!id) {
|
|
15
|
+
setPost({});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
api
|
|
19
|
+
.get(`/api/posts/${id}`)
|
|
20
|
+
.then(setPost)
|
|
21
|
+
.catch((err) => {
|
|
22
|
+
toast.error("Could not load the post", err.message);
|
|
23
|
+
navigate("/posts", { replace: true });
|
|
24
|
+
});
|
|
25
|
+
}, [id, navigate]);
|
|
26
|
+
|
|
27
|
+
if (post === null) return <PageSkeleton />;
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Card title={id ? "Edit post" : "New post"} description="Body supports ## headings, **bold**, *italic*, `code`, [links](https://...), - lists, > quotes, and ``` code blocks.">
|
|
31
|
+
<Form
|
|
32
|
+
key={post.id || "new"}
|
|
33
|
+
url={id ? `/api/posts/${id}` : "/api/posts"}
|
|
34
|
+
method={id ? "PUT" : "POST"}
|
|
35
|
+
successMessage={id ? "Post saved" : "Post created"}
|
|
36
|
+
onSuccess={() => navigate("/posts")}
|
|
37
|
+
className="space-y-4"
|
|
38
|
+
>
|
|
39
|
+
<Input name="title" label="Title" defaultValue={post.title} required />
|
|
40
|
+
<Input name="slug" label="Slug" hint="Leave empty to generate it from the title" defaultValue={post.slug} />
|
|
41
|
+
<Input name="excerpt" label="Excerpt" hint="Shown in listings, RSS, and search results" defaultValue={post.excerpt} />
|
|
42
|
+
<Textarea name="body" label="Body" rows={16} defaultValue={post.body} required />
|
|
43
|
+
<Select name="status" label="Status" defaultValue={post.status || "draft"}>
|
|
44
|
+
<option value="draft">Draft</option>
|
|
45
|
+
<option value="published">Published</option>
|
|
46
|
+
</Select>
|
|
47
|
+
<SubmitButton loadingText="Saving...">{id ? "Save changes" : "Create post"}</SubmitButton>
|
|
48
|
+
</Form>
|
|
49
|
+
</Card>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { Badge, Button, Card, Link, Skeleton, api, isExported, toast } from "@seip/bluebird-js/react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lists every post (drafts included) with edit and delete actions.
|
|
6
|
+
*/
|
|
7
|
+
export default function Posts() {
|
|
8
|
+
const [posts, setPosts] = useState(null);
|
|
9
|
+
|
|
10
|
+
const load = useCallback(async () => {
|
|
11
|
+
try {
|
|
12
|
+
const list = await api.get("/api/posts");
|
|
13
|
+
setPosts(Array.isArray(list) ? list : []);
|
|
14
|
+
} catch (err) {
|
|
15
|
+
toast.error("Could not load posts", err.message);
|
|
16
|
+
setPosts([]);
|
|
17
|
+
}
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
load();
|
|
22
|
+
}, [load]);
|
|
23
|
+
|
|
24
|
+
const remove = async (post) => {
|
|
25
|
+
if (!window.confirm(`Delete "${post.title}"?`)) return;
|
|
26
|
+
try {
|
|
27
|
+
await api.delete(`/api/posts/${post.id}`);
|
|
28
|
+
toast.success("Post deleted");
|
|
29
|
+
load();
|
|
30
|
+
} catch (err) {
|
|
31
|
+
toast.error("Could not delete the post", err.message);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div className="space-y-6">
|
|
37
|
+
<div className="flex items-center justify-between gap-4">
|
|
38
|
+
<h1 className="text-3xl font-semibold tracking-tight text-foreground">Posts</h1>
|
|
39
|
+
<Link
|
|
40
|
+
to="/posts/new"
|
|
41
|
+
className="btn btn-primary"
|
|
42
|
+
>
|
|
43
|
+
New post
|
|
44
|
+
</Link>
|
|
45
|
+
</div>
|
|
46
|
+
|
|
47
|
+
<Card>
|
|
48
|
+
{posts === null ? (
|
|
49
|
+
<div className="space-y-3">
|
|
50
|
+
<Skeleton className="h-10 w-full" />
|
|
51
|
+
<Skeleton className="h-10 w-full" />
|
|
52
|
+
</div>
|
|
53
|
+
) : posts.length === 0 ? (
|
|
54
|
+
<p className="text-sm text-muted-foreground">No posts yet. Write your first one.</p>
|
|
55
|
+
) : (
|
|
56
|
+
<ul className="divide-y divide-border">
|
|
57
|
+
{posts.map((post) => (
|
|
58
|
+
<li key={post.id} className="flex flex-wrap items-center justify-between gap-3 py-3">
|
|
59
|
+
<div className="min-w-0">
|
|
60
|
+
<Link to={`/posts/${post.id}`} className="font-semibold text-foreground hover:text-foreground">
|
|
61
|
+
{post.title}
|
|
62
|
+
</Link>
|
|
63
|
+
<p className="truncate text-xs text-muted-foreground">/blog/{post.slug}</p>
|
|
64
|
+
</div>
|
|
65
|
+
<div className="flex items-center gap-2">
|
|
66
|
+
<Badge tone={post.status === "published" ? "success" : "warning"} soft>{post.status}</Badge>
|
|
67
|
+
{post.status === "published" && !isExported() ? (
|
|
68
|
+
<a href={`/blog/${post.slug}`} target="_blank" rel="noreferrer" className="text-sm text-foreground underline-offset-4 hover:underline">
|
|
69
|
+
View
|
|
70
|
+
</a>
|
|
71
|
+
) : null}
|
|
72
|
+
<Button size="sm" variant="ghost" onClick={() => remove(post)}>
|
|
73
|
+
Delete
|
|
74
|
+
</Button>
|
|
75
|
+
</div>
|
|
76
|
+
</li>
|
|
77
|
+
))}
|
|
78
|
+
</ul>
|
|
79
|
+
)}
|
|
80
|
+
</Card>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|