deneljs 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +57 -2
- package/drizzle/0000_lonely_reptil.sql +27 -0
- package/drizzle/0001_misty_wendigo.sql +74 -0
- package/drizzle/meta/0000_snapshot.json +179 -0
- package/drizzle/meta/0001_snapshot.json +492 -0
- package/drizzle/meta/_journal.json +20 -0
- package/package.json +55 -4
- package/src/admin/auth.ts +851 -0
- package/src/admin/layout.ts +419 -0
- package/src/admin/pages/categories.ts +223 -0
- package/src/admin/pages/dashboard.ts +192 -0
- package/src/admin/pages/media.ts +270 -0
- package/src/admin/pages/posts.ts +804 -0
- package/src/admin/pages/settings.ts +169 -0
- package/src/admin/pages/themes.ts +461 -0
- package/src/admin/pages/users.ts +501 -0
- package/src/admin/routes.ts +455 -0
- package/src/admin/tiptap-bundle.ts +48 -0
- package/src/admin/tiptap-entry.ts +226 -0
- package/src/ai/generator.ts +296 -0
- package/src/ai/index.ts +22 -0
- package/src/ai/schema.ts +13 -0
- package/src/ai/spec.ts +193 -0
- package/src/cli.ts +151 -0
- package/src/db/index.ts +278 -0
- package/src/db/schema.ts +115 -0
- package/src/engine/index.ts +52 -0
- package/src/engine/renderer.ts +533 -0
- package/src/engine/sandbox.ts +108 -0
- package/src/engine/schema.ts +71 -0
- package/src/engine/validator.ts +404 -0
- package/src/routes/admin-api.ts +1035 -0
- package/src/routes/posts.ts +294 -0
- package/src/routes/uploads.ts +119 -0
- package/src/server.ts +672 -0
- package/src/utils/sanitize-html.ts +145 -0
- package/themes/.gitkeep +1 -0
- package/themes/default/assets/style.css +799 -0
- package/themes/default/layouts/base.liquid +31 -0
- package/themes/default/layouts/home.liquid +180 -0
- package/themes/default/layouts/page.liquid +21 -0
- package/themes/default/layouts/post.liquid +55 -0
- package/themes/default/partials/footer.liquid +33 -0
- package/themes/default/partials/navbar.liquid +49 -0
- package/themes/default/theme.json +59 -0
package/src/db/schema.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
2
|
+
|
|
3
|
+
export const userRoles = ["admin", "editor", "author"] as const;
|
|
4
|
+
export type UserRole = (typeof userRoles)[number];
|
|
5
|
+
|
|
6
|
+
export const userStatuses = ["active", "suspended"] as const;
|
|
7
|
+
export type UserStatus = (typeof userStatuses)[number];
|
|
8
|
+
|
|
9
|
+
export const postStatuses = ["draft", "published"] as const;
|
|
10
|
+
export type PostStatus = (typeof postStatuses)[number];
|
|
11
|
+
|
|
12
|
+
/** CMS users with role-based access control. */
|
|
13
|
+
export const users = sqliteTable("users", {
|
|
14
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
15
|
+
name: text("name").notNull(),
|
|
16
|
+
email: text("email").notNull().unique(),
|
|
17
|
+
password_hash: text("password_hash").notNull(),
|
|
18
|
+
role: text("role", { enum: userRoles }).notNull().default("author"),
|
|
19
|
+
avatar_url: text("avatar_url").notNull().default(""),
|
|
20
|
+
status: text("status", { enum: userStatuses }).notNull().default("active"),
|
|
21
|
+
created_at: integer("created_at")
|
|
22
|
+
.notNull()
|
|
23
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/** Post categories. */
|
|
27
|
+
export const categories = sqliteTable("categories", {
|
|
28
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
29
|
+
name: text("name").notNull(),
|
|
30
|
+
slug: text("slug").notNull().unique(),
|
|
31
|
+
description: text("description").notNull().default(""),
|
|
32
|
+
created_at: integer("created_at")
|
|
33
|
+
.notNull()
|
|
34
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** Site-wide key-value settings. */
|
|
38
|
+
export const settings = sqliteTable("settings", {
|
|
39
|
+
key: text("key").primaryKey(),
|
|
40
|
+
value: text("value").notNull(),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Blog posts and page content. */
|
|
44
|
+
export const posts = sqliteTable("posts", {
|
|
45
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
46
|
+
slug: text("slug").notNull().unique(),
|
|
47
|
+
title: text("title").notNull(),
|
|
48
|
+
content: text("content").notNull(),
|
|
49
|
+
excerpt: text("excerpt").notNull().default(""),
|
|
50
|
+
featured_image: text("featured_image").notNull().default(""),
|
|
51
|
+
category_id: integer("category_id").references(() => categories.id, {
|
|
52
|
+
onDelete: "set null",
|
|
53
|
+
}),
|
|
54
|
+
author_id: integer("author_id").references(() => users.id, {
|
|
55
|
+
onDelete: "set null",
|
|
56
|
+
}),
|
|
57
|
+
meta_title: text("meta_title").notNull().default(""),
|
|
58
|
+
meta_description: text("meta_description").notNull().default(""),
|
|
59
|
+
status: text("status", { enum: postStatuses }).notNull().default("draft"),
|
|
60
|
+
created_at: integer("created_at")
|
|
61
|
+
.notNull()
|
|
62
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
63
|
+
updated_at: integer("updated_at")
|
|
64
|
+
.notNull()
|
|
65
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/** Uploaded media files stored in public/uploads/. */
|
|
69
|
+
export const media = sqliteTable("media", {
|
|
70
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
71
|
+
filename: text("filename").notNull().unique(),
|
|
72
|
+
original_name: text("original_name").notNull(),
|
|
73
|
+
mime_type: text("mime_type").notNull(),
|
|
74
|
+
size_bytes: integer("size_bytes").notNull(),
|
|
75
|
+
url: text("url").notNull(),
|
|
76
|
+
alt_text: text("alt_text").notNull().default(""),
|
|
77
|
+
uploaded_by: integer("uploaded_by").references(() => users.id, {
|
|
78
|
+
onDelete: "set null",
|
|
79
|
+
}),
|
|
80
|
+
created_at: integer("created_at")
|
|
81
|
+
.notNull()
|
|
82
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
/** Registered themes and their JSON configuration payloads. */
|
|
86
|
+
export const themes = sqliteTable("themes", {
|
|
87
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
88
|
+
slug: text("slug").notNull().unique(),
|
|
89
|
+
name: text("name").notNull(),
|
|
90
|
+
version: text("version").notNull(),
|
|
91
|
+
author: text("author").notNull(),
|
|
92
|
+
config_json: text("config_json").notNull().default("{}"),
|
|
93
|
+
custom_settings_json: text("custom_settings_json").notNull().default("{}"),
|
|
94
|
+
created_at: integer("created_at")
|
|
95
|
+
.notNull()
|
|
96
|
+
.$defaultFn(() => Math.floor(Date.now() / 1000)),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export type User = typeof users.$inferSelect;
|
|
100
|
+
export type NewUser = typeof users.$inferInsert;
|
|
101
|
+
|
|
102
|
+
export type Category = typeof categories.$inferSelect;
|
|
103
|
+
export type NewCategory = typeof categories.$inferInsert;
|
|
104
|
+
|
|
105
|
+
export type Setting = typeof settings.$inferSelect;
|
|
106
|
+
export type NewSetting = typeof settings.$inferInsert;
|
|
107
|
+
|
|
108
|
+
export type Post = typeof posts.$inferSelect;
|
|
109
|
+
export type NewPost = typeof posts.$inferInsert;
|
|
110
|
+
|
|
111
|
+
export type MediaItem = typeof media.$inferSelect;
|
|
112
|
+
export type NewMediaItem = typeof media.$inferInsert;
|
|
113
|
+
|
|
114
|
+
export type Theme = typeof themes.$inferSelect;
|
|
115
|
+
export type NewTheme = typeof themes.$inferInsert;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export {
|
|
2
|
+
clearAllThemeCaches,
|
|
3
|
+
clearThemeCache,
|
|
4
|
+
createRenderer,
|
|
5
|
+
mergeThemeSettings,
|
|
6
|
+
renderTemplate,
|
|
7
|
+
renderThemePage,
|
|
8
|
+
resolveThemeAssetPath,
|
|
9
|
+
resolveThemeDirectory,
|
|
10
|
+
THEMES_ROOT,
|
|
11
|
+
type PagePaginationContext,
|
|
12
|
+
type PageTemplateContext,
|
|
13
|
+
type PostTemplateItem,
|
|
14
|
+
type RendererOptions,
|
|
15
|
+
type RenderThemeContext,
|
|
16
|
+
type SiteCategoryItem,
|
|
17
|
+
type SiteTemplateContext,
|
|
18
|
+
type WhitelistedTemplateScope,
|
|
19
|
+
} from "./renderer";
|
|
20
|
+
export {
|
|
21
|
+
getThemeConfigErrors,
|
|
22
|
+
LIQUID_FILENAME_PATTERN,
|
|
23
|
+
parseThemeConfig,
|
|
24
|
+
THEME_SLUG_PATTERN,
|
|
25
|
+
ThemeConfigSchema,
|
|
26
|
+
ThemeLayoutsSchema,
|
|
27
|
+
ThemeSettingFieldSchema,
|
|
28
|
+
ThemeSettingTypeSchema,
|
|
29
|
+
type ThemeConfig,
|
|
30
|
+
type ThemeLayoutName,
|
|
31
|
+
type ThemeLayouts,
|
|
32
|
+
type ThemeSettingField,
|
|
33
|
+
type ThemeSettingType,
|
|
34
|
+
} from "./schema";
|
|
35
|
+
export {
|
|
36
|
+
ALLOWED_THEME_EXTENSIONS,
|
|
37
|
+
MAX_THEME_ZIP_BYTES,
|
|
38
|
+
MAX_THEME_ZIP_FILES,
|
|
39
|
+
REQUIRED_THEME_FILES,
|
|
40
|
+
validateAndInstallThemeZip,
|
|
41
|
+
validateThemeConfig,
|
|
42
|
+
validateThemeSettingsOverrides,
|
|
43
|
+
type InstalledThemeSummary,
|
|
44
|
+
type SettingsValidationResult,
|
|
45
|
+
type ThemeUploadResult,
|
|
46
|
+
type ValidationResult,
|
|
47
|
+
} from "./validator";
|
|
48
|
+
export {
|
|
49
|
+
extractZipSafely,
|
|
50
|
+
isSafeArchivePath,
|
|
51
|
+
type ZipExtractionOptions,
|
|
52
|
+
} from "./sandbox";
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { eq } from "drizzle-orm";
|
|
2
|
+
import { Liquid, type LiquidOptions } from "liquidjs";
|
|
3
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import { join, resolve, sep } from "node:path";
|
|
5
|
+
import { db, schema } from "../db";
|
|
6
|
+
import { sanitizeRichHtml } from "../utils/sanitize-html";
|
|
7
|
+
import {
|
|
8
|
+
parseThemeConfig,
|
|
9
|
+
THEME_SLUG_PATTERN,
|
|
10
|
+
type ThemeConfig,
|
|
11
|
+
type ThemeLayoutName,
|
|
12
|
+
} from "./schema";
|
|
13
|
+
|
|
14
|
+
const THEME_SLUG_REGEX = new RegExp(THEME_SLUG_PATTERN);
|
|
15
|
+
|
|
16
|
+
export const THEMES_ROOT = resolve(
|
|
17
|
+
process.env.THEMES_DIR ?? resolve(import.meta.dir, "../../themes"),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export interface SiteCategoryItem {
|
|
21
|
+
name: string;
|
|
22
|
+
slug: string;
|
|
23
|
+
post_count?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PagePaginationContext {
|
|
27
|
+
page: number;
|
|
28
|
+
per_page: number;
|
|
29
|
+
total_posts: number;
|
|
30
|
+
total_pages: number;
|
|
31
|
+
has_prev: boolean;
|
|
32
|
+
has_next: boolean;
|
|
33
|
+
prev_page: number | null;
|
|
34
|
+
next_page: number | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SiteTemplateContext {
|
|
38
|
+
title: string;
|
|
39
|
+
tagline?: string;
|
|
40
|
+
logo?: string;
|
|
41
|
+
favicon?: string;
|
|
42
|
+
seo_default_desc?: string;
|
|
43
|
+
active_theme?: string;
|
|
44
|
+
categories?: SiteCategoryItem[];
|
|
45
|
+
user?: {
|
|
46
|
+
name: string;
|
|
47
|
+
role: string;
|
|
48
|
+
} | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PageTemplateContext {
|
|
52
|
+
title: string;
|
|
53
|
+
slug?: string;
|
|
54
|
+
content?: string;
|
|
55
|
+
excerpt?: string;
|
|
56
|
+
featured_image?: string;
|
|
57
|
+
meta_title?: string;
|
|
58
|
+
meta_description?: string;
|
|
59
|
+
status?: string;
|
|
60
|
+
created_at?: number;
|
|
61
|
+
category_name?: string;
|
|
62
|
+
category_slug?: string;
|
|
63
|
+
author_name?: string;
|
|
64
|
+
search_query?: string;
|
|
65
|
+
current_category?: {
|
|
66
|
+
name: string;
|
|
67
|
+
slug: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
} | null;
|
|
70
|
+
pagination?: PagePaginationContext | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface PostTemplateItem {
|
|
74
|
+
id: number;
|
|
75
|
+
slug: string;
|
|
76
|
+
title: string;
|
|
77
|
+
content: string;
|
|
78
|
+
excerpt?: string;
|
|
79
|
+
featured_image?: string;
|
|
80
|
+
category_name?: string;
|
|
81
|
+
category_slug?: string;
|
|
82
|
+
author_name?: string;
|
|
83
|
+
status: string;
|
|
84
|
+
created_at: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface RenderThemeContext {
|
|
88
|
+
site?: SiteTemplateContext;
|
|
89
|
+
page?: PageTemplateContext | null;
|
|
90
|
+
posts?: PostTemplateItem[];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface WhitelistedTemplateScope {
|
|
94
|
+
site: {
|
|
95
|
+
title: string;
|
|
96
|
+
tagline: string;
|
|
97
|
+
logo: string;
|
|
98
|
+
favicon: string;
|
|
99
|
+
seo_default_desc: string;
|
|
100
|
+
active_theme: string;
|
|
101
|
+
categories: SiteCategoryItem[];
|
|
102
|
+
user: {
|
|
103
|
+
name: string;
|
|
104
|
+
role: string;
|
|
105
|
+
} | null;
|
|
106
|
+
};
|
|
107
|
+
theme: {
|
|
108
|
+
name: string;
|
|
109
|
+
slug: string;
|
|
110
|
+
version: string;
|
|
111
|
+
author: string;
|
|
112
|
+
description: string;
|
|
113
|
+
settings: Record<string, string>;
|
|
114
|
+
};
|
|
115
|
+
page: PageTemplateContext | null;
|
|
116
|
+
posts: PostTemplateItem[];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface RendererOptions {
|
|
120
|
+
root?: string | string[];
|
|
121
|
+
partials?: string | string[];
|
|
122
|
+
layouts?: string | string[];
|
|
123
|
+
templates?: Record<string, string>;
|
|
124
|
+
strictVariables?: boolean;
|
|
125
|
+
strictFilters?: boolean;
|
|
126
|
+
outputEscape?: LiquidOptions["outputEscape"];
|
|
127
|
+
cache?: boolean;
|
|
128
|
+
relativeReference?: boolean;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface CachedThemeEntry {
|
|
132
|
+
config: ThemeConfig;
|
|
133
|
+
engine: Liquid;
|
|
134
|
+
layoutsDir: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const themeCache = new Map<string, CachedThemeEntry>();
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Verify that a path resolves strictly inside a base directory (including symlinks).
|
|
141
|
+
*/
|
|
142
|
+
function isPathInsideDirectory(baseDir: string, targetPath: string): boolean {
|
|
143
|
+
if (!existsSync(baseDir) || !existsSync(targetPath)) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const realBase = realpathSync(baseDir);
|
|
148
|
+
const realTarget = realpathSync(targetPath);
|
|
149
|
+
const basePrefix = realBase.endsWith(sep) ? realBase : `${realBase}${sep}`;
|
|
150
|
+
return realTarget.startsWith(basePrefix);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Validate a theme slug and resolve its theme directory inside THEMES_ROOT.
|
|
155
|
+
*/
|
|
156
|
+
export function resolveThemeDirectory(themeSlug: string): string {
|
|
157
|
+
if (!THEME_SLUG_REGEX.test(themeSlug)) {
|
|
158
|
+
throw new Error(`Invalid theme slug: "${themeSlug}".`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const themeDir = resolve(THEMES_ROOT, themeSlug);
|
|
162
|
+
if (!isPathInsideDirectory(THEMES_ROOT, themeDir) || !statSync(themeDir).isDirectory()) {
|
|
163
|
+
throw new Error(`Theme "${themeSlug}" was not found.`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return themeDir;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Resolve a static asset path strictly within themes/<slug>/assets/.
|
|
171
|
+
* Returns null if the slug or relative asset path is invalid or escapes the assets root.
|
|
172
|
+
*/
|
|
173
|
+
export function resolveThemeAssetPath(themeSlug: string, rawAssetPath: string): string | null {
|
|
174
|
+
if (!THEME_SLUG_REGEX.test(themeSlug) || !rawAssetPath) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let decoded: string;
|
|
179
|
+
try {
|
|
180
|
+
decoded = decodeURIComponent(rawAssetPath);
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
for (const candidate of [rawAssetPath, decoded]) {
|
|
186
|
+
if (
|
|
187
|
+
!candidate ||
|
|
188
|
+
candidate.includes("\0") ||
|
|
189
|
+
candidate.includes("\\") ||
|
|
190
|
+
candidate.startsWith("/") ||
|
|
191
|
+
/^[a-zA-Z]:/.test(candidate)
|
|
192
|
+
) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const segments = candidate.split("/");
|
|
197
|
+
if (segments.some((segment) => segment === "" || segment === "." || segment === "..")) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const assetsDir = resolve(THEMES_ROOT, themeSlug, "assets");
|
|
203
|
+
const targetPath = resolve(assetsDir, decoded);
|
|
204
|
+
|
|
205
|
+
if (!isPathInsideDirectory(THEMES_ROOT, assetsDir)) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
if (!isPathInsideDirectory(assetsDir, targetPath)) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
if (!statSync(targetPath).isFile()) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return realpathSync(targetPath);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Create a configured LiquidJS engine instance with prototype isolation and HTML escaping.
|
|
220
|
+
*/
|
|
221
|
+
export function createRenderer(options: RendererOptions = {}): Liquid {
|
|
222
|
+
const liquidOptions: LiquidOptions = {
|
|
223
|
+
extname: ".liquid",
|
|
224
|
+
ownPropertyOnly: true,
|
|
225
|
+
strictVariables: options.strictVariables ?? false,
|
|
226
|
+
strictFilters: options.strictFilters ?? true,
|
|
227
|
+
outputEscape: options.outputEscape ?? "escape",
|
|
228
|
+
cache: options.cache ?? false,
|
|
229
|
+
relativeReference: options.relativeReference ?? false,
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
if (options.templates) {
|
|
233
|
+
liquidOptions.templates = options.templates;
|
|
234
|
+
} else {
|
|
235
|
+
if (options.root) {
|
|
236
|
+
liquidOptions.root = options.root;
|
|
237
|
+
}
|
|
238
|
+
if (options.layouts) {
|
|
239
|
+
liquidOptions.layouts = options.layouts;
|
|
240
|
+
}
|
|
241
|
+
if (options.partials) {
|
|
242
|
+
liquidOptions.partials = options.partials;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return new Liquid(liquidOptions);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Render an inline Liquid template string against the provided scope object.
|
|
251
|
+
*/
|
|
252
|
+
export async function renderTemplate(
|
|
253
|
+
template: string,
|
|
254
|
+
scope: Record<string, unknown> = {},
|
|
255
|
+
options: RendererOptions = {},
|
|
256
|
+
): Promise<string> {
|
|
257
|
+
const engine = createRenderer(options);
|
|
258
|
+
return String(await engine.parseAndRender(template, scope));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function loadThemeEntry(themeSlug: string): CachedThemeEntry {
|
|
262
|
+
const cached = themeCache.get(themeSlug);
|
|
263
|
+
if (cached) {
|
|
264
|
+
return cached;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const themeDir = resolveThemeDirectory(themeSlug);
|
|
268
|
+
const manifestPath = join(themeDir, "theme.json");
|
|
269
|
+
if (!isPathInsideDirectory(themeDir, manifestPath) || !statSync(manifestPath).isFile()) {
|
|
270
|
+
throw new Error(`Missing theme.json for theme "${themeSlug}".`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const rawManifest = JSON.parse(readFileSync(manifestPath, "utf8")) as unknown;
|
|
274
|
+
const config = parseThemeConfig(rawManifest);
|
|
275
|
+
if (config.slug !== themeSlug) {
|
|
276
|
+
throw new Error(
|
|
277
|
+
`Theme manifest slug "${config.slug}" does not match directory "${themeSlug}".`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const layoutsDir = resolve(themeDir, "layouts");
|
|
282
|
+
const partialsDir = resolve(themeDir, "partials");
|
|
283
|
+
|
|
284
|
+
if (!isPathInsideDirectory(themeDir, layoutsDir) || !statSync(layoutsDir).isDirectory()) {
|
|
285
|
+
throw new Error(`Missing layouts directory for theme "${themeSlug}".`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const roots = [layoutsDir];
|
|
289
|
+
const partialsDirs: string[] = [];
|
|
290
|
+
if (isPathInsideDirectory(themeDir, partialsDir) && statSync(partialsDir).isDirectory()) {
|
|
291
|
+
roots.push(partialsDir);
|
|
292
|
+
partialsDirs.push(partialsDir);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const engine = createRenderer({
|
|
296
|
+
root: roots,
|
|
297
|
+
layouts: [layoutsDir],
|
|
298
|
+
partials: partialsDirs,
|
|
299
|
+
strictVariables: false,
|
|
300
|
+
strictFilters: true,
|
|
301
|
+
outputEscape: "escape",
|
|
302
|
+
cache: true,
|
|
303
|
+
relativeReference: false,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
const entry: CachedThemeEntry = { config, engine, layoutsDir };
|
|
307
|
+
themeCache.set(themeSlug, entry);
|
|
308
|
+
return entry;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Evict parsed templates and manifest cache for a specific theme slug.
|
|
313
|
+
*/
|
|
314
|
+
export function clearThemeCache(slug: string): void {
|
|
315
|
+
themeCache.delete(slug);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Evict all cached theme engines.
|
|
320
|
+
*/
|
|
321
|
+
export function clearAllThemeCaches(): void {
|
|
322
|
+
themeCache.clear();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Merge theme.json setting defaults with stored custom_settings_json overrides.
|
|
327
|
+
*/
|
|
328
|
+
export function mergeThemeSettings(
|
|
329
|
+
config: ThemeConfig,
|
|
330
|
+
customSettingsJson?: string | null,
|
|
331
|
+
): Record<string, string> {
|
|
332
|
+
const merged: Record<string, string> = {};
|
|
333
|
+
for (const field of config.settings) {
|
|
334
|
+
merged[field.key] = field.default;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (!customSettingsJson) {
|
|
338
|
+
return merged;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
let parsed: unknown;
|
|
342
|
+
try {
|
|
343
|
+
parsed = JSON.parse(customSettingsJson);
|
|
344
|
+
} catch {
|
|
345
|
+
return merged;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
349
|
+
return merged;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const overrides = parsed as Record<string, unknown>;
|
|
353
|
+
for (const field of config.settings) {
|
|
354
|
+
if (!Object.hasOwn(overrides, field.key)) {
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
const value = overrides[field.key];
|
|
358
|
+
if (typeof value !== "string") {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (field.type === "color" && !/^[a-zA-Z0-9#(),.%\s-]+$/.test(value)) {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (field.type === "select" && field.options && !field.options.includes(value)) {
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
merged[field.key] = value;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return merged;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function buildWhitelistedScope(
|
|
374
|
+
themeSlug: string,
|
|
375
|
+
config: ThemeConfig,
|
|
376
|
+
mergedSettings: Record<string, string>,
|
|
377
|
+
context: RenderThemeContext,
|
|
378
|
+
): WhitelistedTemplateScope {
|
|
379
|
+
const site = {
|
|
380
|
+
title: context.site ? String(context.site.title) : config.name,
|
|
381
|
+
tagline: context.site?.tagline ? String(context.site.tagline) : "",
|
|
382
|
+
logo: context.site?.logo ? String(context.site.logo) : "",
|
|
383
|
+
favicon: context.site?.favicon ? String(context.site.favicon) : "",
|
|
384
|
+
seo_default_desc: context.site?.seo_default_desc
|
|
385
|
+
? String(context.site.seo_default_desc)
|
|
386
|
+
: "",
|
|
387
|
+
active_theme: context.site?.active_theme ? String(context.site.active_theme) : themeSlug,
|
|
388
|
+
categories: Array.isArray(context.site?.categories)
|
|
389
|
+
? context.site.categories.map((cat) => ({
|
|
390
|
+
name: String(cat.name),
|
|
391
|
+
slug: String(cat.slug),
|
|
392
|
+
...(cat.post_count !== undefined ? { post_count: Number(cat.post_count) } : {}),
|
|
393
|
+
}))
|
|
394
|
+
: [],
|
|
395
|
+
user: context.site?.user
|
|
396
|
+
? {
|
|
397
|
+
name: String(context.site.user.name),
|
|
398
|
+
role: String(context.site.user.role),
|
|
399
|
+
}
|
|
400
|
+
: null,
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const page: PageTemplateContext | null = context.page
|
|
404
|
+
? {
|
|
405
|
+
title: String(context.page.title),
|
|
406
|
+
...(context.page.slug !== undefined ? { slug: String(context.page.slug) } : {}),
|
|
407
|
+
...(context.page.content !== undefined
|
|
408
|
+
? { content: sanitizeRichHtml(String(context.page.content)) }
|
|
409
|
+
: {}),
|
|
410
|
+
...(context.page.excerpt !== undefined ? { excerpt: String(context.page.excerpt) } : {}),
|
|
411
|
+
...(context.page.featured_image !== undefined
|
|
412
|
+
? { featured_image: String(context.page.featured_image) }
|
|
413
|
+
: {}),
|
|
414
|
+
...(context.page.meta_title !== undefined
|
|
415
|
+
? { meta_title: String(context.page.meta_title) }
|
|
416
|
+
: {}),
|
|
417
|
+
...(context.page.meta_description !== undefined
|
|
418
|
+
? { meta_description: String(context.page.meta_description) }
|
|
419
|
+
: {}),
|
|
420
|
+
...(context.page.status !== undefined ? { status: String(context.page.status) } : {}),
|
|
421
|
+
...(context.page.created_at !== undefined
|
|
422
|
+
? { created_at: Number(context.page.created_at) }
|
|
423
|
+
: {}),
|
|
424
|
+
...(context.page.category_name !== undefined
|
|
425
|
+
? { category_name: String(context.page.category_name) }
|
|
426
|
+
: {}),
|
|
427
|
+
...(context.page.category_slug !== undefined
|
|
428
|
+
? { category_slug: String(context.page.category_slug) }
|
|
429
|
+
: {}),
|
|
430
|
+
...(context.page.author_name !== undefined
|
|
431
|
+
? { author_name: String(context.page.author_name) }
|
|
432
|
+
: {}),
|
|
433
|
+
...(context.page.search_query !== undefined
|
|
434
|
+
? { search_query: String(context.page.search_query) }
|
|
435
|
+
: {}),
|
|
436
|
+
...(context.page.current_category
|
|
437
|
+
? {
|
|
438
|
+
current_category: {
|
|
439
|
+
name: String(context.page.current_category.name),
|
|
440
|
+
slug: String(context.page.current_category.slug),
|
|
441
|
+
...(context.page.current_category.description !== undefined
|
|
442
|
+
? { description: String(context.page.current_category.description) }
|
|
443
|
+
: {}),
|
|
444
|
+
},
|
|
445
|
+
}
|
|
446
|
+
: {}),
|
|
447
|
+
...(context.page.pagination
|
|
448
|
+
? {
|
|
449
|
+
pagination: {
|
|
450
|
+
page: Number(context.page.pagination.page),
|
|
451
|
+
per_page: Number(context.page.pagination.per_page),
|
|
452
|
+
total_posts: Number(context.page.pagination.total_posts),
|
|
453
|
+
total_pages: Number(context.page.pagination.total_pages),
|
|
454
|
+
has_prev: Boolean(context.page.pagination.has_prev),
|
|
455
|
+
has_next: Boolean(context.page.pagination.has_next),
|
|
456
|
+
prev_page:
|
|
457
|
+
context.page.pagination.prev_page !== null
|
|
458
|
+
? Number(context.page.pagination.prev_page)
|
|
459
|
+
: null,
|
|
460
|
+
next_page:
|
|
461
|
+
context.page.pagination.next_page !== null
|
|
462
|
+
? Number(context.page.pagination.next_page)
|
|
463
|
+
: null,
|
|
464
|
+
},
|
|
465
|
+
}
|
|
466
|
+
: {}),
|
|
467
|
+
}
|
|
468
|
+
: null;
|
|
469
|
+
|
|
470
|
+
const posts: PostTemplateItem[] = Array.isArray(context.posts)
|
|
471
|
+
? context.posts.map((post) => ({
|
|
472
|
+
id: Number(post.id),
|
|
473
|
+
slug: String(post.slug),
|
|
474
|
+
title: String(post.title),
|
|
475
|
+
content: sanitizeRichHtml(String(post.content)),
|
|
476
|
+
...(post.excerpt !== undefined ? { excerpt: String(post.excerpt) } : {}),
|
|
477
|
+
...(post.featured_image !== undefined
|
|
478
|
+
? { featured_image: String(post.featured_image) }
|
|
479
|
+
: {}),
|
|
480
|
+
...(post.category_name !== undefined
|
|
481
|
+
? { category_name: String(post.category_name) }
|
|
482
|
+
: {}),
|
|
483
|
+
...(post.category_slug !== undefined
|
|
484
|
+
? { category_slug: String(post.category_slug) }
|
|
485
|
+
: {}),
|
|
486
|
+
...(post.author_name !== undefined ? { author_name: String(post.author_name) } : {}),
|
|
487
|
+
status: String(post.status),
|
|
488
|
+
created_at: Number(post.created_at),
|
|
489
|
+
}))
|
|
490
|
+
: [];
|
|
491
|
+
|
|
492
|
+
return {
|
|
493
|
+
site,
|
|
494
|
+
theme: {
|
|
495
|
+
name: config.name,
|
|
496
|
+
slug: config.slug,
|
|
497
|
+
version: config.version,
|
|
498
|
+
author: config.author,
|
|
499
|
+
description: config.description,
|
|
500
|
+
settings: mergedSettings,
|
|
501
|
+
},
|
|
502
|
+
page,
|
|
503
|
+
posts,
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Render a named layout for a theme with SQLite custom settings and a strictly whitelisted context.
|
|
509
|
+
*/
|
|
510
|
+
export async function renderThemePage(
|
|
511
|
+
themeSlug: string,
|
|
512
|
+
layoutName: ThemeLayoutName,
|
|
513
|
+
context: RenderThemeContext = {},
|
|
514
|
+
): Promise<string> {
|
|
515
|
+
const { config, engine, layoutsDir } = loadThemeEntry(themeSlug);
|
|
516
|
+
const layoutFilename = config.layouts[layoutName];
|
|
517
|
+
const layoutPath = resolve(layoutsDir, layoutFilename);
|
|
518
|
+
|
|
519
|
+
if (!isPathInsideDirectory(layoutsDir, layoutPath) || !statSync(layoutPath).isFile()) {
|
|
520
|
+
throw new Error(`Layout "${layoutName}" (${layoutFilename}) not found in theme "${themeSlug}".`);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const themeRow = db
|
|
524
|
+
.select({ custom_settings_json: schema.themes.custom_settings_json })
|
|
525
|
+
.from(schema.themes)
|
|
526
|
+
.where(eq(schema.themes.slug, themeSlug))
|
|
527
|
+
.get();
|
|
528
|
+
|
|
529
|
+
const mergedSettings = mergeThemeSettings(config, themeRow?.custom_settings_json);
|
|
530
|
+
const scope = buildWhitelistedScope(themeSlug, config, mergedSettings, context);
|
|
531
|
+
|
|
532
|
+
return String(await engine.renderFile(layoutFilename, scope));
|
|
533
|
+
}
|