@vettvangur/design-system 0.0.21 → 0.0.22

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.
@@ -0,0 +1,483 @@
1
+ #!/usr/bin/env node
2
+ import { promises } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { p as parseColors, a as parseTypography, i as inputsSpec } from './inputs-RHH3YuXh.js';
5
+ import { r as readFigma, p as parseButtons } from './generate-tailwind-DsVzILdL.js';
6
+ import chalk from 'chalk';
7
+ import 'node:url';
8
+ import 'node:process';
9
+ import 'node:fs/promises';
10
+ import 'fs';
11
+ import 'path';
12
+ import 'os';
13
+ import 'crypto';
14
+
15
+ // core/razor/generate-razor.mjs
16
+ const CWD = process.cwd();
17
+ const OUT_DIR = path.resolve(CWD, '../Vettvangur.Site/Views/Partials/DesignSystem');
18
+ const FILE_COLORS = path.join(OUT_DIR, 'Colors.cshtml');
19
+ const FILE_TYPO = path.join(OUT_DIR, 'Typography.cshtml');
20
+ const FILE_BUTTONS = path.join(OUT_DIR, 'Buttons.cshtml');
21
+ const FILE_INPUTS = path.join(OUT_DIR, 'Inputs.cshtml');
22
+ const FILE_RICHTEXT = path.join(OUT_DIR, 'Richtext.cshtml');
23
+ const FILE_Table = path.join(OUT_DIR, 'Tables.cshtml');
24
+ const tag = chalk.cyan('[design-system]');
25
+ async function generateRazor() {
26
+ console.log(`${tag} generating razor files...`);
27
+ await promises.mkdir(OUT_DIR, {
28
+ recursive: true
29
+ });
30
+ const figma = await readFigma();
31
+ const [colors, typography, buttons] = await Promise.all([parseColors(), parseTypography(), parseButtons(figma)]);
32
+
33
+ // Colors
34
+ if (colors?.length) {
35
+ await promises.writeFile(FILE_COLORS, renderColors(colors), 'utf8');
36
+ log(FILE_COLORS, colors.length);
37
+ }
38
+
39
+ // Typography (one file)
40
+ if ((typography?.headlines?.length ?? 0) + (typography?.bodies?.length ?? 0) > 0) {
41
+ await promises.writeFile(FILE_TYPO, renderTypography(typography), 'utf8');
42
+ log(FILE_TYPO, (typography.headlines?.length ?? 0) + (typography.bodies?.length ?? 0));
43
+ }
44
+
45
+ // Buttons
46
+ if (buttons?.length) {
47
+ await promises.writeFile(FILE_BUTTONS, renderButtons(buttons), 'utf8');
48
+ log(FILE_BUTTONS, buttons.length);
49
+ }
50
+
51
+ // Inputs
52
+ const hasAnyInputs = Object.values(inputsSpec).some(v => Array.isArray(v) && v.length);
53
+ if (hasAnyInputs) {
54
+ await promises.writeFile(FILE_INPUTS, renderInputs(inputsSpec), 'utf8');
55
+ log(FILE_INPUTS, countInputs(inputsSpec));
56
+ }
57
+
58
+ // Richtext & Tables (static demos)
59
+ await promises.writeFile(FILE_RICHTEXT, renderRichtext(), 'utf8');
60
+ log(FILE_RICHTEXT, 1);
61
+ await promises.writeFile(FILE_Table, renderRichtextTables(), 'utf8');
62
+ log(FILE_Table, 1);
63
+ console.log(`${tag} great success!`);
64
+ }
65
+
66
+ /* -------------------- RENDERERS -------------------- */
67
+
68
+ function renderColors(colors) {
69
+ const items = colors.map(({
70
+ title,
71
+ color
72
+ }) => `
73
+ <div class="styleguide-color">
74
+ <span class="styleguide-color__label label">${h(title)}</span>
75
+ <div class="styleguide-color__swatch" style="background: ${a(color)};"></div>
76
+ <div class="styleguide-color__info">
77
+ <span class="styleguide-color__info-hex label">${h(color)}</span>
78
+ </div>
79
+ </div>`).join('\n');
80
+ return `@* Auto-generated — do not edit by hand *@
81
+ <section class="styleguide-colors styleguide-spacing">
82
+ <div class="container">
83
+ <vv-headline
84
+ identifier="styleguide__headline",
85
+ modifier="headline-h1",
86
+ text="Colors"
87
+ />
88
+
89
+ <div class="styleguide-colors-grid grid grid--gap">
90
+ ${items}
91
+ </div>
92
+ </div>
93
+ </section>
94
+ `;
95
+ }
96
+ function renderTypography({
97
+ headlines = [],
98
+ bodies = []
99
+ }) {
100
+ const headlineItems = headlines.map(hItem => {
101
+ const mod = classToModifier(hItem.class, 'headline');
102
+ const info = infoBlocks(hItem);
103
+ return `
104
+ <div class="styleguide-typography__item grid grid--gap">
105
+ <span class="styleguide-typography__item-name">${h(hItem.title)}</span>
106
+ <vv-headline
107
+ identifier="styleguide__headline"
108
+ modifier="${a(mod)}"
109
+ text="The quick brown fox jumps over the lazy dog."
110
+ />
111
+ ${info ? renderInfo(info) : ''}
112
+ </div>`;
113
+ }).join('\n');
114
+ const bodyItems = bodies.map(b => {
115
+ const info = infoBlocks(b);
116
+ return `
117
+ <div class="styleguide-typography__item grid grid--gap">
118
+ <span class="styleguide-typography__item-name">${h(b.title)}</span>
119
+ <p class="${a(b.class)} styleguide-typography__item-example">
120
+ The quick brown fox jumps over the lazy dog.
121
+ </p>
122
+ ${info ? renderInfo(info) : ''}
123
+ </div>`;
124
+ }).join('\n');
125
+ return `@* Auto-generated — do not edit by hand *@
126
+ <section class="styleguide-typography styleguide-spacing">
127
+ <div class="container">
128
+ <vv-headline
129
+ identifier="styleguide__headline"
130
+ modifier="headline-h1"
131
+ text="Typography"
132
+ />
133
+
134
+ ${headlines.length ? `
135
+ <div class="styleguide-typography__group grid grid--gap">
136
+ <vv-headline
137
+ identifier="styleguide__headline"
138
+ modifier="headline-3"
139
+ text="Headlines"
140
+ />
141
+ <div class="styleguide-typography__list grid grid--gap">
142
+ ${headlineItems}
143
+ </div>
144
+ </div>` : ''}
145
+
146
+ ${bodies.length ? `
147
+ <div class="styleguide-typography__group grid grid--gap">
148
+ <vv-headline
149
+ identifier="styleguide__headline"
150
+ modifier="headline-3"
151
+ text="Body text"
152
+ />
153
+ <div class="styleguide-typography__list grid grid--gap">
154
+ ${bodyItems}
155
+ </div>
156
+ </div>` : ''}
157
+ </div>
158
+ </section>
159
+ `;
160
+ }
161
+ function renderInfo(info) {
162
+ const {
163
+ base,
164
+ desktop,
165
+ mobile
166
+ } = info;
167
+ return `
168
+ <div class="styleguide-typography__info">
169
+ ${base ? `
170
+ <div class="styleguide-typography__info-item">
171
+ <span>Base</span>
172
+ <span><strong>${h(base)}</strong></span>
173
+ </div>` : ''}
174
+ ${desktop ? `
175
+ <div class="styleguide-typography__info-item">
176
+ <span>Desktop</span>
177
+ <span><strong>${h(desktop)}</strong></span>
178
+ </div>` : ''}
179
+ ${mobile ? `
180
+ <div class="styleguide-typography__info-item">
181
+ <span>Mobile</span>
182
+ <span><strong>${h(mobile)}</strong></span>
183
+ </div>` : ''}
184
+ </div>`;
185
+ }
186
+ function renderButtons(buttons) {
187
+ const items = buttons.map(b => {
188
+ const label = pickButtonText(b);
189
+ return `
190
+ <div class="styleguide__button-wrap">
191
+ <vv-button
192
+ identifier="styleguide__button"
193
+ modifier="${a(b)}"
194
+ type="@ButtonType.Button"
195
+ text="${a(label)}"
196
+ />
197
+ </div>`;
198
+ }).join('\n');
199
+ return `@* Auto-generated — do not edit by hand *@
200
+ <section class="styleguide-buttons styleguide-spacing">
201
+ <div class="container">
202
+ <vv-headline
203
+ identifier="styleguide__headline",
204
+ modifier="headline-h1",
205
+ text="Buttons"
206
+ />
207
+
208
+ <div class="grid grid--gap">
209
+ ${items}
210
+ </div>
211
+ </div>
212
+ </section>
213
+ `;
214
+ }
215
+ function pickButtonText(modifier) {
216
+ const m = (modifier || '').toLowerCase();
217
+
218
+ // intent-based overrides
219
+ if (/\b(danger|destructive|error|delete|remove)\b/.test(m)) return 'Delete';
220
+ if (/\b(success|confirm|ok|apply|save)\b/.test(m)) return 'Confirm';
221
+ if (/\b(warn|warning|alert)\b/.test(m)) return 'Proceed with Caution';
222
+ if (/\b(primary|cta|proceed|next)\b/.test(m)) return 'Continue';
223
+ if (/\b(secondary|ghost|tertiary|neutral)\b/.test(m)) return 'Learn More';
224
+ if (/\b(buy|checkout|pay|purchase|cart)\b/.test(m)) return 'Buy Now';
225
+ if (/\b(download|install|get)\b/.test(m)) return 'Download';
226
+ if (/\b(subscribe|signup|register)\b/.test(m)) return 'Subscribe';
227
+ if (/\b(login|signin)\b/.test(m)) return 'Sign In';
228
+ if (/\b(contact|email|message)\b/.test(m)) return 'Contact Us';
229
+
230
+ // size-only or shape-only variants keep a neutral verb
231
+ if (/\b(xs|sm|md|lg|xl|square|pill|round|circle|wide|tall)\b/.test(m)) return seededPick(m, ['Continue', 'Details', 'Open', 'Select', 'View', 'Explore', 'Try', 'Start']);
232
+
233
+ // icon-only hints (still provide text for a11y)
234
+ if (/\b(icon|only|icon-only)\b/.test(m)) return 'Open';
235
+
236
+ // default: deterministic pick
237
+ return seededPick(m, DEFAULT_WORDS);
238
+ }
239
+ const DEFAULT_WORDS = ['Continue', 'Details', 'Select', 'Open', 'Start', 'Try', 'Explore', 'Next', 'View', 'Confirm', 'Apply', 'Proceed', 'Get Quote', 'Book Now', 'Add to Cart', 'Learn More', 'Download', 'Subscribe'];
240
+
241
+ // Deterministic selection from a list based on the modifier string
242
+ function seededPick(seed, list) {
243
+ let h = 0;
244
+ for (let i = 0; i < seed.length; i++) h = h * 131 + seed.charCodeAt(i) >>> 0;
245
+ return list[h % list.length];
246
+ }
247
+ function renderInputs(spec) {
248
+ const {
249
+ inputs = [],
250
+ selects = [],
251
+ checkboxes = [],
252
+ radios = [],
253
+ textareas = []
254
+ } = spec;
255
+ const inputsPartials = inputs.map(i => `
256
+ <vv-input
257
+ id="${a(i.id || '')}"
258
+ label="${a(i.Label ?? i.label ?? 'Input')}"
259
+ placeholder="${a(i.placeholder ?? 'Input placeholder')}"
260
+ is-disabled="${bool(i.isDisabled)}"
261
+ is-readonly="${bool(i.isReadonly)}"
262
+ is-error="${bool(i.isError)}"
263
+ />
264
+ `).join('\n');
265
+ const selectsPartials = selects.map(s => `
266
+ <vv-select
267
+ id="${a(s.id || '')}"
268
+ label="${a(s.Label ?? 'Select')}"
269
+ options="@selectOptions"
270
+ is-disabled="${bool(s.isDisabled)}"
271
+ is-readOnly="${bool(s.isReadonly)}"
272
+ is-error="${bool(s.isError)}"
273
+ />
274
+ `).join('\n');
275
+ const checkboxRows = checkboxes.map(c => `
276
+ <div class="input-group">
277
+ <vv-selection
278
+ id="${a(c.id || '')}"
279
+ label="${a(c.Label ?? c.label ?? 'Checkbox')}"
280
+ type="@HTMLInputType.Checkbox"
281
+ is-disabled="${bool(c.isDisabled)}"
282
+ is-error="${bool(c.isError)}"
283
+ />
284
+ </div>
285
+ `).join('\n');
286
+ const radioRows = radios.map(r => `
287
+ <div class="input-group">
288
+ <vv-selection
289
+ id="${a(r.id || '')}"
290
+ type="@HTMLInputType.Radio"
291
+ name="${a(r.Name ?? r.name ?? 'prettyradio')}"
292
+ label="${a(r.Label ?? r.label ?? 'Radio')}"
293
+ is-disabled="${bool(r.isDisabled)}"
294
+ is-error="${bool(r.isError)}"
295
+ />
296
+ </div>`).join('\n');
297
+ const textareasPartials = textareas.map(t => `
298
+ <vv-textarea
299
+ id="${a(t.id || '')}"
300
+ label="${a(t.Label ?? 'Textarea')}"
301
+ placeholder="${a(t.placeholder ?? 'Textarea placeholder')}"
302
+ is-disabled="${bool(t.isDisabled)}"
303
+ is-readonly="${bool(t.isReadonly)}"
304
+ is-error="${bool(t.isError)}"
305
+ />
306
+ `).join('\n');
307
+ return `@* Auto-generated — do not edit by hand *@
308
+ @{
309
+ var selectOptions = new Dictionary<string, string>() {
310
+ { "option1", "Option value" },
311
+ { "option2", "Option value" },
312
+ { "option3", "Option value" },
313
+ { "option4", "Option value" },
314
+ { "option5", "Option value" },
315
+ { "option6", "Option value" },
316
+ };
317
+ }
318
+
319
+ <section class="styleguide-inputs styleguide-spacing">
320
+ <div class="container">
321
+ <vv-headline
322
+ identifier="styleguide__headline",
323
+ modifier="headline-h1",
324
+ text="Inputs"
325
+ />
326
+
327
+ <div class="styleguide-inputs__scrollitem grid grid--gap grid--align-start">
328
+ ${inputsPartials}
329
+ ${selectsPartials}
330
+ ${checkboxRows}
331
+ ${radioRows}
332
+ ${textareasPartials}
333
+ </div>
334
+ </div>
335
+ </section>
336
+ `;
337
+ }
338
+
339
+ /* ------------ Richtext (static demo) ------------ */
340
+ function renderRichtext() {
341
+ return `@* Auto-generated — do not edit by hand *@
342
+ <section class="w-full">
343
+ <vv-headline
344
+ identifier="styleguide__headline",
345
+ modifier="headline-h1",
346
+ text="Richtext"
347
+ />
348
+
349
+ <div class="richtext">
350
+ <h1>h1</h1>
351
+ <h2>h2</h2>
352
+ <h3>h3</h3>
353
+ <h4>h4</h4>
354
+ <h5>h5</h5>
355
+ <p>
356
+ Ófróðlegt þorviðar <a href="https://www.vettvangur.is/" target="_blank">látist kærðu</a>, allfeginn því að kaupmanni, <strong>hraðasta</strong> ættleri <em>farminum</em> þuríði brústeinunum konungsþræll grænar. Hólmganga hrísflekkur, lifum skalla-grímur tíkr. Meiðinn hatast oddleifsdóttir, dólglegast dyflini, torfafóstri torfunni rak jarðarmenið stirðir. Býsn hvé ölseljunni gerði, jafnvitur lög vaskligr, sám hvortveggja ófrelsi trúfasti hallkelsdóttur sneri mund guðmundarsonum. Sekum meginmerkurinnar, arfsali allmikilli sjaldgæf. Uggasyni sættu konungsþræll gæs, alþýðuskap orðtækið stokkar, orkar mund ormláðs skrumaði hriflusonar virðak. Biðjum meiðinn keppa, taliðr uggasyni, skerðan manngjarnlega hofgyðja vilk allmikilli. Ógott hirðum samdóma, heimamaðr endilangt, erlingur kaldur gjósa seimþollr hallkell landkaupi. Torfunni tungu-oddur þorviðar gufárós, ævifús sekur tröllskessa, umbanda rétttrúaður bráðan svörfuði nýt íþrótt dalalönd halli. Flyðruness haldnar aðsóknar skerðan, auðhnykkjanda mannvandur smálöndum sauðarhöfðinu ferjunni.
357
+ </p>
358
+ <ul>
359
+ <li>List item</li>
360
+ <li>List item</li>
361
+ <li>
362
+ List item
363
+ <ul>
364
+ <li>Nested item</li>
365
+ <li>Nested item</li>
366
+ <li>
367
+ Nested item
368
+ <ul>
369
+ <li>Nested even more item</li>
370
+ <li>Nested even more item</li>
371
+ <li>Nested even more item</li>
372
+ </ul>
373
+ </li>
374
+ </ul>
375
+ </li>
376
+ <li>List item</li>
377
+ </ul>
378
+ <ol>
379
+ <li>List item</li>
380
+ <li>
381
+ List item
382
+ <ol>
383
+ <li>Nested item</li>
384
+ <li>Nested item</li>
385
+ <li>Nested item</li>
386
+ </ol>
387
+ </li>
388
+ <li>List item</li>
389
+ <li>List item</li>
390
+ </ol>
391
+ <p>
392
+ Ófróðlegt þorviðar <a href="https://www.vettvangur.is/" target="_blank">látist kærðu</a>, allfeginn því að kaupmanni, hraðasta ættleri farminum þuríði brústeinunum konungsþræll grænar. Hólmganga hrísflekkur, lifum skalla-grímur tíkr. Meiðinn hatast oddleifsdóttir, dólglegast dyflini, torfafóstri torfunni rak jarðarmenið stirðir. Býsn hvé ölseljunni gerði, jafnvitur lög vaskligr, sám hvortveggja ófrelsi trúfasti hallkelsdóttur sneri mund guðmundarsonum. Sekum meginmerkurinnar, arfsali allmikilli sjaldgæf. Uggasyni sættu konungsþræll gæs, alþýðuskap orðtækið stokkar, orkar mund ormláðs skrumaði hriflusonar virðak. Biðjum meiðinn keppa, taliðr uggasyni, skerðan manngjarnlega hofgyðja vilk allmikilli. Ógott hirðum samdóma, heimamaðr endilangt, erlingur kaldur gjósa seimþollr hallkell landkaupi. Torfunni tungu-oddur þorviðar gufárós, ævifús sekur tröllskessa, umbanda rétttrúaður bráðan svörfuði nýt íþrótt dalalönd halli. Flyðruness haldnar aðsóknar skerðan, auðhnykkjanda mannvandur smálöndum sauðarhöfðinu ferjunni.
393
+ </p>
394
+ </div>
395
+ </section>
396
+ `;
397
+ }
398
+ function renderRichtextTables() {
399
+ return `@* Auto-generated — do not edit by hand *@
400
+ <section class="w-full">
401
+ <vv-headline
402
+ identifier="styleguide__headline",
403
+ modifier="headline-h1",
404
+ text="Tables"
405
+ />
406
+
407
+ <div class="table">
408
+ <table class="table__element">
409
+ <thead>
410
+ <tr>
411
+ <th>Head</th>
412
+ <th>Head</th>
413
+ <th>Head</th>
414
+ <th>Head</th>
415
+ <th>Head</th>
416
+ </tr>
417
+ </thead>
418
+ <tbody>
419
+ <tr>
420
+ <td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
421
+ </tr>
422
+ <tr>
423
+ <td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
424
+ </tr>
425
+ <tr>
426
+ <td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
427
+ </tr>
428
+ </tbody>
429
+ </table>
430
+ </div>
431
+ </section>
432
+ `;
433
+ }
434
+
435
+ /* -------------------- HELPERS -------------------- */
436
+ function infoBlocks(item) {
437
+ const base = pair(item.size, item.lineheight);
438
+ const desktop = item?.config?.desktop ? pair(item.config.desktop.size, item.config.desktop.lineHeight) : '';
439
+ const mobile = item?.config?.mobile ? pair(item.config.mobile.size, item.config.mobile.lineHeight) : '';
440
+ if (!base && !desktop && !mobile) {
441
+ return '';
442
+ }
443
+ return {
444
+ base,
445
+ desktop,
446
+ mobile
447
+ };
448
+ }
449
+ function pair(size, lh) {
450
+ if (!size && !lh) {
451
+ return '';
452
+ }
453
+ return `Size: ${size ?? '-'} / Line height: ${lh ?? '-'}`;
454
+ }
455
+ function classToModifier(cls = '', prefix = 'headline') {
456
+ // Accept:
457
+ // - "headline-h1" (preferred)
458
+ // - "headline1" (legacy)
459
+ // - "headline_1" (legacy)
460
+ // Always return "headline-h1".
461
+ const re = new RegExp(`^${prefix}[-_]?h?(\\d+)$`, 'i');
462
+ const m = re.exec(cls.trim());
463
+ if (m) return `${prefix}-h${m[1]}`;
464
+ return cls; // do not mutilate unknown strings
465
+ }
466
+ function countInputs(spec) {
467
+ return ['inputs', 'selects', 'checkboxes', 'radios', 'textareas'].reduce((sum, k) => sum + (spec[k]?.length || 0), 0);
468
+ }
469
+ function a(s = '') {
470
+ return String(s).replaceAll('"', '&quot;');
471
+ }
472
+ function h(s = '') {
473
+ return String(s).replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
474
+ }
475
+ function bool(v) {
476
+ return v ? 'true' : 'false';
477
+ }
478
+ function log(file, n) {
479
+ const name = path.basename(file);
480
+ console.log(`${tag} ✅ ${name} (${n})`);
481
+ }
482
+
483
+ export { generateRazor as default };