codepen-demokit 1.0.0 → 1.1.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 +13 -7
- package/bin/cli.mjs +2 -1
- package/lib/blocks.mjs +1067 -0
- package/lib/build-json.mjs +84 -16
- package/lib/build-site.mjs +67 -2
- package/lib/config.mjs +10 -1
- package/lib/init.mjs +551 -118
- package/lib/serve.mjs +141 -3
- package/lib/sync-codepen.mjs +66 -17
- package/package.json +6 -1
- package/shell/index.html +26 -5
- package/shell/scripts.js +78 -11
- package/shell/styles.css +204 -40
- package/templates/AGENTS.md +8 -7
- package/templates/README.md +1 -0
package/lib/build-json.mjs
CHANGED
|
@@ -2,10 +2,13 @@ import { randomBytes } from 'node:crypto';
|
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import { watch } from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
+
import { readClassicHeadMetadata, readPenBlocks } from './blocks.mjs';
|
|
5
6
|
|
|
6
7
|
const MAX_SCAN_DEPTH = 4;
|
|
7
8
|
const MAX_HTML_HEAD_BYTES = 16 * 1024;
|
|
8
9
|
const MAX_CDPN_JSON_BYTES = 64 * 1024;
|
|
10
|
+
const MAX_DEMO_TAGS = 10;
|
|
11
|
+
const CDPN_JSON_FILENAME = '.cdpn.json';
|
|
9
12
|
|
|
10
13
|
const ACRONYMS = new Map([
|
|
11
14
|
['ai', 'AI'],
|
|
@@ -102,12 +105,16 @@ function decodeHtmlEntities(input) {
|
|
|
102
105
|
});
|
|
103
106
|
}
|
|
104
107
|
|
|
105
|
-
function
|
|
106
|
-
const cleaned =
|
|
108
|
+
function normalizePlainText(value, maxLength) {
|
|
109
|
+
const cleaned = String(value).replace(/\s+/g, ' ').trim();
|
|
107
110
|
if (!cleaned) return null;
|
|
108
111
|
return cleaned.length > maxLength ? `${cleaned.slice(0, maxLength - 1)}…` : cleaned;
|
|
109
112
|
}
|
|
110
113
|
|
|
114
|
+
function normalizeText(value, maxLength) {
|
|
115
|
+
return normalizePlainText(decodeHtmlEntities(value), maxLength);
|
|
116
|
+
}
|
|
117
|
+
|
|
111
118
|
function extractTitle(headHtml) {
|
|
112
119
|
const match = /<title\b[^>]{0,200}>([\s\S]{0,500}?)<\/title>/i.exec(headHtml);
|
|
113
120
|
return match ? normalizeText(match[1], 160) : null;
|
|
@@ -139,6 +146,23 @@ function extractMetaDescription(headHtml) {
|
|
|
139
146
|
return null;
|
|
140
147
|
}
|
|
141
148
|
|
|
149
|
+
function extractCdpnTags(rawTags) {
|
|
150
|
+
if (!Array.isArray(rawTags)) return [];
|
|
151
|
+
const seen = new Set();
|
|
152
|
+
const tags = [];
|
|
153
|
+
for (const rawTag of rawTags) {
|
|
154
|
+
if (typeof rawTag !== 'string') continue;
|
|
155
|
+
const normalized = normalizePlainText(rawTag, 48);
|
|
156
|
+
if (!normalized) continue;
|
|
157
|
+
const key = normalized.toLowerCase();
|
|
158
|
+
if (seen.has(key)) continue;
|
|
159
|
+
seen.add(key);
|
|
160
|
+
tags.push(normalized);
|
|
161
|
+
if (tags.length >= MAX_DEMO_TAGS) break;
|
|
162
|
+
}
|
|
163
|
+
return tags;
|
|
164
|
+
}
|
|
165
|
+
|
|
142
166
|
function sanitizeCodePenUrl(rawUrl) {
|
|
143
167
|
if (typeof rawUrl !== 'string') return null;
|
|
144
168
|
try {
|
|
@@ -202,36 +226,80 @@ async function resolveSafeRealDir(candidatePath, srcDir, srcRealpath) {
|
|
|
202
226
|
}
|
|
203
227
|
}
|
|
204
228
|
|
|
205
|
-
async function readDemoEntry(segments, srcDir, srcRealpath) {
|
|
229
|
+
async function readDemoEntry(segments, demoRealpath, srcDir, srcRealpath) {
|
|
206
230
|
const demoDir = path.resolve(srcDir, ...segments);
|
|
231
|
+
const cdpnFile = await resolveSafeRealFile(
|
|
232
|
+
path.join(demoDir, CDPN_JSON_FILENAME),
|
|
233
|
+
srcDir,
|
|
234
|
+
srcRealpath,
|
|
235
|
+
);
|
|
207
236
|
const indexFile = await resolveSafeRealFile(path.join(demoDir, 'index.html'), srcDir, srcRealpath);
|
|
208
|
-
if (!indexFile) return null;
|
|
209
237
|
|
|
210
|
-
let
|
|
211
|
-
let
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
238
|
+
let classicIndexFile = null;
|
|
239
|
+
let hasUnsupportedBlocks = false;
|
|
240
|
+
if (!indexFile) {
|
|
241
|
+
const penBlocks = await readPenBlocks(demoRealpath, srcRealpath);
|
|
242
|
+
if (penBlocks?.unsupportedBlocks?.length > 0) {
|
|
243
|
+
hasUnsupportedBlocks = true;
|
|
244
|
+
} else if (penBlocks?.hasClassic) {
|
|
245
|
+
classicIndexFile = await resolveSafeRealFile(
|
|
246
|
+
path.join(demoDir, 'index.pen.html'),
|
|
247
|
+
srcDir,
|
|
248
|
+
srcRealpath,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (!cdpnFile && !indexFile && !classicIndexFile && !hasUnsupportedBlocks) return null;
|
|
254
|
+
|
|
255
|
+
let htmlTitle = null;
|
|
256
|
+
let htmlDescription = null;
|
|
257
|
+
if (indexFile) {
|
|
258
|
+
try {
|
|
259
|
+
const headHtml = await readPartialUtf8(indexFile.realPath, MAX_HTML_HEAD_BYTES);
|
|
260
|
+
htmlTitle = extractTitle(headHtml);
|
|
261
|
+
htmlDescription = extractMetaDescription(headHtml);
|
|
262
|
+
} catch {
|
|
263
|
+
// Fall back to .cdpn.json or folder name when HTML metadata cannot be read
|
|
264
|
+
}
|
|
265
|
+
} else if (classicIndexFile) {
|
|
266
|
+
try {
|
|
267
|
+
const classicHead = await readClassicHeadMetadata(demoRealpath, srcRealpath);
|
|
268
|
+
if (classicHead) {
|
|
269
|
+
htmlTitle = extractTitle(classicHead);
|
|
270
|
+
htmlDescription = extractMetaDescription(classicHead);
|
|
271
|
+
}
|
|
272
|
+
} catch {
|
|
273
|
+
// Fall back to .cdpn.json or folder name when classic config metadata cannot be read
|
|
274
|
+
}
|
|
218
275
|
}
|
|
219
276
|
|
|
220
277
|
let codepenUrl = null;
|
|
221
|
-
|
|
278
|
+
let cdpnTitle = null;
|
|
279
|
+
let cdpnDescription = null;
|
|
280
|
+
let tags = [];
|
|
222
281
|
if (cdpnFile && cdpnFile.size <= MAX_CDPN_JSON_BYTES) {
|
|
223
282
|
try {
|
|
224
283
|
const rawJson = await fs.readFile(cdpnFile.realPath, 'utf8');
|
|
225
284
|
const parsed = JSON.parse(rawJson);
|
|
226
285
|
codepenUrl = sanitizeCodePenUrl(parsed?.url);
|
|
286
|
+
if (typeof parsed?.info?.title === 'string') {
|
|
287
|
+
cdpnTitle = normalizePlainText(parsed.info.title, 160);
|
|
288
|
+
}
|
|
289
|
+
if (typeof parsed?.info?.description === 'string') {
|
|
290
|
+
cdpnDescription = normalizePlainText(parsed.info.description, 320);
|
|
291
|
+
}
|
|
292
|
+
tags = extractCdpnTags(parsed?.info?.tags);
|
|
227
293
|
} catch {
|
|
228
294
|
codepenUrl = null;
|
|
295
|
+
tags = [];
|
|
229
296
|
}
|
|
230
297
|
}
|
|
231
298
|
|
|
232
299
|
return {
|
|
233
|
-
title:
|
|
234
|
-
description,
|
|
300
|
+
title: cdpnTitle ?? htmlTitle ?? formatFolderTitle(segments.at(-1)),
|
|
301
|
+
description: cdpnDescription ?? htmlDescription,
|
|
302
|
+
tags,
|
|
235
303
|
path: `${segments.join('/')}/`,
|
|
236
304
|
href: `./${segments.map(encodeURIComponent).join('/')}/`,
|
|
237
305
|
codepenUrl,
|
|
@@ -261,7 +329,7 @@ async function scanGroupNode(dirRealpath, segments, depth, visitedRealpaths, src
|
|
|
261
329
|
|
|
262
330
|
visitedRealpaths.add(childRealDir);
|
|
263
331
|
|
|
264
|
-
const demoEntry = await readDemoEntry(childSegments, srcDir, srcRealpath);
|
|
332
|
+
const demoEntry = await readDemoEntry(childSegments, childRealDir, srcDir, srcRealpath);
|
|
265
333
|
if (demoEntry) {
|
|
266
334
|
demos.push(demoEntry);
|
|
267
335
|
continue;
|
package/lib/build-site.mjs
CHANGED
|
@@ -2,6 +2,13 @@ import fs from 'node:fs/promises';
|
|
|
2
2
|
import { existsSync, watch } from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import {
|
|
6
|
+
PEN_HTML_SUFFIX,
|
|
7
|
+
applyPackagesBlock,
|
|
8
|
+
compileClassicPenHtml,
|
|
9
|
+
readPenBlocks,
|
|
10
|
+
renderUnsupportedBlocksPage,
|
|
11
|
+
} from './blocks.mjs';
|
|
5
12
|
import { buildDemosJson } from './build-json.mjs';
|
|
6
13
|
import { CONFIG_FILENAME, loadSiteConfig, renderShellIndexHtml } from './config.mjs';
|
|
7
14
|
|
|
@@ -16,8 +23,34 @@ function isWithinRoot(candidatePath, rootPath) {
|
|
|
16
23
|
return candidatePath === rootPath || candidatePath.startsWith(rootPath + path.sep);
|
|
17
24
|
}
|
|
18
25
|
|
|
19
|
-
async function copySrcTree(
|
|
26
|
+
async function copySrcTree(
|
|
27
|
+
currentSrcDir,
|
|
28
|
+
currentDestDir,
|
|
29
|
+
srcRealpath,
|
|
30
|
+
isRootLevel,
|
|
31
|
+
inheritedPenContext = null,
|
|
32
|
+
) {
|
|
33
|
+
const localPenContext = await readPenBlocks(currentSrcDir, srcRealpath);
|
|
34
|
+
const penContext = localPenContext ?? inheritedPenContext;
|
|
35
|
+
|
|
36
|
+
if (localPenContext && localPenContext.unsupportedBlocks.length > 0) {
|
|
37
|
+
const relFromSrc = path.relative(srcRealpath, currentSrcDir);
|
|
38
|
+
const relSegments = relFromSrc ? relFromSrc.split(path.sep).filter(Boolean) : [];
|
|
39
|
+
const homeHref = relSegments.length > 0 ? '../'.repeat(relSegments.length) : null;
|
|
40
|
+
const demoRelPath =
|
|
41
|
+
relSegments.length > 0 ? `${relSegments.join('/')}/` : `${path.basename(currentSrcDir)}/`;
|
|
42
|
+
const { html } = await renderUnsupportedBlocksPage({
|
|
43
|
+
penContext: localPenContext,
|
|
44
|
+
rootRealpath: srcRealpath,
|
|
45
|
+
demoRelPath,
|
|
46
|
+
homeHref,
|
|
47
|
+
});
|
|
48
|
+
await fs.writeFile(path.join(currentDestDir, 'index.html'), html, 'utf8');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
20
52
|
const entries = await fs.readdir(currentSrcDir, { withFileTypes: true });
|
|
53
|
+
const existingNamesLower = new Set(entries.map((entry) => entry.name.toLowerCase()));
|
|
21
54
|
|
|
22
55
|
for (const entry of entries) {
|
|
23
56
|
// Skip dotfiles/dotdirs (.cdpn.json, .cdpnignore, .codepen, .DS_Store, .gitkeep, etc.) and node_modules
|
|
@@ -42,8 +75,40 @@ async function copySrcTree(currentSrcDir, currentDestDir, srcRealpath, isRootLev
|
|
|
42
75
|
|
|
43
76
|
if (lstat.isDirectory()) {
|
|
44
77
|
await fs.mkdir(destPath, { recursive: true });
|
|
45
|
-
await copySrcTree(realSrcPath, destPath, srcRealpath, false);
|
|
78
|
+
await copySrcTree(realSrcPath, destPath, srcRealpath, false, penContext);
|
|
46
79
|
} else if (lstat.isFile()) {
|
|
80
|
+
const lowerName = entry.name.toLowerCase();
|
|
81
|
+
|
|
82
|
+
if (penContext?.hasClassic && lowerName.endsWith(PEN_HTML_SUFFIX)) {
|
|
83
|
+
const outName = `${entry.name.slice(0, -PEN_HTML_SUFFIX.length)}.html`;
|
|
84
|
+
if (!existingNamesLower.has(outName.toLowerCase())) {
|
|
85
|
+
const compiledHtml = await compileClassicPenHtml({
|
|
86
|
+
penHtmlPath: realSrcPath,
|
|
87
|
+
penContext,
|
|
88
|
+
rootRealpath: srcRealpath,
|
|
89
|
+
});
|
|
90
|
+
if (compiledHtml !== null) {
|
|
91
|
+
await fs.writeFile(path.join(currentDestDir, outName), compiledHtml, 'utf8');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (
|
|
98
|
+
penContext?.hasPackages &&
|
|
99
|
+
lowerName.endsWith('.html') &&
|
|
100
|
+
!lowerName.endsWith(PEN_HTML_SUFFIX)
|
|
101
|
+
) {
|
|
102
|
+
const rawHtml = await fs.readFile(realSrcPath, 'utf8');
|
|
103
|
+
const transformedHtml = await applyPackagesBlock({
|
|
104
|
+
html: rawHtml,
|
|
105
|
+
penContext,
|
|
106
|
+
rootRealpath: srcRealpath,
|
|
107
|
+
});
|
|
108
|
+
await fs.writeFile(destPath, transformedHtml, 'utf8');
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
47
112
|
await fs.copyFile(realSrcPath, destPath);
|
|
48
113
|
}
|
|
49
114
|
}
|
package/lib/config.mjs
CHANGED
|
@@ -236,10 +236,19 @@ export function escapeHtml(value) {
|
|
|
236
236
|
.replace(/'/g, ''');
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
const CODEPEN_DEMOKIT_NPM_URL = 'https://npmjs.com/package/codepen-demokit';
|
|
240
|
+
|
|
241
|
+
function renderFooterHtml(footerText) {
|
|
242
|
+
return escapeHtml(footerText).replaceAll(
|
|
243
|
+
'codepen-demokit',
|
|
244
|
+
`<a href="${CODEPEN_DEMOKIT_NPM_URL}" target="_blank" rel="noopener noreferrer">codepen-demokit</a>`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
239
248
|
export function renderShellIndexHtml(templateHtml, siteConfig) {
|
|
240
249
|
const site = { ...DEFAULT_SITE_CONFIG, ...siteConfig };
|
|
241
250
|
return templateHtml
|
|
242
251
|
.replaceAll('{{SITE_TITLE}}', escapeHtml(site.title))
|
|
243
252
|
.replaceAll('{{SITE_SUBTITLE}}', escapeHtml(site.subtitle))
|
|
244
|
-
.replaceAll('{{SITE_FOOTER}}',
|
|
253
|
+
.replaceAll('{{SITE_FOOTER}}', renderFooterHtml(site.footer));
|
|
245
254
|
}
|