@diagrammo/dgmo 0.14.1 → 0.15.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/src/index.ts CHANGED
@@ -1,692 +1,199 @@
1
1
  // ============================================================
2
- // Diagnostics
3
- // ============================================================
4
-
5
- export { makeDgmoError, formatDgmoError } from './diagnostics';
6
- export type { DgmoError, DgmoSeverity } from './diagnostics';
7
-
8
- // ============================================================
9
- // Arrow helpers (in-arrow label validation)
10
- // ============================================================
11
-
12
- export {
13
- parseInArrowLabel,
14
- validateLabelCharacters,
15
- matchColorParens,
16
- ARROW_DIAGNOSTIC_CODES,
17
- } from './utils/arrows';
18
- export type { ParseInArrowLabelResult } from './utils/arrows';
19
-
20
- // ============================================================
21
- // Unified API
22
- // ============================================================
23
-
24
- export { render } from './render';
25
-
26
- // ============================================================
27
- // Chart-type registry (single source of truth)
28
- // ============================================================
29
-
30
- export { chartTypes, BETA_CHART_IDS } from './chart-types';
31
- export type { ChartTypeMeta } from './chart-types';
32
-
33
- export {
34
- normalize as normalizeChartTypePrompt,
35
- matchesContiguously,
36
- scoreChartType,
37
- confidence as chartTypeConfidence,
38
- suggestChartTypes,
39
- MIN_PRIMARY_SCORE,
40
- AMBIGUITY_THRESHOLD,
41
- } from './chart-type-scoring';
42
- export type {
43
- ChartTypeScore,
44
- Confidence as ChartTypeConfidence,
45
- SuggestionResult as ChartTypeSuggestionResult,
46
- } from './chart-type-scoring';
47
-
48
- // ============================================================
49
- // Router
50
- // ============================================================
51
-
52
- export {
53
- parseDgmoChartType,
54
- parseDgmo,
55
- getRenderCategory,
56
- isExtendedChartType,
57
- getAllChartTypes,
58
- CHART_TYPE_DESCRIPTIONS,
59
- chartTypeParsers,
60
- knownChartTypeIds,
61
- } from './dgmo-router';
62
- export type { RenderCategory } from './dgmo-router';
63
-
64
- // ============================================================
65
- // Parsers
66
- // ============================================================
67
-
68
- export { parseChart, parseDataRowValues } from './chart';
69
- export type { ParsedChart, ChartType, ChartDataPoint, ChartEra } from './chart';
70
-
71
- export { parseExtendedChart } from './echarts';
72
- export type { ParsedExtendedChart, ExtendedChartType } from './echarts';
73
-
74
- export {
75
- parseVisualization,
76
- orderArcNodes,
77
- parseTimelineDate,
78
- addDurationToDate,
79
- formatDateLabel,
80
- } from './d3';
81
- export { computeTimeTicks } from './utils/time-ticks';
82
- export type {
83
- ParsedVisualization,
84
- VisualizationType,
85
- D3ExportDimensions,
86
- ArcLink,
87
- ArcNodeGroup,
88
- } from './d3';
89
-
90
- export {
91
- parseSequenceDgmo,
92
- parseSequenceDgmo as parseSequenceDiagram,
93
- looksLikeSequence,
94
- isSequenceBlock,
95
- isSequenceNote,
96
- } from './sequence/parser';
97
- export type {
98
- ParsedSequenceDgmo,
99
- SequenceParticipant,
100
- SequenceMessage,
101
- SequenceBlock,
102
- ElseIfBranch,
103
- SequenceSection,
104
- SequenceNote,
105
- SequenceElement,
106
- SequenceGroup,
107
- ParticipantType,
108
- } from './sequence/parser';
109
-
110
- export {
111
- inferParticipantType,
112
- RULE_COUNT,
113
- } from './sequence/participant-inference';
114
-
115
- export { parseFlowchart, looksLikeFlowchart } from './graph/flowchart-parser';
116
-
117
- export { parseState, looksLikeState } from './graph/state-parser';
118
- export { renderState, renderStateForExport } from './graph/state-renderer';
119
- export { collapseStateGroups } from './graph/state-collapse';
120
- export type { StateCollapseResult } from './graph/state-collapse';
121
-
122
- export { parseClassDiagram, looksLikeClassDiagram } from './class/parser';
123
-
124
- export type {
125
- ParsedClassDiagram,
126
- ClassNode,
127
- ClassMember,
128
- ClassRelationship,
129
- ClassModifier,
130
- MemberVisibility,
131
- RelationshipType,
132
- } from './class/types';
133
-
134
- export { layoutClassDiagram } from './class/layout';
135
- export type {
136
- ClassLayoutResult,
137
- ClassLayoutNode,
138
- ClassLayoutEdge,
139
- } from './class/layout';
140
-
141
- export {
142
- renderClassDiagram,
143
- renderClassDiagramForExport,
144
- } from './class/renderer';
145
-
146
- export { parseERDiagram, looksLikeERDiagram } from './er/parser';
147
-
148
- export type {
149
- ParsedERDiagram,
150
- ERTable,
151
- ERColumn,
152
- ERRelationship,
153
- ERConstraint,
154
- ERCardinality,
155
- } from './er/types';
156
-
157
- export { layoutERDiagram } from './er/layout';
158
- export type { ERLayoutResult, ERLayoutNode, ERLayoutEdge } from './er/layout';
159
-
160
- export { renderERDiagram, renderERDiagramForExport } from './er/renderer';
161
-
162
- export type {
163
- ParsedGraph,
164
- GraphNode,
165
- GraphEdge,
166
- GraphGroup,
167
- GraphShape,
168
- GraphDirection,
169
- } from './graph/types';
170
-
171
- export type { TagGroup, TagEntry } from './utils/tag-groups';
172
-
173
- export { parseInlineMarkdown, truncateBareUrl } from './utils/inline-markdown';
174
- export type { InlineSpan } from './utils/inline-markdown';
175
-
176
- export {
177
- normalizeName,
178
- displayName,
179
- getOrCreateName,
180
- } from './utils/name-normalize';
181
- export type { NameEntry, GetOrCreateNameResult } from './utils/name-normalize';
182
-
183
- export { parseOrg } from './org/parser';
184
- export type { ParsedOrg, OrgNode } from './org/parser';
185
-
186
- export { layoutOrg } from './org/layout';
187
- export type {
188
- OrgLayoutResult,
189
- OrgLayoutNode,
190
- OrgLayoutEdge,
191
- OrgContainerBounds,
192
- } from './org/layout';
193
-
194
- export { renderOrg, renderOrgForExport } from './org/renderer';
195
-
196
- export { parseKanban } from './kanban/parser';
197
- export type {
198
- ParsedKanban,
199
- KanbanColumn,
200
- KanbanCard,
201
- KanbanTagGroup,
202
- KanbanTagEntry,
203
- } from './kanban/types';
204
- export {
205
- computeCardMove,
206
- computeCardArchive,
207
- isArchiveColumn,
208
- } from './kanban/mutations';
209
- export { renderKanban, renderKanbanForExport } from './kanban/renderer';
210
-
211
- export { parseC4 } from './c4/parser';
212
- export type {
213
- ParsedC4,
214
- C4Element,
215
- C4ElementType,
216
- C4Shape,
217
- C4ArrowType,
218
- C4Relationship,
219
- C4Group,
220
- C4DeploymentNode,
221
- C4TagGroup,
222
- C4TagEntry,
223
- } from './c4/types';
224
-
225
- export {
226
- layoutC4Context,
227
- layoutC4Containers,
228
- layoutC4Components,
229
- layoutC4Deployment,
230
- rollUpContextRelationships,
231
- } from './c4/layout';
232
- export type {
233
- C4LayoutResult,
234
- C4LayoutNode,
235
- C4LayoutEdge,
236
- C4LayoutBoundary,
237
- C4LegendGroup,
238
- C4LegendEntry,
239
- ContextRelationship,
240
- } from './c4/layout';
241
-
242
- export {
243
- renderC4Context,
244
- renderC4ContextForExport,
245
- renderC4Containers,
246
- renderC4ContainersForExport,
247
- renderC4ComponentsForExport,
248
- renderC4Deployment,
249
- renderC4DeploymentForExport,
250
- } from './c4/renderer';
251
-
252
- export { parseBoxesAndLines } from './boxes-and-lines/parser';
253
- export type {
254
- ParsedBoxesAndLines,
255
- BLNode,
256
- BLEdge,
257
- BLGroup,
258
- } from './boxes-and-lines/types';
259
- export { layoutBoxesAndLines } from './boxes-and-lines/layout';
260
- export type {
261
- BLLayoutResult,
262
- BLLayoutNode,
263
- BLLayoutEdge,
264
- BLLayoutGroup,
265
- } from './boxes-and-lines/layout';
266
- export {
267
- renderBoxesAndLines,
268
- renderBoxesAndLinesForExport,
269
- } from './boxes-and-lines/renderer';
270
-
271
- export { collapseBoxesAndLines } from './boxes-and-lines/collapse';
272
- export type { BLCollapseResult } from './boxes-and-lines/collapse';
273
-
274
- export { parseSitemap, looksLikeSitemap } from './sitemap/parser';
275
-
276
- export type {
277
- ParsedSitemap,
278
- SitemapNode,
279
- SitemapEdge,
280
- SitemapDirection,
281
- } from './sitemap/types';
282
-
283
- export { layoutSitemap } from './sitemap/layout';
284
- export type {
285
- SitemapLayoutResult,
286
- SitemapLayoutNode,
287
- SitemapLayoutEdge,
288
- SitemapContainerBounds,
289
- SitemapLegendGroup,
290
- SitemapLegendEntry,
291
- } from './sitemap/layout';
292
-
293
- export { renderSitemap, renderSitemapForExport } from './sitemap/renderer';
294
-
295
- export { collapseSitemapTree } from './sitemap/collapse';
296
-
297
- // ── Infra Chart ────────────────────────────────────────────
298
- export { parseInfra } from './infra/parser';
299
- export type {
300
- ParsedInfra,
301
- InfraNode,
302
- InfraEdge,
303
- InfraGroup,
304
- InfraTagGroup,
305
- InfraProperty,
306
- InfraDiagnostic,
307
- InfraComputeParams,
308
- InfraBehaviorKey,
309
- } from './infra/types';
310
- export { INFRA_BEHAVIOR_KEYS } from './infra/types';
311
- export { computeInfra } from './infra/compute';
312
- export type {
313
- ComputedInfraModel,
314
- ComputedInfraNode,
315
- ComputedInfraEdge,
316
- InfraLatencyPercentiles,
317
- InfraAvailabilityPercentiles,
318
- InfraCbState,
319
- } from './infra/types';
320
- export { validateInfra, validateComputed } from './infra/validation';
321
- export { inferRoles, collectDiagramRoles } from './infra/roles';
322
- export type { InfraRole } from './infra/roles';
323
- export { layoutInfra } from './infra/layout';
324
- export type {
325
- InfraLayoutResult,
326
- InfraLayoutNode,
327
- InfraLayoutEdge,
328
- InfraLayoutGroup,
329
- } from './infra/layout';
330
- export {
331
- renderInfra,
332
- parseAndLayoutInfra,
333
- computeInfraLegendGroups,
334
- } from './infra/renderer';
335
- export type { InfraLegendGroup, InfraPlaybackState } from './infra/renderer';
336
- export type { CollapsedSitemapResult } from './sitemap/collapse';
337
-
338
- // ── Gantt Chart ───────────────────────────────────────────
339
- export { parseGantt } from './gantt/parser';
340
- export { calculateSchedule } from './gantt/calculator';
341
- export { renderGantt, buildTagLaneRowList } from './gantt/renderer';
342
- export type {
343
- GanttInteractiveOptions,
344
- GanttRow,
345
- GanttGroupRow,
346
- GanttTaskRow,
347
- GanttLaneHeaderRow,
348
- } from './gantt/renderer';
349
- export { resolveTaskName, collectTasks } from './gantt/resolver';
350
- export type {
351
- ParsedGantt,
352
- GanttTask,
353
- GanttGroup,
354
- GanttParallelBlock,
355
- GanttNode,
356
- GanttDependency,
357
- GanttHolidays,
358
- GanttEra,
359
- GanttMarker,
360
- GanttOptions,
361
- Duration,
362
- DurationUnit,
363
- ResolvedSchedule,
364
- ResolvedTask,
365
- ResolvedGroup,
366
- } from './gantt/types';
367
-
368
- // ── PERT Diagram ──────────────────────────────────────────
369
- export { parsePert, looksLikePert, extractPertSymbols } from './pert/parser';
370
- export { analyzePert } from './pert/analyzer';
371
- export { layoutPert, relayoutPert } from './pert/layout';
372
- export type { LayoutOverrides as PertLayoutOverrides } from './pert/internal';
373
- export { normalizePertSourceForShare } from './pert/share-normalize';
374
- export {
375
- renderPert,
376
- renderPertForExport,
377
- renderPertAnalysisBlock,
378
- measurePertAnalysisBlock,
379
- highlightPertCriticalPath,
380
- highlightPertSet,
381
- pertLegendEntries,
382
- pertLegendBlockWidth,
383
- PERT_LEGEND_PILL_HEIGHT,
384
- renderLegendBlock as renderPertLegendBlock,
385
- resetPertCriticalPath,
386
- resetPertHighlight,
387
- } from './pert/renderer';
388
- export type { PertRenderOptions } from './pert/renderer';
389
- export type {
390
- Anchor as PertAnchor,
391
- ParsedPert,
392
- PertActivity,
393
- PertEdge,
394
- PertGroup,
395
- PertMilestone,
396
- PertOptions,
397
- PertDirection,
398
- NodeDetail,
399
- ResolvedActivity,
400
- ResolvedGroup as ResolvedPertGroup,
401
- ResolvedPert,
402
- MonteCarloResult,
403
- PertLayoutNode,
404
- PertLayoutEdge,
405
- PertLayoutGroup,
406
- LayoutResult as PertLayoutResult,
407
- } from './pert/types';
408
-
409
- export { collapseOrgTree, focusOrgTree } from './org/collapse';
410
- export type {
411
- CollapsedOrgResult,
412
- FocusOrgResult,
413
- AncestorInfo,
414
- } from './org/collapse';
415
-
416
- export { parseMindmap } from './mindmap/parser';
417
- export type {
418
- MindmapNode,
419
- ParsedMindmap,
420
- MindmapLayoutNode,
421
- MindmapLayoutEdge,
422
- MindmapLayoutResult,
423
- } from './mindmap/types';
424
- export { layoutMindmap } from './mindmap/layout';
425
- export { renderMindmap, renderMindmapForExport } from './mindmap/renderer';
426
- export { collapseMindmapTree } from './mindmap/collapse';
427
- export type { CollapsedMindmapResult } from './mindmap/collapse';
428
-
429
- export { parseWireframe } from './wireframe/parser';
430
- export type {
431
- ParsedWireframe,
432
- WireframeElement,
433
- WireframeElementType,
434
- WireframeFormFactor,
435
- } from './wireframe/types';
436
- export { layoutWireframe } from './wireframe/layout';
437
- export type { WireframeLayout, WireframeLayoutNode } from './wireframe/layout';
438
- export { renderWireframe } from './wireframe/renderer';
439
-
440
- export { parseTechRadar } from './tech-radar/parser';
441
- export { computeRadarLayout, getRadarGeometry } from './tech-radar/layout';
442
- export {
443
- renderTechRadar,
444
- renderTechRadarForExport,
445
- } from './tech-radar/renderer';
446
- export {
447
- renderQuadrantFocus,
448
- renderQuadrantFocusForExport,
449
- } from './tech-radar/interactive';
450
- export type {
451
- ParsedTechRadar,
452
- TechRadarRing,
453
- TechRadarQuadrant,
454
- TechRadarBlip,
455
- TechRadarLayoutPoint,
456
- QuadrantPosition,
457
- BlipTrend,
458
- } from './tech-radar/types';
459
-
460
- export { parseCycle } from './cycle/parser';
461
- export { computeCycleLayout } from './cycle/layout';
462
- export { renderCycle, renderCycleForExport } from './cycle/renderer';
463
- export type { CycleRenderOptions } from './cycle/renderer';
464
- export type {
465
- ParsedCycle,
466
- CycleNode,
467
- CycleEdge,
468
- CycleLayoutNode,
469
- CycleLayoutEdge,
470
- CycleLayoutResult,
471
- } from './cycle/types';
472
-
473
- export { parseJourneyMap } from './journey-map/parser';
474
- export { layoutJourneyMap } from './journey-map/layout';
475
- export type { JourneyMapLayout } from './journey-map/layout';
476
- export {
477
- renderJourneyMap,
478
- renderJourneyMapForExport,
479
- } from './journey-map/renderer';
480
- export type {
481
- ParsedJourneyMap,
482
- JourneyMapPhase,
483
- JourneyMapStep,
484
- JourneyMapPersona,
485
- JourneyMapAnnotation,
486
- } from './journey-map/types';
487
- export type { JourneyMapInteractiveOptions } from './journey-map/renderer';
488
-
489
- export { parsePyramid } from './pyramid/parser';
490
- export { renderPyramid, renderPyramidForExport } from './pyramid/renderer';
491
- export type { ParsedPyramid, PyramidLayer } from './pyramid/types';
492
-
493
- export { parseRing } from './ring/parser';
494
- export { renderRing, renderRingForExport } from './ring/renderer';
495
- export type { ParsedRing, RingLayer } from './ring/types';
496
-
497
- export type { RaciDragSource, RaciInteractionHandlers } from './raci';
498
- export {
499
- parseRaci,
500
- renderRaci,
501
- renderRaciForExport,
502
- VARIANTS as RACI_VARIANTS,
503
- RACI_ERROR_CODES,
504
- RACI_WARNING_CODES,
505
- cellReplace as raciCellReplace,
506
- cellAppendMarker as raciCellAppendMarker,
507
- cellRemove as raciCellRemove,
508
- cellCycle as raciCellCycle,
509
- } from './raci';
510
- export type {
511
- ParsedRaci,
512
- RaciVariant,
513
- RaciMarker,
514
- RaciTask,
515
- RaciPhase,
516
- RaciRoleAssignment,
517
- } from './raci';
518
-
519
- export { resolveOrgImports } from './org/resolver';
520
- export type {
521
- ReadFileFn,
522
- ResolveImportsResult,
523
- ImportSource,
524
- } from './org/resolver';
525
-
526
- export { layoutGraph } from './graph/layout';
527
- export type {
528
- LayoutResult,
529
- LayoutNode,
530
- LayoutEdge,
531
- LayoutGroup,
532
- LayoutOptions,
533
- } from './graph/layout';
534
-
535
- export {
536
- renderFlowchart,
537
- renderFlowchartForExport,
538
- } from './graph/flowchart-renderer';
539
-
540
- // ============================================================
541
- // Config Builders (produce framework-specific config objects)
542
- // ============================================================
543
-
544
- export {
545
- buildExtendedChartOption,
546
- buildSimpleChartOption,
547
- renderExtendedChartForExport,
548
- getExtendedChartLegendGroups,
549
- getSimpleChartLegendGroups,
550
- computeScatterLabelGraphics,
551
- } from './echarts';
552
- export type { ScatterLabelPoint } from './echarts';
553
- export {
554
- renderLegendSvg,
555
- renderLegendSvgFromConfig,
556
- type LegendGroupData,
557
- } from './utils/legend-svg';
558
- export { LEGEND_HEIGHT, LEGEND_GEAR_PILL_W } from './utils/legend-constants';
559
- export { renderLegendD3 } from './utils/legend-d3';
560
- export {
561
- computeLegendLayout,
562
- controlsGroupCapsuleWidth,
563
- getLegendReservedHeight,
564
- } from './utils/legend-layout';
565
- export type {
566
- LegendConfig,
567
- LegendState,
568
- LegendCallbacks,
569
- LegendPosition,
570
- LegendMode,
571
- LegendControl,
572
- LegendLayout,
573
- LegendHandle,
574
- LegendPalette,
575
- } from './utils/legend-types';
576
- // ============================================================
577
- // Renderers (produce SVG output)
578
- // ============================================================
579
-
580
- export {
581
- renderSlopeChart,
582
- renderArcDiagram,
583
- renderTimeline,
584
- renderWordCloud,
585
- renderVenn,
586
- renderQuadrant,
587
- renderForExport,
588
- } from './d3';
589
-
590
- export {
591
- renderSequenceDiagram,
592
- buildRenderSequence,
593
- computeActivations,
594
- applyPositionOverrides,
595
- applyGroupOrdering,
596
- groupMessagesBySection,
597
- buildNoteMessageMap,
598
- } from './sequence/renderer';
599
- export type {
600
- RenderStep,
601
- Activation,
602
- SectionMessageGroup,
603
- SequenceRenderOptions,
604
- } from './sequence/renderer';
605
-
606
- export { applyCollapseProjection } from './sequence/collapse';
607
- export type { CollapsedView } from './sequence/collapse';
608
-
609
- // ============================================================
610
- // Colors & Palettes
611
- // ============================================================
612
-
613
- export {
614
- resolveColor,
615
- resolveColorWithDiagnostic,
616
- colorNames,
617
- nord,
618
- seriesColors,
619
- RECOGNIZED_COLOR_NAMES,
620
- isRecognizedColorName,
621
- } from './colors';
622
-
623
- export {
624
- // Registry
625
- getPalette,
626
- getAvailablePalettes,
627
- registerPalette,
628
- isValidHex,
629
- // Utilities
630
- hexToHSL,
631
- hslToHex,
632
- hexToHSLString,
633
- tint,
634
- shade,
635
- getSeriesColors,
636
- contrastText,
637
- // Palette definitions
638
- nordPalette,
639
- solarizedPalette,
640
- catppuccinPalette,
641
- rosePinePalette,
642
- gruvboxPalette,
643
- tokyoNightPalette,
644
- oneDarkPalette,
645
- boldPalette,
646
- draculaPalette,
647
- monokaiPalette,
648
- } from './palettes';
649
-
650
- export type { PaletteConfig, PaletteColors } from './palettes';
651
- export { mix, shapeFill } from './palettes/color-utils';
652
-
653
- // ============================================================
654
- // Sharing (URL encoding/decoding)
655
- // ============================================================
656
-
657
- export {
658
- encodeDiagramUrl,
659
- decodeDiagramUrl,
660
- encodeViewState,
661
- decodeViewState,
662
- } from './sharing';
663
- export type {
664
- EncodeDiagramUrlOptions,
665
- EncodeDiagramUrlResult,
666
- CompactViewState,
667
- DecodedDiagramUrl,
2
+ // @diagrammo/dgmo — Public API
3
+ // ============================================================
4
+ //
5
+ // This is the frozen public surface. ~12 exports. Everything else lives in
6
+ // `@diagrammo/dgmo/internal` (unstable, no semver), `@diagrammo/dgmo/editor`,
7
+ // `@diagrammo/dgmo/highlight`, or `@diagrammo/dgmo/auto`.
8
+ //
9
+ // If you need something not exported here, import from `/internal` and
10
+ // accept the no-semver contract.
11
+
12
+ import { render as renderInternal } from './render';
13
+ import {
14
+ encodeDiagramUrl as encodeDiagramUrlInternal,
15
+ decodeDiagramUrl as decodeDiagramUrlInternal,
668
16
  } from './sharing';
669
-
670
- // ============================================================
671
- // Completion (symbol extraction API)
672
- // ============================================================
673
-
674
- export {
675
- registerExtractor,
676
- extractDiagramSymbols,
677
- COMPLETION_REGISTRY,
678
- CHART_TYPES,
679
- METADATA_KEY_SET,
680
- ENTITY_TYPES,
681
- PIPE_METADATA,
682
- extractTagDeclarations,
683
- } from './completion';
684
- export type {
685
- DiagramSymbols,
686
- ExtractFn,
687
- DirectiveSpec,
688
- DirectiveValueSpec,
689
- PipeKeySpec,
690
- } from './completion';
691
-
692
- export { parseFirstLine, ALL_CHART_TYPES } from './utils/parsing';
17
+ import { parseDgmo as validate } from './dgmo-router';
18
+ import { palettes, getPalette } from './palettes';
19
+ import type { PaletteConfig } from './palettes/types';
20
+ import type { Theme } from './themes';
21
+ import { formatDgmoError, type DgmoError } from './diagnostics';
22
+
23
+ // ============================================================
24
+ // render(text, options?)
25
+ // ============================================================
26
+
27
+ export interface RenderOptions {
28
+ theme?: Theme;
29
+ palette?: PaletteConfig;
30
+ /**
31
+ * How to handle parse errors:
32
+ * 'svg' — render an inline error SVG (default)
33
+ * 'silent' — return empty svg + diagnostics; caller handles UI
34
+ * 'throw' — throw an Error with the diagnostics
35
+ */
36
+ onError?: 'svg' | 'silent' | 'throw';
37
+ }
38
+
39
+ export interface RenderResult {
40
+ svg: string;
41
+ diagnostics: DgmoError[];
42
+ }
43
+
44
+ /**
45
+ * Render DGMO source to an SVG string.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { render, palettes, themes } from '@diagrammo/dgmo';
50
+ *
51
+ * const { svg } = await render(text, {
52
+ * palette: palettes.catppuccin,
53
+ * theme: themes.dark,
54
+ * });
55
+ * document.getElementById('chart').innerHTML = svg;
56
+ * ```
57
+ */
58
+ export async function render(
59
+ text: string,
60
+ options?: RenderOptions
61
+ ): Promise<RenderResult> {
62
+ const palette = options?.palette ?? palettes.nord;
63
+ const onError = options?.onError ?? 'svg';
64
+
65
+ const result = await renderInternal(text, {
66
+ theme: options?.theme,
67
+ palette: palette.id,
68
+ });
69
+
70
+ const errors = result.diagnostics.filter((d) => d.severity === 'error');
71
+
72
+ if (errors.length === 0) {
73
+ return result;
74
+ }
75
+
76
+ if (onError === 'throw') {
77
+ throw new Error(
78
+ `DGMO parse error: ${errors.map(formatDgmoError).join('; ')}`
79
+ );
80
+ }
81
+
82
+ if (onError === 'silent') {
83
+ return result;
84
+ }
85
+
86
+ // 'svg' (default): render an inline error SVG so the user sees the problem
87
+ return {
88
+ svg: result.svg || renderErrorSvg(errors, palette, options?.theme),
89
+ diagnostics: result.diagnostics,
90
+ };
91
+ }
92
+
93
+ function renderErrorSvg(
94
+ errors: DgmoError[],
95
+ palette: PaletteConfig,
96
+ theme?: Theme
97
+ ): string {
98
+ const colors = palette[theme === 'dark' ? 'dark' : 'light'];
99
+ const bg = theme === 'transparent' ? 'transparent' : colors.bg;
100
+ const fg = colors.destructive ?? colors.text;
101
+ const muted = colors.textMuted ?? colors.text;
102
+ const lines = errors.slice(0, 5).map((e) => formatDgmoError(e));
103
+ const height = 60 + lines.length * 20;
104
+ const body = lines
105
+ .map(
106
+ (line, i) =>
107
+ `<text x="20" y="${60 + i * 20}" fill="${fg}" font-family="ui-monospace, monospace" font-size="12">${escapeXml(line)}</text>`
108
+ )
109
+ .join('');
110
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="640" height="${height}" viewBox="0 0 640 ${height}"><rect width="100%" height="100%" fill="${bg}"/><text x="20" y="34" fill="${muted}" font-family="system-ui, sans-serif" font-size="13" font-weight="600">DGMO parse error</text>${body}</svg>`;
111
+ }
112
+
113
+ function escapeXml(s: string): string {
114
+ return s
115
+ .replace(/&/g, '&amp;')
116
+ .replace(/</g, '&lt;')
117
+ .replace(/>/g, '&gt;')
118
+ .replace(/"/g, '&quot;');
119
+ }
120
+
121
+ // ============================================================
122
+ // validate(text)
123
+ // ============================================================
124
+
125
+ export { validate };
126
+
127
+ // ============================================================
128
+ // formatDgmoError(err)
129
+ // ============================================================
130
+
131
+ export { formatDgmoError };
132
+
133
+ // ============================================================
134
+ // Sharing — encodeDiagramUrl / decodeDiagramUrl
135
+ // ============================================================
136
+
137
+ export interface EncodeDiagramUrlOptions {
138
+ baseUrl?: string;
139
+ palette?: PaletteConfig;
140
+ theme?: Theme;
141
+ filename?: string;
142
+ }
143
+
144
+ /**
145
+ * Encode DGMO text into a shareable URL. Returns null if the compressed
146
+ * payload exceeds the 8 KB URL limit.
147
+ */
148
+ export function encodeDiagramUrl(
149
+ text: string,
150
+ options?: EncodeDiagramUrlOptions
151
+ ): string | null {
152
+ const internalTheme =
153
+ options?.theme === 'light' || options?.theme === 'dark'
154
+ ? options.theme
155
+ : undefined;
156
+ const result = encodeDiagramUrlInternal(text, {
157
+ baseUrl: options?.baseUrl,
158
+ palette: options?.palette?.id,
159
+ theme: internalTheme,
160
+ filename: options?.filename,
161
+ });
162
+ return 'error' in result && result.error ? null : (result.url ?? null);
163
+ }
164
+
165
+ export interface DecodedDiagramUrl {
166
+ text: string;
167
+ palette?: PaletteConfig;
168
+ theme?: Theme;
169
+ filename?: string;
170
+ }
171
+
172
+ /**
173
+ * Decode a share URL back into DGMO text plus optional palette/theme/filename.
174
+ * Returns null if the URL contains no valid DGMO payload.
175
+ */
176
+ export function decodeDiagramUrl(url: string): DecodedDiagramUrl | null {
177
+ const decoded = decodeDiagramUrlInternal(url);
178
+ if (!decoded.dsl) return null;
179
+ const palette = decoded.palette ? getPalette(decoded.palette) : undefined;
180
+ const out: DecodedDiagramUrl = { text: decoded.dsl };
181
+ if (palette) out.palette = palette;
182
+ if (decoded.theme) out.theme = decoded.theme;
183
+ if (decoded.filename) out.filename = decoded.filename;
184
+ return out;
185
+ }
186
+
187
+ // ============================================================
188
+ // Palettes + themes (namespaces)
189
+ // ============================================================
190
+
191
+ export { palettes } from './palettes';
192
+ export { themes, type Theme } from './themes';
193
+
194
+ // ============================================================
195
+ // Public types
196
+ // ============================================================
197
+
198
+ export type { PaletteConfig, PaletteColors } from './palettes/types';
199
+ export type { DgmoError, DgmoSeverity } from './diagnostics';