@bendyline/squisq 1.5.2 → 2.0.1
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 +63 -10
- package/dist/{Doc-BBVGq1_9.d.ts → Doc-ylqWKDc_.d.ts} +248 -37
- package/dist/{ImageEditDoc-FuyGtt6o.d.ts → ImageEditDoc-D828IXJ1.d.ts} +1 -1
- package/dist/annotationCoercion-Dr_DY9Hx.d.ts +86 -0
- package/dist/{chunk-T5UK6YOB.js → chunk-2PWOO4ZE.js} +60 -17
- package/dist/chunk-2PWOO4ZE.js.map +1 -0
- package/dist/{chunk-VWUFZ6ZG.js → chunk-3E5F2XMR.js} +2 -29
- package/dist/chunk-3E5F2XMR.js.map +1 -0
- package/dist/chunk-4X3JQXNM.js +35 -0
- package/dist/chunk-4X3JQXNM.js.map +1 -0
- package/dist/{chunk-P3K7NLHF.js → chunk-5DKBFXPY.js} +31 -121
- package/dist/chunk-5DKBFXPY.js.map +1 -0
- package/dist/{chunk-DLXZMS5K.js → chunk-5J4HKRJF.js} +51 -3
- package/dist/chunk-5J4HKRJF.js.map +1 -0
- package/dist/chunk-5UYI3RJB.js +2503 -0
- package/dist/chunk-5UYI3RJB.js.map +1 -0
- package/dist/{chunk-BEQODJWV.js → chunk-66QO7XFB.js} +11 -291
- package/dist/chunk-66QO7XFB.js.map +1 -0
- package/dist/{chunk-HWVFJAAH.js → chunk-AMIR72JP.js} +28 -10
- package/dist/chunk-AMIR72JP.js.map +1 -0
- package/dist/chunk-CY2R2VPR.js +884 -0
- package/dist/chunk-CY2R2VPR.js.map +1 -0
- package/dist/chunk-FVJUUTEM.js +74 -0
- package/dist/chunk-FVJUUTEM.js.map +1 -0
- package/dist/{chunk-Q3ROPT5H.js → chunk-GZY66SDN.js} +1 -1
- package/dist/{chunk-Q3ROPT5H.js.map → chunk-GZY66SDN.js.map} +1 -1
- package/dist/chunk-JOQYPRDZ.js +127 -0
- package/dist/chunk-JOQYPRDZ.js.map +1 -0
- package/dist/{chunk-A4O7GIWE.js → chunk-MMPBOUK7.js} +10 -73
- package/dist/chunk-MMPBOUK7.js.map +1 -0
- package/dist/{chunk-LDQ2HJIX.js → chunk-PXLVXTLW.js} +252 -54
- package/dist/chunk-PXLVXTLW.js.map +1 -0
- package/dist/{chunk-TQWLI6S2.js → chunk-Q7LROV2H.js} +4 -2
- package/dist/{chunk-TQWLI6S2.js.map → chunk-Q7LROV2H.js.map} +1 -1
- package/dist/{chunk-GPLTCSXO.js → chunk-R2GC6RLD.js} +7 -7
- package/dist/chunk-R2GC6RLD.js.map +1 -0
- package/dist/chunk-R2YZTCLO.js +292 -0
- package/dist/chunk-R2YZTCLO.js.map +1 -0
- package/dist/chunk-S7A5D7ZJ.js +174 -0
- package/dist/chunk-S7A5D7ZJ.js.map +1 -0
- package/dist/chunk-TFJVXUHH.js +2358 -0
- package/dist/chunk-TFJVXUHH.js.map +1 -0
- package/dist/{chunk-LTN6I7QW.js → chunk-TUWMK62B.js} +55 -4
- package/dist/chunk-TUWMK62B.js.map +1 -0
- package/dist/{chunk-IMSCRLLV.js → chunk-VR7UPJ7C.js} +35 -25
- package/dist/chunk-VR7UPJ7C.js.map +1 -0
- package/dist/{chunk-KKNUBQ6Y.js → chunk-W4O2TX5I.js} +157 -92
- package/dist/chunk-W4O2TX5I.js.map +1 -0
- package/dist/{chunk-5TYS5V3G.js → chunk-X6DUONGT.js} +4204 -3560
- package/dist/chunk-X6DUONGT.js.map +1 -0
- package/dist/doc/index.d.ts +2334 -5
- package/dist/doc/index.js +127 -33
- package/dist/generate/index.d.ts +7 -2
- package/dist/generate/index.js +1 -1
- package/dist/imageEdit/index.d.ts +9 -6
- package/dist/imageEdit/index.js +3 -3
- package/dist/index.d.ts +12 -10
- package/dist/index.js +246 -80
- package/dist/jsonForm/index.d.ts +7 -3
- package/dist/jsonForm/index.js +9 -4
- package/dist/markdown/index.d.ts +6 -70
- package/dist/markdown/index.js +13 -9
- package/dist/narration/index.d.ts +618 -0
- package/dist/narration/index.js +82 -0
- package/dist/recommend/index.d.ts +15 -1
- package/dist/recommend/index.js +2 -2
- package/dist/schemas/index.d.ts +28 -6
- package/dist/schemas/index.js +15 -13
- package/dist/spatial/index.d.ts +0 -1
- package/dist/spatial/index.js +1 -1
- package/dist/storage/index.js +1 -1
- package/dist/{themeLibrary-CehcJhzz.d.ts → themeLibrary-RjVgLpUi.d.ts} +10 -11
- package/dist/timing/index.js +5 -3
- package/dist/transform/index.d.ts +35 -24
- package/dist/transform/index.js +9 -10
- package/dist/{types-DlAZ7MW4.d.ts → types-BwMPl2CB.d.ts} +1 -1
- package/dist/{types-D98li1gV.d.ts → types-C4kiRn_k.d.ts} +3 -1
- package/dist/versions/index.d.ts +2 -1
- package/dist/versions/index.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/applyNarrationTiming.test.ts +190 -0
- package/src/__tests__/applyRenderStyle.test.ts +3 -3
- package/src/__tests__/asciiDiagramBattle.test.ts +246 -0
- package/src/__tests__/asciiDiagramDetect.test.ts +162 -0
- package/src/__tests__/asciiDiagramMapping.test.ts +38 -0
- package/src/__tests__/asciiDiagramParse.test.ts +233 -0
- package/src/__tests__/asciiDiagramPipeline.test.ts +273 -0
- package/src/__tests__/asciiDiagramRender.test.ts +162 -0
- package/src/__tests__/asciiDiagramRepair.test.ts +84 -0
- package/src/__tests__/asciiTimelineDetect.test.ts +157 -0
- package/src/__tests__/asciiTimelineParse.test.ts +205 -0
- package/src/__tests__/asciiTimelinePipeline.test.ts +349 -0
- package/src/__tests__/asciiTimelineRender.test.ts +376 -0
- package/src/__tests__/attrTokens.test.ts +5 -0
- package/src/__tests__/contentContainer.test.ts +43 -0
- package/src/__tests__/customTemplatesFrontmatter.test.ts +0 -15
- package/src/__tests__/customThemesFrontmatter.test.ts +1 -0
- package/src/__tests__/dataTable.test.ts +25 -15
- package/src/__tests__/diagramBlock.test.ts +3 -3
- package/src/__tests__/diagramText.test.ts +26 -0
- package/src/__tests__/docToMarkdownMedia.test.ts +83 -0
- package/src/__tests__/drawingBlock.test.ts +3 -3
- package/src/__tests__/fixtures/asciiDiagramBattle.ts +512 -0
- package/src/__tests__/fixtures/asciiDiagrams.ts +287 -0
- package/src/__tests__/fixtures/asciiTimelines.ts +35 -0
- package/src/__tests__/fixtures/treeviewFixtures.ts +243 -0
- package/src/__tests__/getLayers.test.ts +32 -18
- package/src/__tests__/getLayersFallback.test.ts +8 -8
- package/src/__tests__/imageEditVersions.test.ts +2 -0
- package/src/__tests__/jsonForm.pointer.test.ts +47 -1
- package/src/__tests__/layoutBlock.test.ts +3 -3
- package/src/__tests__/majorApiTypes.test.ts +18 -0
- package/src/__tests__/markdown.test.ts +54 -4
- package/src/__tests__/markdownSanitize.test.ts +7 -5
- package/src/__tests__/matchFontFamily.test.ts +114 -0
- package/src/__tests__/materializeBlockLayers.test.ts +135 -0
- package/src/__tests__/materializeTestUtils.ts +17 -0
- package/src/__tests__/narrationAlign.test.ts +150 -0
- package/src/__tests__/narrationFeatures.test.ts +71 -0
- package/src/__tests__/narrationNuclei.test.ts +45 -0
- package/src/__tests__/narrationPacing.test.ts +189 -0
- package/src/__tests__/narrationScript.test.ts +149 -0
- package/src/__tests__/narrationSessionRepro.test.ts +62 -0
- package/src/__tests__/narrationSidecar.test.ts +88 -0
- package/src/__tests__/narrationSyllables.test.ts +65 -0
- package/src/__tests__/narrationTestSignals.ts +167 -0
- package/src/__tests__/narrationVad.test.ts +66 -0
- package/src/__tests__/resolveDocTheme.test.ts +16 -2
- package/src/__tests__/rootExports.test.ts +18 -0
- package/src/__tests__/scopedContentContainer.test.ts +20 -0
- package/src/__tests__/shapeGeometry.test.ts +12 -0
- package/src/__tests__/templateAnnotationParse.test.ts +10 -0
- package/src/__tests__/templateInputs.test.ts +69 -0
- package/src/__tests__/templateMetadata.test.ts +2 -1
- package/src/__tests__/templates.test.ts +143 -17
- package/src/__tests__/themeCompile.test.ts +49 -22
- package/src/__tests__/transformProvenance.test.ts +160 -0
- package/src/__tests__/transformV2.test.ts +40 -14
- package/src/__tests__/treeviewBattle.test.ts +76 -0
- package/src/__tests__/treeviewDetect.test.ts +168 -0
- package/src/__tests__/treeviewParse.test.ts +118 -0
- package/src/__tests__/treeviewPipeline.test.ts +183 -0
- package/src/__tests__/treeviewRender.test.ts +91 -0
- package/src/__tests__/versions.test.ts +44 -0
- package/src/doc/applyNarrationTiming.ts +244 -0
- package/src/doc/asciiDiagram/chars.ts +210 -0
- package/src/doc/asciiDiagram/detect.ts +175 -0
- package/src/doc/asciiDiagram/index.ts +38 -0
- package/src/doc/asciiDiagram/mapping.ts +225 -0
- package/src/doc/asciiDiagram/parse.ts +1095 -0
- package/src/doc/asciiDiagram/render.ts +963 -0
- package/src/doc/asciiDiagram/repair.ts +400 -0
- package/src/doc/asciiDiagram/types.ts +65 -0
- package/src/doc/asciiTimeline/detect.ts +138 -0
- package/src/doc/asciiTimeline/index.ts +26 -0
- package/src/doc/asciiTimeline/mapping.ts +43 -0
- package/src/doc/asciiTimeline/parse.ts +870 -0
- package/src/doc/asciiTimeline/render.ts +427 -0
- package/src/doc/asciiTimeline/types.ts +73 -0
- package/src/doc/asciiTimeline/wrappedFlow.ts +427 -0
- package/src/doc/audioMapping.ts +60 -46
- package/src/doc/customTemplatesFrontmatter.ts +0 -41
- package/src/doc/docToMarkdown.ts +70 -2
- package/src/doc/index.ts +76 -4
- package/src/doc/markdownToDoc.ts +138 -2
- package/src/doc/materializeBlockLayers.ts +304 -0
- package/src/doc/mediaAnnotations.ts +20 -0
- package/src/doc/resolveDocTheme.ts +8 -7
- package/src/doc/templateInputs.ts +185 -2
- package/src/doc/templates/__tests__/customTemplateRegistry.test.ts +22 -8
- package/src/doc/templates/__tests__/listBlock.test.ts +64 -0
- package/src/doc/templates/diagramBlock.ts +237 -38
- package/src/doc/templates/fallbackBlock.ts +1 -1
- package/src/doc/templates/index.ts +134 -279
- package/src/doc/templates/inputDescriptors.ts +20 -1
- package/src/doc/templates/listBlock.ts +88 -30
- package/src/doc/templates/metadata.ts +10 -0
- package/src/doc/templates/persistentLayers.ts +12 -126
- package/src/doc/templates/registry.ts +88 -0
- package/src/doc/templates/templateNames.ts +22 -0
- package/src/doc/templates/timelineBlock.ts +613 -0
- package/src/doc/templates/treeBlock.ts +131 -0
- package/src/doc/treeview/chars.ts +72 -0
- package/src/doc/treeview/detect.ts +145 -0
- package/src/doc/treeview/index.ts +30 -0
- package/src/doc/treeview/mapping.ts +95 -0
- package/src/doc/treeview/parse.ts +201 -0
- package/src/doc/treeview/render.ts +41 -0
- package/src/doc/treeview/types.ts +43 -0
- package/src/doc/utils/applyRenderStyle.ts +2 -9
- package/src/doc/utils/diagramText.ts +78 -0
- package/src/doc/utils/shapeGeometry.ts +67 -5
- package/src/doc/validate.ts +1 -1
- package/src/generate/templateMapper.ts +7 -0
- package/src/imageEdit/export.ts +6 -5
- package/src/imageEdit/versionPaths.ts +13 -26
- package/src/imageEdit/versions.ts +21 -100
- package/src/index.ts +1 -0
- package/src/internal/immutable.ts +58 -0
- package/src/jsonForm/index.ts +9 -1
- package/src/jsonForm/pointer.ts +68 -23
- package/src/markdown/attrTokens.ts +7 -5
- package/src/markdown/index.ts +1 -0
- package/src/markdown/stringify.ts +4 -9
- package/src/markdown/utils.ts +36 -14
- package/src/narration/align.ts +514 -0
- package/src/narration/features.ts +231 -0
- package/src/narration/index.ts +86 -0
- package/src/narration/nuclei.ts +118 -0
- package/src/narration/pacing.ts +211 -0
- package/src/narration/script.ts +239 -0
- package/src/narration/session.ts +100 -0
- package/src/narration/sidecar.ts +187 -0
- package/src/narration/syllables.ts +85 -0
- package/src/narration/trace.ts +50 -0
- package/src/narration/types.ts +299 -0
- package/src/narration/vad.ts +131 -0
- package/src/recommend/numberHighlight.ts +25 -0
- package/src/recommend/templates.ts +108 -12
- package/src/schemas/BlockTemplates.ts +156 -7
- package/src/schemas/Doc.ts +72 -9
- package/src/schemas/Media.ts +12 -2
- package/src/schemas/Theme.ts +45 -25
- package/src/schemas/Viewport.ts +2 -2
- package/src/schemas/fontStacks.ts +79 -0
- package/src/schemas/themeCompile.ts +15 -0
- package/src/schemas/themeConstants.ts +2 -0
- package/src/schemas/themeLibrary.ts +25 -19
- package/src/schemas/themeValidator.ts +1 -1
- package/src/schemas/themes/gezellig.json +1 -1
- package/src/spatial/Geohash.ts +0 -3
- package/src/storage/ContentContainer.ts +17 -9
- package/src/storage/ScopedContentContainer.ts +45 -15
- package/src/transform/applyTransform.ts +5 -5
- package/src/transform/index.ts +3 -1
- package/src/transform/registry.ts +169 -49
- package/src/transform/templateSelector.ts +21 -2
- package/src/transform/timingAllocator.ts +152 -21
- package/src/transform/types.ts +14 -1
- package/src/versions/gitignore.ts +34 -0
- package/src/versions/index.ts +2 -0
- package/src/versions/operations.ts +18 -104
- package/src/versions/paths.ts +13 -28
- package/src/versions/snapshotStore.ts +202 -0
- package/src/versions/types.ts +3 -1
- package/dist/chunk-5TYS5V3G.js.map +0 -1
- package/dist/chunk-A4O7GIWE.js.map +0 -1
- package/dist/chunk-BEQODJWV.js.map +0 -1
- package/dist/chunk-DLXZMS5K.js.map +0 -1
- package/dist/chunk-GPLTCSXO.js.map +0 -1
- package/dist/chunk-HWVFJAAH.js.map +0 -1
- package/dist/chunk-IMSCRLLV.js.map +0 -1
- package/dist/chunk-JELIRVDP.js +0 -24
- package/dist/chunk-JELIRVDP.js.map +0 -1
- package/dist/chunk-KKNUBQ6Y.js.map +0 -1
- package/dist/chunk-LDQ2HJIX.js.map +0 -1
- package/dist/chunk-LTN6I7QW.js.map +0 -1
- package/dist/chunk-P3K7NLHF.js.map +0 -1
- package/dist/chunk-T5UK6YOB.js.map +0 -1
- package/dist/chunk-UY7KGQ5R.js +0 -192
- package/dist/chunk-UY7KGQ5R.js.map +0 -1
- package/dist/chunk-VLJJHDAC.js +0 -218
- package/dist/chunk-VLJJHDAC.js.map +0 -1
- package/dist/chunk-VWUFZ6ZG.js.map +0 -1
- package/dist/story/index.d.ts +0 -1801
- package/dist/story/index.js +0 -279
- package/src/doc/getLayers.ts +0 -195
- /package/dist/{story → narration}/index.js.map +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character classification for ASCII/Unicode box-and-line diagram art.
|
|
3
|
+
*
|
|
4
|
+
* The parse side accepts a superset (light/rounded/heavy/double Unicode
|
|
5
|
+
* box-drawing plus ASCII `+ - |` and both arrow families); the render
|
|
6
|
+
* side always emits one of the two `StyleVocab` tables below.
|
|
7
|
+
*
|
|
8
|
+
* Internal to the asciiDiagram module — not exported from the barrel.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const TL_CORNERS = new Set(['┌', '╭', '┏', '╔']);
|
|
12
|
+
const TR_CORNERS = new Set(['┐', '╮', '┓', '╗']);
|
|
13
|
+
const BL_CORNERS = new Set(['└', '╰', '┗', '╚']);
|
|
14
|
+
const BR_CORNERS = new Set(['┘', '╯', '┛', '╝']);
|
|
15
|
+
const HORIZ = new Set(['─', '━', '═', '-']);
|
|
16
|
+
const VERT = new Set(['│', '┃', '║', '|']);
|
|
17
|
+
const JUNCTIONS = new Set([
|
|
18
|
+
'├',
|
|
19
|
+
'┤',
|
|
20
|
+
'┬',
|
|
21
|
+
'┴',
|
|
22
|
+
'┼',
|
|
23
|
+
'┣',
|
|
24
|
+
'┫',
|
|
25
|
+
'┳',
|
|
26
|
+
'┻',
|
|
27
|
+
'╋',
|
|
28
|
+
'╠',
|
|
29
|
+
'╣',
|
|
30
|
+
'╦',
|
|
31
|
+
'╩',
|
|
32
|
+
'╬',
|
|
33
|
+
'╞',
|
|
34
|
+
'╡',
|
|
35
|
+
'╥',
|
|
36
|
+
'╨',
|
|
37
|
+
'╫',
|
|
38
|
+
'╪',
|
|
39
|
+
]);
|
|
40
|
+
/** Junctions whose stems include a vertical component (legal on side borders). */
|
|
41
|
+
const SIDE_JUNCTIONS = new Set(['├', '┤', '┼', '┣', '┫', '╋', '╠', '╣', '╬', '╞', '╡', '╫', '╪']);
|
|
42
|
+
/** Junctions whose stems include a horizontal component (legal on top/bottom borders). */
|
|
43
|
+
const FLAT_JUNCTIONS = new Set(['┬', '┴', '┼', '┳', '┻', '╋', '╦', '╩', '╬', '╥', '╨', '╫', '╪']);
|
|
44
|
+
|
|
45
|
+
const ARROW_UP = new Set(['▲', '↑']);
|
|
46
|
+
const ARROW_DOWN = new Set(['▼', '↓']);
|
|
47
|
+
const ARROW_LEFT = new Set(['◄', '←', '◀']);
|
|
48
|
+
const ARROW_RIGHT = new Set(['►', '→', '▶']);
|
|
49
|
+
|
|
50
|
+
export type ArrowDirection = 'up' | 'down' | 'left' | 'right';
|
|
51
|
+
|
|
52
|
+
export function isTlCorner(ch: string): boolean {
|
|
53
|
+
return TL_CORNERS.has(ch);
|
|
54
|
+
}
|
|
55
|
+
export function isTrCorner(ch: string): boolean {
|
|
56
|
+
return TR_CORNERS.has(ch);
|
|
57
|
+
}
|
|
58
|
+
export function isBlCorner(ch: string): boolean {
|
|
59
|
+
return BL_CORNERS.has(ch);
|
|
60
|
+
}
|
|
61
|
+
export function isBrCorner(ch: string): boolean {
|
|
62
|
+
return BR_CORNERS.has(ch);
|
|
63
|
+
}
|
|
64
|
+
export function isAnyCorner(ch: string): boolean {
|
|
65
|
+
return TL_CORNERS.has(ch) || TR_CORNERS.has(ch) || BL_CORNERS.has(ch) || BR_CORNERS.has(ch);
|
|
66
|
+
}
|
|
67
|
+
export function isHoriz(ch: string): boolean {
|
|
68
|
+
return HORIZ.has(ch);
|
|
69
|
+
}
|
|
70
|
+
export function isVert(ch: string): boolean {
|
|
71
|
+
return VERT.has(ch);
|
|
72
|
+
}
|
|
73
|
+
export function isJunction(ch: string): boolean {
|
|
74
|
+
return JUNCTIONS.has(ch);
|
|
75
|
+
}
|
|
76
|
+
export function isPlus(ch: string): boolean {
|
|
77
|
+
return ch === '+';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Acceptable interior of a top/bottom border walk (corners excluded). */
|
|
81
|
+
export function isFlatBorderChar(ch: string): boolean {
|
|
82
|
+
return isHoriz(ch) || FLAT_JUNCTIONS.has(ch) || isPlus(ch);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Acceptable interior of a left/right border walk (corners excluded). */
|
|
86
|
+
export function isSideBorderChar(ch: string): boolean {
|
|
87
|
+
return isVert(ch) || SIDE_JUNCTIONS.has(ch) || isPlus(ch);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Direction of an unconditional (Unicode) arrowhead, or null. ASCII
|
|
92
|
+
* `^ v < >` are ambiguous with prose and must be gated on an adjacent
|
|
93
|
+
* line character by the caller — use {@link asciiArrowDirection}.
|
|
94
|
+
*/
|
|
95
|
+
export function arrowDirection(ch: string): ArrowDirection | null {
|
|
96
|
+
if (ARROW_UP.has(ch)) return 'up';
|
|
97
|
+
if (ARROW_DOWN.has(ch)) return 'down';
|
|
98
|
+
if (ARROW_LEFT.has(ch)) return 'left';
|
|
99
|
+
if (ARROW_RIGHT.has(ch)) return 'right';
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Direction for ASCII arrow candidates (`^ v V < >`); caller applies the adjacency gate. */
|
|
104
|
+
export function asciiArrowDirection(ch: string): ArrowDirection | null {
|
|
105
|
+
if (ch === '^') return 'up';
|
|
106
|
+
if (ch === 'v' || ch === 'V') return 'down';
|
|
107
|
+
if (ch === '<') return 'left';
|
|
108
|
+
if (ch === '>') return 'right';
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** True for any character that can be part of an edge polyline (lines, turns, junctions). */
|
|
113
|
+
export function isLineChar(ch: string): boolean {
|
|
114
|
+
return isHoriz(ch) || isVert(ch) || isJunction(ch) || isPlus(ch) || isAnyCorner(ch);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** True for structural Unicode box-drawing characters (used for the style vote). */
|
|
118
|
+
export function isUnicodeStructural(ch: string): boolean {
|
|
119
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
120
|
+
// Box Drawing block U+2500–U+257F plus the arrow glyphs we emit.
|
|
121
|
+
return (
|
|
122
|
+
(code >= 0x2500 && code <= 0x257f) ||
|
|
123
|
+
ARROW_UP.has(ch) ||
|
|
124
|
+
ARROW_DOWN.has(ch) ||
|
|
125
|
+
ch === '←' ||
|
|
126
|
+
ch === '→' ||
|
|
127
|
+
ch === '◄' ||
|
|
128
|
+
ch === '►' ||
|
|
129
|
+
ch === '◀' ||
|
|
130
|
+
ch === '▶'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** True for structural ASCII drawing characters (used for the style vote). */
|
|
135
|
+
export function isAsciiStructural(ch: string): boolean {
|
|
136
|
+
return ch === '+' || ch === '-' || ch === '|';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface StyleVocab {
|
|
140
|
+
tl: string;
|
|
141
|
+
tr: string;
|
|
142
|
+
bl: string;
|
|
143
|
+
br: string;
|
|
144
|
+
h: string;
|
|
145
|
+
v: string;
|
|
146
|
+
/** Horizontal border cell with a stem descending below it (e.g. a line leaving a bottom border). */
|
|
147
|
+
stemDown: string;
|
|
148
|
+
/** Horizontal border cell with a stem ascending above it (e.g. a line arriving at a top border). */
|
|
149
|
+
stemUp: string;
|
|
150
|
+
/** Vertical border cell with a stem extending leftward. */
|
|
151
|
+
stemLeft: string;
|
|
152
|
+
/** Vertical border cell with a stem extending rightward. */
|
|
153
|
+
stemRight: string;
|
|
154
|
+
cross: string;
|
|
155
|
+
arrowUp: string;
|
|
156
|
+
arrowDown: string;
|
|
157
|
+
arrowLeft: string;
|
|
158
|
+
arrowRight: string;
|
|
159
|
+
/** Turn glyphs for routed edges: connects the two named directions. */
|
|
160
|
+
turnDownRight: string;
|
|
161
|
+
turnDownLeft: string;
|
|
162
|
+
turnUpRight: string;
|
|
163
|
+
turnUpLeft: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const UNICODE_VOCAB: StyleVocab = {
|
|
167
|
+
tl: '┌',
|
|
168
|
+
tr: '┐',
|
|
169
|
+
bl: '└',
|
|
170
|
+
br: '┘',
|
|
171
|
+
h: '─',
|
|
172
|
+
v: '│',
|
|
173
|
+
stemDown: '┬',
|
|
174
|
+
stemUp: '┴',
|
|
175
|
+
stemLeft: '┤',
|
|
176
|
+
stemRight: '├',
|
|
177
|
+
cross: '┼',
|
|
178
|
+
arrowUp: '▲',
|
|
179
|
+
arrowDown: '▼',
|
|
180
|
+
arrowLeft: '◄',
|
|
181
|
+
arrowRight: '►',
|
|
182
|
+
// Named by the pair of open sides: e.g. turnDownRight opens downward and
|
|
183
|
+
// to the right (a line coming from below turning to the right) = '┌'.
|
|
184
|
+
turnDownRight: '┌',
|
|
185
|
+
turnDownLeft: '┐',
|
|
186
|
+
turnUpRight: '└',
|
|
187
|
+
turnUpLeft: '┘',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const ASCII_VOCAB: StyleVocab = {
|
|
191
|
+
tl: '+',
|
|
192
|
+
tr: '+',
|
|
193
|
+
bl: '+',
|
|
194
|
+
br: '+',
|
|
195
|
+
h: '-',
|
|
196
|
+
v: '|',
|
|
197
|
+
stemDown: '+',
|
|
198
|
+
stemUp: '+',
|
|
199
|
+
stemLeft: '+',
|
|
200
|
+
stemRight: '+',
|
|
201
|
+
cross: '+',
|
|
202
|
+
arrowUp: '^',
|
|
203
|
+
arrowDown: 'v',
|
|
204
|
+
arrowLeft: '<',
|
|
205
|
+
arrowRight: '>',
|
|
206
|
+
turnDownRight: '+',
|
|
207
|
+
turnDownLeft: '+',
|
|
208
|
+
turnUpRight: '+',
|
|
209
|
+
turnUpLeft: '+',
|
|
210
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ASCII diagram detection: is this fence content a box-and-line diagram?
|
|
3
|
+
*
|
|
4
|
+
* False positives are the expensive failure (they hide real code behind a
|
|
5
|
+
* canvas / template); false negatives just leave a code block alone. Every
|
|
6
|
+
* rejection path records a machine-greppable reason so the corpus tests
|
|
7
|
+
* act as the contract for any future threshold tuning.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { MarkdownCodeBlock } from '../../markdown/types.js';
|
|
11
|
+
import { parseAsciiDiagramWithStats } from './parse.js';
|
|
12
|
+
import type { AsciiDiagramDetection } from './types.js';
|
|
13
|
+
|
|
14
|
+
/** Fence languages eligible for diagram detection (plus no language at all). */
|
|
15
|
+
export const ASCII_DIAGRAM_FENCE_LANGS: ReadonlySet<string> = new Set([
|
|
16
|
+
'text',
|
|
17
|
+
'txt',
|
|
18
|
+
'plaintext',
|
|
19
|
+
'plain',
|
|
20
|
+
'ascii',
|
|
21
|
+
'diagram',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const MAX_LINES = 400;
|
|
25
|
+
const MAX_COLS = 400;
|
|
26
|
+
const MIN_LINES = 3;
|
|
27
|
+
const MAX_LOOSE_RATIO = 0.25;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Box-drawing corners, junctions, and (Unicode) verticals — characters that
|
|
31
|
+
* signal a box border leaked into a node's label, i.e. a mis-aligned parse.
|
|
32
|
+
* ASCII `|`/`+`/`-` and the horizontal `─` are excluded: they occur in real
|
|
33
|
+
* label text (`stdin | stdout`, `read-only`, `A + B`).
|
|
34
|
+
*/
|
|
35
|
+
const STRUCTURAL_IN_LABEL = /[│┃║┌┐└┘├┤┬┴┼╭╮╰╯┏┓┗┛╔╗╚╝┣┫┳┻╋╠╣╦╩╬╞╡╥╨╫╪]/;
|
|
36
|
+
|
|
37
|
+
export function isEligibleAsciiFenceLang(lang: string | null | undefined): boolean {
|
|
38
|
+
if (lang === null || lang === undefined) return true;
|
|
39
|
+
const normalized = lang.trim().toLowerCase();
|
|
40
|
+
return normalized === '' || ASCII_DIAGRAM_FENCE_LANGS.has(normalized);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* True when the fence LANGUAGE is the explicit `diagram` tag — an author-set
|
|
45
|
+
* "this is a diagram" marker that survives round-trips (the language class
|
|
46
|
+
* round-trips; fence meta does not). Detection of a tagged fence is lenient
|
|
47
|
+
* (≥1 box, no lattice rejector) so a degenerate diagram stays a diagram.
|
|
48
|
+
*/
|
|
49
|
+
export function isExplicitDiagramLang(lang: string | null | undefined): boolean {
|
|
50
|
+
return typeof lang === 'string' && lang.trim().toLowerCase() === 'diagram';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface DetectDiagramOptions {
|
|
54
|
+
/** The fence is explicitly `diagram`-tagged: accept ≥1 box, skip rejectors. */
|
|
55
|
+
explicit?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function detectAsciiDiagram(
|
|
59
|
+
text: string,
|
|
60
|
+
opts: DetectDiagramOptions = {},
|
|
61
|
+
): AsciiDiagramDetection {
|
|
62
|
+
const explicit = opts.explicit === true;
|
|
63
|
+
const reasons: string[] = [];
|
|
64
|
+
|
|
65
|
+
// ---- Cheap prefilter (no parse) ----------------------------------------
|
|
66
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
67
|
+
if (lines.length < MIN_LINES) {
|
|
68
|
+
return { isDiagram: false, reasons: [`too-few-lines(${lines.length})`] };
|
|
69
|
+
}
|
|
70
|
+
if (lines.length > MAX_LINES) {
|
|
71
|
+
return { isDiagram: false, reasons: [`too-many-lines(${lines.length})`] };
|
|
72
|
+
}
|
|
73
|
+
let maxCols = 0;
|
|
74
|
+
for (const line of lines) maxCols = Math.max(maxCols, line.length);
|
|
75
|
+
if (maxCols > MAX_COLS) {
|
|
76
|
+
return { isDiagram: false, reasons: [`too-wide(${maxCols})`] };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!explicit) {
|
|
80
|
+
let tlCandidates = 0;
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
// Unicode TL corners, junction-started shared borders (`├──`), and
|
|
83
|
+
// ASCII `+-` starts. Junctions over-count (file trees use `├──`), but
|
|
84
|
+
// the prefilter only gates the full parse — box tracing decides.
|
|
85
|
+
const unicodeMatches = line.match(/[┌╭┏╔]|[├┬┼┣┳╋╠╦╬][─━═]/gu);
|
|
86
|
+
tlCandidates += unicodeMatches ? unicodeMatches.length : 0;
|
|
87
|
+
const plusMatches = line.match(/\+[-+]/g);
|
|
88
|
+
tlCandidates += plusMatches ? plusMatches.length : 0;
|
|
89
|
+
}
|
|
90
|
+
if (tlCandidates < 2) {
|
|
91
|
+
return { isDiagram: false, reasons: [`too-few-corner-candidates(${tlCandidates})`] };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Markdown/GFM table shape: mostly `| … |` rows plus a separator row.
|
|
96
|
+
const nonBlank = lines.filter((l) => l.trim().length > 0);
|
|
97
|
+
const pipeRows = nonBlank.filter((l) => /^\s*\|.*\|\s*$/.test(l)).length;
|
|
98
|
+
const hasGfmSeparator = nonBlank.some((l) => /^\s*\|?[\s:|-]+\|?\s*$/.test(l) && l.includes('-'));
|
|
99
|
+
if (pipeRows > nonBlank.length / 2 && hasGfmSeparator) {
|
|
100
|
+
return { isDiagram: false, reasons: ['markdown-table'] };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ---- Full parse + acceptance -------------------------------------------
|
|
104
|
+
const { diagram, stats } = parseAsciiDiagramWithStats(text);
|
|
105
|
+
|
|
106
|
+
const minBoxes = explicit ? 1 : 2;
|
|
107
|
+
if (diagram.nodes.length < minBoxes) {
|
|
108
|
+
return { isDiagram: false, reasons: [`too-few-boxes(${diagram.nodes.length})`] };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Garbled-label guard: a clean box label never contains box-drawing
|
|
112
|
+
// corners/junctions/verticals — those only leak in when the label columns
|
|
113
|
+
// are desynced from the box borders (hand-drawn art whose labels overflow
|
|
114
|
+
// their boxes and shove every following column out of alignment row-to-row,
|
|
115
|
+
// so no parser can map the text back to the boxes). When a meaningful share
|
|
116
|
+
// of labels are corrupt this way, the "diagram" would render as garbage, so
|
|
117
|
+
// decline detection and let the fence fall back to a faithful code block.
|
|
118
|
+
const garbled = diagram.nodes.filter((n) => STRUCTURAL_IN_LABEL.test(n.label)).length;
|
|
119
|
+
if (garbled > 0 && garbled >= diagram.nodes.length * 0.25) {
|
|
120
|
+
return { isDiagram: false, reasons: [`garbled-labels(${garbled}/${diagram.nodes.length})`] };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!explicit) {
|
|
124
|
+
// Table-lattice rejector: an edge-less shared-border grid of ≥2×2 boxes
|
|
125
|
+
// is a psql/MySQL-style result table, not a diagram. Single-column
|
|
126
|
+
// stacks still pass (a legitimate "layer stack" diagram).
|
|
127
|
+
if (diagram.edges.length === 0 && diagram.nodes.length >= 4 && isLattice(diagram)) {
|
|
128
|
+
return { isDiagram: false, reasons: ['table-lattice'] };
|
|
129
|
+
}
|
|
130
|
+
if (stats.totalNonSpaceCells > 0) {
|
|
131
|
+
const looseRatio = stats.looseNonSpaceCells / stats.totalNonSpaceCells;
|
|
132
|
+
if (looseRatio > MAX_LOOSE_RATIO) {
|
|
133
|
+
return { isDiagram: false, reasons: [`loose-ratio(${looseRatio.toFixed(2)})`] };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
reasons.push(`boxes(${diagram.nodes.length})`, `edges(${diagram.edges.length})`);
|
|
139
|
+
if (explicit) reasons.push('explicit');
|
|
140
|
+
return { isDiagram: true, diagram, reasons };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Convenience gate for pipeline callers: lang allowlist + content detection. */
|
|
144
|
+
export function isAsciiDiagramFence(node: MarkdownCodeBlock): boolean {
|
|
145
|
+
if (node.type !== 'code') return false;
|
|
146
|
+
if (!isEligibleAsciiFenceLang(node.lang)) return false;
|
|
147
|
+
return detectAsciiDiagram(node.value, { explicit: isExplicitDiagramLang(node.lang) }).isDiagram;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isLattice(diagram: {
|
|
151
|
+
nodes: ReadonlyArray<{
|
|
152
|
+
col: number;
|
|
153
|
+
row: number;
|
|
154
|
+
wCols: number;
|
|
155
|
+
hRows: number;
|
|
156
|
+
containerId?: string;
|
|
157
|
+
}>;
|
|
158
|
+
}): boolean {
|
|
159
|
+
const leaves = diagram.nodes.filter((n) => !n.containerId);
|
|
160
|
+
if (leaves.length < 4) return false;
|
|
161
|
+
const rowBands = new Set(leaves.map((n) => n.row));
|
|
162
|
+
const colBands = new Set(leaves.map((n) => n.col));
|
|
163
|
+
if (rowBands.size < 2 || colBands.size < 2) return false;
|
|
164
|
+
// Every box shares a border line with some other box.
|
|
165
|
+
return leaves.every((a) =>
|
|
166
|
+
leaves.some((b) => {
|
|
167
|
+
if (a === b) return false;
|
|
168
|
+
const aR1 = a.row + a.hRows - 1;
|
|
169
|
+
const aC1 = a.col + a.wCols - 1;
|
|
170
|
+
const bR1 = b.row + b.hRows - 1;
|
|
171
|
+
const bC1 = b.col + b.wCols - 1;
|
|
172
|
+
return a.row === bR1 || aR1 === b.row || a.col === bC1 || aC1 === b.col;
|
|
173
|
+
}),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ASCII diagram codec: detect / parse / render box-and-line art, plus
|
|
3
|
+
* mapping helpers between the grid model and the diagram canvas model.
|
|
4
|
+
*
|
|
5
|
+
* The fence text is the source of truth end-to-end: the editor parses it
|
|
6
|
+
* live for an interactive canvas and re-renders it after each semantic
|
|
7
|
+
* edit; the doc pipeline derives `templateData.nodes`/`edges` from it so
|
|
8
|
+
* preview/player/exports show the same diagram.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export { parseAsciiDiagram, parseAsciiDiagramWithStats } from './parse.js';
|
|
12
|
+
export type { AsciiParseStats } from './parse.js';
|
|
13
|
+
export { renderAsciiDiagram } from './render.js';
|
|
14
|
+
export type { RenderAsciiDiagramOptions } from './render.js';
|
|
15
|
+
export {
|
|
16
|
+
ASCII_DIAGRAM_FENCE_LANGS,
|
|
17
|
+
detectAsciiDiagram,
|
|
18
|
+
isAsciiDiagramFence,
|
|
19
|
+
isEligibleAsciiFenceLang,
|
|
20
|
+
isExplicitDiagramLang,
|
|
21
|
+
} from './detect.js';
|
|
22
|
+
export type { DetectDiagramOptions } from './detect.js';
|
|
23
|
+
export { repairAsciiDiagram, isRepairableDiagram } from './repair.js';
|
|
24
|
+
export type { RepairResult } from './repair.js';
|
|
25
|
+
export {
|
|
26
|
+
asciiCellToCanvas,
|
|
27
|
+
asciiDiagramFromBlocks,
|
|
28
|
+
asciiDiagramFromTemplateData,
|
|
29
|
+
asciiDiagramToTemplateData,
|
|
30
|
+
canvasToAsciiCell,
|
|
31
|
+
} from './mapping.js';
|
|
32
|
+
export { ASCII_CHAR_H, ASCII_CHAR_W } from './types.js';
|
|
33
|
+
export type {
|
|
34
|
+
AsciiDiagram,
|
|
35
|
+
AsciiDiagramDetection,
|
|
36
|
+
AsciiDiagramEdge,
|
|
37
|
+
AsciiDiagramNode,
|
|
38
|
+
} from './types.js';
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mapping between the grid-native ASCII diagram model and the diagram
|
|
3
|
+
* canvas / template model (canvas units, the same space as `Block.x`/`y`).
|
|
4
|
+
*
|
|
5
|
+
* Quantization uses plain `round()` per axis — monotonic and local, so a
|
|
6
|
+
* small canvas nudge either changes nothing or moves exactly one cell
|
|
7
|
+
* (cluster-based schemes flap under small moves and churn the art).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
DiagramEdgeAnchor,
|
|
12
|
+
DiagramTemplateEdge,
|
|
13
|
+
DiagramTemplateNode,
|
|
14
|
+
} from '../../schemas/BlockTemplates.js';
|
|
15
|
+
import type { Block } from '../../schemas/Doc.js';
|
|
16
|
+
import { computeDiagramLayout } from '../templates/diagramLayout.js';
|
|
17
|
+
import {
|
|
18
|
+
ASCII_CHAR_H,
|
|
19
|
+
ASCII_CHAR_W,
|
|
20
|
+
type AsciiDiagram,
|
|
21
|
+
type AsciiDiagramEdge,
|
|
22
|
+
type AsciiDiagramNode,
|
|
23
|
+
} from './types.js';
|
|
24
|
+
|
|
25
|
+
export function asciiCellToCanvas(col: number, row: number): { x: number; y: number } {
|
|
26
|
+
return { x: col * ASCII_CHAR_W, y: row * ASCII_CHAR_H };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function canvasToAsciiCell(x: number, y: number): { col: number; row: number } {
|
|
30
|
+
return {
|
|
31
|
+
col: Math.max(0, Math.round(x / ASCII_CHAR_W)),
|
|
32
|
+
row: Math.max(0, Math.round(y / ASCII_CHAR_H)),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Grid model → template/canvas model (`templateData.nodes`/`edges`). */
|
|
37
|
+
export function asciiDiagramToTemplateData(diagram: AsciiDiagram): {
|
|
38
|
+
nodes: DiagramTemplateNode[];
|
|
39
|
+
edges: DiagramTemplateEdge[];
|
|
40
|
+
} {
|
|
41
|
+
const nodes: DiagramTemplateNode[] = diagram.nodes.map((n) => ({
|
|
42
|
+
id: n.id,
|
|
43
|
+
label: n.label,
|
|
44
|
+
...asciiCellToCanvas(n.col, n.row),
|
|
45
|
+
w: n.wCols * ASCII_CHAR_W,
|
|
46
|
+
h: n.hRows * ASCII_CHAR_H,
|
|
47
|
+
...(n.containerId ? { container: n.containerId } : {}),
|
|
48
|
+
}));
|
|
49
|
+
const anchors = inferEdgeAnchors(nodes, diagram.edges);
|
|
50
|
+
const edges: DiagramTemplateEdge[] = diagram.edges.map((e, index) => ({
|
|
51
|
+
source: e.source,
|
|
52
|
+
target: e.target,
|
|
53
|
+
...(e.label ? { label: e.label } : {}),
|
|
54
|
+
...(e.directed ? {} : { directed: false }),
|
|
55
|
+
...anchors[index],
|
|
56
|
+
routing: 'orthogonal',
|
|
57
|
+
}));
|
|
58
|
+
return { nodes, edges };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface EdgeAnchorPair {
|
|
62
|
+
sourceAnchor: DiagramEdgeAnchor;
|
|
63
|
+
targetAnchor: DiagramEdgeAnchor;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Recover the visual intent carried by orthogonal ASCII art. Fan-out ports
|
|
68
|
+
* spread across the source face; fan-in ports spread across the target face.
|
|
69
|
+
* A single endpoint stays centered on its facing side.
|
|
70
|
+
*/
|
|
71
|
+
function inferEdgeAnchors(
|
|
72
|
+
nodes: readonly DiagramTemplateNode[],
|
|
73
|
+
edges: readonly AsciiDiagramEdge[],
|
|
74
|
+
): EdgeAnchorPair[] {
|
|
75
|
+
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
76
|
+
const pairs = edges.map((edge) => {
|
|
77
|
+
const source = nodeById.get(edge.source);
|
|
78
|
+
const target = nodeById.get(edge.target);
|
|
79
|
+
if (!source || !target) {
|
|
80
|
+
return {
|
|
81
|
+
sourceAnchor: { side: 'right' as const, offset: 0.5 },
|
|
82
|
+
targetAnchor: { side: 'left' as const, offset: 0.5 },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const sourceW = source.w ?? ASCII_CHAR_W * 12;
|
|
86
|
+
const sourceH = source.h ?? ASCII_CHAR_H * 3;
|
|
87
|
+
const targetW = target.w ?? ASCII_CHAR_W * 12;
|
|
88
|
+
const targetH = target.h ?? ASCII_CHAR_H * 3;
|
|
89
|
+
const dx = target.x + targetW / 2 - (source.x + sourceW / 2);
|
|
90
|
+
const dy = target.y + targetH / 2 - (source.y + sourceH / 2);
|
|
91
|
+
const horizontalDistance = Math.abs(dx) / Math.max(1, (sourceW + targetW) / 2);
|
|
92
|
+
const verticalDistance = Math.abs(dy) / Math.max(1, (sourceH + targetH) / 2);
|
|
93
|
+
if (verticalDistance >= horizontalDistance) {
|
|
94
|
+
return dy >= 0
|
|
95
|
+
? {
|
|
96
|
+
sourceAnchor: { side: 'bottom' as const, offset: 0.5 },
|
|
97
|
+
targetAnchor: { side: 'top' as const, offset: 0.5 },
|
|
98
|
+
}
|
|
99
|
+
: {
|
|
100
|
+
sourceAnchor: { side: 'top' as const, offset: 0.5 },
|
|
101
|
+
targetAnchor: { side: 'bottom' as const, offset: 0.5 },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return dx >= 0
|
|
105
|
+
? {
|
|
106
|
+
sourceAnchor: { side: 'right' as const, offset: 0.5 },
|
|
107
|
+
targetAnchor: { side: 'left' as const, offset: 0.5 },
|
|
108
|
+
}
|
|
109
|
+
: {
|
|
110
|
+
sourceAnchor: { side: 'left' as const, offset: 0.5 },
|
|
111
|
+
targetAnchor: { side: 'right' as const, offset: 0.5 },
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
distributeOffsets(pairs, edges, nodes, 'source');
|
|
116
|
+
distributeOffsets(pairs, edges, nodes, 'target');
|
|
117
|
+
return pairs;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function distributeOffsets(
|
|
121
|
+
pairs: EdgeAnchorPair[],
|
|
122
|
+
edges: readonly AsciiDiagramEdge[],
|
|
123
|
+
nodes: readonly DiagramTemplateNode[],
|
|
124
|
+
endpoint: 'source' | 'target',
|
|
125
|
+
): void {
|
|
126
|
+
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
127
|
+
const groups = new Map<string, number[]>();
|
|
128
|
+
for (let index = 0; index < edges.length; index++) {
|
|
129
|
+
const edge = edges[index];
|
|
130
|
+
const anchor = endpoint === 'source' ? pairs[index].sourceAnchor : pairs[index].targetAnchor;
|
|
131
|
+
const nodeId = endpoint === 'source' ? edge.source : edge.target;
|
|
132
|
+
const key = `${nodeId}:${anchor.side}`;
|
|
133
|
+
const group = groups.get(key) ?? [];
|
|
134
|
+
group.push(index);
|
|
135
|
+
groups.set(key, group);
|
|
136
|
+
}
|
|
137
|
+
for (const indexes of groups.values()) {
|
|
138
|
+
if (indexes.length < 2) continue;
|
|
139
|
+
const side =
|
|
140
|
+
endpoint === 'source'
|
|
141
|
+
? pairs[indexes[0]].sourceAnchor.side
|
|
142
|
+
: pairs[indexes[0]].targetAnchor.side;
|
|
143
|
+
indexes.sort((a, b) => {
|
|
144
|
+
const oppositeA = nodeById.get(endpoint === 'source' ? edges[a].target : edges[a].source);
|
|
145
|
+
const oppositeB = nodeById.get(endpoint === 'source' ? edges[b].target : edges[b].source);
|
|
146
|
+
const axisA = side === 'top' || side === 'bottom' ? (oppositeA?.x ?? 0) : (oppositeA?.y ?? 0);
|
|
147
|
+
const axisB = side === 'top' || side === 'bottom' ? (oppositeB?.x ?? 0) : (oppositeB?.y ?? 0);
|
|
148
|
+
return axisA - axisB || a - b;
|
|
149
|
+
});
|
|
150
|
+
indexes.forEach((edgeIndex, rank) => {
|
|
151
|
+
const anchor =
|
|
152
|
+
endpoint === 'source' ? pairs[edgeIndex].sourceAnchor : pairs[edgeIndex].targetAnchor;
|
|
153
|
+
anchor.offset = (rank + 1) / (indexes.length + 1);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Template/canvas model → grid model, quantizing positions and sizes. */
|
|
159
|
+
export function asciiDiagramFromTemplateData(
|
|
160
|
+
nodes: readonly DiagramTemplateNode[],
|
|
161
|
+
edges: readonly DiagramTemplateEdge[],
|
|
162
|
+
options: { style?: 'unicode' | 'ascii' } = {},
|
|
163
|
+
): AsciiDiagram {
|
|
164
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
165
|
+
const outNodes: AsciiDiagramNode[] = nodes.map((n) => {
|
|
166
|
+
const { col, row } = canvasToAsciiCell(n.x, n.y);
|
|
167
|
+
const labelLines = n.label.length > 0 ? n.label.split('\n') : [];
|
|
168
|
+
const innerW = labelLines.reduce((w, l) => Math.max(w, l.length), 0);
|
|
169
|
+
const wCols =
|
|
170
|
+
typeof n.w === 'number' ? Math.max(3, Math.round(n.w / ASCII_CHAR_W)) : innerW + 4;
|
|
171
|
+
const hRows =
|
|
172
|
+
typeof n.h === 'number' ? Math.max(3, Math.round(n.h / ASCII_CHAR_H)) : labelLines.length + 2;
|
|
173
|
+
return {
|
|
174
|
+
id: n.id,
|
|
175
|
+
label: n.label,
|
|
176
|
+
col,
|
|
177
|
+
row,
|
|
178
|
+
wCols: Math.max(wCols, 3),
|
|
179
|
+
hRows: Math.max(hRows, 3),
|
|
180
|
+
...(n.container && ids.has(n.container) && n.container !== n.id
|
|
181
|
+
? { containerId: n.container }
|
|
182
|
+
: {}),
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
return {
|
|
186
|
+
nodes: outNodes,
|
|
187
|
+
edges: edges
|
|
188
|
+
.filter((e) => ids.has(e.source) && ids.has(e.target))
|
|
189
|
+
.map((e) => ({
|
|
190
|
+
source: e.source,
|
|
191
|
+
target: e.target,
|
|
192
|
+
...(e.label ? { label: e.label } : {}),
|
|
193
|
+
directed: e.directed !== false,
|
|
194
|
+
})),
|
|
195
|
+
style: options.style ?? 'unicode',
|
|
196
|
+
warnings: [],
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Legacy heading-based diagram children → ASCII diagram model. Used to
|
|
202
|
+
* migrate `### Node {#id x= y= connectsTo=}` sections to ASCII fences;
|
|
203
|
+
* positions run through the shared `computeDiagramLayout` so unpinned
|
|
204
|
+
* children get the same grid placement as the legacy renderer.
|
|
205
|
+
*/
|
|
206
|
+
export function asciiDiagramFromBlocks(
|
|
207
|
+
children: readonly Block[],
|
|
208
|
+
options: { style?: 'unicode' | 'ascii' } = {},
|
|
209
|
+
): AsciiDiagram {
|
|
210
|
+
const layout = computeDiagramLayout(children);
|
|
211
|
+
const nodes: DiagramTemplateNode[] = layout.nodes.map((n) => ({
|
|
212
|
+
id: n.id,
|
|
213
|
+
label: n.label,
|
|
214
|
+
x: n.x,
|
|
215
|
+
y: n.y,
|
|
216
|
+
}));
|
|
217
|
+
const edges: DiagramTemplateEdge[] = layout.edges.map((e) => ({
|
|
218
|
+
source: e.source,
|
|
219
|
+
target: e.target,
|
|
220
|
+
...(e.type ? { label: e.type } : {}),
|
|
221
|
+
}));
|
|
222
|
+
const diagram = asciiDiagramFromTemplateData(nodes, edges, options);
|
|
223
|
+
diagram.warnings.push(...layout.warnings);
|
|
224
|
+
return diagram;
|
|
225
|
+
}
|