codepen-demokit 0.0.0-stage → 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 +235 -2
- package/bin/cli.mjs +155 -0
- package/lib/blocks.mjs +1067 -0
- package/lib/build-json.mjs +507 -0
- package/lib/build-site.mjs +258 -0
- package/lib/config.mjs +254 -0
- package/lib/init.mjs +1084 -0
- package/lib/serve.mjs +507 -0
- package/lib/sync-codepen.mjs +1375 -0
- package/package.json +40 -4
- package/shell/index.html +69 -0
- package/shell/scripts.js +268 -0
- package/shell/styles.css +463 -0
- package/templates/AGENTS.md +77 -0
- package/templates/README.md +63 -0
- package/templates/config.json.example +10 -0
- package/templates/gitignore +27 -0
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codepen-demokit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A zero-config framework for organizing, previewing, deploying, and syncing self-contained web demos with CodePen 2.0.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"codepen-demokit": "./bin/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
"./blocks": "./lib/blocks.mjs",
|
|
11
|
+
"./build-json": "./lib/build-json.mjs",
|
|
12
|
+
"./build-site": "./lib/build-site.mjs",
|
|
13
|
+
"./config": "./lib/config.mjs",
|
|
14
|
+
"./init": "./lib/init.mjs",
|
|
15
|
+
"./serve": "./lib/serve.mjs",
|
|
16
|
+
"./sync-codepen": "./lib/sync-codepen.mjs"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "./bin/cli.mjs start",
|
|
20
|
+
"build": "./bin/cli.mjs build"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"bin/",
|
|
24
|
+
"lib/",
|
|
25
|
+
"shell/",
|
|
26
|
+
"templates/",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"codepen",
|
|
31
|
+
"codepen-commander",
|
|
32
|
+
"demos",
|
|
33
|
+
"web-demos",
|
|
34
|
+
"static-site"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"codepen-commander": "^1.3.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/shell/index.html
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="color-scheme" content="light dark">
|
|
7
|
+
<title>{{SITE_TITLE}}</title>
|
|
8
|
+
<link rel="stylesheet" href="./styles.css">
|
|
9
|
+
<script type="module" src="./scripts.js"></script>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<header class="site-header">
|
|
13
|
+
<h1>{{SITE_TITLE}}</h1>
|
|
14
|
+
<p class="subtitle">{{SITE_SUBTITLE}}</p>
|
|
15
|
+
</header>
|
|
16
|
+
|
|
17
|
+
<main class="demo-directory" id="demo-directory">
|
|
18
|
+
<p class="directory-status">Loading demos…</p>
|
|
19
|
+
<noscript>
|
|
20
|
+
<section class="empty-state">
|
|
21
|
+
<h2>JavaScript required</h2>
|
|
22
|
+
<p>Enable JavaScript to load the list of demos from <code>demos.json</code>.</p>
|
|
23
|
+
</section>
|
|
24
|
+
</noscript>
|
|
25
|
+
</main>
|
|
26
|
+
|
|
27
|
+
<footer class="site-footer">
|
|
28
|
+
<p>{{SITE_FOOTER}}</p>
|
|
29
|
+
</footer>
|
|
30
|
+
|
|
31
|
+
<template id="demo-card-template">
|
|
32
|
+
<li class="demo-card">
|
|
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>
|
|
58
|
+
</div>
|
|
59
|
+
</li>
|
|
60
|
+
</template>
|
|
61
|
+
|
|
62
|
+
<template id="empty-state-template">
|
|
63
|
+
<section class="empty-state">
|
|
64
|
+
<h2>No demos yet</h2>
|
|
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>
|
|
66
|
+
</section>
|
|
67
|
+
</template>
|
|
68
|
+
</body>
|
|
69
|
+
</html>
|
package/shell/scripts.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
const directoryEl = document.getElementById('demo-directory');
|
|
2
|
+
const cardTemplate = document.getElementById('demo-card-template');
|
|
3
|
+
const emptyStateTemplate = document.getElementById('empty-state-template');
|
|
4
|
+
|
|
5
|
+
function toSafeLocalHref(rawHref) {
|
|
6
|
+
if (typeof rawHref !== 'string' || !rawHref.startsWith('./')) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const resolved = new URL(rawHref, window.location.href);
|
|
11
|
+
if (
|
|
12
|
+
resolved.origin !== window.location.origin ||
|
|
13
|
+
(resolved.protocol !== 'http:' && resolved.protocol !== 'https:')
|
|
14
|
+
) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return rawHref;
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function toSafeCodePenUrl(rawUrl) {
|
|
24
|
+
if (typeof rawUrl !== 'string') {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const parsed = new URL(rawUrl);
|
|
29
|
+
if (parsed.protocol !== 'https:') {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (parsed.hostname !== 'codepen.io' && !parsed.hostname.endsWith('.codepen.io')) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return parsed.href;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function createHeading(level, className) {
|
|
42
|
+
const clamped = Math.min(Math.max(level, 2), 6);
|
|
43
|
+
const heading = document.createElement(`h${clamped}`);
|
|
44
|
+
heading.className = className;
|
|
45
|
+
return heading;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createDemoCard(demo, headingLevel) {
|
|
49
|
+
const safeHref = toSafeLocalHref(demo?.href);
|
|
50
|
+
if (!safeHref) return null;
|
|
51
|
+
|
|
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(/^\.\//, '');
|
|
54
|
+
const fragment = cardTemplate.content.cloneNode(true);
|
|
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);
|
|
60
|
+
|
|
61
|
+
const headingEl = createHeading(headingLevel, 'demo-card-title');
|
|
62
|
+
headingEl.textContent = title;
|
|
63
|
+
pathWrapperEl.after(headingEl);
|
|
64
|
+
|
|
65
|
+
const bodyEl = fragment.querySelector('.demo-body');
|
|
66
|
+
const descriptionEl = fragment.querySelector('.demo-description');
|
|
67
|
+
if (typeof demo?.description === 'string' && demo.description.trim()) {
|
|
68
|
+
descriptionEl.textContent = demo.description.trim();
|
|
69
|
+
} else {
|
|
70
|
+
descriptionEl.remove();
|
|
71
|
+
}
|
|
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}`);
|
|
98
|
+
|
|
99
|
+
const codepenEl = fragment.querySelector('.demo-codepen');
|
|
100
|
+
const safeCodePenUrl = toSafeCodePenUrl(demo?.codepenUrl);
|
|
101
|
+
if (safeCodePenUrl) {
|
|
102
|
+
codepenEl.setAttribute('href', safeCodePenUrl);
|
|
103
|
+
codepenEl.setAttribute('aria-label', `Edit ${title} on CodePen`);
|
|
104
|
+
} else {
|
|
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');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return fragment;
|
|
113
|
+
}
|
|
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
|
+
|
|
152
|
+
function createGroupSection(headingText, demos, subgroups = [], headingLevel = 2) {
|
|
153
|
+
const cards = (Array.isArray(demos) ? demos : [])
|
|
154
|
+
.map((demo) => createDemoCard(demo, headingLevel + 1))
|
|
155
|
+
.filter(Boolean);
|
|
156
|
+
|
|
157
|
+
const childSections = (Array.isArray(subgroups) ? subgroups : [])
|
|
158
|
+
.map((subgroup) => {
|
|
159
|
+
const subHeading =
|
|
160
|
+
typeof subgroup?.title === 'string' && subgroup.title.trim()
|
|
161
|
+
? subgroup.title.trim()
|
|
162
|
+
: String(subgroup?.id ?? 'Demos');
|
|
163
|
+
return createGroupSection(subHeading, subgroup?.demos, subgroup?.groups, headingLevel + 1);
|
|
164
|
+
})
|
|
165
|
+
.filter(Boolean);
|
|
166
|
+
|
|
167
|
+
if (cards.length === 0 && childSections.length === 0) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const section = document.createElement('section');
|
|
172
|
+
section.className = 'demo-group';
|
|
173
|
+
|
|
174
|
+
const headingEl = createHeading(headingLevel, 'demo-group-title');
|
|
175
|
+
headingEl.textContent = headingText;
|
|
176
|
+
section.append(headingEl);
|
|
177
|
+
|
|
178
|
+
if (cards.length > 0) {
|
|
179
|
+
const gridEl = document.createElement('ul');
|
|
180
|
+
gridEl.className = 'demo-grid';
|
|
181
|
+
gridEl.setAttribute('role', 'list');
|
|
182
|
+
gridEl.replaceChildren(...cards);
|
|
183
|
+
section.append(gridEl);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (childSections.length > 0) {
|
|
187
|
+
const subgroupsEl = document.createElement('div');
|
|
188
|
+
subgroupsEl.className = 'demo-subgroups';
|
|
189
|
+
subgroupsEl.replaceChildren(...childSections);
|
|
190
|
+
section.append(subgroupsEl);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return section;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function createCode(text) {
|
|
197
|
+
const code = document.createElement('code');
|
|
198
|
+
code.textContent = text;
|
|
199
|
+
return code;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function createErrorState() {
|
|
203
|
+
const section = document.createElement('section');
|
|
204
|
+
section.className = 'empty-state';
|
|
205
|
+
|
|
206
|
+
const heading = document.createElement('h2');
|
|
207
|
+
heading.textContent = 'Unable to load demos';
|
|
208
|
+
|
|
209
|
+
const paragraph = document.createElement('p');
|
|
210
|
+
paragraph.append(
|
|
211
|
+
'Run ',
|
|
212
|
+
createCode('npm run build'),
|
|
213
|
+
' to generate ',
|
|
214
|
+
createCode('dist/demos.json'),
|
|
215
|
+
', or start the dev server with ',
|
|
216
|
+
createCode('npm start'),
|
|
217
|
+
' to build and watch it automatically.',
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
section.append(heading, paragraph);
|
|
221
|
+
return section;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function loadDemos() {
|
|
225
|
+
try {
|
|
226
|
+
const response = await fetch('./demos.json', {
|
|
227
|
+
cache: 'no-store',
|
|
228
|
+
signal: AbortSignal.timeout(5_000),
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
throw new Error(`HTTP ${response.status}`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const data = await response.json();
|
|
236
|
+
const groups = Array.isArray(data?.groups) ? data.groups : [];
|
|
237
|
+
const standalone = Array.isArray(data?.standalone) ? data.standalone : [];
|
|
238
|
+
|
|
239
|
+
const sections = [];
|
|
240
|
+
|
|
241
|
+
for (const group of groups) {
|
|
242
|
+
const heading =
|
|
243
|
+
typeof group?.title === 'string' && group.title.trim()
|
|
244
|
+
? group.title.trim()
|
|
245
|
+
: String(group?.id ?? 'Demos');
|
|
246
|
+
const section = createGroupSection(heading, group?.demos, group?.groups, 2);
|
|
247
|
+
if (section) sections.push(section);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (standalone.length > 0) {
|
|
251
|
+
const standaloneHeading = sections.length > 0 ? 'Standalone Demos' : 'Demos';
|
|
252
|
+
const section = createGroupSection(standaloneHeading, standalone, [], 2);
|
|
253
|
+
if (section) sections.push(section);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (sections.length === 0) {
|
|
257
|
+
directoryEl.replaceChildren(emptyStateTemplate.content.cloneNode(true));
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
directoryEl.replaceChildren(...sections);
|
|
262
|
+
syncPathOverflowFallback();
|
|
263
|
+
} catch {
|
|
264
|
+
directoryEl.replaceChildren(createErrorState());
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
loadDemos();
|