ghostcn 1.2.0 → 1.4.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/README.md +182 -1
- package/dist/commands/add.d.ts +6 -1
- package/dist/commands/add.js +121 -83
- package/dist/commands/diff.d.ts +5 -0
- package/dist/commands/diff.js +54 -0
- package/dist/commands/playground.d.ts +3 -0
- package/dist/commands/playground.js +29 -0
- package/dist/index.js +16 -2
- package/dist/registry/components/alert.json +62 -0
- package/dist/registry/components/archive-header.json +57 -0
- package/dist/registry/components/author-card.json +23 -2
- package/dist/registry/components/author-list.json +55 -0
- package/dist/registry/components/badge.json +51 -0
- package/dist/registry/components/button.json +74 -0
- package/dist/registry/components/card.json +70 -0
- package/dist/registry/components/featured-posts.json +18 -1
- package/dist/registry/components/input.json +94 -0
- package/dist/registry/components/member-cta.json +15 -1
- package/dist/registry/components/newsletter-form.json +23 -2
- package/dist/registry/components/pagination.json +12 -1
- package/dist/registry/components/post-card.json +25 -2
- package/dist/registry/components/post-grid.json +54 -0
- package/dist/registry/components/post-header.json +18 -1
- package/dist/registry/components/post-navigation.json +41 -0
- package/dist/registry/components/pricing-table.json +31 -2
- package/dist/registry/components/related-posts.json +53 -0
- package/dist/registry/components/site-footer.json +15 -1
- package/dist/registry/components/site-header.json +16 -1
- package/dist/registry/components/table-of-contents.json +47 -0
- package/dist/registry/components/tag-grid.json +64 -0
- package/dist/registry/components/tag-list.json +38 -0
- package/dist/registry/index.json +131 -0
- package/dist/templates/playground/index.html +5 -0
- package/dist/templates/playground/playground.css +18 -0
- package/dist/templates/playground/playground.js +71 -0
- package/dist/templates/playground/preview.css +5 -0
- package/dist/templates/playground/preview.js +12 -0
- package/dist/templates/theme-tools.mjs +1 -1
- package/dist/utils/component-diff.d.ts +18 -0
- package/dist/utils/component-diff.js +93 -0
- package/dist/utils/component-files.d.ts +9 -0
- package/dist/utils/component-files.js +48 -0
- package/dist/utils/component-history.d.ts +11 -0
- package/dist/utils/component-history.js +47 -0
- package/dist/utils/component-installer.d.ts +2 -0
- package/dist/utils/component-installer.js +34 -37
- package/dist/utils/component-plan.d.ts +7 -0
- package/dist/utils/component-plan.js +43 -0
- package/dist/utils/playground-server.d.ts +9 -0
- package/dist/utils/playground-server.js +189 -0
- package/dist/utils/preview-renderer.d.ts +143 -0
- package/dist/utils/preview-renderer.js +70 -0
- package/dist/utils/registry.d.ts +7 -0
- package/dist/utils/registry.js +12 -18
- package/dist/utils/theme-files.d.ts +2 -0
- package/dist/utils/theme-files.js +41 -3
- package/dist/utils/theme-scaffold.js +12 -0
- package/package.json +2 -2
|
@@ -8,53 +8,50 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const registry_1 = require("./registry");
|
|
10
10
|
const theme_files_1 = require("./theme-files");
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
if (file.placement === 'theme') {
|
|
14
|
-
if (!file.target)
|
|
15
|
-
throw new Error(`Registry file "${file.name}" is missing its theme target.`);
|
|
16
|
-
return file.target;
|
|
17
|
-
}
|
|
18
|
-
let alias = '';
|
|
19
|
-
if (file.type === 'partial')
|
|
20
|
-
alias = config.aliases?.partials || 'partials/components';
|
|
21
|
-
if (file.type === 'style')
|
|
22
|
-
alias = config.aliases?.styles || 'assets/css/components';
|
|
23
|
-
if (file.type === 'js')
|
|
24
|
-
alias = config.aliases?.js || 'assets/js/components';
|
|
25
|
-
if (!alias)
|
|
26
|
-
throw new Error(`Unsupported registry file type "${file.type}".`);
|
|
27
|
-
return path_1.default.join(alias, file.name);
|
|
28
|
-
}
|
|
11
|
+
const component_plan_1 = require("./component-plan");
|
|
12
|
+
const component_history_1 = require("./component-history");
|
|
29
13
|
async function installComponent(options) {
|
|
30
14
|
const name = (0, registry_1.sanitizeComponentName)(options.componentName);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
15
|
+
(0, component_history_1.readHistory)(options.themeRoot); // Validate before any component files are written.
|
|
16
|
+
const plan = await (0, component_plan_1.componentPlan)(options.themeRoot, options.config, name);
|
|
17
|
+
// Validate managed layout/asset targets before any component writes.
|
|
18
|
+
(0, theme_files_1.safeThemePath)(options.themeRoot, 'default.hbs');
|
|
19
|
+
if (plan.some(entry => entry.manifest.files.some(file => file.type === 'style')))
|
|
20
|
+
(0, theme_files_1.safeThemePath)(options.themeRoot, options.config.cssFile || 'assets/css/ghostcn.css');
|
|
21
|
+
const results = [];
|
|
22
|
+
for (const { manifest, files } of plan) {
|
|
23
|
+
const isDependency = manifest.name !== name;
|
|
24
|
+
const result = { name: manifest.name, installed: [], skipped: [] };
|
|
25
|
+
const baseline = {};
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
const relativeTarget = file.relative;
|
|
28
|
+
const targetPath = file.target;
|
|
29
|
+
if (fs_1.default.existsSync(targetPath) && (isDependency || !options.overwrite)) {
|
|
30
|
+
const replace = !isDependency && options.shouldOverwrite ? await options.shouldOverwrite(relativeTarget.replace(/\\/g, '/')) : false;
|
|
31
|
+
if (!replace) {
|
|
32
|
+
result.skipped.push(relativeTarget);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
46
35
|
}
|
|
36
|
+
fs_1.default.mkdirSync(path_1.default.dirname(targetPath), { recursive: true });
|
|
37
|
+
fs_1.default.writeFileSync(targetPath, file.content, 'utf8');
|
|
38
|
+
baseline[relativeTarget] = file.content;
|
|
39
|
+
result.installed.push(relativeTarget);
|
|
47
40
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
if (result.installed.length)
|
|
42
|
+
(0, component_history_1.recordInstalledFiles)(options.themeRoot, manifest.name, baseline);
|
|
43
|
+
results.push(result);
|
|
51
44
|
}
|
|
52
|
-
|
|
45
|
+
const result = results[results.length - 1];
|
|
46
|
+
result.dependencies = results.slice(0, -1);
|
|
47
|
+
if (plan.some(entry => entry.manifest.files.some(file => file.type === 'style'))) {
|
|
53
48
|
result.styleSync = (0, theme_files_1.syncGhostcnStyles)({
|
|
54
49
|
themeRoot: options.themeRoot,
|
|
55
50
|
cssFile: options.config.cssFile || 'assets/css/ghostcn.css',
|
|
56
51
|
stylesDir: options.config.aliases?.styles || 'assets/css/components'
|
|
57
52
|
});
|
|
58
53
|
}
|
|
54
|
+
if (plan.some(entry => entry.manifest.files.some(file => file.type === 'js')))
|
|
55
|
+
result.scriptSync = (0, theme_files_1.syncGhostcnScripts)(options.themeRoot, options.config.aliases?.js || 'assets/js/components');
|
|
59
56
|
return result;
|
|
60
57
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ComponentManifest } from './registry';
|
|
2
|
+
import { GhostcnConfig } from './theme-project';
|
|
3
|
+
import { componentFiles } from './component-files';
|
|
4
|
+
export declare function componentPlan(themeRoot: string, config: GhostcnConfig, name: string, load?: (name: string) => Promise<ComponentManifest | null>): Promise<{
|
|
5
|
+
manifest: ComponentManifest;
|
|
6
|
+
files: ReturnType<typeof componentFiles>;
|
|
7
|
+
}[]>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.componentPlan = componentPlan;
|
|
4
|
+
const registry_1 = require("./registry");
|
|
5
|
+
const component_files_1 = require("./component-files");
|
|
6
|
+
// Resolve and validate the entire dependency graph before an installer writes any files.
|
|
7
|
+
// The caller owns overwrite policy; this helper only supplies a dependency-first plan.
|
|
8
|
+
async function componentPlan(themeRoot, config, name, load = registry_1.getComponent) {
|
|
9
|
+
const visiting = new Set();
|
|
10
|
+
const completed = new Set();
|
|
11
|
+
const targets = new Map();
|
|
12
|
+
const entries = [];
|
|
13
|
+
async function visit(requested) {
|
|
14
|
+
const key = (0, registry_1.sanitizeComponentName)(requested);
|
|
15
|
+
if (visiting.has(key))
|
|
16
|
+
throw new Error(`Component dependency cycle at "${key}".`);
|
|
17
|
+
if (completed.has(key))
|
|
18
|
+
return;
|
|
19
|
+
const manifest = await load(key);
|
|
20
|
+
if (!manifest)
|
|
21
|
+
throw new Error(`Component "${key}" was not found in the registry.`);
|
|
22
|
+
if (manifest.name !== key)
|
|
23
|
+
throw new Error(`Registry returned an unexpected component for "${key}".`);
|
|
24
|
+
if (manifest.dependencies !== undefined && (!Array.isArray(manifest.dependencies) || manifest.dependencies.some(value => typeof value !== 'string')))
|
|
25
|
+
throw new Error(`Invalid dependencies for "${key}".`);
|
|
26
|
+
visiting.add(key);
|
|
27
|
+
for (const dependency of manifest.dependencies || [])
|
|
28
|
+
await visit(dependency);
|
|
29
|
+
const files = (0, component_files_1.componentFiles)(themeRoot, config, manifest);
|
|
30
|
+
for (const file of files) {
|
|
31
|
+
// Case-insensitive Windows paths must not allow graph entries to silently collide.
|
|
32
|
+
const target = process.platform === 'win32' ? file.target.toLowerCase() : file.target;
|
|
33
|
+
if (targets.has(target))
|
|
34
|
+
throw new Error(`Components "${targets.get(target)}" and "${key}" target the same file.`);
|
|
35
|
+
targets.set(target, key);
|
|
36
|
+
}
|
|
37
|
+
entries.push({ manifest, files });
|
|
38
|
+
visiting.delete(key);
|
|
39
|
+
completed.add(key);
|
|
40
|
+
}
|
|
41
|
+
await visit(name);
|
|
42
|
+
return entries;
|
|
43
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.startPlayground = startPlayground;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const http_1 = __importDefault(require("http"));
|
|
10
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
11
|
+
const registry_1 = require("./registry");
|
|
12
|
+
const component_plan_1 = require("./component-plan");
|
|
13
|
+
const component_files_1 = require("./component-files");
|
|
14
|
+
const theme_files_1 = require("./theme-files");
|
|
15
|
+
const preview_renderer_1 = require("./preview-renderer");
|
|
16
|
+
const escape = (value) => value.replace(/[&<>"']/g, character => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[character]));
|
|
17
|
+
const mime = { '.css': 'text/css', '.js': 'text/javascript', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.svg': 'image/svg+xml', '.avif': 'image/avif', '.woff': 'font/woff', '.woff2': 'font/woff2', '.ttf': 'font/ttf' };
|
|
18
|
+
const templatesRoot = path_1.default.resolve(__dirname, '../templates/playground');
|
|
19
|
+
function collectThemeFiles(themeRoot, directory, extension) {
|
|
20
|
+
const start = (0, theme_files_1.safeThemePath)(themeRoot, directory);
|
|
21
|
+
const results = [];
|
|
22
|
+
function walk(target) {
|
|
23
|
+
if (!fs_1.default.existsSync(target))
|
|
24
|
+
return;
|
|
25
|
+
const stat = fs_1.default.lstatSync(target);
|
|
26
|
+
if (stat.isSymbolicLink())
|
|
27
|
+
throw new Error('Playground refuses symlinked theme files.');
|
|
28
|
+
if (stat.isDirectory())
|
|
29
|
+
for (const item of fs_1.default.readdirSync(target)) {
|
|
30
|
+
if (results.length > 10000)
|
|
31
|
+
throw new Error('Too many playground theme files.');
|
|
32
|
+
walk((0, theme_files_1.safeThemePath)(themeRoot, path_1.default.relative(themeRoot, path_1.default.join(target, item))));
|
|
33
|
+
}
|
|
34
|
+
else if (stat.isFile() && (!extension || target.endsWith(extension)))
|
|
35
|
+
results.push(target);
|
|
36
|
+
}
|
|
37
|
+
walk(start);
|
|
38
|
+
return results;
|
|
39
|
+
}
|
|
40
|
+
function themeStyles(themeRoot, config) {
|
|
41
|
+
const layout = (0, theme_files_1.safeThemePath)(themeRoot, 'default.hbs');
|
|
42
|
+
const source = fs_1.default.existsSync(layout) ? fs_1.default.readFileSync(layout, 'utf8') : '';
|
|
43
|
+
const links = [];
|
|
44
|
+
for (const link of source.match(/<link\b[^>]*>/g) || []) {
|
|
45
|
+
if (!/rel=["']stylesheet["']/.test(link))
|
|
46
|
+
continue;
|
|
47
|
+
const asset = link.match(/\{\{asset\s+["']([^"']+\.css)["']\s*\}\}/)?.[1] || link.match(/href=["']\/assets\/([^"']+\.css)["']/)?.[1];
|
|
48
|
+
if (asset && !links.includes(asset)) {
|
|
49
|
+
(0, theme_files_1.safeThemePath)(themeRoot, `assets/${asset}`);
|
|
50
|
+
links.push(asset);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!links.length) {
|
|
54
|
+
const fallback = [config.cssFile, ...collectThemeFiles(themeRoot, config.aliases.styles, '.css').map(file => path_1.default.relative(themeRoot, file).replace(/\\/g, '/')), 'assets/built/screen.css'];
|
|
55
|
+
for (const relative of fallback)
|
|
56
|
+
if (fs_1.default.existsSync((0, theme_files_1.safeThemePath)(themeRoot, relative)))
|
|
57
|
+
links.push(relative.replace(/^assets\//, ''));
|
|
58
|
+
}
|
|
59
|
+
return links;
|
|
60
|
+
}
|
|
61
|
+
async function startPlayground(options) {
|
|
62
|
+
const themeRoot = fs_1.default.realpathSync(options.themeRoot);
|
|
63
|
+
const config = JSON.parse(fs_1.default.readFileSync((0, theme_files_1.safeThemePath)(themeRoot, 'components.json'), 'utf8').replace(/^\uFEFF/, ''));
|
|
64
|
+
if (!['css', 'tailwind'].includes(config.style) || !config.aliases)
|
|
65
|
+
throw new Error('Invalid components.json. Run ghostcn doctor.');
|
|
66
|
+
(0, component_files_1.componentPartialName)(config, 'button');
|
|
67
|
+
const port = options.port ?? 4190;
|
|
68
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535)
|
|
69
|
+
throw new Error('Port must be an integer between 0 and 65535.');
|
|
70
|
+
const catalog = [];
|
|
71
|
+
for (const item of await (0, registry_1.getRegistryIndex)()) {
|
|
72
|
+
const manifest = await (0, registry_1.getComponent)(item.name);
|
|
73
|
+
if (manifest)
|
|
74
|
+
catalog.push(manifest);
|
|
75
|
+
}
|
|
76
|
+
const plans = new Map();
|
|
77
|
+
for (const manifest of catalog)
|
|
78
|
+
plans.set(manifest.name, await (0, component_plan_1.componentPlan)(themeRoot, config, manifest.name, async (name) => catalog.find(item => item.name === name) || null));
|
|
79
|
+
const installed = () => catalog.filter(manifest => {
|
|
80
|
+
const file = (0, component_files_1.componentFiles)(themeRoot, config, manifest).find(file => file.relative.endsWith('.hbs'));
|
|
81
|
+
return file && fs_1.default.existsSync(file.target);
|
|
82
|
+
});
|
|
83
|
+
const revision = () => {
|
|
84
|
+
const files = [(0, theme_files_1.safeThemePath)(themeRoot, 'default.hbs'), ...collectThemeFiles(themeRoot, 'partials', '.hbs'), ...collectThemeFiles(themeRoot, 'assets')];
|
|
85
|
+
const hash = crypto_1.default.createHash('sha256');
|
|
86
|
+
for (const file of files.sort())
|
|
87
|
+
if (fs_1.default.existsSync(file)) {
|
|
88
|
+
const stat = fs_1.default.statSync(file);
|
|
89
|
+
hash.update(`${file}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`);
|
|
90
|
+
}
|
|
91
|
+
return hash.digest('hex');
|
|
92
|
+
};
|
|
93
|
+
let actualPort = port;
|
|
94
|
+
const server = http_1.default.createServer((request, response) => {
|
|
95
|
+
const send = (status, body, type = 'text/html') => {
|
|
96
|
+
response.writeHead(status, {
|
|
97
|
+
'Content-Type': `${type}${type.startsWith('text/') || type === 'application/json' ? '; charset=utf-8' : ''}`,
|
|
98
|
+
'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'no-referrer',
|
|
99
|
+
'Content-Security-Policy': "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; frame-src 'self'; form-action 'none'; base-uri 'none'; frame-ancestors 'self'"
|
|
100
|
+
});
|
|
101
|
+
response.end(request.method === 'HEAD' ? undefined : body);
|
|
102
|
+
};
|
|
103
|
+
if (!['GET', 'HEAD'].includes(request.method || '')) {
|
|
104
|
+
send(405, 'Read-only playground.', 'text/plain');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (![`127.0.0.1:${actualPort}`, `localhost:${actualPort}`].includes(request.headers.host || '')) {
|
|
108
|
+
send(403, 'Localhost access only.', 'text/plain');
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const url = new URL(request.url || '/', `http://127.0.0.1:${actualPort}`);
|
|
113
|
+
if (url.pathname === '/') {
|
|
114
|
+
send(200, fs_1.default.readFileSync(path_1.default.join(templatesRoot, 'index.html')));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (url.pathname === '/catalog') {
|
|
118
|
+
send(200, JSON.stringify({ style: config.style, components: installed().map(manifest => ({ name: manifest.name, description: manifest.description, parameters: manifest.parameters || {}, styleSlots: manifest.styleSlots || {}, partial: manifest.name === 'pagination' ? 'pagination' : (0, component_files_1.componentPartialName)(config, manifest.name), defaults: preview_renderer_1.demoParameters[manifest.name] || {} })) }), 'application/json');
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (url.pathname === '/revision') {
|
|
122
|
+
send(200, JSON.stringify({ revision: revision() }), 'application/json');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (url.pathname === '/playground.css' || url.pathname === '/playground.js' || url.pathname === '/preview.js' || url.pathname === '/preview.css') {
|
|
126
|
+
send(200, fs_1.default.readFileSync(path_1.default.join(templatesRoot, path_1.default.basename(url.pathname))), mime[path_1.default.extname(url.pathname)]);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (url.pathname.startsWith('/theme-assets/')) {
|
|
130
|
+
const relative = decodeURIComponent(url.pathname.slice('/theme-assets/'.length));
|
|
131
|
+
if (!mime[path_1.default.extname(relative).toLowerCase()] || /(^|[\\/])\.\.?([\\/]|$)/.test(relative)) {
|
|
132
|
+
send(403, 'Unsupported asset.', 'text/plain');
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const asset = (0, theme_files_1.safeThemePath)(themeRoot, `assets/${relative}`);
|
|
136
|
+
if (!fs_1.default.existsSync(asset) || !fs_1.default.statSync(asset).isFile()) {
|
|
137
|
+
send(404, 'Asset not found. Run your theme build.', 'text/plain');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
send(200, fs_1.default.readFileSync(asset), mime[path_1.default.extname(relative).toLowerCase()]);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (url.pathname.startsWith('/preview/')) {
|
|
144
|
+
const name = url.pathname.slice('/preview/'.length);
|
|
145
|
+
const manifest = installed().find(item => item.name === name);
|
|
146
|
+
if (!manifest) {
|
|
147
|
+
send(404, 'Component is not installed. Run ghostcn add first.', 'text/plain');
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const partials = {};
|
|
151
|
+
for (const dependency of plans.get(name) || [])
|
|
152
|
+
for (const file of dependency.files.filter(file => file.relative.endsWith('.hbs'))) {
|
|
153
|
+
// Partial blocks have fallback content, so missing dependencies can otherwise
|
|
154
|
+
// appear to render successfully while silently dropping the card wrapper.
|
|
155
|
+
if (!fs_1.default.existsSync((0, theme_files_1.safeThemePath)(themeRoot, file.relative)))
|
|
156
|
+
throw new Error(`Missing component dependency partial: ${file.relative}. Run ghostcn add ${name}.`);
|
|
157
|
+
}
|
|
158
|
+
for (const file of collectThemeFiles(themeRoot, 'partials', '.hbs'))
|
|
159
|
+
partials[path_1.default.relative(path_1.default.join(themeRoot, 'partials'), file).replace(/\\/g, '/').replace(/\.hbs$/, '')] = fs_1.default.readFileSync(file, 'utf8');
|
|
160
|
+
const parameters = { ...(preview_renderer_1.demoParameters[name] || {}) };
|
|
161
|
+
const keys = new Set([...Object.keys(manifest.parameters || {}), ...Object.keys(manifest.styleSlots || {})]);
|
|
162
|
+
for (const key of keys)
|
|
163
|
+
if (url.searchParams.has(key))
|
|
164
|
+
parameters[key] = ['disabled', 'required', 'invalid', 'urgent'].includes(key) ? url.searchParams.get(key) === 'true' : url.searchParams.get(key);
|
|
165
|
+
const state = preview_renderer_1.previewStates.includes(url.searchParams.get('state') || '') ? url.searchParams.get('state') : 'visitor';
|
|
166
|
+
const scheme = url.searchParams.get('scheme');
|
|
167
|
+
const nameInTheme = name === 'pagination' ? 'pagination' : (0, component_files_1.componentPartialName)(config, name);
|
|
168
|
+
const context = { ...(name === 'author-card' ? preview_renderer_1.demoAuthor : preview_renderer_1.previewDemoContext), ...parameters };
|
|
169
|
+
let source = (0, preview_renderer_1.previewInvocation)(nameInTheme, parameters);
|
|
170
|
+
if (name === 'pagination')
|
|
171
|
+
source = `{{> "pagination" pagination ${Object.keys(parameters).map(key => `${key}=${key}`).join(' ')}}}`;
|
|
172
|
+
let html = (0, preview_renderer_1.renderPreviewTemplate)(partials, source, { state, context });
|
|
173
|
+
if (name === 'table-of-contents')
|
|
174
|
+
html += '<article class="ghcn-article-content"><h2>Start with your story</h2><p>Demo article.</p><h3>Make room for readers</h3><p>A subsection.</p><h2>Keep it yours</h2><p>Your theme, your style.</p></article>';
|
|
175
|
+
const scripts = manifest.files.filter(file => file.type === 'js').map(file => (0, component_files_1.componentFiles)(themeRoot, config, manifest).find(target => target.relative.endsWith(`/${file.name}`))).filter(Boolean);
|
|
176
|
+
const styles = themeStyles(themeRoot, config).map(asset => `<link rel="stylesheet" href="/theme-assets/${escape(asset)}">`).join('');
|
|
177
|
+
const scriptTags = scripts.map(file => `<script defer src="/theme-assets/${escape(file.relative.replace(/^assets\//, ''))}"></script>`).join('');
|
|
178
|
+
send(200, `<!doctype html><html lang="en" class="${scheme === 'dark' ? 'dark-mode dark' : scheme === 'auto' ? 'auto-color' : ''}" data-theme="${scheme === 'dark' ? 'dark' : 'light'}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escape(name)} preview</title>${styles}<link rel="stylesheet" href="/preview.css"></head><body><main class="playground-preview">${html.trim() || '<p>This component is hidden for the selected settings.</p>'}<p id="preview-feedback" role="status"></p></main>${scriptTags}<script defer src="/preview.js"></script></body></html>`);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
send(404, 'Not found.', 'text/plain');
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
send(400, `<!doctype html><html lang="en"><head><title>Preview error</title></head><body><h1>Cannot render preview</h1><p>${escape(error.message)}</p><p>Check missing partials, run your theme build, or use Ghost to test helpers not supported by this synthetic preview.</p></body></html>`);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', () => { server.removeListener('error', reject); actualPort = server.address().port; resolve(); }); });
|
|
188
|
+
return { server, url: `http://127.0.0.1:${actualPort}`, close: () => new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())) };
|
|
189
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export declare const demoImage: string;
|
|
2
|
+
export declare const demoAuthor: {
|
|
3
|
+
name: string;
|
|
4
|
+
url: string;
|
|
5
|
+
bio: string;
|
|
6
|
+
location: string;
|
|
7
|
+
count: {
|
|
8
|
+
posts: number;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare const demoPost: {
|
|
12
|
+
title: string;
|
|
13
|
+
url: string;
|
|
14
|
+
excerpt: string;
|
|
15
|
+
custom_excerpt: string;
|
|
16
|
+
feature_image: string;
|
|
17
|
+
feature_image_alt: string;
|
|
18
|
+
primary_author: {
|
|
19
|
+
name: string;
|
|
20
|
+
url: string;
|
|
21
|
+
bio: string;
|
|
22
|
+
location: string;
|
|
23
|
+
count: {
|
|
24
|
+
posts: number;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
primary_tag: {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
url: string;
|
|
31
|
+
visibility: string;
|
|
32
|
+
};
|
|
33
|
+
tags: {
|
|
34
|
+
name: string;
|
|
35
|
+
description: string;
|
|
36
|
+
url: string;
|
|
37
|
+
visibility: string;
|
|
38
|
+
}[];
|
|
39
|
+
access: boolean;
|
|
40
|
+
};
|
|
41
|
+
export declare const demoPosts: {
|
|
42
|
+
title: string;
|
|
43
|
+
url: string;
|
|
44
|
+
excerpt: string;
|
|
45
|
+
custom_excerpt: string;
|
|
46
|
+
feature_image: string;
|
|
47
|
+
feature_image_alt: string;
|
|
48
|
+
primary_author: {
|
|
49
|
+
name: string;
|
|
50
|
+
url: string;
|
|
51
|
+
bio: string;
|
|
52
|
+
location: string;
|
|
53
|
+
count: {
|
|
54
|
+
posts: number;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
primary_tag: {
|
|
58
|
+
name: string;
|
|
59
|
+
description: string;
|
|
60
|
+
url: string;
|
|
61
|
+
visibility: string;
|
|
62
|
+
};
|
|
63
|
+
tags: {
|
|
64
|
+
name: string;
|
|
65
|
+
description: string;
|
|
66
|
+
url: string;
|
|
67
|
+
visibility: string;
|
|
68
|
+
}[];
|
|
69
|
+
access: boolean;
|
|
70
|
+
}[];
|
|
71
|
+
export declare const previewStates: string[];
|
|
72
|
+
export declare const previewDemoContext: {
|
|
73
|
+
posts: {
|
|
74
|
+
title: string;
|
|
75
|
+
url: string;
|
|
76
|
+
excerpt: string;
|
|
77
|
+
custom_excerpt: string;
|
|
78
|
+
feature_image: string;
|
|
79
|
+
feature_image_alt: string;
|
|
80
|
+
primary_author: {
|
|
81
|
+
name: string;
|
|
82
|
+
url: string;
|
|
83
|
+
bio: string;
|
|
84
|
+
location: string;
|
|
85
|
+
count: {
|
|
86
|
+
posts: number;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
primary_tag: {
|
|
90
|
+
name: string;
|
|
91
|
+
description: string;
|
|
92
|
+
url: string;
|
|
93
|
+
visibility: string;
|
|
94
|
+
};
|
|
95
|
+
tags: {
|
|
96
|
+
name: string;
|
|
97
|
+
description: string;
|
|
98
|
+
url: string;
|
|
99
|
+
visibility: string;
|
|
100
|
+
}[];
|
|
101
|
+
access: boolean;
|
|
102
|
+
}[];
|
|
103
|
+
pagination: {
|
|
104
|
+
page: number;
|
|
105
|
+
pages: number;
|
|
106
|
+
prev: number;
|
|
107
|
+
next: number;
|
|
108
|
+
};
|
|
109
|
+
title: string;
|
|
110
|
+
url: string;
|
|
111
|
+
excerpt: string;
|
|
112
|
+
custom_excerpt: string;
|
|
113
|
+
feature_image: string;
|
|
114
|
+
feature_image_alt: string;
|
|
115
|
+
primary_author: {
|
|
116
|
+
name: string;
|
|
117
|
+
url: string;
|
|
118
|
+
bio: string;
|
|
119
|
+
location: string;
|
|
120
|
+
count: {
|
|
121
|
+
posts: number;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
primary_tag: {
|
|
125
|
+
name: string;
|
|
126
|
+
description: string;
|
|
127
|
+
url: string;
|
|
128
|
+
visibility: string;
|
|
129
|
+
};
|
|
130
|
+
tags: {
|
|
131
|
+
name: string;
|
|
132
|
+
description: string;
|
|
133
|
+
url: string;
|
|
134
|
+
visibility: string;
|
|
135
|
+
}[];
|
|
136
|
+
access: boolean;
|
|
137
|
+
};
|
|
138
|
+
export declare const demoParameters: Record<string, Record<string, string>>;
|
|
139
|
+
export declare function renderPreviewTemplate(partials: Record<string, string>, template: string, options?: {
|
|
140
|
+
state?: string;
|
|
141
|
+
context?: Record<string, unknown>;
|
|
142
|
+
}): string;
|
|
143
|
+
export declare function previewInvocation(partialName: string, parameters: Record<string, unknown>): string;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.demoParameters = exports.previewDemoContext = exports.previewStates = exports.demoPosts = exports.demoPost = exports.demoAuthor = exports.demoImage = void 0;
|
|
7
|
+
exports.renderPreviewTemplate = renderPreviewTemplate;
|
|
8
|
+
exports.previewInvocation = previewInvocation;
|
|
9
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
10
|
+
// Synthetic data only. This is shared by static docs and the local playground,
|
|
11
|
+
// never used by production themes, never connected to Ghost/Stripe/email APIs.
|
|
12
|
+
exports.demoImage = 'data:image/svg+xml,' + encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="675"><rect width="1200" height="675" fill="#172f45"/><path d="M0 470h1200v205H0z" fill="#e46735"/><text x="80" y="180" font-family="sans-serif" font-size="72" fill="white">Ghostcn Journal</text><text x="80" y="270" font-family="sans-serif" font-size="36" fill="#b7cad9">Demo feature image</text></svg>');
|
|
13
|
+
exports.demoAuthor = { name: 'Alex Reader', url: '/author/alex/', bio: 'Writing about independent publishing, thoughtful design, and building a home for your readers.', location: 'Everywhere', count: { posts: 12 } };
|
|
14
|
+
const tags = [{ name: 'Publishing', description: 'Stories about independent publishing.', url: '/tag/publishing/', visibility: 'public' }, { name: 'Design', description: 'The details that make it yours.', url: '/tag/design/', visibility: 'public' }];
|
|
15
|
+
exports.demoPost = { title: 'A home for independent ideas', url: '/a-home-for-independent-ideas/', excerpt: 'Good stories deserve a place of their own. Build a publication around your ideas, your community, and your voice.', custom_excerpt: 'Good stories deserve a place of their own.', feature_image: exports.demoImage, feature_image_alt: 'Demo feature image for Ghostcn Journal', primary_author: exports.demoAuthor, primary_tag: tags[0], tags, access: true };
|
|
16
|
+
exports.demoPosts = [exports.demoPost, { ...exports.demoPost, title: 'Make room for your readers' }, { ...exports.demoPost, title: 'The details that make it yours' }];
|
|
17
|
+
const tiers = [{ id: 'demo-free', type: 'free', name: 'Reader', description: 'Follow the publication.', benefits: ['New posts in your inbox'] }, { id: 'demo-paid', type: 'paid', name: 'Supporter', description: 'Support independent writing.', currency: 'USD', monthly_price: 500, yearly_price: 5000, benefits: ['Everything in Reader', 'Members-only stories'] }];
|
|
18
|
+
exports.previewStates = ['visitor', 'free', 'paid', 'disabled', 'invite', 'restricted'];
|
|
19
|
+
exports.previewDemoContext = { ...exports.demoPost, posts: exports.demoPosts, pagination: { page: 2, pages: 4, prev: 1, next: 3 } };
|
|
20
|
+
exports.demoParameters = {
|
|
21
|
+
button: { text: 'Read the story' },
|
|
22
|
+
card: { heading: 'Make yourself at home', description: 'A small card, ready for your content.', body: 'Build something for your readers.', footer: 'Your publication. Your style.' },
|
|
23
|
+
badge: { text: 'Featured' },
|
|
24
|
+
input: { label: 'Email address', type: 'email', name: 'email', placeholder: 'you@example.com', hint: 'We will keep your email private.', hintId: 'demo-email-hint' },
|
|
25
|
+
alert: { heading: 'You are all set', description: 'Your theme is ready to customize.' },
|
|
26
|
+
'post-grid': { heading: 'Latest stories' }, 'author-list': { heading: 'Meet the authors' }, 'tag-grid': { heading: 'Explore topics' },
|
|
27
|
+
'archive-header': { heading: 'Publishing', description: 'Ideas for a publication of your own.', eyebrow: 'Topic', archiveImage: exports.demoImage, imageAlt: 'Demo archive image' }
|
|
28
|
+
};
|
|
29
|
+
function renderPreviewTemplate(partials, template, options = {}) {
|
|
30
|
+
const state = options.state || 'visitor';
|
|
31
|
+
const hbs = handlebars_1.default.create();
|
|
32
|
+
hbs.registerPartial(partials);
|
|
33
|
+
hbs.registerHelper('foreach', (items, opts) => {
|
|
34
|
+
const visible = (items || []).filter(item => opts.hash.visibility !== 'public' || item.visibility !== 'internal');
|
|
35
|
+
if (!visible.length)
|
|
36
|
+
return opts.inverse ? opts.inverse({}) : '';
|
|
37
|
+
return visible.map((item, index) => opts.fn(item, { data: { ...opts.data, index, first: index === 0, last: index === visible.length - 1 } })).join('');
|
|
38
|
+
});
|
|
39
|
+
hbs.registerHelper('prev_post', opts => opts.fn({ ...exports.demoPost, title: 'The previous story', url: '/previous-story/' }, { data: opts.data }));
|
|
40
|
+
hbs.registerHelper('next_post', opts => opts.fn({ ...exports.demoPost, title: 'The next story', url: '/next-story/' }, { data: opts.data }));
|
|
41
|
+
hbs.registerHelper('get', (resource, opts) => opts.fn(resource === 'tiers' ? { tiers } : resource === 'authors' ? { authors: [exports.demoAuthor] } : resource === 'tags' ? { tags } : { posts: exports.demoPosts }, { data: opts.data }));
|
|
42
|
+
hbs.registerHelper('match', function (value, expected, opts) { return value === expected ? opts.fn(this) : opts.inverse(this); });
|
|
43
|
+
hbs.registerHelper('has', function (opts) { return String(opts.data?.index) === opts.hash.index ? opts.fn(this) : opts.inverse(this); });
|
|
44
|
+
hbs.registerHelper('post', opts => opts.fn(exports.demoPost, { data: opts.data }));
|
|
45
|
+
hbs.registerHelper('primary_author', function (opts) { return opts.fn(this.primary_author, { data: opts.data }); });
|
|
46
|
+
hbs.registerHelper('img_url', value => value);
|
|
47
|
+
hbs.registerHelper('url', function () { return this.url; });
|
|
48
|
+
hbs.registerHelper('excerpt', function () { return this.excerpt; });
|
|
49
|
+
hbs.registerHelper('asset', value => `/theme-assets/${value}`);
|
|
50
|
+
hbs.registerHelper('reading_time', () => '4 min read');
|
|
51
|
+
hbs.registerHelper('date', opts => opts.hash.format === 'YYYY' ? '2026' : opts.hash.format === 'YYYY-MM-DD' ? '2026-10-01' : '1 Oct 2026');
|
|
52
|
+
hbs.registerHelper('plural', (count, opts) => String(count === 0 ? opts.hash.empty : count === 1 ? opts.hash.singular : opts.hash.plural).replace('%', String(count)));
|
|
53
|
+
hbs.registerHelper('social_url', () => 'https://example.com');
|
|
54
|
+
hbs.registerHelper('page_url', page => `/page/${page}/`);
|
|
55
|
+
hbs.registerHelper('navigation', () => new hbs.SafeString('<ul class="nav"><li class="nav-current"><a href="/">Home</a></li><li><a href="/about/">About</a></li><li><a href="/archive/">Archive</a></li></ul>'));
|
|
56
|
+
hbs.registerHelper('search', () => new hbs.SafeString('<button type="button" class="ghcn-icon-button ghcn-button--ghost" aria-label="Search"><svg width="20" height="20" viewBox="0 0 24 24" aria-hidden="true"><circle cx="10" cy="10" r="6" fill="none" stroke="currentColor" stroke-width="2"/><path d="m15 15 6 6" stroke="currentColor" stroke-width="2"/></svg></button>'));
|
|
57
|
+
hbs.registerHelper('price', (amount, opts) => new Intl.NumberFormat('en', { style: 'currency', currency: opts.hash.currency }).format(amount / 100));
|
|
58
|
+
const site = { title: 'Ghostcn Journal', url: '/', description: 'Independent ideas, shared with your readers.', locale: 'en', members_enabled: state !== 'disabled', paid_members_enabled: state !== 'disabled', members_invite_only: state === 'invite', allow_self_signup: !['disabled', 'invite', 'restricted'].includes(state) };
|
|
59
|
+
const data = { site, member: ['free', 'paid'].includes(state) ? { paid: state === 'paid' } : undefined };
|
|
60
|
+
return hbs.compile(template)(options.context || exports.previewDemoContext, { data });
|
|
61
|
+
}
|
|
62
|
+
function previewInvocation(partialName, parameters) {
|
|
63
|
+
// Values are bound as data, never interpolated into executable Handlebars source.
|
|
64
|
+
if (!/^[a-zA-Z0-9/_-]+$/.test(partialName))
|
|
65
|
+
throw new Error('Unsafe partial name.');
|
|
66
|
+
const keys = Object.keys(parameters);
|
|
67
|
+
if (keys.some(key => !/^[a-zA-Z][a-zA-Z0-9]*$/.test(key)))
|
|
68
|
+
throw new Error('Unsafe preview parameter.');
|
|
69
|
+
return `{{> "${partialName}" ${keys.map(key => `${key}=${key}`).join(' ')}}}`;
|
|
70
|
+
}
|
package/dist/utils/registry.d.ts
CHANGED
|
@@ -13,6 +13,12 @@ export interface ComponentManifest {
|
|
|
13
13
|
dependencies?: string[];
|
|
14
14
|
ghost_version?: string;
|
|
15
15
|
files: ComponentFile[];
|
|
16
|
+
styleSlots?: Record<string, string[]>;
|
|
17
|
+
parameters?: Record<string, {
|
|
18
|
+
description: string;
|
|
19
|
+
options?: string[];
|
|
20
|
+
default?: string;
|
|
21
|
+
}>;
|
|
16
22
|
gscan?: {
|
|
17
23
|
rules_satisfied: string[];
|
|
18
24
|
notes: string;
|
|
@@ -26,6 +32,7 @@ export interface RegistryItem {
|
|
|
26
32
|
files: string[];
|
|
27
33
|
}
|
|
28
34
|
export declare function sanitizeComponentName(name: string): string;
|
|
35
|
+
export declare function fetchRegistryJson<T>(url: string, timeoutMs?: number): Promise<T>;
|
|
29
36
|
export declare function findLocalIndex(): RegistryItem[] | null;
|
|
30
37
|
export declare function findLocalManifest(componentName: string): ComponentManifest | null;
|
|
31
38
|
export declare function getRegistryIndex(): Promise<RegistryItem[]>;
|