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/serve.mjs
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
PEN_HTML_SUFFIX,
|
|
6
|
+
resolveServedHtmlWithBlocks,
|
|
7
|
+
resolveUnsupportedBlocksPage,
|
|
8
|
+
resolveVirtualClassicHtml,
|
|
9
|
+
} from './blocks.mjs';
|
|
4
10
|
import { buildDemosJson, watchDemos } from './build-json.mjs';
|
|
5
11
|
import { SHELL_DIR } from './build-site.mjs';
|
|
6
12
|
import { loadSiteConfig, renderShellIndexHtml } from './config.mjs';
|
|
@@ -134,7 +140,7 @@ async function resolveServeRoot(srcDir, srcRealpath, targetArg) {
|
|
|
134
140
|
};
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
async function resolveSafePath(rawPathname, rootDir, rootRealpath) {
|
|
143
|
+
async function resolveSafePath(rawPathname, rootDir, rootRealpath, isRootServe) {
|
|
138
144
|
let decodedPath;
|
|
139
145
|
try {
|
|
140
146
|
decodedPath = decodeURIComponent(rawPathname);
|
|
@@ -162,6 +168,45 @@ async function resolveSafePath(rawPathname, rootDir, rootRealpath) {
|
|
|
162
168
|
try {
|
|
163
169
|
stat = await fs.stat(resolvedPath);
|
|
164
170
|
} catch {
|
|
171
|
+
if (!decodedPath.endsWith('/') && segments.length > 0) {
|
|
172
|
+
const requestedFilename = segments.at(-1);
|
|
173
|
+
const lowerFilename = requestedFilename.toLowerCase();
|
|
174
|
+
if (lowerFilename.endsWith('.html') && !lowerFilename.endsWith(PEN_HTML_SUFFIX)) {
|
|
175
|
+
const parentDir = path.dirname(resolvedPath);
|
|
176
|
+
if (isWithinRoot(parentDir, rootDir)) {
|
|
177
|
+
try {
|
|
178
|
+
const parentStat = await fs.stat(parentDir);
|
|
179
|
+
if (parentStat.isDirectory()) {
|
|
180
|
+
const realParentDir = await fs.realpath(parentDir);
|
|
181
|
+
if (isWithinRoot(realParentDir, rootRealpath)) {
|
|
182
|
+
const unsupportedPage = await resolveUnsupportedBlocksPage({
|
|
183
|
+
dirRealpath: realParentDir,
|
|
184
|
+
rootRealpath,
|
|
185
|
+
isRootServe,
|
|
186
|
+
});
|
|
187
|
+
if (unsupportedPage) {
|
|
188
|
+
return {
|
|
189
|
+
status: 501,
|
|
190
|
+
errorHtml: unsupportedPage.html,
|
|
191
|
+
csp: unsupportedPage.csp,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const virtualHtml = await resolveVirtualClassicHtml({
|
|
195
|
+
dirRealpath: realParentDir,
|
|
196
|
+
requestedFilename,
|
|
197
|
+
rootRealpath,
|
|
198
|
+
});
|
|
199
|
+
if (virtualHtml !== null) {
|
|
200
|
+
return { status: 200, virtualHtml, contentType: MIME_TYPES['.html'] };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} catch {
|
|
205
|
+
// Fall through to 404
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
165
210
|
return { status: 404, message: 'Not Found' };
|
|
166
211
|
}
|
|
167
212
|
|
|
@@ -171,6 +216,29 @@ async function resolveSafePath(rawPathname, rootDir, rootRealpath) {
|
|
|
171
216
|
return { status: 301, redirect: normalizedRedirect };
|
|
172
217
|
}
|
|
173
218
|
|
|
219
|
+
let realDirPath;
|
|
220
|
+
try {
|
|
221
|
+
realDirPath = await fs.realpath(resolvedPath);
|
|
222
|
+
} catch {
|
|
223
|
+
return { status: 404, message: 'Not Found' };
|
|
224
|
+
}
|
|
225
|
+
if (!isWithinRoot(realDirPath, rootRealpath)) {
|
|
226
|
+
return { status: 403, message: 'Forbidden' };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const unsupportedPage = await resolveUnsupportedBlocksPage({
|
|
230
|
+
dirRealpath: realDirPath,
|
|
231
|
+
rootRealpath,
|
|
232
|
+
isRootServe,
|
|
233
|
+
});
|
|
234
|
+
if (unsupportedPage) {
|
|
235
|
+
return {
|
|
236
|
+
status: 501,
|
|
237
|
+
errorHtml: unsupportedPage.html,
|
|
238
|
+
csp: unsupportedPage.csp,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
174
242
|
const indexPath = path.join(resolvedPath, 'index.html');
|
|
175
243
|
try {
|
|
176
244
|
const indexStat = await fs.stat(indexPath);
|
|
@@ -183,6 +251,18 @@ async function resolveSafePath(rawPathname, rootDir, rootRealpath) {
|
|
|
183
251
|
}
|
|
184
252
|
return { status: 200, filePath: realIndexPath, size: indexStat.size };
|
|
185
253
|
} catch {
|
|
254
|
+
try {
|
|
255
|
+
const virtualHtml = await resolveVirtualClassicHtml({
|
|
256
|
+
dirRealpath: realDirPath,
|
|
257
|
+
requestedFilename: 'index.html',
|
|
258
|
+
rootRealpath,
|
|
259
|
+
});
|
|
260
|
+
if (virtualHtml !== null) {
|
|
261
|
+
return { status: 200, virtualHtml, contentType: MIME_TYPES['.html'] };
|
|
262
|
+
}
|
|
263
|
+
} catch {
|
|
264
|
+
// Fall through to 404
|
|
265
|
+
}
|
|
186
266
|
return { status: 404, message: 'Not Found' };
|
|
187
267
|
}
|
|
188
268
|
}
|
|
@@ -196,6 +276,22 @@ async function resolveSafePath(rawPathname, rootDir, rootRealpath) {
|
|
|
196
276
|
return { status: 403, message: 'Forbidden' };
|
|
197
277
|
}
|
|
198
278
|
|
|
279
|
+
const lowerFilePath = realFilePath.toLowerCase();
|
|
280
|
+
if (lowerFilePath.endsWith('.html') && !lowerFilePath.endsWith(PEN_HTML_SUFFIX)) {
|
|
281
|
+
const unsupportedPage = await resolveUnsupportedBlocksPage({
|
|
282
|
+
dirRealpath: path.dirname(realFilePath),
|
|
283
|
+
rootRealpath,
|
|
284
|
+
isRootServe,
|
|
285
|
+
});
|
|
286
|
+
if (unsupportedPage) {
|
|
287
|
+
return {
|
|
288
|
+
status: 501,
|
|
289
|
+
errorHtml: unsupportedPage.html,
|
|
290
|
+
csp: unsupportedPage.csp,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
199
295
|
return { status: 200, filePath: realFilePath, size: stat.size };
|
|
200
296
|
}
|
|
201
297
|
|
|
@@ -274,7 +370,7 @@ export async function startServer({
|
|
|
274
370
|
return;
|
|
275
371
|
}
|
|
276
372
|
|
|
277
|
-
const result = await resolveSafePath(requestUrl.pathname, rootDir, rootRealpath);
|
|
373
|
+
const result = await resolveSafePath(requestUrl.pathname, rootDir, rootRealpath, isRootServe);
|
|
278
374
|
|
|
279
375
|
if (result.status === 301 && result.redirect) {
|
|
280
376
|
res.writeHead(301, {
|
|
@@ -318,6 +414,37 @@ export async function startServer({
|
|
|
318
414
|
return;
|
|
319
415
|
}
|
|
320
416
|
|
|
417
|
+
if (result.status === 501 && typeof result.errorHtml === 'string') {
|
|
418
|
+
const content = Buffer.from(result.errorHtml, 'utf8');
|
|
419
|
+
res.writeHead(501, {
|
|
420
|
+
...BASE_SECURITY_HEADERS,
|
|
421
|
+
...(result.csp ? { 'Content-Security-Policy': result.csp } : {}),
|
|
422
|
+
'Content-Type': MIME_TYPES['.html'],
|
|
423
|
+
'Content-Length': content.byteLength,
|
|
424
|
+
});
|
|
425
|
+
if (req.method === 'HEAD') {
|
|
426
|
+
res.end();
|
|
427
|
+
} else {
|
|
428
|
+
res.end(content);
|
|
429
|
+
}
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (result.status === 200 && typeof result.virtualHtml === 'string') {
|
|
434
|
+
const content = Buffer.from(result.virtualHtml, 'utf8');
|
|
435
|
+
res.writeHead(200, {
|
|
436
|
+
...BASE_SECURITY_HEADERS,
|
|
437
|
+
'Content-Type': result.contentType ?? MIME_TYPES['.html'],
|
|
438
|
+
'Content-Length': content.byteLength,
|
|
439
|
+
});
|
|
440
|
+
if (req.method === 'HEAD') {
|
|
441
|
+
res.end();
|
|
442
|
+
} else {
|
|
443
|
+
res.end(content);
|
|
444
|
+
}
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
321
448
|
if (result.status !== 200 || !result.filePath) {
|
|
322
449
|
sendError(res, result.status, result.message ?? 'Error');
|
|
323
450
|
return;
|
|
@@ -327,7 +454,18 @@ export async function startServer({
|
|
|
327
454
|
const contentType = MIME_TYPES[ext] ?? 'application/octet-stream';
|
|
328
455
|
|
|
329
456
|
try {
|
|
330
|
-
|
|
457
|
+
let content;
|
|
458
|
+
if (ext === '.html') {
|
|
459
|
+
const blockResult = await resolveServedHtmlWithBlocks({
|
|
460
|
+
filePath: result.filePath,
|
|
461
|
+
rootRealpath,
|
|
462
|
+
});
|
|
463
|
+
content = blockResult.transformed
|
|
464
|
+
? Buffer.from(blockResult.html, 'utf8')
|
|
465
|
+
: await fs.readFile(result.filePath);
|
|
466
|
+
} else {
|
|
467
|
+
content = await fs.readFile(result.filePath);
|
|
468
|
+
}
|
|
331
469
|
res.writeHead(200, {
|
|
332
470
|
...BASE_SECURITY_HEADERS,
|
|
333
471
|
'Content-Type': contentType,
|
package/lib/sync-codepen.mjs
CHANGED
|
@@ -30,7 +30,8 @@ function usage() {
|
|
|
30
30
|
console.error(
|
|
31
31
|
'Usage:\n' +
|
|
32
32
|
' codepen-demokit cdpn <push|pull|sync|delete> [folder-in-src] [--dry-run] [--force]\n' +
|
|
33
|
-
' codepen-demokit cdpn clone [options] <url> [folder]'
|
|
33
|
+
' codepen-demokit cdpn clone [options] <url> [folder]\n' +
|
|
34
|
+
' codepen-demokit cdpn new [options] [folder]'
|
|
34
35
|
);
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -71,7 +72,7 @@ function parseArgs(argv) {
|
|
|
71
72
|
mode = arg;
|
|
72
73
|
} else {
|
|
73
74
|
console.error(
|
|
74
|
-
`Error: Unknown mode '${arg}' (expected 'push', 'pull', 'sync', 'delete', or '
|
|
75
|
+
`Error: Unknown mode '${arg}' (expected 'push', 'pull', 'sync', 'delete', 'clone', or 'new').`
|
|
75
76
|
);
|
|
76
77
|
usage();
|
|
77
78
|
process.exit(1);
|
|
@@ -90,7 +91,7 @@ function parseArgs(argv) {
|
|
|
90
91
|
|
|
91
92
|
if (!mode) {
|
|
92
93
|
console.error(
|
|
93
|
-
"Error: Missing mode (expected 'push', 'pull', 'sync', 'delete', or '
|
|
94
|
+
"Error: Missing mode (expected 'push', 'pull', 'sync', 'delete', 'clone', or 'new')."
|
|
94
95
|
);
|
|
95
96
|
usage();
|
|
96
97
|
process.exit(1);
|
|
@@ -677,7 +678,7 @@ function hasHelpFlag(argv) {
|
|
|
677
678
|
return false;
|
|
678
679
|
}
|
|
679
680
|
|
|
680
|
-
function
|
|
681
|
+
function normalizeSrcFolderArg(rawFolder, demosRealpath) {
|
|
681
682
|
if (
|
|
682
683
|
rawFolder.includes('\0') ||
|
|
683
684
|
rawFolder.includes('\\') ||
|
|
@@ -757,7 +758,18 @@ function normalizeCloneFolderArg(rawFolder, demosRealpath) {
|
|
|
757
758
|
return segments.join('/');
|
|
758
759
|
}
|
|
759
760
|
|
|
760
|
-
|
|
761
|
+
const NEW_VALUE_FLAGS = new Set([
|
|
762
|
+
'-t',
|
|
763
|
+
'--title',
|
|
764
|
+
'-d',
|
|
765
|
+
'--description',
|
|
766
|
+
'--tags',
|
|
767
|
+
'--access',
|
|
768
|
+
'--password',
|
|
769
|
+
'--template',
|
|
770
|
+
]);
|
|
771
|
+
|
|
772
|
+
function parsePassthroughArgs(argv, demosRealpath, { folderPositionalIndex, valueFlags = new Set() }) {
|
|
761
773
|
let explicitProfile = null;
|
|
762
774
|
let afterDoubleDash = false;
|
|
763
775
|
let positionalCount = 0;
|
|
@@ -798,14 +810,21 @@ function parseCloneArgs(argv, demosRealpath) {
|
|
|
798
810
|
continue;
|
|
799
811
|
}
|
|
800
812
|
|
|
813
|
+
if (!afterDoubleDash && valueFlags.has(arg)) {
|
|
814
|
+
forwardedArgs.push(arg);
|
|
815
|
+
if (i + 1 < argv.length) {
|
|
816
|
+
forwardedArgs.push(argv[++i]);
|
|
817
|
+
}
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
|
|
801
821
|
if (!afterDoubleDash && arg.startsWith('-')) {
|
|
802
822
|
forwardedArgs.push(arg);
|
|
803
823
|
continue;
|
|
804
824
|
}
|
|
805
825
|
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
forwardedArgs.push(normalizeCloneFolderArg(arg, demosRealpath));
|
|
826
|
+
if (positionalCount === folderPositionalIndex) {
|
|
827
|
+
forwardedArgs.push(normalizeSrcFolderArg(arg, demosRealpath));
|
|
809
828
|
} else {
|
|
810
829
|
forwardedArgs.push(arg);
|
|
811
830
|
}
|
|
@@ -815,14 +834,20 @@ function parseCloneArgs(argv, demosRealpath) {
|
|
|
815
834
|
return { explicitProfile, forwardedArgs };
|
|
816
835
|
}
|
|
817
836
|
|
|
818
|
-
|
|
837
|
+
function runCodePenSubcommand({
|
|
838
|
+
subcommand,
|
|
839
|
+
argv,
|
|
840
|
+
projectDir = process.cwd(),
|
|
841
|
+
folderPositionalIndex,
|
|
842
|
+
valueFlags,
|
|
843
|
+
}) {
|
|
819
844
|
const resolvedProjectDir = path.resolve(projectDir);
|
|
820
845
|
const { bin: cdpnBin } = resolveCdpnBinary(resolvedProjectDir);
|
|
821
846
|
|
|
822
847
|
if (hasHelpFlag(argv)) {
|
|
823
848
|
const helpRes = spawnSync(
|
|
824
849
|
cdpnBin.command,
|
|
825
|
-
[...cdpnBin.prefixArgs,
|
|
850
|
+
[...cdpnBin.prefixArgs, subcommand, '--help'],
|
|
826
851
|
{
|
|
827
852
|
cwd: resolvedProjectDir,
|
|
828
853
|
stdio: 'inherit',
|
|
@@ -830,7 +855,7 @@ export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
|
830
855
|
}
|
|
831
856
|
);
|
|
832
857
|
if (helpRes.error) {
|
|
833
|
-
console.error(`Error: Failed to run cdpn
|
|
858
|
+
console.error(`Error: Failed to run cdpn ${subcommand}: ${helpRes.error.message}`);
|
|
834
859
|
process.exit(1);
|
|
835
860
|
}
|
|
836
861
|
if ((helpRes.status ?? 0) !== 0) {
|
|
@@ -857,7 +882,11 @@ export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
|
857
882
|
process.exit(1);
|
|
858
883
|
}
|
|
859
884
|
|
|
860
|
-
const { explicitProfile, forwardedArgs } =
|
|
885
|
+
const { explicitProfile, forwardedArgs } = parsePassthroughArgs(
|
|
886
|
+
argv,
|
|
887
|
+
demosRealpath,
|
|
888
|
+
{ folderPositionalIndex, valueFlags }
|
|
889
|
+
);
|
|
861
890
|
|
|
862
891
|
let profileToUse = explicitProfile;
|
|
863
892
|
if (!profileToUse) {
|
|
@@ -873,7 +902,7 @@ export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
|
873
902
|
|
|
874
903
|
const finalArgs = [
|
|
875
904
|
...(profileToUse ? ['--profile', profileToUse] : []),
|
|
876
|
-
|
|
905
|
+
subcommand,
|
|
877
906
|
...forwardedArgs,
|
|
878
907
|
];
|
|
879
908
|
|
|
@@ -891,7 +920,7 @@ export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
|
891
920
|
});
|
|
892
921
|
|
|
893
922
|
if (result.error) {
|
|
894
|
-
console.error(`Error: Failed to run cdpn
|
|
923
|
+
console.error(`Error: Failed to run cdpn ${subcommand}: ${result.error.message}`);
|
|
895
924
|
process.exit(1);
|
|
896
925
|
}
|
|
897
926
|
if (result.signal) {
|
|
@@ -902,10 +931,32 @@ export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
|
902
931
|
}
|
|
903
932
|
}
|
|
904
933
|
|
|
934
|
+
export function runCodePenClone(argv, { projectDir = process.cwd() } = {}) {
|
|
935
|
+
return runCodePenSubcommand({
|
|
936
|
+
subcommand: 'clone',
|
|
937
|
+
argv,
|
|
938
|
+
projectDir,
|
|
939
|
+
folderPositionalIndex: 1, // 0 = <url>, 1 = [folder]
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
export function runCodePenNew(argv, { projectDir = process.cwd() } = {}) {
|
|
944
|
+
return runCodePenSubcommand({
|
|
945
|
+
subcommand: 'new',
|
|
946
|
+
argv,
|
|
947
|
+
projectDir,
|
|
948
|
+
folderPositionalIndex: 0, // 0 = [folder]
|
|
949
|
+
valueFlags: NEW_VALUE_FLAGS,
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
|
|
905
953
|
export function runCodePenSync(argv, { projectDir = process.cwd() } = {}) {
|
|
906
954
|
if (argv[0] === 'clone') {
|
|
907
955
|
return runCodePenClone(argv.slice(1), { projectDir });
|
|
908
956
|
}
|
|
957
|
+
if (argv[0] === 'new') {
|
|
958
|
+
return runCodePenNew(argv.slice(1), { projectDir });
|
|
959
|
+
}
|
|
909
960
|
|
|
910
961
|
const resolvedProjectDir = path.resolve(projectDir);
|
|
911
962
|
const demosDir = path.resolve(resolvedProjectDir, 'src');
|
|
@@ -948,9 +999,7 @@ export function runCodePenSync(argv, { projectDir = process.cwd() } = {}) {
|
|
|
948
999
|
console.log(` ✓ Authenticated profile "${auth.name}" (${auth.keyType} key)`);
|
|
949
1000
|
|
|
950
1001
|
// 3. Report discovered demos
|
|
951
|
-
const scopeSummary = scope
|
|
952
|
-
? `under ./src/${scope}`
|
|
953
|
-
: '(excluding ./src/index.html)';
|
|
1002
|
+
const scopeSummary = scope ? `under ./src/${scope}` : 'under ./src';
|
|
954
1003
|
console.log(` ✓ Found ${demos.length} demo folder(s) ${scopeSummary}\n`);
|
|
955
1004
|
|
|
956
1005
|
const summary = {
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codepen-demokit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A zero-config framework for organizing, previewing, deploying, and syncing self-contained web demos with CodePen 2.0.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"codepen-demokit": "./bin/cli.mjs"
|
|
8
8
|
},
|
|
9
9
|
"exports": {
|
|
10
|
+
"./blocks": "./lib/blocks.mjs",
|
|
10
11
|
"./build-json": "./lib/build-json.mjs",
|
|
11
12
|
"./build-site": "./lib/build-site.mjs",
|
|
12
13
|
"./config": "./lib/config.mjs",
|
|
@@ -14,6 +15,10 @@
|
|
|
14
15
|
"./serve": "./lib/serve.mjs",
|
|
15
16
|
"./sync-codepen": "./lib/sync-codepen.mjs"
|
|
16
17
|
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "./bin/cli.mjs start",
|
|
20
|
+
"build": "./bin/cli.mjs build"
|
|
21
|
+
},
|
|
17
22
|
"files": [
|
|
18
23
|
"bin/",
|
|
19
24
|
"lib/",
|
package/shell/index.html
CHANGED
|
@@ -30,10 +30,31 @@
|
|
|
30
30
|
|
|
31
31
|
<template id="demo-card-template">
|
|
32
32
|
<li class="demo-card">
|
|
33
|
-
<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
<div class="demo-path">
|
|
34
|
+
<code class="demo-path-text"></code>
|
|
35
|
+
</div>
|
|
36
|
+
<div class="demo-body">
|
|
37
|
+
<p class="demo-description"></p>
|
|
38
|
+
<ul class="demo-tags" aria-label="Tags"></ul>
|
|
39
|
+
</div>
|
|
40
|
+
<div class="demo-footer">
|
|
41
|
+
<a class="demo-btn demo-btn-primary demo-open">
|
|
42
|
+
<svg class="demo-btn-icon demo-open-icon" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
|
|
43
|
+
<path d="M4.75 3.1a.75.75 0 0 1 1.13-.65l7.5 4.4a.75.75 0 0 1 0 1.3l-7.5 4.4a.75.75 0 0 1-1.13-.65V3.1Z" />
|
|
44
|
+
</svg>
|
|
45
|
+
<span>Open Demo</span>
|
|
46
|
+
</a>
|
|
47
|
+
<a class="demo-btn demo-btn-secondary demo-codepen" target="_blank" rel="noopener noreferrer">
|
|
48
|
+
<svg class="demo-btn-icon demo-codepen-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
49
|
+
<polygon points="8,1.5 14.5,5.5 8,9.5 1.5,5.5" />
|
|
50
|
+
<polygon points="8,6.5 14.5,10.5 8,14.5 1.5,10.5" />
|
|
51
|
+
<line x1="1.5" y1="5.5" x2="1.5" y2="10.5" />
|
|
52
|
+
<line x1="14.5" y1="5.5" x2="14.5" y2="10.5" />
|
|
53
|
+
<line x1="8" y1="1.5" x2="8" y2="6.5" />
|
|
54
|
+
<line x1="8" y1="9.5" x2="8" y2="14.5" />
|
|
55
|
+
</svg>
|
|
56
|
+
<span>Edit on CodePen</span>
|
|
57
|
+
</a>
|
|
37
58
|
</div>
|
|
38
59
|
</li>
|
|
39
60
|
</template>
|
|
@@ -41,7 +62,7 @@
|
|
|
41
62
|
<template id="empty-state-template">
|
|
42
63
|
<section class="empty-state">
|
|
43
64
|
<h2>No demos yet</h2>
|
|
44
|
-
<p>Create a self-contained demo folder inside <code>src/</code> (e.g., <code>src/my-demo/</code> or <code>src/my-group/my-demo/</code>) with
|
|
65
|
+
<p>Create a self-contained demo folder inside <code>src/</code> (e.g., <code>src/my-demo/</code> or <code>src/my-group/my-demo/</code>) with a <code>.cdpn.json</code> or <code>index.html</code> file. Refresh this page and it will appear automatically.</p>
|
|
45
66
|
</section>
|
|
46
67
|
</template>
|
|
47
68
|
</body>
|
package/shell/scripts.js
CHANGED
|
@@ -50,17 +50,19 @@ function createDemoCard(demo, headingLevel) {
|
|
|
50
50
|
if (!safeHref) return null;
|
|
51
51
|
|
|
52
52
|
const title = typeof demo?.title === 'string' && demo.title.trim() ? demo.title.trim() : demo.path;
|
|
53
|
+
const pathText = typeof demo?.path === 'string' ? demo.path : safeHref.replace(/^\.\//, '');
|
|
53
54
|
const fragment = cardTemplate.content.cloneNode(true);
|
|
54
|
-
|
|
55
|
+
|
|
56
|
+
const pathWrapperEl = fragment.querySelector('.demo-path');
|
|
57
|
+
const pathTextEl = fragment.querySelector('.demo-path-text');
|
|
58
|
+
pathTextEl.textContent = pathText;
|
|
59
|
+
pathWrapperEl.setAttribute('title', pathText);
|
|
55
60
|
|
|
56
61
|
const headingEl = createHeading(headingLevel, 'demo-card-title');
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
linkEl.setAttribute('href', safeHref);
|
|
60
|
-
linkEl.textContent = title;
|
|
61
|
-
headingEl.append(linkEl);
|
|
62
|
-
cardEl.prepend(headingEl);
|
|
62
|
+
headingEl.textContent = title;
|
|
63
|
+
pathWrapperEl.after(headingEl);
|
|
63
64
|
|
|
65
|
+
const bodyEl = fragment.querySelector('.demo-body');
|
|
64
66
|
const descriptionEl = fragment.querySelector('.demo-description');
|
|
65
67
|
if (typeof demo?.description === 'string' && demo.description.trim()) {
|
|
66
68
|
descriptionEl.textContent = demo.description.trim();
|
|
@@ -68,21 +70,85 @@ function createDemoCard(demo, headingLevel) {
|
|
|
68
70
|
descriptionEl.remove();
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
+
const tagsEl = fragment.querySelector('.demo-tags');
|
|
74
|
+
const rawTags = Array.isArray(demo?.tags) ? demo.tags : [];
|
|
75
|
+
const tagItems = [];
|
|
76
|
+
for (const rawTag of rawTags) {
|
|
77
|
+
if (typeof rawTag !== 'string') continue;
|
|
78
|
+
const cleaned = rawTag.trim();
|
|
79
|
+
if (!cleaned) continue;
|
|
80
|
+
const li = document.createElement('li');
|
|
81
|
+
li.className = 'demo-tag';
|
|
82
|
+
li.textContent = cleaned;
|
|
83
|
+
tagItems.push(li);
|
|
84
|
+
}
|
|
85
|
+
if (tagItems.length > 0) {
|
|
86
|
+
tagsEl.replaceChildren(...tagItems);
|
|
87
|
+
} else {
|
|
88
|
+
tagsEl.remove();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (bodyEl.childElementCount === 0) {
|
|
92
|
+
bodyEl.classList.add('is-empty');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const openEl = fragment.querySelector('.demo-open');
|
|
96
|
+
openEl.setAttribute('href', safeHref);
|
|
97
|
+
openEl.setAttribute('aria-label', `Open Demo: ${title}`);
|
|
73
98
|
|
|
74
99
|
const codepenEl = fragment.querySelector('.demo-codepen');
|
|
75
100
|
const safeCodePenUrl = toSafeCodePenUrl(demo?.codepenUrl);
|
|
76
101
|
if (safeCodePenUrl) {
|
|
77
102
|
codepenEl.setAttribute('href', safeCodePenUrl);
|
|
78
|
-
codepenEl.setAttribute('aria-label', `
|
|
103
|
+
codepenEl.setAttribute('aria-label', `Edit ${title} on CodePen`);
|
|
79
104
|
} else {
|
|
80
|
-
codepenEl.
|
|
105
|
+
codepenEl.removeAttribute('href');
|
|
106
|
+
codepenEl.removeAttribute('target');
|
|
107
|
+
codepenEl.removeAttribute('rel');
|
|
108
|
+
codepenEl.setAttribute('aria-disabled', 'true');
|
|
109
|
+
codepenEl.setAttribute('title', 'Not linked to CodePen');
|
|
81
110
|
}
|
|
82
111
|
|
|
83
112
|
return fragment;
|
|
84
113
|
}
|
|
85
114
|
|
|
115
|
+
const SUPPORTS_SCROLL_TIMELINE =
|
|
116
|
+
typeof CSS !== 'undefined' &&
|
|
117
|
+
typeof CSS.supports === 'function' &&
|
|
118
|
+
CSS.supports('animation-timeline: scroll(self inline)');
|
|
119
|
+
|
|
120
|
+
let pathResizeObserver = null;
|
|
121
|
+
|
|
122
|
+
function syncPathOverflowFallback() {
|
|
123
|
+
if (SUPPORTS_SCROLL_TIMELINE) return;
|
|
124
|
+
|
|
125
|
+
const updateEl = (el) => {
|
|
126
|
+
const overflows = el.scrollWidth > el.clientWidth + 1;
|
|
127
|
+
el.toggleAttribute('data-overflows', overflows);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const paths = directoryEl.querySelectorAll('.demo-path');
|
|
131
|
+
if (pathResizeObserver) {
|
|
132
|
+
pathResizeObserver.disconnect();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (typeof ResizeObserver === 'function') {
|
|
136
|
+
pathResizeObserver = new ResizeObserver((entries) => {
|
|
137
|
+
for (const entry of entries) {
|
|
138
|
+
updateEl(entry.target);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
for (const el of paths) {
|
|
142
|
+
updateEl(el);
|
|
143
|
+
pathResizeObserver.observe(el);
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
for (const el of paths) {
|
|
147
|
+
updateEl(el);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
86
152
|
function createGroupSection(headingText, demos, subgroups = [], headingLevel = 2) {
|
|
87
153
|
const cards = (Array.isArray(demos) ? demos : [])
|
|
88
154
|
.map((demo) => createDemoCard(demo, headingLevel + 1))
|
|
@@ -193,6 +259,7 @@ async function loadDemos() {
|
|
|
193
259
|
}
|
|
194
260
|
|
|
195
261
|
directoryEl.replaceChildren(...sections);
|
|
262
|
+
syncPathOverflowFallback();
|
|
196
263
|
} catch {
|
|
197
264
|
directoryEl.replaceChildren(createErrorState());
|
|
198
265
|
}
|