ghostcn 1.4.1 → 1.6.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/dist/commands/create.js +16 -9
- package/dist/commands/diff.js +47 -5
- package/dist/commands/init.js +22 -10
- package/dist/commands/list.js +22 -4
- package/dist/registry/components/accordion.json +66 -0
- package/dist/registry/components/alert.json +1 -1
- package/dist/registry/components/announcement-bar.json +79 -0
- package/dist/registry/components/archive-header.json +2 -2
- package/dist/registry/components/author-card.json +2 -2
- package/dist/registry/components/author-list.json +2 -2
- package/dist/registry/components/avatar.json +62 -0
- package/dist/registry/components/badge.json +1 -1
- package/dist/registry/components/breadcrumbs.json +59 -0
- package/dist/registry/components/button.json +1 -1
- package/dist/registry/components/card.json +1 -1
- package/dist/registry/components/featured-posts.json +2 -2
- package/dist/registry/components/input.json +8 -2
- package/dist/registry/components/member-cta.json +1 -1
- package/dist/registry/components/newsletter-form.json +12 -3
- package/dist/registry/components/pagination.json +2 -2
- package/dist/registry/components/post-card.json +2 -2
- package/dist/registry/components/post-grid.json +2 -2
- package/dist/registry/components/post-header.json +2 -2
- package/dist/registry/components/post-navigation.json +1 -1
- package/dist/registry/components/pricing-table.json +24 -4
- package/dist/registry/components/reading-progress.json +73 -0
- package/dist/registry/components/related-posts.json +7 -1
- package/dist/registry/components/share-buttons.json +76 -0
- package/dist/registry/components/site-footer.json +2 -2
- package/dist/registry/components/site-header.json +1 -1
- package/dist/registry/components/table-of-contents.json +13 -3
- package/dist/registry/components/tag-grid.json +2 -2
- package/dist/registry/components/tag-list.json +1 -1
- package/dist/registry/components/theme-toggle.json +65 -0
- package/dist/registry/components/visibility-badge.json +71 -0
- package/dist/registry/index.json +89 -4
- package/dist/templates/playground/index.html +2 -2
- package/dist/templates/playground/playground.css +8 -1
- package/dist/templates/playground/playground.js +39 -5
- package/dist/templates/playground/preview.css +1 -0
- package/dist/templates/theme-tools.mjs +62 -24
- package/dist/utils/component-diff.d.ts +1 -1
- package/dist/utils/component-diff.js +46 -17
- package/dist/utils/component-files.js +3 -2
- package/dist/utils/component-history.js +1 -1
- package/dist/utils/design-defaults.json +17 -0
- package/dist/utils/playground-server.js +34 -12
- package/dist/utils/preview-renderer.d.ts +5 -0
- package/dist/utils/preview-renderer.js +30 -8
- package/dist/utils/registry.js +15 -5
- package/dist/utils/theme-files.js +14 -7
- package/dist/utils/theme-project.js +6 -4
- package/dist/utils/theme-scaffold.js +27 -14
- package/dist/utils/theme.d.ts +3 -3
- package/dist/utils/theme.js +108 -49
- package/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "visibility-badge",
|
|
3
|
+
"type": "components:members",
|
|
4
|
+
"description": "Ghost-native access indicator pill for members-only, paid-only, tier-restricted, or public posts.",
|
|
5
|
+
"dependencies": [],
|
|
6
|
+
"ghost_version": ">=5.0.0",
|
|
7
|
+
"parameters": {
|
|
8
|
+
"variant": {
|
|
9
|
+
"description": "Visual treatment.",
|
|
10
|
+
"options": [
|
|
11
|
+
"default",
|
|
12
|
+
"outline",
|
|
13
|
+
"subtle"
|
|
14
|
+
],
|
|
15
|
+
"default": "default"
|
|
16
|
+
},
|
|
17
|
+
"size": {
|
|
18
|
+
"description": "Badge scale.",
|
|
19
|
+
"options": [
|
|
20
|
+
"sm",
|
|
21
|
+
"default",
|
|
22
|
+
"lg"
|
|
23
|
+
],
|
|
24
|
+
"default": "default"
|
|
25
|
+
},
|
|
26
|
+
"showPublic": {
|
|
27
|
+
"description": "Boolean. Render a Public badge for public posts (hidden by default)."
|
|
28
|
+
},
|
|
29
|
+
"membersLabel": {
|
|
30
|
+
"description": "Custom label for members-only posts.",
|
|
31
|
+
"default": "Members only"
|
|
32
|
+
},
|
|
33
|
+
"paidLabel": {
|
|
34
|
+
"description": "Custom label for paid-only posts.",
|
|
35
|
+
"default": "Paid only"
|
|
36
|
+
},
|
|
37
|
+
"tiersLabel": {
|
|
38
|
+
"description": "Custom label for specific-tier posts.",
|
|
39
|
+
"default": "Specific tiers"
|
|
40
|
+
},
|
|
41
|
+
"publicLabel": {
|
|
42
|
+
"description": "Custom label for public posts when showPublic=true.",
|
|
43
|
+
"default": "Public"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
{
|
|
48
|
+
"name": "visibility-badge.hbs",
|
|
49
|
+
"type": "partial",
|
|
50
|
+
"target": "partials/components/visibility-badge.hbs",
|
|
51
|
+
"content": "{{!-- Styling parameters: class --}}\n{{#with this as |ghostcnStyles|}}\n{{#match visibility \"members\"}}\n<span class=\"ghcn-visibility-badge{{#if ghostcnStyles.class}} {{ghostcnStyles.class}}{{/if}}\" data-visibility=\"members\" data-variant=\"{{#match variant \"outline\"}}outline{{else}}{{#match variant \"subtle\"}}subtle{{else}}default{{/match}}{{/match}}\" data-size=\"{{#match size \"sm\"}}sm{{else}}{{#match size \"lg\"}}lg{{else}}default{{/match}}{{/match}}\"><svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" width=\"12\" height=\"12\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><rect x=\"3\" y=\"7\" width=\"10\" height=\"7\" rx=\"1.5\"/><path d=\"M5.5 7V5a2.5 2.5 0 0 1 5 0v2\"/></svg><span>{{#if membersLabel}}{{membersLabel}}{{else}}Members only{{/if}}</span></span>\n{{else}}{{#match visibility \"paid\"}}\n<span class=\"ghcn-visibility-badge{{#if ghostcnStyles.class}} {{ghostcnStyles.class}}{{/if}}\" data-visibility=\"paid\" data-variant=\"{{#match variant \"outline\"}}outline{{else}}{{#match variant \"subtle\"}}subtle{{else}}default{{/match}}{{/match}}\" data-size=\"{{#match size \"sm\"}}sm{{else}}{{#match size \"lg\"}}lg{{else}}default{{/match}}{{/match}}\"><svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" width=\"12\" height=\"12\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"m8 2.2 1.8 3.6 4 .6-2.9 2.8.7 4L8 11.3l-3.6 1.9.7-4-2.9-2.8 4-.6L8 2.2Z\"/></svg><span>{{#if paidLabel}}{{paidLabel}}{{else}}Paid only{{/if}}</span></span>\n{{else}}{{#match visibility \"tiers\"}}\n<span class=\"ghcn-visibility-badge{{#if ghostcnStyles.class}} {{ghostcnStyles.class}}{{/if}}\" data-visibility=\"tiers\" data-variant=\"{{#match variant \"outline\"}}outline{{else}}{{#match variant \"subtle\"}}subtle{{else}}default{{/match}}{{/match}}\" data-size=\"{{#match size \"sm\"}}sm{{else}}{{#match size \"lg\"}}lg{{else}}default{{/match}}{{/match}}\"><svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" width=\"12\" height=\"12\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"m8 2.2 1.8 3.6 4 .6-2.9 2.8.7 4L8 11.3l-3.6 1.9.7-4-2.9-2.8 4-.6L8 2.2Z\"/></svg><span>{{#if tiersLabel}}{{tiersLabel}}{{else}}Specific tiers{{/if}}</span></span>\n{{else}}{{#if showPublic}}\n<span class=\"ghcn-visibility-badge{{#if ghostcnStyles.class}} {{ghostcnStyles.class}}{{/if}}\" data-visibility=\"public\" data-variant=\"{{#match variant \"outline\"}}outline{{else}}{{#match variant \"subtle\"}}subtle{{else}}default{{/match}}{{/match}}\" data-size=\"{{#match size \"sm\"}}sm{{else}}{{#match size \"lg\"}}lg{{else}}default{{/match}}{{/match}}\"><svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" width=\"12\" height=\"12\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><circle cx=\"8\" cy=\"8\" r=\"5.5\"/><path d=\"M2.5 8h11M8 2.5a8.5 8.5 0 0 1 0 11 8.5 8.5 0 0 1 0-11Z\"/></svg><span>{{#if publicLabel}}{{publicLabel}}{{else}}Public{{/if}}</span></span>\n{{/if}}{{/match}}{{/match}}{{/match}}\n{{/with}}"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "visibility-badge.css",
|
|
55
|
+
"type": "style",
|
|
56
|
+
"target": "assets/css/components/visibility-badge.css",
|
|
57
|
+
"content": ".ghcn-visibility-badge { box-sizing: border-box; display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border: 1px solid var(--border); border-radius: 9999px; background: var(--secondary); color: var(--secondary-foreground); font-family: inherit; font-size: 12px; font-weight: 600; line-height: 1.25; white-space: nowrap; }\n.ghcn-visibility-badge:where([data-visibility=\"paid\"]),\n.ghcn-visibility-badge:where([data-visibility=\"tiers\"]) { background: var(--primary); color: var(--primary-foreground); border-color: transparent; }\n.ghcn-visibility-badge:where([data-size=\"sm\"]) { font-size: 11px; padding: 2px 7px; gap: 4px; }\n.ghcn-visibility-badge:where([data-size=\"lg\"]) { font-size: 14px; padding: 4px 11px; gap: 6px; }\n.ghcn-visibility-badge:where([data-variant=\"outline\"]) { background: transparent; color: var(--foreground); border-color: var(--border); }\n.ghcn-visibility-badge:where([data-variant=\"outline\"][data-visibility=\"paid\"]),\n.ghcn-visibility-badge:where([data-variant=\"outline\"][data-visibility=\"tiers\"]) { border-color: var(--primary); color: var(--primary); }\n.ghcn-visibility-badge:where([data-variant=\"subtle\"]) { background: transparent; border-color: transparent; padding-inline: 2px; color: var(--muted-foreground); }\n.ghcn-visibility-badge:where([data-variant=\"subtle\"][data-visibility=\"paid\"]),\n.ghcn-visibility-badge:where([data-variant=\"subtle\"][data-visibility=\"tiers\"]) { color: var(--foreground); }\n"
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
"gscan": {
|
|
61
|
+
"rules_satisfied": [
|
|
62
|
+
"GS010-PJ-VALID"
|
|
63
|
+
],
|
|
64
|
+
"notes": "Uses native Ghost post {{visibility}} property (public, members, paid, tiers)."
|
|
65
|
+
},
|
|
66
|
+
"styleSlots": {
|
|
67
|
+
"class": [
|
|
68
|
+
".ghcn-visibility-badge"
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
}
|
package/dist/registry/index.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
{
|
|
3
3
|
"name": "newsletter-form",
|
|
4
4
|
"description": "A native Ghost members subscription form with state handling.",
|
|
5
|
-
"category": "components:
|
|
5
|
+
"category": "components:members",
|
|
6
6
|
"ghost_version": ">=5.0.0",
|
|
7
7
|
"files": [
|
|
8
8
|
"newsletter-form.hbs",
|
|
@@ -11,12 +11,13 @@
|
|
|
11
11
|
},
|
|
12
12
|
{
|
|
13
13
|
"name": "pricing-table",
|
|
14
|
-
"description": "
|
|
15
|
-
"category": "components:
|
|
14
|
+
"description": "Live Ghost tiers, prices, currencies, benefits, and progressive monthly/yearly toggle with Portal checkout.",
|
|
15
|
+
"category": "components:members",
|
|
16
16
|
"ghost_version": ">=5.0.0",
|
|
17
17
|
"files": [
|
|
18
18
|
"pricing-table.hbs",
|
|
19
|
-
"pricing-table.css"
|
|
19
|
+
"pricing-table.css",
|
|
20
|
+
"pricing-table.js"
|
|
20
21
|
]
|
|
21
22
|
},
|
|
22
23
|
{
|
|
@@ -190,6 +191,26 @@
|
|
|
190
191
|
"alert.css"
|
|
191
192
|
]
|
|
192
193
|
},
|
|
194
|
+
{
|
|
195
|
+
"name": "avatar",
|
|
196
|
+
"description": "A reusable author or user avatar with image and initials fallback.",
|
|
197
|
+
"category": "components:ui",
|
|
198
|
+
"ghost_version": ">=5.0.0",
|
|
199
|
+
"files": [
|
|
200
|
+
"avatar.hbs",
|
|
201
|
+
"avatar.css"
|
|
202
|
+
]
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
"name": "accordion",
|
|
206
|
+
"description": "Zero-JS collapsible disclosure built on native details and summary elements.",
|
|
207
|
+
"category": "components:ui",
|
|
208
|
+
"ghost_version": ">=5.0.0",
|
|
209
|
+
"files": [
|
|
210
|
+
"accordion.hbs",
|
|
211
|
+
"accordion.css"
|
|
212
|
+
]
|
|
213
|
+
},
|
|
193
214
|
{
|
|
194
215
|
"name": "post-grid",
|
|
195
216
|
"description": "A responsive grid using post-card; pass posts from a loop, archive or get query.",
|
|
@@ -229,5 +250,69 @@
|
|
|
229
250
|
"archive-header.hbs",
|
|
230
251
|
"archive-header.css"
|
|
231
252
|
]
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"name": "theme-toggle",
|
|
256
|
+
"description": "Light, dark, and system theme switcher syncing .dark-mode and .auto-color with localStorage persistence.",
|
|
257
|
+
"category": "components:layout",
|
|
258
|
+
"ghost_version": ">=5.0.0",
|
|
259
|
+
"files": [
|
|
260
|
+
"theme-toggle.hbs",
|
|
261
|
+
"theme-toggle.css",
|
|
262
|
+
"theme-toggle.js"
|
|
263
|
+
]
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"name": "announcement-bar",
|
|
267
|
+
"description": "Dismissible top publication announcement bar with optional link and session persistence.",
|
|
268
|
+
"category": "components:layout",
|
|
269
|
+
"ghost_version": ">=5.0.0",
|
|
270
|
+
"files": [
|
|
271
|
+
"announcement-bar.hbs",
|
|
272
|
+
"announcement-bar.css",
|
|
273
|
+
"announcement-bar.js"
|
|
274
|
+
]
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"name": "reading-progress",
|
|
278
|
+
"description": "Thin scroll-linked reading progress bar tracking article scroll depth.",
|
|
279
|
+
"category": "components:content",
|
|
280
|
+
"ghost_version": ">=5.0.0",
|
|
281
|
+
"files": [
|
|
282
|
+
"reading-progress.hbs",
|
|
283
|
+
"reading-progress.css",
|
|
284
|
+
"reading-progress.js"
|
|
285
|
+
]
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
"name": "share-buttons",
|
|
289
|
+
"description": "Accessible article share links (Copy link, Bluesky, X, LinkedIn, Email, and native Web Share).",
|
|
290
|
+
"category": "components:content",
|
|
291
|
+
"ghost_version": ">=5.0.0",
|
|
292
|
+
"files": [
|
|
293
|
+
"share-buttons.hbs",
|
|
294
|
+
"share-buttons.css",
|
|
295
|
+
"share-buttons.js"
|
|
296
|
+
]
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
"name": "breadcrumbs",
|
|
300
|
+
"description": "Accessible breadcrumb navigation with Schema.org BreadcrumbList microdata for posts, tags, and authors.",
|
|
301
|
+
"category": "components:content",
|
|
302
|
+
"ghost_version": ">=5.0.0",
|
|
303
|
+
"files": [
|
|
304
|
+
"breadcrumbs.hbs",
|
|
305
|
+
"breadcrumbs.css"
|
|
306
|
+
]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"name": "visibility-badge",
|
|
310
|
+
"description": "Ghost-native access indicator pill for members-only, paid-only, tier-restricted, or public posts.",
|
|
311
|
+
"category": "components:members",
|
|
312
|
+
"ghost_version": ">=5.0.0",
|
|
313
|
+
"files": [
|
|
314
|
+
"visibility-badge.hbs",
|
|
315
|
+
"visibility-badge.css"
|
|
316
|
+
]
|
|
232
317
|
}
|
|
233
318
|
]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<!doctype html>
|
|
2
2
|
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Ghostcn playground</title><link rel="stylesheet" href="/playground.css"><script defer src="/playground.js"></script></head>
|
|
3
3
|
<body><header><p class="eyebrow">LOCAL COMPONENT PLAYGROUND</p><h1>Build it your way.</h1><p>Installed theme files. Synthetic content. No emails, payments or Ghost settings changes.</p></header>
|
|
4
|
-
<main><aside aria-label="Preview controls"><label>Component<select id="component"></select></label><p id="description"></p><label>Theme<select id="scheme"><option value="light">Light</option><option value="dark">Dark</option><option value="auto">Auto</option></select></label><label>Member state<select id="state"><option value="visitor">Visitor</option><option value="free">Free member</option><option value="paid">Paid member</option><option value="disabled">Membership disabled</option><option value="invite">Invite only</option><option value="restricted">Self-signup restricted</option></select></label><div id="parameters"></div><button id="reload" type="button">Reload preview</button><p id="status" role="status"></p></aside>
|
|
5
|
-
<section aria-label="Component preview"><iframe id="preview" title="Installed component preview" sandbox="allow-scripts"></iframe><h2>Use in your template</h2><pre><code id="usage"></code></pre><p>Classes only preview if your CSS defines them. For Tailwind, put utilities in a scanned .hbs file and keep <code>npm run dev</code> running. Verify real data, editor cards and member behavior in Ghost.</p><p>Preview changes are temporary. Copy the usage into your theme to keep them.</p></section></main></body></html>
|
|
4
|
+
<main><aside aria-label="Preview controls"><label>Component<select id="component"></select></label><p id="description"></p><label>Theme<select id="scheme"><option value="light">Light</option><option value="dark">Dark</option><option value="auto">Auto</option></select></label><label>Viewport<select id="viewport"><option value="full">Full width</option><option value="desktop">Desktop (1024px)</option><option value="tablet">Tablet (768px)</option><option value="mobile">Mobile (375px)</option></select></label><label>Member state<select id="state"><option value="visitor">Visitor</option><option value="free">Free member</option><option value="paid">Paid member</option><option value="free-only">Free-only site</option><option value="disabled">Membership disabled</option><option value="invite">Invite only</option><option value="restricted">Self-signup restricted</option></select></label><label>Post access<select id="access"><option value="true">Public / accessible</option><option value="false">Protected (paywall)</option></select></label><div id="parameters"></div><button id="reload" type="button">Reload preview</button><p id="status" role="status"></p></aside>
|
|
5
|
+
<section aria-label="Component preview"><div id="frame-stage" data-viewport="full"><iframe id="preview" title="Installed component preview" sandbox="allow-scripts"></iframe></div><div class="usage-header"><h2>Use in your template</h2><button id="copy-usage" type="button" class="copy-button">Copy snippet</button></div><pre><code id="usage"></code></pre><p>Classes only preview if your CSS defines them. For Tailwind, put utilities in a scanned .hbs file and keep <code>npm run dev</code> running. Verify real data, editor cards and member behavior in Ghost.</p><p>Preview changes are temporary. Copy the usage into your theme to keep them.</p></section></main></body></html>
|
|
@@ -13,6 +13,13 @@ input, select, button { font: inherit; width: 100%; min-width: 0; padding: 10px;
|
|
|
13
13
|
button { cursor: pointer; }
|
|
14
14
|
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid #bca1f8; outline-offset: 3px; }
|
|
15
15
|
label small, #description { color: #a0bdcf; }
|
|
16
|
-
|
|
16
|
+
#frame-stage { display: flex; justify-content: center; }
|
|
17
|
+
#frame-stage[data-viewport="desktop"] iframe { max-width: 1024px; }
|
|
18
|
+
#frame-stage[data-viewport="tablet"] iframe { max-width: 768px; }
|
|
19
|
+
#frame-stage[data-viewport="mobile"] iframe { max-width: 375px; }
|
|
20
|
+
.usage-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 20px; }
|
|
21
|
+
.usage-header h2 { margin: 0; }
|
|
22
|
+
.copy-button { width: auto; padding: 6px 14px; font-size: 13px; }
|
|
23
|
+
iframe { width: 100%; height: 650px; border: 1px solid #516475; border-radius: 8px; background: #fff; transition: max-width 200ms ease; }
|
|
17
24
|
pre { white-space: pre-wrap; overflow-wrap: anywhere; background: #1d2731; padding: 20px; border-radius: 8px; }
|
|
18
25
|
@media(max-width:760px) { main { grid-template-columns: 1fr; } aside { border-right: 0; border-bottom: 1px solid #34414e; } header, aside, section { padding: 20px; } }
|
|
@@ -1,21 +1,39 @@
|
|
|
1
1
|
const component = document.querySelector('#component');
|
|
2
2
|
const parameters = document.querySelector('#parameters');
|
|
3
3
|
const status = document.querySelector('#status');
|
|
4
|
+
const viewport = document.querySelector('#viewport');
|
|
5
|
+
const frameStage = document.querySelector('#frame-stage');
|
|
6
|
+
const copyUsage = document.querySelector('#copy-usage');
|
|
4
7
|
const controls = new Map();
|
|
5
|
-
|
|
8
|
+
const booleanParams = new Set(['disabled', 'required', 'invalid', 'urgent', 'decorative', 'open', 'hideDismiss', 'showPublic']);
|
|
9
|
+
let catalog = [], revision, busy = false, debounceTimer;
|
|
6
10
|
const current = () => catalog.find(item => item.name === component.value);
|
|
7
11
|
function render() {
|
|
8
12
|
const item = current();
|
|
9
13
|
if (!item) return;
|
|
10
|
-
const query = new URLSearchParams({
|
|
14
|
+
const query = new URLSearchParams({
|
|
15
|
+
scheme: document.querySelector('#scheme').value,
|
|
16
|
+
state: document.querySelector('#state').value,
|
|
17
|
+
access: document.querySelector('#access')?.value || 'true'
|
|
18
|
+
});
|
|
11
19
|
const args = [];
|
|
12
20
|
for (const [name, input] of controls) {
|
|
13
21
|
query.set(name, input.value);
|
|
14
|
-
if (
|
|
22
|
+
if (booleanParams.has(name)) {
|
|
23
|
+
if (input.value === 'true') args.push(`${name}=true`);
|
|
24
|
+
} else if (input.value) {
|
|
25
|
+
args.push(`${name}=${JSON.stringify(input.value)}`);
|
|
26
|
+
}
|
|
15
27
|
}
|
|
16
28
|
document.querySelector('#preview').src = `/preview/${item.name}?${query}`;
|
|
17
29
|
const context = item.name === 'author-card' ? ' primary_author' : item.name === 'pagination' ? ' pagination' : '';
|
|
18
|
-
document.querySelector('#usage').textContent =
|
|
30
|
+
document.querySelector('#usage').textContent = args.length
|
|
31
|
+
? `{{> "${item.partial}"${context}${args.map(arg => `\n ${arg}`).join('')}\n}}`
|
|
32
|
+
: `{{> "${item.partial}"${context}}}`;
|
|
33
|
+
}
|
|
34
|
+
function scheduleRender() {
|
|
35
|
+
clearTimeout(debounceTimer);
|
|
36
|
+
debounceTimer = setTimeout(render, 180);
|
|
19
37
|
}
|
|
20
38
|
function selectComponent() {
|
|
21
39
|
controls.clear(); parameters.replaceChildren();
|
|
@@ -24,7 +42,7 @@ function selectComponent() {
|
|
|
24
42
|
document.querySelector('#description').textContent = item.description;
|
|
25
43
|
const definitions = { ...item.parameters, ...Object.fromEntries(Object.keys(item.styleSlots).map(key => [key, { description: key === 'class' ? 'Root classes for this instance.' : `Classes for ${item.styleSlots[key].join(', ')}.` }])) };
|
|
26
44
|
for (const [name, definition] of Object.entries(definitions)) {
|
|
27
|
-
const boolean =
|
|
45
|
+
const boolean = booleanParams.has(name);
|
|
28
46
|
const label = document.createElement('label'); label.append(document.createTextNode(name));
|
|
29
47
|
const input = document.createElement(definition.options || boolean ? 'select' : 'input');
|
|
30
48
|
input.name = name;
|
|
@@ -33,6 +51,7 @@ function selectComponent() {
|
|
|
33
51
|
const help = document.createElement('small'); help.textContent = definition.description;
|
|
34
52
|
label.append(input, help); parameters.append(label); controls.set(name, input);
|
|
35
53
|
input.addEventListener('change', render);
|
|
54
|
+
if (input.tagName === 'INPUT') input.addEventListener('input', scheduleRender);
|
|
36
55
|
}
|
|
37
56
|
status.textContent = 'Read-only preview. Your files have not been changed.';
|
|
38
57
|
render();
|
|
@@ -50,6 +69,21 @@ async function loadCatalog() {
|
|
|
50
69
|
component.addEventListener('change', selectComponent);
|
|
51
70
|
document.querySelector('#scheme').addEventListener('change', render);
|
|
52
71
|
document.querySelector('#state').addEventListener('change', render);
|
|
72
|
+
document.querySelector('#access')?.addEventListener('change', render);
|
|
73
|
+
viewport?.addEventListener('change', () => {
|
|
74
|
+
if (frameStage) frameStage.dataset.viewport = viewport.value;
|
|
75
|
+
});
|
|
76
|
+
copyUsage?.addEventListener('click', async () => {
|
|
77
|
+
const snippet = document.querySelector('#usage')?.textContent || '';
|
|
78
|
+
if (!snippet) return;
|
|
79
|
+
try {
|
|
80
|
+
await navigator.clipboard.writeText(snippet);
|
|
81
|
+
copyUsage.textContent = 'Copied';
|
|
82
|
+
setTimeout(() => { copyUsage.textContent = 'Copy snippet'; }, 1500);
|
|
83
|
+
} catch {
|
|
84
|
+
status.textContent = 'Copy manually from the template snippet below.';
|
|
85
|
+
}
|
|
86
|
+
});
|
|
53
87
|
document.querySelector('#reload').addEventListener('click', render);
|
|
54
88
|
loadCatalog().catch(error => { status.textContent = error.message; });
|
|
55
89
|
setInterval(async () => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* Only the preview shell, never component defaults. */
|
|
2
|
+
:root { --ghost-accent-color: #385bc6; }
|
|
2
3
|
body { margin: 0; background: var(--background); color: var(--foreground); font: 16px/1.5 ui-sans-serif,system-ui,sans-serif; }
|
|
3
4
|
.playground-preview { padding: 24px; overflow-wrap: anywhere; }
|
|
4
5
|
#preview-feedback { font-size: 14px; color: var(--muted-foreground); margin-top: 16px; }
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import zlib from 'node:zlib';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
4
5
|
|
|
5
6
|
function inside(root, relative) {
|
|
6
7
|
const target = path.resolve(root, relative);
|
|
7
8
|
if (target === root || !target.startsWith(root + path.sep)) throw new Error(`Path escapes theme: ${relative}`);
|
|
8
|
-
// Never follow links when inspecting or packaging a theme.
|
|
9
|
+
// Never follow links (including dangling symlinks) when inspecting or packaging a theme.
|
|
9
10
|
let current = root;
|
|
10
11
|
for (const part of path.relative(root, target).split(path.sep)) {
|
|
11
12
|
current = path.join(current, part);
|
|
12
|
-
if (fs.
|
|
13
|
+
if (fs.lstatSync(current, { throwIfNoEntry: false })?.isSymbolicLink()) throw new Error(`Symlink is not supported: ${relative}`);
|
|
13
14
|
}
|
|
14
15
|
return target;
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
const stripHbsComments = source => source.replace(/\{\{!--[\s\S]*?--\}\}/g, '').replace(/\{\{![\s\S]*?\}\}/g, '');
|
|
19
|
+
|
|
17
20
|
export function inspectTheme(themeRoot) {
|
|
18
21
|
const root = path.resolve(themeRoot);
|
|
19
22
|
const errors = [], warnings = [];
|
|
@@ -26,8 +29,11 @@ export function inspectTheme(themeRoot) {
|
|
|
26
29
|
}
|
|
27
30
|
if (exists('package.json')) {
|
|
28
31
|
pkg = JSON.parse(read('package.json'));
|
|
29
|
-
if (!pkg.engines?.ghost) errors.push('package.json needs engines.ghost');
|
|
30
|
-
|
|
32
|
+
if (!pkg.engines?.ghost && !pkg.engines?.['ghost-api']) errors.push('package.json needs engines.ghost');
|
|
33
|
+
const hasAuthorEmail = typeof pkg.author === 'string'
|
|
34
|
+
? /<[^>\s]+@[^>\s]+>/.test(pkg.author)
|
|
35
|
+
: Boolean(pkg.author?.email);
|
|
36
|
+
if (!hasAuthorEmail) errors.push('package.json needs author.email');
|
|
31
37
|
}
|
|
32
38
|
if (exists('components.json')) {
|
|
33
39
|
config = JSON.parse(read('components.json'));
|
|
@@ -51,35 +57,51 @@ export function inspectTheme(themeRoot) {
|
|
|
51
57
|
};
|
|
52
58
|
const templates = fs.readdirSync(root).filter(name => name.endsWith('.hbs'));
|
|
53
59
|
if (exists('partials')) scan('partials', file => { if (file.endsWith('.hbs')) templates.push(file); });
|
|
60
|
+
if (exists('members')) scan('members', file => { if (file.endsWith('.hbs')) templates.push(file); });
|
|
54
61
|
for (const file of templates) {
|
|
55
|
-
const source = read(file)
|
|
62
|
+
const source = stripHbsComments(read(file));
|
|
56
63
|
for (const match of source.matchAll(/\{\{asset\s+["']([^"']+)["']/g)) {
|
|
57
64
|
if (!exists(`assets/${match[1]}`)) errors.push(`${file}: missing asset ${match[1]} (run your build)`);
|
|
58
65
|
}
|
|
59
|
-
for (const match of source.matchAll(/\{\{(?:#>|>)\s*["']([^"']+)["']/g)) {
|
|
60
|
-
|
|
66
|
+
for (const match of source.matchAll(/\{\{(?:#>|>)\s*(?:["']([^"']+)["']|([a-zA-Z0-9_/-]+))/g)) {
|
|
67
|
+
const partial = match[1] || match[2];
|
|
68
|
+
if (!partial || partial.startsWith('@')) continue;
|
|
69
|
+
if (!exists(`partials/${partial}.hbs`)) errors.push(`${file}: missing partial ${partial}`);
|
|
61
70
|
}
|
|
62
71
|
}
|
|
63
72
|
if (exists('default.hbs')) {
|
|
64
73
|
const layout = read('default.hbs');
|
|
74
|
+
const stripped = stripHbsComments(layout);
|
|
65
75
|
const linked = new Set();
|
|
66
|
-
for (const tag of
|
|
76
|
+
for (const tag of stripped.matchAll(/<link\b[^>]*>/gi)) {
|
|
67
77
|
if (!/\brel\s*=\s*["']stylesheet["']/i.test(tag[0])) continue;
|
|
68
78
|
const asset = tag[0].match(/\{\{asset\s+["']([^"']+)["']/);
|
|
69
79
|
if (asset) linked.add(asset[1]);
|
|
70
80
|
}
|
|
81
|
+
const linkedScripts = new Set();
|
|
82
|
+
for (const tag of stripped.matchAll(/<script\b[^>]*>/gi)) {
|
|
83
|
+
const asset = tag[0].match(/\{\{asset\s+["']([^"']+)["']/);
|
|
84
|
+
if (asset) linkedScripts.add(asset[1]);
|
|
85
|
+
}
|
|
71
86
|
for (const helper of ['ghost_head', 'ghost_foot']) {
|
|
72
|
-
if (!new RegExp(`\\{\\{\\s*${helper}\\s*\\}\\}`).test(
|
|
87
|
+
if (!new RegExp(`\\{\\{\\s*${helper}\\s*\\}\\}`).test(stripped)) errors.push(`default.hbs needs {{${helper}}}`);
|
|
73
88
|
}
|
|
74
89
|
if (config && !linked.has(path.relative(path.join(root, 'assets'), inside(root, config.cssFile)).replaceAll('\\', '/'))) {
|
|
75
90
|
errors.push('default.hbs does not link the configured ghostcn stylesheet');
|
|
76
91
|
}
|
|
77
92
|
if (config?.aliases?.styles && exists(config.aliases.styles)) {
|
|
78
|
-
for (const name of fs.readdirSync(inside(root, config.aliases.styles)).filter(name => name.endsWith('.css'))) {
|
|
93
|
+
for (const name of fs.readdirSync(inside(root, config.aliases.styles)).filter(name => name.toLowerCase().endsWith('.css'))) {
|
|
79
94
|
const asset = path.relative(path.join(root, 'assets'), inside(root, path.join(config.aliases.styles, name))).replaceAll('\\', '/');
|
|
80
95
|
if (!linked.has(asset)) errors.push(`default.hbs does not link component stylesheet ${asset}`);
|
|
81
96
|
}
|
|
82
97
|
}
|
|
98
|
+
const jsDir = config?.aliases?.js || 'assets/js/components';
|
|
99
|
+
if (config && exists(jsDir)) {
|
|
100
|
+
for (const name of fs.readdirSync(inside(root, jsDir)).filter(name => name.toLowerCase().endsWith('.js'))) {
|
|
101
|
+
const asset = path.relative(path.join(root, 'assets'), inside(root, path.join(jsDir, name))).replaceAll('\\', '/');
|
|
102
|
+
if (!linkedScripts.has(asset)) errors.push(`default.hbs does not link component script ${asset}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
83
105
|
}
|
|
84
106
|
} catch (error) { errors.push(error.message); }
|
|
85
107
|
return { errors: [...new Set(errors)], warnings: [...new Set(warnings)] };
|
|
@@ -98,25 +120,25 @@ export function packageTheme(themeRoot) {
|
|
|
98
120
|
const root = path.resolve(themeRoot);
|
|
99
121
|
const result = inspectTheme(root);
|
|
100
122
|
if (result.errors.length) throw new Error(result.errors.join('\n'));
|
|
101
|
-
const pkg = JSON.parse(fs.readFileSync(inside(root, 'package.json'), 'utf8'));
|
|
123
|
+
const pkg = JSON.parse(fs.readFileSync(inside(root, 'package.json'), 'utf8').replace(/^\uFEFF/, ''));
|
|
102
124
|
if (!/^[a-z0-9][a-z0-9-]*$/.test(pkg.name) || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/.test(pkg.version)) {
|
|
103
125
|
throw new Error('Packaging needs a safe lowercase theme name and a semantic version in package.json');
|
|
104
126
|
}
|
|
105
|
-
const files = fs.readdirSync(root).filter(name => name.endsWith('.hbs') || ['package.json', 'LICENSE', 'LICENSE.md'].includes(name));
|
|
127
|
+
const files = fs.readdirSync(root).filter(name => name.endsWith('.hbs') || ['package.json', 'LICENSE', 'LICENSE.md', 'routes.yaml'].includes(name));
|
|
106
128
|
// Keep a source stylesheet if an existing theme actually links it at runtime.
|
|
107
129
|
const sourceStyles = new Set(['assets/css/source.css', 'assets/css/ghostcn-tailwind.css']);
|
|
108
130
|
const linkedStyles = new Set();
|
|
109
|
-
const findLinks = dir => {
|
|
131
|
+
const findLinks = (dir, insideTemplateDir = false) => {
|
|
110
132
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
111
133
|
if (entry.name.startsWith('.') || entry.name === 'node_modules' || entry.name === 'dist' || entry.name === 'assets') continue;
|
|
112
134
|
const target = path.join(dir, entry.name);
|
|
113
|
-
if (entry.isDirectory() && entry.name === 'partials') {
|
|
135
|
+
if (entry.isDirectory() && (insideTemplateDir || entry.name === 'partials' || entry.name === 'members')) {
|
|
114
136
|
inside(root, path.relative(root, target));
|
|
115
|
-
findLinks(target);
|
|
137
|
+
findLinks(target, true);
|
|
116
138
|
}
|
|
117
139
|
else if (entry.isFile() && entry.name.endsWith('.hbs')) {
|
|
118
140
|
inside(root, path.relative(root, target));
|
|
119
|
-
for (const match of fs.readFileSync(target, 'utf8').matchAll(/\{\{asset\s+["']([^"']+)["']/g)) linkedStyles.add(`assets/${match[1]}`);
|
|
141
|
+
for (const match of stripHbsComments(fs.readFileSync(target, 'utf8')).matchAll(/\{\{asset\s+["']([^"']+)["']/g)) linkedStyles.add(`assets/${match[1]}`);
|
|
120
142
|
}
|
|
121
143
|
}
|
|
122
144
|
};
|
|
@@ -130,24 +152,28 @@ export function packageTheme(themeRoot) {
|
|
|
130
152
|
else if (entry.isFile() && !relative.endsWith('.map') && (!sourceStyles.has(relative) || linkedStyles.has(relative))) files.push(relative);
|
|
131
153
|
}
|
|
132
154
|
};
|
|
133
|
-
for (const dir of ['partials', 'assets']) if (
|
|
155
|
+
for (const dir of ['partials', 'assets', 'locales', 'members']) if (existsSafe(root, dir)) collect(dir);
|
|
134
156
|
const chunks = [], directory = [];
|
|
135
157
|
let offset = 0;
|
|
136
158
|
for (const file of files.sort()) {
|
|
137
159
|
const name = Buffer.from(file.replaceAll('\\', '/'));
|
|
138
160
|
const data = fs.readFileSync(inside(root, file));
|
|
161
|
+
const deflated = zlib.deflateRawSync(data);
|
|
162
|
+
const useDeflate = deflated.length < data.length;
|
|
163
|
+
const payload = useDeflate ? deflated : data;
|
|
164
|
+
const method = useDeflate ? 8 : 0;
|
|
139
165
|
const crc = crc32(data);
|
|
140
166
|
const local = Buffer.alloc(30);
|
|
141
|
-
local.writeUInt32LE(0x04034b50); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x800, 6);
|
|
167
|
+
local.writeUInt32LE(0x04034b50); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x800, 6); local.writeUInt16LE(method, 8);
|
|
142
168
|
local.writeUInt16LE(33, 12); // 1980-01-01: deterministic archive timestamps
|
|
143
|
-
local.writeUInt32LE(crc, 14); local.writeUInt32LE(
|
|
144
|
-
chunks.push(local, name,
|
|
169
|
+
local.writeUInt32LE(crc, 14); local.writeUInt32LE(payload.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(name.length, 26);
|
|
170
|
+
chunks.push(local, name, payload);
|
|
145
171
|
const central = Buffer.alloc(46);
|
|
146
|
-
central.writeUInt32LE(0x02014b50); central.writeUInt16LE(20, 4); central.writeUInt16LE(20, 6); central.writeUInt16LE(0x800, 8);
|
|
147
|
-
central.writeUInt16LE(33, 14); central.writeUInt32LE(crc, 16); central.writeUInt32LE(
|
|
172
|
+
central.writeUInt32LE(0x02014b50); central.writeUInt16LE(20, 4); central.writeUInt16LE(20, 6); central.writeUInt16LE(0x800, 8); central.writeUInt16LE(method, 10);
|
|
173
|
+
central.writeUInt16LE(33, 14); central.writeUInt32LE(crc, 16); central.writeUInt32LE(payload.length, 20); central.writeUInt32LE(data.length, 24);
|
|
148
174
|
central.writeUInt16LE(name.length, 28); central.writeUInt32LE(offset, 42);
|
|
149
175
|
directory.push(central, name);
|
|
150
|
-
offset += local.length + name.length +
|
|
176
|
+
offset += local.length + name.length + payload.length;
|
|
151
177
|
}
|
|
152
178
|
if (files.length > 65535 || offset > 0xffffffff) throw new Error('Theme exceeds ZIP32 limits');
|
|
153
179
|
const central = Buffer.concat(directory), end = Buffer.alloc(22);
|
|
@@ -160,6 +186,11 @@ export function packageTheme(themeRoot) {
|
|
|
160
186
|
return { output, files };
|
|
161
187
|
}
|
|
162
188
|
|
|
189
|
+
function existsSafe(root, dir) {
|
|
190
|
+
const target = inside(root, dir);
|
|
191
|
+
return fs.existsSync(target) && fs.statSync(target).isDirectory();
|
|
192
|
+
}
|
|
193
|
+
|
|
163
194
|
export function reportTheme(themeRoot) {
|
|
164
195
|
const result = inspectTheme(themeRoot);
|
|
165
196
|
for (const warning of result.warnings) console.warn(`WARN: ${warning}`);
|
|
@@ -169,7 +200,14 @@ export function reportTheme(themeRoot) {
|
|
|
169
200
|
return true;
|
|
170
201
|
}
|
|
171
202
|
|
|
172
|
-
|
|
203
|
+
const isEntrypoint = (argvPath, metaUrl) => {
|
|
204
|
+
if (!argvPath) return false;
|
|
205
|
+
const left = path.resolve(argvPath);
|
|
206
|
+
const right = fileURLToPath(metaUrl);
|
|
207
|
+
return process.platform === 'win32' ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (isEntrypoint(process.argv[1], import.meta.url)) {
|
|
173
211
|
const root = path.resolve(process.argv[3] || process.cwd());
|
|
174
212
|
try {
|
|
175
213
|
if (process.argv[2] === 'pack') console.log(`Created ${packageTheme(root).output}`);
|
|
@@ -15,4 +15,4 @@ export declare function compareComponent(themeRoot: string, config: GhostcnConfi
|
|
|
15
15
|
incomingRevision: string;
|
|
16
16
|
files: FileComparison[];
|
|
17
17
|
};
|
|
18
|
-
export declare function unifiedDiff(before: string | undefined, after: string | undefined, label: string): string;
|
|
18
|
+
export declare function unifiedDiff(before: string | undefined, after: string | undefined, label: string, contextLines?: number): string;
|
|
@@ -28,6 +28,7 @@ async function comparisonManifest(name, latest) {
|
|
|
28
28
|
throw new Error('Registry returned an invalid component manifest.');
|
|
29
29
|
return manifest;
|
|
30
30
|
}
|
|
31
|
+
const normalizeEol = (value) => value?.replace(/\r\n/g, '\n');
|
|
31
32
|
function compareComponent(themeRoot, config, manifest) {
|
|
32
33
|
const files = (0, component_files_1.componentFiles)(themeRoot, config, manifest);
|
|
33
34
|
const saved = (0, component_history_1.readHistory)(themeRoot).components[manifest.name];
|
|
@@ -38,18 +39,21 @@ function compareComponent(themeRoot, config, manifest) {
|
|
|
38
39
|
const baseline = saved?.files[relative];
|
|
39
40
|
const local = fs_1.default.existsSync(target) ? fs_1.default.readFileSync(target, 'utf8') : undefined;
|
|
40
41
|
const next = incoming[relative];
|
|
42
|
+
const normBaseline = normalizeEol(baseline);
|
|
43
|
+
const normLocal = normalizeEol(local);
|
|
44
|
+
const normNext = normalizeEol(next);
|
|
41
45
|
let status;
|
|
42
46
|
if (next === undefined)
|
|
43
47
|
status = 'removed-upstream';
|
|
44
48
|
else if (local === undefined)
|
|
45
49
|
status = 'missing';
|
|
46
|
-
else if (
|
|
50
|
+
else if (normLocal === normNext)
|
|
47
51
|
status = 'unchanged';
|
|
48
52
|
else if (baseline === undefined)
|
|
49
53
|
status = 'untracked';
|
|
50
|
-
else if (
|
|
54
|
+
else if (normBaseline === normNext)
|
|
51
55
|
status = 'locally-modified';
|
|
52
|
-
else if (
|
|
56
|
+
else if (normBaseline === normLocal)
|
|
53
57
|
status = 'upstream-changed';
|
|
54
58
|
else
|
|
55
59
|
status = 'conflict';
|
|
@@ -57,37 +61,62 @@ function compareComponent(themeRoot, config, manifest) {
|
|
|
57
61
|
});
|
|
58
62
|
return { name: manifest.name, installedRevision: saved?.revision, incomingRevision: (0, component_history_1.revision)(incoming), files: comparisons };
|
|
59
63
|
}
|
|
60
|
-
//
|
|
61
|
-
function unifiedDiff(before, after, label) {
|
|
62
|
-
if (before === after)
|
|
64
|
+
// Contextual line diff (3-line hunks) with bounded memory. Large files remain reviewable as replacement hunks.
|
|
65
|
+
function unifiedDiff(before, after, label, contextLines = 3) {
|
|
66
|
+
if (normalizeEol(before) === normalizeEol(after))
|
|
63
67
|
return '';
|
|
64
|
-
const split = (value) => value?.
|
|
68
|
+
const split = (value) => normalizeEol(value)?.match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
65
69
|
const a = split(before);
|
|
66
70
|
const b = split(after);
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
71
|
+
const header = [`--- ${label} (local)`, `+++ ${label} (registry)`];
|
|
72
|
+
const formatLine = (prefix, value) => {
|
|
73
|
+
const out = [prefix + (value.endsWith('\n') ? value.slice(0, -1) : value)];
|
|
70
74
|
if (!value.endsWith('\n'))
|
|
71
|
-
|
|
75
|
+
out.push('\');
|
|
76
|
+
return out;
|
|
72
77
|
};
|
|
73
78
|
if (a.length * b.length > 1_000_000) {
|
|
74
|
-
a.
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
const out = [...header, `@@ -${a.length ? 1 : 0},${a.length} +${b.length ? 1 : 0},${b.length} @@`];
|
|
80
|
+
a.forEach(value => out.push(...formatLine('-', value)));
|
|
81
|
+
b.forEach(value => out.push(...formatLine('+', value)));
|
|
82
|
+
return out.join('\n');
|
|
77
83
|
}
|
|
78
84
|
const width = b.length + 1;
|
|
79
85
|
const matrix = new Uint32Array((a.length + 1) * width);
|
|
80
86
|
for (let i = a.length - 1; i >= 0; i--)
|
|
81
87
|
for (let j = b.length - 1; j >= 0; j--)
|
|
82
88
|
matrix[i * width + j] = a[i] === b[j] ? 1 + matrix[(i + 1) * width + j + 1] : Math.max(matrix[(i + 1) * width + j], matrix[i * width + j + 1]);
|
|
89
|
+
const ops = [];
|
|
83
90
|
let i = 0, j = 0;
|
|
84
91
|
while (i < a.length || j < b.length) {
|
|
85
92
|
if (i < a.length && j < b.length && a[i] === b[j])
|
|
86
|
-
|
|
93
|
+
ops.push({ kind: ' ', value: a[i], oldLine: i + 1, newLine: j + 1 }), i++, j++;
|
|
87
94
|
else if (i < a.length && (j === b.length || matrix[(i + 1) * width + j] >= matrix[i * width + j + 1]))
|
|
88
|
-
|
|
95
|
+
ops.push({ kind: '-', value: a[i], oldLine: i + 1, newLine: j + 1 }), i++;
|
|
89
96
|
else
|
|
90
|
-
|
|
97
|
+
ops.push({ kind: '+', value: b[j], oldLine: i + 1, newLine: j + 1 }), j++;
|
|
98
|
+
}
|
|
99
|
+
const ranges = [];
|
|
100
|
+
ops.forEach((op, idx) => {
|
|
101
|
+
if (op.kind === ' ')
|
|
102
|
+
return;
|
|
103
|
+
const start = Math.max(0, idx - contextLines);
|
|
104
|
+
const end = Math.min(ops.length - 1, idx + contextLines);
|
|
105
|
+
if (ranges.length && start <= ranges[ranges.length - 1][1] + 1)
|
|
106
|
+
ranges[ranges.length - 1][1] = end;
|
|
107
|
+
else
|
|
108
|
+
ranges.push([start, end]);
|
|
109
|
+
});
|
|
110
|
+
const lines = [...header];
|
|
111
|
+
for (const [start, end] of ranges) {
|
|
112
|
+
const slice = ops.slice(start, end + 1);
|
|
113
|
+
const oldCount = slice.filter(op => op.kind !== '+').length;
|
|
114
|
+
const newCount = slice.filter(op => op.kind !== '-').length;
|
|
115
|
+
const oldStart = oldCount ? slice.find(op => op.kind !== '+').oldLine : Math.max(0, slice[0].oldLine - 1);
|
|
116
|
+
const newStart = newCount ? slice.find(op => op.kind !== '-').newLine : Math.max(0, slice[0].newLine - 1);
|
|
117
|
+
lines.push(`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@`);
|
|
118
|
+
for (const op of slice)
|
|
119
|
+
lines.push(...formatLine(op.kind, op.value));
|
|
91
120
|
}
|
|
92
121
|
return lines.join('\n');
|
|
93
122
|
}
|