@bendyline/squisq 1.5.2 → 2.0.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.
Files changed (263) hide show
  1. package/README.md +63 -10
  2. package/dist/{Doc-BBVGq1_9.d.ts → Doc-ylqWKDc_.d.ts} +248 -37
  3. package/dist/{ImageEditDoc-FuyGtt6o.d.ts → ImageEditDoc-D828IXJ1.d.ts} +1 -1
  4. package/dist/annotationCoercion-Dr_DY9Hx.d.ts +86 -0
  5. package/dist/{chunk-VWUFZ6ZG.js → chunk-3E5F2XMR.js} +2 -29
  6. package/dist/chunk-3E5F2XMR.js.map +1 -0
  7. package/dist/chunk-4X3JQXNM.js +35 -0
  8. package/dist/chunk-4X3JQXNM.js.map +1 -0
  9. package/dist/{chunk-P3K7NLHF.js → chunk-5DKBFXPY.js} +31 -121
  10. package/dist/chunk-5DKBFXPY.js.map +1 -0
  11. package/dist/{chunk-DLXZMS5K.js → chunk-5J4HKRJF.js} +51 -3
  12. package/dist/chunk-5J4HKRJF.js.map +1 -0
  13. package/dist/chunk-5UYI3RJB.js +2503 -0
  14. package/dist/chunk-5UYI3RJB.js.map +1 -0
  15. package/dist/{chunk-BEQODJWV.js → chunk-66QO7XFB.js} +11 -291
  16. package/dist/chunk-66QO7XFB.js.map +1 -0
  17. package/dist/{chunk-KKNUBQ6Y.js → chunk-6PJRB2CF.js} +156 -91
  18. package/dist/chunk-6PJRB2CF.js.map +1 -0
  19. package/dist/{chunk-HWVFJAAH.js → chunk-AMIR72JP.js} +28 -10
  20. package/dist/chunk-AMIR72JP.js.map +1 -0
  21. package/dist/chunk-FVJUUTEM.js +74 -0
  22. package/dist/chunk-FVJUUTEM.js.map +1 -0
  23. package/dist/{chunk-5TYS5V3G.js → chunk-GF3BVNLU.js} +4127 -3538
  24. package/dist/chunk-GF3BVNLU.js.map +1 -0
  25. package/dist/{chunk-Q3ROPT5H.js → chunk-GZY66SDN.js} +1 -1
  26. package/dist/{chunk-Q3ROPT5H.js.map → chunk-GZY66SDN.js.map} +1 -1
  27. package/dist/chunk-JOQYPRDZ.js +127 -0
  28. package/dist/chunk-JOQYPRDZ.js.map +1 -0
  29. package/dist/{chunk-A4O7GIWE.js → chunk-LYNJEOAC.js} +10 -73
  30. package/dist/chunk-LYNJEOAC.js.map +1 -0
  31. package/dist/{chunk-LDQ2HJIX.js → chunk-PXLVXTLW.js} +252 -54
  32. package/dist/chunk-PXLVXTLW.js.map +1 -0
  33. package/dist/{chunk-TQWLI6S2.js → chunk-Q7LROV2H.js} +4 -2
  34. package/dist/{chunk-TQWLI6S2.js.map → chunk-Q7LROV2H.js.map} +1 -1
  35. package/dist/{chunk-T5UK6YOB.js → chunk-QTSJOY54.js} +60 -17
  36. package/dist/chunk-QTSJOY54.js.map +1 -0
  37. package/dist/chunk-QVANWDPH.js +2358 -0
  38. package/dist/chunk-QVANWDPH.js.map +1 -0
  39. package/dist/{chunk-GPLTCSXO.js → chunk-R2GC6RLD.js} +7 -7
  40. package/dist/chunk-R2GC6RLD.js.map +1 -0
  41. package/dist/chunk-R2YZTCLO.js +292 -0
  42. package/dist/chunk-R2YZTCLO.js.map +1 -0
  43. package/dist/chunk-S7A5D7ZJ.js +174 -0
  44. package/dist/chunk-S7A5D7ZJ.js.map +1 -0
  45. package/dist/chunk-T3TI7GSZ.js +884 -0
  46. package/dist/chunk-T3TI7GSZ.js.map +1 -0
  47. package/dist/{chunk-LTN6I7QW.js → chunk-TUWMK62B.js} +55 -4
  48. package/dist/chunk-TUWMK62B.js.map +1 -0
  49. package/dist/{chunk-IMSCRLLV.js → chunk-VR7UPJ7C.js} +35 -25
  50. package/dist/chunk-VR7UPJ7C.js.map +1 -0
  51. package/dist/doc/index.d.ts +2334 -5
  52. package/dist/doc/index.js +127 -33
  53. package/dist/generate/index.d.ts +7 -2
  54. package/dist/generate/index.js +1 -1
  55. package/dist/imageEdit/index.d.ts +9 -6
  56. package/dist/imageEdit/index.js +3 -3
  57. package/dist/index.d.ts +12 -10
  58. package/dist/index.js +246 -80
  59. package/dist/jsonForm/index.d.ts +7 -3
  60. package/dist/jsonForm/index.js +9 -4
  61. package/dist/markdown/index.d.ts +6 -70
  62. package/dist/markdown/index.js +13 -9
  63. package/dist/narration/index.d.ts +618 -0
  64. package/dist/narration/index.js +82 -0
  65. package/dist/recommend/index.d.ts +15 -1
  66. package/dist/recommend/index.js +2 -2
  67. package/dist/schemas/index.d.ts +28 -6
  68. package/dist/schemas/index.js +15 -13
  69. package/dist/spatial/index.d.ts +0 -1
  70. package/dist/spatial/index.js +1 -1
  71. package/dist/storage/index.js +1 -1
  72. package/dist/{themeLibrary-CehcJhzz.d.ts → themeLibrary-RjVgLpUi.d.ts} +10 -11
  73. package/dist/timing/index.js +5 -3
  74. package/dist/transform/index.d.ts +35 -24
  75. package/dist/transform/index.js +9 -10
  76. package/dist/{types-DlAZ7MW4.d.ts → types-BwMPl2CB.d.ts} +1 -1
  77. package/dist/{types-D98li1gV.d.ts → types-C4kiRn_k.d.ts} +3 -1
  78. package/dist/versions/index.d.ts +2 -1
  79. package/dist/versions/index.js +2 -2
  80. package/package.json +6 -6
  81. package/src/__tests__/applyNarrationTiming.test.ts +190 -0
  82. package/src/__tests__/applyRenderStyle.test.ts +3 -3
  83. package/src/__tests__/asciiDiagramBattle.test.ts +246 -0
  84. package/src/__tests__/asciiDiagramDetect.test.ts +162 -0
  85. package/src/__tests__/asciiDiagramMapping.test.ts +38 -0
  86. package/src/__tests__/asciiDiagramParse.test.ts +233 -0
  87. package/src/__tests__/asciiDiagramPipeline.test.ts +273 -0
  88. package/src/__tests__/asciiDiagramRender.test.ts +162 -0
  89. package/src/__tests__/asciiDiagramRepair.test.ts +84 -0
  90. package/src/__tests__/asciiTimelineDetect.test.ts +157 -0
  91. package/src/__tests__/asciiTimelineParse.test.ts +205 -0
  92. package/src/__tests__/asciiTimelinePipeline.test.ts +349 -0
  93. package/src/__tests__/asciiTimelineRender.test.ts +376 -0
  94. package/src/__tests__/attrTokens.test.ts +5 -0
  95. package/src/__tests__/contentContainer.test.ts +43 -0
  96. package/src/__tests__/customTemplatesFrontmatter.test.ts +0 -15
  97. package/src/__tests__/dataTable.test.ts +25 -15
  98. package/src/__tests__/diagramBlock.test.ts +3 -3
  99. package/src/__tests__/diagramText.test.ts +26 -0
  100. package/src/__tests__/docToMarkdownMedia.test.ts +83 -0
  101. package/src/__tests__/drawingBlock.test.ts +3 -3
  102. package/src/__tests__/fixtures/asciiDiagramBattle.ts +512 -0
  103. package/src/__tests__/fixtures/asciiDiagrams.ts +287 -0
  104. package/src/__tests__/fixtures/asciiTimelines.ts +35 -0
  105. package/src/__tests__/fixtures/treeviewFixtures.ts +243 -0
  106. package/src/__tests__/getLayers.test.ts +32 -18
  107. package/src/__tests__/getLayersFallback.test.ts +8 -8
  108. package/src/__tests__/imageEditVersions.test.ts +2 -0
  109. package/src/__tests__/jsonForm.pointer.test.ts +47 -1
  110. package/src/__tests__/layoutBlock.test.ts +3 -3
  111. package/src/__tests__/majorApiTypes.test.ts +18 -0
  112. package/src/__tests__/markdown.test.ts +41 -0
  113. package/src/__tests__/matchFontFamily.test.ts +114 -0
  114. package/src/__tests__/materializeBlockLayers.test.ts +135 -0
  115. package/src/__tests__/materializeTestUtils.ts +17 -0
  116. package/src/__tests__/narrationAlign.test.ts +150 -0
  117. package/src/__tests__/narrationFeatures.test.ts +71 -0
  118. package/src/__tests__/narrationNuclei.test.ts +45 -0
  119. package/src/__tests__/narrationPacing.test.ts +189 -0
  120. package/src/__tests__/narrationScript.test.ts +149 -0
  121. package/src/__tests__/narrationSessionRepro.test.ts +62 -0
  122. package/src/__tests__/narrationSidecar.test.ts +88 -0
  123. package/src/__tests__/narrationSyllables.test.ts +65 -0
  124. package/src/__tests__/narrationTestSignals.ts +167 -0
  125. package/src/__tests__/narrationVad.test.ts +66 -0
  126. package/src/__tests__/resolveDocTheme.test.ts +16 -2
  127. package/src/__tests__/rootExports.test.ts +18 -0
  128. package/src/__tests__/scopedContentContainer.test.ts +20 -0
  129. package/src/__tests__/shapeGeometry.test.ts +12 -0
  130. package/src/__tests__/templateAnnotationParse.test.ts +10 -0
  131. package/src/__tests__/templateInputs.test.ts +69 -0
  132. package/src/__tests__/templateMetadata.test.ts +2 -1
  133. package/src/__tests__/templates.test.ts +143 -17
  134. package/src/__tests__/themeCompile.test.ts +49 -22
  135. package/src/__tests__/transformProvenance.test.ts +160 -0
  136. package/src/__tests__/transformV2.test.ts +40 -14
  137. package/src/__tests__/treeviewBattle.test.ts +76 -0
  138. package/src/__tests__/treeviewDetect.test.ts +168 -0
  139. package/src/__tests__/treeviewParse.test.ts +118 -0
  140. package/src/__tests__/treeviewPipeline.test.ts +183 -0
  141. package/src/__tests__/treeviewRender.test.ts +91 -0
  142. package/src/__tests__/versions.test.ts +44 -0
  143. package/src/doc/applyNarrationTiming.ts +244 -0
  144. package/src/doc/asciiDiagram/chars.ts +210 -0
  145. package/src/doc/asciiDiagram/detect.ts +175 -0
  146. package/src/doc/asciiDiagram/index.ts +38 -0
  147. package/src/doc/asciiDiagram/mapping.ts +225 -0
  148. package/src/doc/asciiDiagram/parse.ts +1095 -0
  149. package/src/doc/asciiDiagram/render.ts +963 -0
  150. package/src/doc/asciiDiagram/repair.ts +400 -0
  151. package/src/doc/asciiDiagram/types.ts +65 -0
  152. package/src/doc/asciiTimeline/detect.ts +138 -0
  153. package/src/doc/asciiTimeline/index.ts +26 -0
  154. package/src/doc/asciiTimeline/mapping.ts +43 -0
  155. package/src/doc/asciiTimeline/parse.ts +870 -0
  156. package/src/doc/asciiTimeline/render.ts +427 -0
  157. package/src/doc/asciiTimeline/types.ts +73 -0
  158. package/src/doc/asciiTimeline/wrappedFlow.ts +427 -0
  159. package/src/doc/audioMapping.ts +60 -46
  160. package/src/doc/customTemplatesFrontmatter.ts +0 -41
  161. package/src/doc/docToMarkdown.ts +70 -2
  162. package/src/doc/index.ts +76 -4
  163. package/src/doc/markdownToDoc.ts +135 -1
  164. package/src/doc/materializeBlockLayers.ts +304 -0
  165. package/src/doc/mediaAnnotations.ts +20 -0
  166. package/src/doc/resolveDocTheme.ts +8 -7
  167. package/src/doc/templateInputs.ts +185 -2
  168. package/src/doc/templates/__tests__/customTemplateRegistry.test.ts +22 -8
  169. package/src/doc/templates/diagramBlock.ts +237 -38
  170. package/src/doc/templates/fallbackBlock.ts +1 -1
  171. package/src/doc/templates/index.ts +134 -279
  172. package/src/doc/templates/inputDescriptors.ts +20 -1
  173. package/src/doc/templates/metadata.ts +10 -0
  174. package/src/doc/templates/persistentLayers.ts +12 -126
  175. package/src/doc/templates/registry.ts +88 -0
  176. package/src/doc/templates/templateNames.ts +22 -0
  177. package/src/doc/templates/timelineBlock.ts +613 -0
  178. package/src/doc/templates/treeBlock.ts +131 -0
  179. package/src/doc/treeview/chars.ts +72 -0
  180. package/src/doc/treeview/detect.ts +145 -0
  181. package/src/doc/treeview/index.ts +30 -0
  182. package/src/doc/treeview/mapping.ts +95 -0
  183. package/src/doc/treeview/parse.ts +201 -0
  184. package/src/doc/treeview/render.ts +41 -0
  185. package/src/doc/treeview/types.ts +43 -0
  186. package/src/doc/utils/applyRenderStyle.ts +2 -9
  187. package/src/doc/utils/diagramText.ts +78 -0
  188. package/src/doc/utils/shapeGeometry.ts +67 -5
  189. package/src/doc/validate.ts +1 -1
  190. package/src/generate/templateMapper.ts +7 -0
  191. package/src/imageEdit/export.ts +6 -5
  192. package/src/imageEdit/versionPaths.ts +13 -26
  193. package/src/imageEdit/versions.ts +21 -100
  194. package/src/index.ts +1 -0
  195. package/src/internal/immutable.ts +58 -0
  196. package/src/jsonForm/index.ts +9 -1
  197. package/src/jsonForm/pointer.ts +68 -23
  198. package/src/markdown/attrTokens.ts +7 -5
  199. package/src/markdown/index.ts +1 -0
  200. package/src/markdown/stringify.ts +4 -9
  201. package/src/markdown/utils.ts +36 -14
  202. package/src/narration/align.ts +514 -0
  203. package/src/narration/features.ts +231 -0
  204. package/src/narration/index.ts +86 -0
  205. package/src/narration/nuclei.ts +118 -0
  206. package/src/narration/pacing.ts +211 -0
  207. package/src/narration/script.ts +239 -0
  208. package/src/narration/session.ts +100 -0
  209. package/src/narration/sidecar.ts +187 -0
  210. package/src/narration/syllables.ts +85 -0
  211. package/src/narration/trace.ts +50 -0
  212. package/src/narration/types.ts +299 -0
  213. package/src/narration/vad.ts +131 -0
  214. package/src/recommend/numberHighlight.ts +25 -0
  215. package/src/recommend/templates.ts +108 -12
  216. package/src/schemas/BlockTemplates.ts +156 -7
  217. package/src/schemas/Doc.ts +72 -9
  218. package/src/schemas/Media.ts +12 -2
  219. package/src/schemas/Theme.ts +45 -25
  220. package/src/schemas/Viewport.ts +2 -2
  221. package/src/schemas/fontStacks.ts +79 -0
  222. package/src/schemas/themeCompile.ts +15 -0
  223. package/src/schemas/themeConstants.ts +2 -0
  224. package/src/schemas/themeLibrary.ts +25 -19
  225. package/src/schemas/themeValidator.ts +1 -1
  226. package/src/spatial/Geohash.ts +0 -3
  227. package/src/storage/ContentContainer.ts +17 -9
  228. package/src/storage/ScopedContentContainer.ts +45 -15
  229. package/src/transform/applyTransform.ts +5 -5
  230. package/src/transform/index.ts +3 -1
  231. package/src/transform/registry.ts +169 -49
  232. package/src/transform/templateSelector.ts +21 -2
  233. package/src/transform/timingAllocator.ts +152 -21
  234. package/src/transform/types.ts +14 -1
  235. package/src/versions/gitignore.ts +34 -0
  236. package/src/versions/index.ts +2 -0
  237. package/src/versions/operations.ts +18 -104
  238. package/src/versions/paths.ts +13 -28
  239. package/src/versions/snapshotStore.ts +202 -0
  240. package/src/versions/types.ts +3 -1
  241. package/dist/chunk-5TYS5V3G.js.map +0 -1
  242. package/dist/chunk-A4O7GIWE.js.map +0 -1
  243. package/dist/chunk-BEQODJWV.js.map +0 -1
  244. package/dist/chunk-DLXZMS5K.js.map +0 -1
  245. package/dist/chunk-GPLTCSXO.js.map +0 -1
  246. package/dist/chunk-HWVFJAAH.js.map +0 -1
  247. package/dist/chunk-IMSCRLLV.js.map +0 -1
  248. package/dist/chunk-JELIRVDP.js +0 -24
  249. package/dist/chunk-JELIRVDP.js.map +0 -1
  250. package/dist/chunk-KKNUBQ6Y.js.map +0 -1
  251. package/dist/chunk-LDQ2HJIX.js.map +0 -1
  252. package/dist/chunk-LTN6I7QW.js.map +0 -1
  253. package/dist/chunk-P3K7NLHF.js.map +0 -1
  254. package/dist/chunk-T5UK6YOB.js.map +0 -1
  255. package/dist/chunk-UY7KGQ5R.js +0 -192
  256. package/dist/chunk-UY7KGQ5R.js.map +0 -1
  257. package/dist/chunk-VLJJHDAC.js +0 -218
  258. package/dist/chunk-VLJJHDAC.js.map +0 -1
  259. package/dist/chunk-VWUFZ6ZG.js.map +0 -1
  260. package/dist/story/index.d.ts +0 -1801
  261. package/dist/story/index.js +0 -279
  262. package/src/doc/getLayers.ts +0 -195
  263. /package/dist/{story → narration}/index.js.map +0 -0
@@ -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
+ }