@aionbuilders/nabu 0.1.0-beta.0 → 0.1.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 (306) hide show
  1. package/CHANGELOG.md +523 -0
  2. package/LICENSE +21 -0
  3. package/README.md +72 -341
  4. package/dist/behaviors/text/RichText.svelte +24 -4
  5. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
  6. package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
  7. package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
  8. package/dist/behaviors/text/markdown-syntax.js +81 -0
  9. package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
  10. package/dist/behaviors/text/rich-text.extension.js +95 -38
  11. package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
  12. package/dist/behaviors/text/text.behavior.svelte.js +549 -83
  13. package/dist/behaviors/text/typography.extension.d.ts +2 -1
  14. package/dist/behaviors/text/typography.extension.js +3 -2
  15. package/dist/blocks/BlockList.svelte +48 -0
  16. package/dist/blocks/BlockList.svelte.d.ts +17 -0
  17. package/dist/blocks/Nabu.svelte +169 -25
  18. package/dist/blocks/Nabu.svelte.d.ts +13 -0
  19. package/dist/blocks/block.extension.d.ts +3 -0
  20. package/dist/blocks/block.extension.js +4 -7
  21. package/dist/blocks/block.svelte.d.ts +350 -86
  22. package/dist/blocks/block.svelte.js +370 -59
  23. package/dist/blocks/blockquote/Blockquote.svelte +1 -2
  24. package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
  25. package/dist/blocks/blockquote/blockquote.extension.js +14 -5
  26. package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
  27. package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
  28. package/dist/blocks/callout/Callout.svelte +6 -8
  29. package/dist/blocks/callout/callout.extension.d.ts +4 -1
  30. package/dist/blocks/callout/callout.extension.js +19 -3
  31. package/dist/blocks/callout/callout.svelte.d.ts +5 -3
  32. package/dist/blocks/callout/callout.svelte.js +11 -6
  33. package/dist/blocks/chunks.d.ts +49 -0
  34. package/dist/blocks/chunks.js +90 -0
  35. package/dist/blocks/code/Code.svelte +7 -8
  36. package/dist/blocks/code/code.extension.d.ts +4 -1
  37. package/dist/blocks/code/code.extension.js +22 -2
  38. package/dist/blocks/code/code.svelte.d.ts +9 -2
  39. package/dist/blocks/code/code.svelte.js +49 -25
  40. package/dist/blocks/container.utils.d.ts +85 -13
  41. package/dist/blocks/container.utils.js +338 -178
  42. package/dist/blocks/dialogue/Dialogue.svelte +8 -4
  43. package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
  44. package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
  45. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
  46. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
  47. package/dist/blocks/dialogue/index.d.ts +12 -1
  48. package/dist/blocks/dialogue/index.js +51 -35
  49. package/dist/blocks/divider/divider.extension.d.ts +23 -1
  50. package/dist/blocks/divider/divider.extension.js +85 -19
  51. package/dist/blocks/divider/divider.svelte.d.ts +7 -2
  52. package/dist/blocks/divider/divider.svelte.js +9 -4
  53. package/dist/blocks/file/File.svelte +13 -6
  54. package/dist/blocks/file/file.extension.d.ts +4 -1
  55. package/dist/blocks/file/file.extension.js +9 -1
  56. package/dist/blocks/file/file.svelte.d.ts +3 -3
  57. package/dist/blocks/file/file.svelte.js +20 -14
  58. package/dist/blocks/heading/Heading.svelte +1 -2
  59. package/dist/blocks/heading/heading.svelte.d.ts +23 -11
  60. package/dist/blocks/heading/heading.svelte.js +68 -13
  61. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
  62. package/dist/blocks/heading/index.d.ts +2 -2
  63. package/dist/blocks/heading/index.js +33 -8
  64. package/dist/blocks/image/Image.svelte +5 -2
  65. package/dist/blocks/image/image.extension.d.ts +4 -1
  66. package/dist/blocks/image/image.extension.js +9 -1
  67. package/dist/blocks/image/image.svelte.d.ts +17 -6
  68. package/dist/blocks/image/image.svelte.js +33 -11
  69. package/dist/blocks/index.d.ts +1 -1
  70. package/dist/blocks/index.js +1 -1
  71. package/dist/blocks/list/List.svelte +1 -0
  72. package/dist/blocks/list/ListItem.svelte +1 -0
  73. package/dist/blocks/list/convert.d.ts +20 -0
  74. package/dist/blocks/list/convert.js +92 -0
  75. package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
  76. package/dist/blocks/list/html-flat.d.ts +78 -0
  77. package/dist/blocks/list/html-flat.js +244 -0
  78. package/dist/blocks/list/indent.d.ts +86 -0
  79. package/dist/blocks/list/indent.js +205 -0
  80. package/dist/blocks/list/index.d.ts +6 -3
  81. package/dist/blocks/list/index.js +42 -31
  82. package/dist/blocks/list/list-item.svelte.d.ts +31 -17
  83. package/dist/blocks/list/list-item.svelte.js +99 -100
  84. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
  85. package/dist/blocks/list/list.behavior.svelte.js +6 -1
  86. package/dist/blocks/list/list.svelte.d.ts +89 -18
  87. package/dist/blocks/list/list.svelte.js +129 -125
  88. package/dist/blocks/list/markdown.d.ts +149 -0
  89. package/dist/blocks/list/markdown.js +442 -0
  90. package/dist/blocks/list/normalize.d.ts +11 -0
  91. package/dist/blocks/list/normalize.js +396 -0
  92. package/dist/blocks/list/paste.d.ts +45 -0
  93. package/dist/blocks/list/paste.js +242 -0
  94. package/dist/blocks/list/step-back.d.ts +24 -0
  95. package/dist/blocks/list/step-back.js +87 -0
  96. package/dist/blocks/list/to-list.d.ts +7 -0
  97. package/dist/blocks/list/to-list.js +44 -0
  98. package/dist/blocks/list/todos-toggle.d.ts +20 -0
  99. package/dist/blocks/list/todos-toggle.js +48 -0
  100. package/dist/blocks/media/markdown.d.ts +12 -0
  101. package/dist/blocks/media/markdown.js +26 -0
  102. package/dist/blocks/media/media-block.svelte.d.ts +77 -2
  103. package/dist/blocks/media/media-block.svelte.js +124 -12
  104. package/dist/blocks/media/media.helpers.d.ts +6 -1
  105. package/dist/blocks/media/media.helpers.js +43 -6
  106. package/dist/blocks/megablock.svelte.d.ts +50 -19
  107. package/dist/blocks/megablock.svelte.js +114 -74
  108. package/dist/blocks/move.d.ts +23 -0
  109. package/dist/blocks/move.js +92 -0
  110. package/dist/blocks/nabu.svelte.d.ts +764 -71
  111. package/dist/blocks/nabu.svelte.js +1746 -189
  112. package/dist/blocks/paragraph/Paragraph.svelte +1 -2
  113. package/dist/blocks/paragraph/index.d.ts +2 -2
  114. package/dist/blocks/paragraph/index.js +35 -13
  115. package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
  116. package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
  117. package/dist/blocks/paragraph/table.html.d.ts +9 -0
  118. package/dist/blocks/paragraph/table.html.js +111 -0
  119. package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
  120. package/dist/blocks/paragraph/table.markdown.js +90 -0
  121. package/dist/blocks/region/Region.svelte +264 -0
  122. package/dist/blocks/region/Region.svelte.d.ts +11 -0
  123. package/dist/blocks/region/actions.d.ts +135 -0
  124. package/dist/blocks/region/actions.js +402 -0
  125. package/dist/blocks/region/close.d.ts +35 -0
  126. package/dist/blocks/region/close.js +130 -0
  127. package/dist/blocks/region/commands.d.ts +6 -0
  128. package/dist/blocks/region/commands.js +277 -0
  129. package/dist/blocks/region/create.d.ts +72 -0
  130. package/dist/blocks/region/create.js +208 -0
  131. package/dist/blocks/region/end-marker.d.ts +35 -0
  132. package/dist/blocks/region/end-marker.js +51 -0
  133. package/dist/blocks/region/facts.d.ts +69 -0
  134. package/dist/blocks/region/facts.js +152 -0
  135. package/dist/blocks/region/format.d.ts +91 -0
  136. package/dist/blocks/region/format.js +260 -0
  137. package/dist/blocks/region/index.d.ts +14 -0
  138. package/dist/blocks/region/index.js +14 -0
  139. package/dist/blocks/region/insert.d.ts +59 -0
  140. package/dist/blocks/region/insert.js +102 -0
  141. package/dist/blocks/region/messages.d.ts +254 -0
  142. package/dist/blocks/region/messages.js +131 -0
  143. package/dist/blocks/region/normalize.d.ts +7 -0
  144. package/dist/blocks/region/normalize.js +170 -0
  145. package/dist/blocks/region/region.extension.d.ts +58 -0
  146. package/dist/blocks/region/region.extension.js +119 -0
  147. package/dist/blocks/region/region.svelte.d.ts +221 -0
  148. package/dist/blocks/region/region.svelte.js +582 -0
  149. package/dist/blocks/region/regions.svelte.d.ts +141 -0
  150. package/dist/blocks/region/regions.svelte.js +276 -0
  151. package/dist/blocks/region/serialize.d.ts +57 -0
  152. package/dist/blocks/region/serialize.js +112 -0
  153. package/dist/blocks/region/types.d.ts +255 -0
  154. package/dist/blocks/region/types.js +204 -0
  155. package/dist/blocks/selection.svelte.d.ts +106 -8
  156. package/dist/blocks/selection.svelte.js +622 -74
  157. package/dist/blocks/subthread.d.ts +80 -0
  158. package/dist/blocks/subthread.js +167 -0
  159. package/dist/blocks/text-block.svelte.d.ts +34 -13
  160. package/dist/blocks/text-block.svelte.js +42 -8
  161. package/dist/blocks/toggle/Toggle.svelte +4 -15
  162. package/dist/blocks/toggle/markdown.d.ts +20 -0
  163. package/dist/blocks/toggle/markdown.js +112 -0
  164. package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
  165. package/dist/blocks/toggle/toggle.extension.js +6 -0
  166. package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
  167. package/dist/blocks/toggle/toggle.svelte.js +134 -34
  168. package/dist/blocks/undo-selection.d.ts +175 -0
  169. package/dist/blocks/undo-selection.js +360 -0
  170. package/dist/blocks/video/Video.svelte +5 -2
  171. package/dist/blocks/video/video.extension.d.ts +4 -1
  172. package/dist/blocks/video/video.extension.js +9 -1
  173. package/dist/blocks/video/video.svelte.d.ts +6 -5
  174. package/dist/blocks/video/video.svelte.js +18 -9
  175. package/dist/extend.d.ts +55 -0
  176. package/dist/extend.js +88 -0
  177. package/dist/files/assets.clipboard.d.ts +77 -0
  178. package/dist/files/assets.clipboard.js +152 -0
  179. package/dist/files/files.svelte.d.ts +321 -19
  180. package/dist/files/files.svelte.js +847 -43
  181. package/dist/files/index.d.ts +3 -1
  182. package/dist/files/index.js +3 -1
  183. package/dist/files/outbox.d.ts +69 -0
  184. package/dist/files/outbox.js +48 -0
  185. package/dist/files/status.d.ts +65 -0
  186. package/dist/files/status.js +87 -0
  187. package/dist/files/store.d.ts +97 -0
  188. package/dist/files/store.js +65 -0
  189. package/dist/files/uploads.svelte.d.ts +174 -0
  190. package/dist/files/uploads.svelte.js +504 -0
  191. package/dist/index.d.ts +68 -9
  192. package/dist/index.js +117 -10
  193. package/dist/nabu-theme.css +21 -1
  194. package/dist/paste/html.paste.d.ts +74 -3
  195. package/dist/paste/html.paste.js +550 -86
  196. package/dist/paste/markdown-inline.d.ts +86 -0
  197. package/dist/paste/markdown-inline.js +528 -0
  198. package/dist/paste/markdown.paste.d.ts +2 -8
  199. package/dist/paste/markdown.paste.js +78 -94
  200. package/dist/paste/nabu.paste.d.ts +3 -0
  201. package/dist/paste/nabu.paste.js +29 -3
  202. package/dist/paste/plain-text.paste.d.ts +2 -2
  203. package/dist/paste/plain-text.paste.js +3 -3
  204. package/dist/persistence/asset-store.d.ts +51 -0
  205. package/dist/persistence/asset-store.js +103 -0
  206. package/dist/persistence/autosave.d.ts +53 -0
  207. package/dist/persistence/autosave.js +62 -0
  208. package/dist/persistence/compact.d.ts +162 -0
  209. package/dist/persistence/compact.js +141 -0
  210. package/dist/persistence/history.d.ts +77 -0
  211. package/dist/persistence/history.js +176 -0
  212. package/dist/persistence/indexeddb.d.ts +190 -4
  213. package/dist/persistence/indexeddb.js +333 -29
  214. package/dist/persistence/merge.d.ts +38 -0
  215. package/dist/persistence/merge.js +36 -0
  216. package/dist/persistence/outbox.d.ts +25 -0
  217. package/dist/persistence/outbox.js +40 -0
  218. package/dist/persistence/persistence.d.ts +70 -9
  219. package/dist/persistence/persistence.js +507 -35
  220. package/dist/persistence/recovery.d.ts +79 -0
  221. package/dist/persistence/recovery.js +79 -0
  222. package/dist/persistence/release.d.ts +34 -0
  223. package/dist/persistence/release.js +64 -0
  224. package/dist/persistence/state.svelte.d.ts +53 -0
  225. package/dist/persistence/state.svelte.js +48 -0
  226. package/dist/persistence/sync.d.ts +39 -0
  227. package/dist/persistence/sync.js +138 -0
  228. package/dist/persistence/tail.d.ts +42 -0
  229. package/dist/persistence/tail.js +103 -0
  230. package/dist/presets.d.ts +29 -26
  231. package/dist/presets.js +35 -13
  232. package/dist/themes/atelier.css +318 -0
  233. package/dist/ui/CommandChooser.svelte +153 -0
  234. package/dist/ui/CommandChooser.svelte.d.ts +13 -0
  235. package/dist/ui/CommandInvite.svelte +99 -0
  236. package/dist/ui/CommandInvite.svelte.d.ts +12 -0
  237. package/dist/ui/DragHandle.svelte +97 -21
  238. package/dist/ui/FloatingToolbar.svelte +75 -27
  239. package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
  240. package/dist/ui/SlashMenu.svelte +147 -15
  241. package/dist/ui/SlashMenu.svelte.d.ts +20 -0
  242. package/dist/ui/anchor.d.ts +112 -0
  243. package/dist/ui/anchor.js +136 -0
  244. package/dist/ui/index.d.ts +7 -1
  245. package/dist/ui/index.js +12 -1
  246. package/dist/ui/slash-menu.svelte.d.ts +79 -7
  247. package/dist/ui/slash-menu.svelte.js +85 -51
  248. package/dist/utils/command-confirm.svelte.d.ts +349 -0
  249. package/dist/utils/command-confirm.svelte.js +673 -0
  250. package/dist/utils/commands.d.ts +328 -0
  251. package/dist/utils/commands.js +416 -0
  252. package/dist/utils/dom-scope.d.ts +14 -0
  253. package/dist/utils/dom-scope.js +20 -0
  254. package/dist/utils/drop.d.ts +160 -0
  255. package/dist/utils/drop.js +305 -0
  256. package/dist/utils/extensions.d.ts +439 -45
  257. package/dist/utils/extensions.js +271 -29
  258. package/dist/utils/html.d.ts +44 -0
  259. package/dist/utils/html.js +183 -0
  260. package/dist/utils/index.d.ts +3 -0
  261. package/dist/utils/index.js +3 -0
  262. package/dist/utils/keys.d.ts +120 -0
  263. package/dist/utils/keys.js +248 -0
  264. package/dist/utils/links.d.ts +22 -0
  265. package/dist/utils/links.js +34 -0
  266. package/dist/utils/placeholders.d.ts +81 -3
  267. package/dist/utils/placeholders.js +73 -3
  268. package/dist/utils/selection.svelte.d.ts +47 -19
  269. package/dist/utils/selection.svelte.js +60 -10
  270. package/dist/utils/spellcheck.d.ts +9 -0
  271. package/dist/utils/spellcheck.js +17 -0
  272. package/dist/utils/tab.d.ts +31 -0
  273. package/dist/utils/tab.js +117 -0
  274. package/dist/utils/words.d.ts +7 -0
  275. package/dist/utils/words.js +62 -0
  276. package/dist/vite.d.ts +9 -3
  277. package/dist/vite.js +19 -8
  278. package/package.json +54 -20
  279. package/dist/blocks/variable.svelte.d.ts +0 -47
  280. package/dist/blocks/variable.svelte.js +0 -51
  281. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  282. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  283. package/dist/blocks/zone/Zone.svelte +0 -272
  284. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  286. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  288. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  290. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  291. package/dist/blocks/zone/index.d.ts +0 -2
  292. package/dist/blocks/zone/index.js +0 -2
  293. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  294. package/dist/blocks/zone/zone.extension.js +0 -237
  295. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  296. package/dist/blocks/zone/zone.svelte.js +0 -608
  297. package/dist/overlays/context/Context.svelte +0 -182
  298. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  299. package/dist/overlays/context/collect.d.ts +0 -17
  300. package/dist/overlays/context/collect.js +0 -33
  301. package/dist/overlays/context/context.extension.d.ts +0 -2
  302. package/dist/overlays/context/context.extension.js +0 -8
  303. package/dist/overlays/context/context.svelte.d.ts +0 -82
  304. package/dist/overlays/context/context.svelte.js +0 -159
  305. package/dist/overlays/context/index.d.ts +0 -3
  306. package/dist/overlays/context/index.js +0 -3
@@ -0,0 +1,141 @@
1
+ /**
2
+ * One division of the document, as computed by the registry (ADR 018 §3.4). A read-only VIEW over a region BLOCK, a container
3
+ * of the tree: the structure (`level`, `parent`, `children`, `index`, `path`) is derived from the tree; `title` / `fields` /
4
+ * `label` / `blocks` / `wordCount` read reactive state when you read them.
5
+ *
6
+ * A region is a `region` block at the root of the document or directly in the body of another region. Its BODY is the
7
+ * children of its block: any blocks, sub-regions included.
8
+ */
9
+ export class Region {
10
+ /**
11
+ * @param {Regions} registry
12
+ * @param {{ block: RegionBlock, def: RegionTypeDef | undefined, type: string, level: number, position: number }} init
13
+ */
14
+ constructor(registry: Regions, { block, def, type, level, position }: {
15
+ block: RegionBlock;
16
+ def: RegionTypeDef | undefined;
17
+ type: string;
18
+ level: number;
19
+ position: number;
20
+ });
21
+ /** The region block. @type {RegionBlock} */
22
+ block: RegionBlock;
23
+ /** The declared type, or `undefined` when the block's type is not declared (a document from a richer editor). @type {RegionTypeDef | undefined} */
24
+ def: RegionTypeDef | undefined;
25
+ /** The region type id. */
26
+ type: string;
27
+ /** Depth, 1 = highest. A region whose type is not declared gets the deepest declared level. */
28
+ level: number;
29
+ /** Rank among all regions, in document order (0-based). */
30
+ position: number;
31
+ /** The number within its numbering scope (see `numbering` in ADR 016 §3.2), `null` when not numbered. @type {number | null} */
32
+ index: number | null;
33
+ /** The indexes of the numbered ancestors, then of this region ([2, 1]). @type {number[]} */
34
+ path: number[];
35
+ /** The nearest region ancestor. @type {Region | null} */ parent: Region | null;
36
+ /** The direct sub-regions, in order. @type {Region[]} */ children: Region[];
37
+ /** Alias of {@link Region#block}, from the time a region was a marker. @type {RegionBlock} */
38
+ get marker(): RegionBlock;
39
+ /** The path as text ("2.1"; empty when nothing on the way is numbered). */
40
+ get pathString(): string;
41
+ /** The fields of the region (reactive); `title` is the plain text of the header. @type {Record<string, any>} */
42
+ get fields(): Record<string, any>;
43
+ /** The title: the plain text of the header (reactive). */
44
+ get title(): string;
45
+ /** The label template applied ("1.1 The ferryman"). Reactive (the title). Empty when the template yields nothing. */
46
+ get label(): string;
47
+ /** The label template applied with an EMPTY title ("1.1", "Chapter 2"): what the seam shows before the editable title. Empty when the template holds only the title. */
48
+ get prefix(): string;
49
+ /** The direct children of the body (sub-regions included). Reactive. @type {Block[]} */
50
+ get blocks(): Block[];
51
+ /** The direct children of the body that are not regions: the region's own content. Reactive. @type {Block[]} */
52
+ get ownBlocks(): Block[];
53
+ /**
54
+ * Words of the body text, sub-regions' bodies included, summed from the blocks' own `wordCount` (no rendering). NO title is
55
+ * counted: neither this region's nor its sub-regions' (a region block's own `wordCount` is its body only). Reactive.
56
+ */
57
+ get wordCount(): number;
58
+ /** Words of the region's own content: body text outside sub-regions (their titles and bodies excluded, like every title). Reactive. */
59
+ get ownWordCount(): number;
60
+ /** Sets a field of the region (validated, own undo step); `title` replaces the header text. @param {string} name @param {unknown} value */
61
+ setField(name: string, value: unknown): boolean;
62
+ #private;
63
+ }
64
+ /**
65
+ * The content before the first root region: outside any region (ADR 018 §3.3).
66
+ */
67
+ export class Preamble {
68
+ /** @param {Block[]} blocks */
69
+ constructor(blocks: Block[]);
70
+ /** The root blocks before the first root region. @type {Block[]} */
71
+ blocks: Block[];
72
+ /** Root index just after the last block of the preamble (exclusive). */
73
+ get end(): number;
74
+ /** Words of the preamble (plain blocks: it holds no region, so no title). */
75
+ get wordCount(): number;
76
+ }
77
+ /**
78
+ * Document-level reactive registry of the regions (ADR 018 §3.4): a pure, derived view over the TREE (stores nothing in
79
+ * Loro). Lazy and cheap: ONE pass over the root blocks and the bodies of the regions builds everything, and it only re-runs
80
+ * when the STRUCTURE changes (a region inserted / moved / deleted / retyped, a child added to a region body), not when
81
+ * text is typed. Obtain it with `Regions.get(nabu)`; `RegionsExtension` also installs it as `nabu.regions`.
82
+ */
83
+ export class Regions {
84
+ /**
85
+ * @param {Nabu} nabu
86
+ * @returns {Regions}
87
+ */
88
+ static get(nabu: Nabu): Regions;
89
+ /** @param {Nabu} nabu @param {RegionsConfig} config */
90
+ constructor(nabu: Nabu, config: RegionsConfig);
91
+ /**
92
+ * System hook (`nabu.systems`, called when the editor mounts): follows the focus of the editor, for {@link Regions#caret}.
93
+ * Idempotent.
94
+ */
95
+ start(): void;
96
+ /** System hook: stops following the focus. */
97
+ stop(): void;
98
+ /** The resolved configuration (declared types). */
99
+ get config(): RegionsConfig;
100
+ /** Every region, in document order (pre-order of the tree). @type {Region[]} */
101
+ get list(): Region[];
102
+ /** The top-level regions; each one holds its `children`. @type {Region[]} */
103
+ get tree(): Region[];
104
+ /** The number of regions. */
105
+ get count(): number;
106
+ /**
107
+ * The root blocks before the first root region (the whole document when there is no region). They are outside any region.
108
+ * @type {Preamble}
109
+ */
110
+ get preamble(): Preamble;
111
+ /**
112
+ * The deepest region containing a block: its nearest region ancestor, the block itself when it is a region. `target` is a
113
+ * block or an `{ block }` position (as the selection offers); omitted = the block holding the selection anchor. `null`
114
+ * outside any region (the preamble) or when there is nothing to resolve.
115
+ * @param {Block | { block: Block } | null} [target]
116
+ * @returns {Region | null}
117
+ */
118
+ at(target?: Block | {
119
+ block: Block;
120
+ } | null): Region | null;
121
+ /**
122
+ * The region that shows the caret: the DEEPEST region holding the selection anchor (a title counts), only while the editor has
123
+ * the focus. `null` otherwise. Reactive; computed once for all the regions (each `Region.svelte` compares itself to it).
124
+ * @type {Region | null}
125
+ */
126
+ caret: Region | null;
127
+ /** The region of a region block. @param {Block | string} block @returns {Region | null} */
128
+ of(block: Block | string): Region | null;
129
+ /** The region after this one, in document order (a sub-region, then the following sibling…). @param {Region} region @returns {Region | null} */
130
+ next(region: Region): Region | null;
131
+ /** The region before this one, in document order. @param {Region} region @returns {Region | null} */
132
+ prev(region: Region): Region | null;
133
+ /** @param {Region} region @returns {Region | null} */
134
+ parentOf(region: Region): Region | null;
135
+ #private;
136
+ }
137
+ import type { RegionBlock } from "./region.svelte.js";
138
+ import type { RegionTypeDef } from "./types.js";
139
+ import type { Block } from "../block.svelte.js";
140
+ import type { RegionsConfig } from "./types.js";
141
+ import type { Nabu } from "../nabu.svelte.js";
@@ -0,0 +1,276 @@
1
+ import { formatLabel, resolveConfig } from "./types.js";
2
+
3
+ /**
4
+ * @import { Nabu } from "../nabu.svelte.js";
5
+ * @import { Block } from "../block.svelte.js";
6
+ * @import { RegionBlock } from "./region.svelte.js";
7
+ * @import { RegionsConfig, RegionTypeDef } from "./types.js";
8
+ */
9
+
10
+ /** Block type of a region (the registry detects regions by it, without importing the block class). */
11
+ const REGION_TYPE = 'region';
12
+
13
+ /**
14
+ * One division of the document, as computed by the registry (ADR 018 §3.4). A read-only VIEW over a region BLOCK, a container
15
+ * of the tree: the structure (`level`, `parent`, `children`, `index`, `path`) is derived from the tree; `title` / `fields` /
16
+ * `label` / `blocks` / `wordCount` read reactive state when you read them.
17
+ *
18
+ * A region is a `region` block at the root of the document or directly in the body of another region. Its BODY is the
19
+ * children of its block: any blocks, sub-regions included.
20
+ */
21
+ export class Region {
22
+ /**
23
+ * @param {Regions} registry
24
+ * @param {{ block: RegionBlock, def: RegionTypeDef | undefined, type: string, level: number, position: number }} init
25
+ */
26
+ constructor(registry, { block, def, type, level, position }) {
27
+ /** The region block. @type {RegionBlock} */
28
+ this.block = block;
29
+ /** The declared type, or `undefined` when the block's type is not declared (a document from a richer editor). @type {RegionTypeDef | undefined} */
30
+ this.def = def;
31
+ /** The region type id. */
32
+ this.type = type;
33
+ /** Depth, 1 = highest. A region whose type is not declared gets the deepest declared level. */
34
+ this.level = level;
35
+ /** Rank among all regions, in document order (0-based). */
36
+ this.position = position;
37
+ /** The number within its numbering scope (see `numbering` in ADR 016 §3.2), `null` when not numbered. @type {number | null} */
38
+ this.index = null;
39
+ /** The indexes of the numbered ancestors, then of this region ([2, 1]). @type {number[]} */
40
+ this.path = [];
41
+ /** The nearest region ancestor. @type {Region | null} */ this.parent = null;
42
+ /** The direct sub-regions, in order. @type {Region[]} */ this.children = [];
43
+ }
44
+
45
+ /** Alias of {@link Region#block}, from the time a region was a marker. @type {RegionBlock} */
46
+ get marker() { return this.block; }
47
+
48
+ /** The path as text ("2.1"; empty when nothing on the way is numbered). */
49
+ get pathString() { return this.path.join('.'); }
50
+
51
+ /** The fields of the region (reactive); `title` is the plain text of the header. @type {Record<string, any>} */
52
+ get fields() { return this.block.fields; }
53
+
54
+ /** The title: the plain text of the header (reactive). */
55
+ get title() { return this.block.title; }
56
+
57
+ /** @param {string} title */
58
+ #format(title) {
59
+ return formatLabel(this.def?.label ?? '{title}', {
60
+ index: this.index, path: this.pathString, title, type: this.type, level: this.level,
61
+ });
62
+ }
63
+
64
+ /** The label template applied ("1.1 The ferryman"). Reactive (the title). Empty when the template yields nothing. */
65
+ get label() { return this.#format(this.title); }
66
+
67
+ /** The label template applied with an EMPTY title ("1.1", "Chapter 2"): what the seam shows before the editable title. Empty when the template holds only the title. */
68
+ get prefix() { return this.#format(''); }
69
+
70
+ /** The direct children of the body (sub-regions included). Reactive. @type {Block[]} */
71
+ get blocks() { return this.block.children; }
72
+
73
+ /** The direct children of the body that are not regions: the region's own content. Reactive. @type {Block[]} */
74
+ get ownBlocks() { return this.block.children.filter((b) => b.type !== REGION_TYPE); }
75
+
76
+ /**
77
+ * Words of the body text, sub-regions' bodies included, summed from the blocks' own `wordCount` (no rendering). NO title is
78
+ * counted: neither this region's nor its sub-regions' (a region block's own `wordCount` is its body only). Reactive.
79
+ */
80
+ get wordCount() { return this.block.children.reduce((sum, b) => sum + (b.wordCount ?? 0), 0); }
81
+
82
+ /** Words of the region's own content: body text outside sub-regions (their titles and bodies excluded, like every title). Reactive. */
83
+ get ownWordCount() { return this.ownBlocks.reduce((sum, b) => sum + (b.wordCount ?? 0), 0); }
84
+
85
+ /** Sets a field of the region (validated, own undo step); `title` replaces the header text. @param {string} name @param {unknown} value */
86
+ setField(name, value) { return this.block.setField(name, value); }
87
+ }
88
+
89
+ /**
90
+ * The content before the first root region: outside any region (ADR 018 §3.3).
91
+ */
92
+ export class Preamble {
93
+ /** @param {Block[]} blocks */
94
+ constructor(blocks) {
95
+ /** The root blocks before the first root region. @type {Block[]} */
96
+ this.blocks = blocks;
97
+ }
98
+ /** Root index just after the last block of the preamble (exclusive). */
99
+ get end() { return this.blocks.length; }
100
+ /** Words of the preamble (plain blocks: it holds no region, so no title). */
101
+ get wordCount() { return this.blocks.reduce((sum, b) => sum + (b.wordCount ?? 0), 0); }
102
+ }
103
+
104
+ /**
105
+ * Document-level reactive registry of the regions (ADR 018 §3.4): a pure, derived view over the TREE (stores nothing in
106
+ * Loro). Lazy and cheap: ONE pass over the root blocks and the bodies of the regions builds everything, and it only re-runs
107
+ * when the STRUCTURE changes (a region inserted / moved / deleted / retyped, a child added to a region body), not when
108
+ * text is typed. Obtain it with `Regions.get(nabu)`; `RegionsExtension` also installs it as `nabu.regions`.
109
+ */
110
+ export class Regions {
111
+ /** @type {Nabu} */ #nabu;
112
+ /** @type {RegionsConfig} */ #config;
113
+
114
+ /** Whether the editor of this registry holds the focus (set by `focusin` / `focusout`, see {@link Regions#start}). */
115
+ #focused = $state(false);
116
+ /** @type {(() => void) | null} */
117
+ #stopFocus = null;
118
+
119
+ /** @param {Nabu} nabu @param {RegionsConfig} config */
120
+ constructor(nabu, config) { this.#nabu = nabu; this.#config = config; }
121
+
122
+ /**
123
+ * System hook (`nabu.systems`, called when the editor mounts): follows the focus of the editor, for {@link Regions#caret}.
124
+ * Idempotent.
125
+ */
126
+ start() {
127
+ if (this.#stopFocus || typeof document === 'undefined') return;
128
+ // MY root only: the focus in another editor of the page (a neighbour, a nested one) is not "the focus is in this editor".
129
+ /** @param {EventTarget | null} target */
130
+ const inEditor = (target) => target instanceof Node && this.#nabu.selection.owns(target);
131
+ const onIn = (/** @type {FocusEvent} */ e) => this.#setFocused(inEditor(e.target));
132
+ const onOut = (/** @type {FocusEvent} */ e) => this.#setFocused(inEditor(e.relatedTarget)); // no related target: the focus left the page or went nowhere
133
+ document.addEventListener('focusin', onIn);
134
+ document.addEventListener('focusout', onOut);
135
+ this.#focused = inEditor(document.activeElement);
136
+ this.#stopFocus = () => { document.removeEventListener('focusin', onIn); document.removeEventListener('focusout', onOut); };
137
+ }
138
+
139
+ /** The latest value asked of {@link Regions#setFocused}, written by the microtask that is already scheduled (null: none). @type {boolean | null} */
140
+ #focusWanted = null;
141
+
142
+ /**
143
+ * Write `#focused` from a focus event, DEFERRED to a microtask. The browser fires `focusout` synchronously when a focused node
144
+ * is removed from the DOM, i.e. possibly in the middle of Svelte's own work (an effect teardown, a block being destroyed): a
145
+ * `$state` write there throws `state_unsafe_mutation`. The value is decided now (the event's targets), only the write waits;
146
+ * the last value asked wins, so an out-then-in of one focus move ends on "in".
147
+ * @param {boolean} value
148
+ */
149
+ #setFocused(value) {
150
+ const scheduled = this.#focusWanted !== null;
151
+ this.#focusWanted = value;
152
+ if (scheduled) return;
153
+ queueMicrotask(() => {
154
+ const wanted = this.#focusWanted;
155
+ this.#focusWanted = null;
156
+ if (wanted !== null && wanted !== this.#focused) this.#focused = wanted;
157
+ });
158
+ }
159
+
160
+ /** System hook: stops following the focus. */
161
+ stop() {
162
+ this.#stopFocus?.();
163
+ this.#stopFocus = null;
164
+ this.#setFocused(false); // deferred as well: `stop` runs in an effect teardown
165
+ }
166
+
167
+ /**
168
+ * @param {Nabu} nabu
169
+ * @returns {Regions}
170
+ */
171
+ static get(nabu) {
172
+ if (!nabu.systems.has('regions')) {
173
+ const config = /** @type {any} */ (nabu.registry.get(REGION_TYPE))?.config ?? resolveConfig();
174
+ nabu.systems.set('regions', new Regions(nabu, config));
175
+ }
176
+ return /** @type {Regions} */ (nabu.systems.get('regions'));
177
+ }
178
+
179
+ /** The resolved configuration (declared types). */
180
+ get config() { return this.#config; }
181
+
182
+ /** The one pass. @type {{ list: Region[], tree: Region[], preamble: Preamble, byId: Map<string, Region> }} */
183
+ #structure = $derived.by(() => {
184
+ /** @type {Region[]} */ const list = [];
185
+ /** @type {Region[]} */ const tree = [];
186
+ /** @type {Map<string, Region>} */ const byId = new Map();
187
+ /** @type {Map<string, number>} */ const counters = new Map(); // numbering: per (parent, type) or per type
188
+ const roots = this.#nabu.children;
189
+
190
+ /** @param {Block[]} blocks @param {Region | null} parent */
191
+ const walk = (blocks, parent) => {
192
+ for (const block of blocks) {
193
+ if (block.type !== REGION_TYPE) continue; // a region lives at the root or in a region body, nowhere else
194
+ const regionBlock = /** @type {RegionBlock} */ (block);
195
+ const type = regionBlock.regionType;
196
+ const def = this.#config.types.get(type);
197
+ const level = def ? def.level : this.#config.maxLevel;
198
+ const region = new Region(this, { block: regionBlock, def, type, level, position: list.length });
199
+ region.parent = parent;
200
+ (parent ? parent.children : tree).push(region);
201
+
202
+ const numbering = def ? def.numbering : 'none';
203
+ if (numbering !== 'none') {
204
+ const key = numbering === 'document' ? `d|${type}` : `p|${parent ? parent.position : -1}|${type}`;
205
+ region.index = (counters.get(key) ?? 0) + 1;
206
+ counters.set(key, region.index);
207
+ }
208
+ region.path = [...(parent ? parent.path : []), ...(region.index == null ? [] : [region.index])];
209
+
210
+ list.push(region);
211
+ byId.set(block.id, region);
212
+ walk(regionBlock.children, region);
213
+ }
214
+ };
215
+ walk(roots, null);
216
+
217
+ const firstRegion = roots.findIndex((b) => b.type === REGION_TYPE);
218
+ return { list, tree, preamble: new Preamble(firstRegion < 0 ? [...roots] : roots.slice(0, firstRegion)), byId };
219
+ });
220
+
221
+ /** Every region, in document order (pre-order of the tree). @type {Region[]} */
222
+ get list() { return this.#structure.list; }
223
+
224
+ /** The top-level regions; each one holds its `children`. @type {Region[]} */
225
+ get tree() { return this.#structure.tree; }
226
+
227
+ /** The number of regions. */
228
+ get count() { return this.#structure.list.length; }
229
+
230
+ /**
231
+ * The root blocks before the first root region (the whole document when there is no region). They are outside any region.
232
+ * @type {Preamble}
233
+ */
234
+ get preamble() { return this.#structure.preamble; }
235
+
236
+ /**
237
+ * The deepest region containing a block: its nearest region ancestor, the block itself when it is a region. `target` is a
238
+ * block or an `{ block }` position (as the selection offers); omitted = the block holding the selection anchor. `null`
239
+ * outside any region (the preamble) or when there is nothing to resolve.
240
+ * @param {Block | { block: Block } | null} [target]
241
+ * @returns {Region | null}
242
+ */
243
+ at(target) {
244
+ /** @type {Block | null | undefined} */
245
+ let block = target === undefined
246
+ ? this.#nabu.selection.anchorBlock
247
+ : (target && typeof (/** @type {any} */ (target)).id === 'string' ? /** @type {Block} */ (target) : /** @type {any} */ (target)?.block);
248
+ const { byId } = this.#structure;
249
+ for (; block; block = /** @type {Block | null} */ (block.parent)) {
250
+ const region = byId.get(block.id);
251
+ if (region) return region;
252
+ }
253
+ return null;
254
+ }
255
+
256
+ /**
257
+ * The region that shows the caret: the DEEPEST region holding the selection anchor (a title counts), only while the editor has
258
+ * the focus. `null` otherwise. Reactive; computed once for all the regions (each `Region.svelte` compares itself to it).
259
+ * @type {Region | null}
260
+ */
261
+ caret = $derived.by(() => (this.#focused ? this.at() : null));
262
+
263
+ /** The region of a region block. @param {Block | string} block @returns {Region | null} */
264
+ of(block) {
265
+ return this.#structure.byId.get(typeof block === 'string' ? block : block.id) ?? null;
266
+ }
267
+
268
+ /** The region after this one, in document order (a sub-region, then the following sibling…). @param {Region} region @returns {Region | null} */
269
+ next(region) { return this.#structure.list[region.position + 1] ?? null; }
270
+
271
+ /** The region before this one, in document order. @param {Region} region @returns {Region | null} */
272
+ prev(region) { return this.#structure.list[region.position - 1] ?? null; }
273
+
274
+ /** @param {Region} region @returns {Region | null} */
275
+ parentOf(region) { return region.parent; }
276
+ }
@@ -0,0 +1,57 @@
1
+ /** The heading level of a region type: its level clamped to 1..6 (the deepest declared level for an undeclared type). @param {RegionsConfig} config @param {string} regionType @returns {number} */
2
+ export function regionHeadingLevel(config: RegionsConfig, regionType: string): number;
3
+ /**
4
+ * Whether the closing comment must be written for a region of type `regionType` followed by `follow`: unless nothing follows,
5
+ * or a region of the same or a higher level opens (reading it closes this region), what follows would be read INSIDE the
6
+ * region. A region whose parent closes (`'close'`) is closed too: the parent's closing comment would close it otherwise.
7
+ * @param {RegionsConfig} config @param {string} regionType @param {Follow} follow
8
+ * @returns {boolean}
9
+ */
10
+ export function closingNeeded(config: RegionsConfig, regionType: string, follow: Follow): boolean;
11
+ /**
12
+ * What follows a region in the text, from its following siblings (as markdown) and what follows its parent.
13
+ * @param {Iterable<{ region: number } | string>} after the following siblings, nearest first, read lazily: a region is `{ region: level }`, anything else its markdown
14
+ * @param {Follow} tail what follows the parent (or the document: `'end'`)
15
+ * @returns {Follow}
16
+ */
17
+ export function followOf(after: Iterable<{
18
+ region: number;
19
+ } | string>, tail: Follow): Follow;
20
+ /** The type of some props: the given one, else the default type. @param {RegionsConfig} config @param {{ regionType?: string }} props @returns {string} */
21
+ export function regionTypeOf(config: RegionsConfig, props: {
22
+ regionType?: string;
23
+ }): string;
24
+ /**
25
+ * A region as markdown.
26
+ * @param {RegionsConfig} config
27
+ * @param {{ regionType?: string, fields?: Record<string, any> }} props
28
+ * @param {string} titleMarkdown the title as inline markdown
29
+ * @param {string[]} body the markdown of each body block
30
+ * @param {{ close?: boolean }} [options] `close`: write the closing comment (see {@link closingNeeded})
31
+ * @returns {string}
32
+ */
33
+ export function regionBlockToMarkdown(config: RegionsConfig, props: {
34
+ regionType?: string;
35
+ fields?: Record<string, any>;
36
+ }, titleMarkdown: string, body: string[], { close }?: {
37
+ close?: boolean;
38
+ }): string;
39
+ /**
40
+ * A region as HTML.
41
+ * @param {RegionsConfig} config
42
+ * @param {{ regionType?: string, fields?: Record<string, any> }} props
43
+ * @param {string} titleHtml the title as inline HTML (already escaped)
44
+ * @param {string[]} body the HTML of each body block
45
+ * @returns {string}
46
+ */
47
+ export function regionBlockToHtml(config: RegionsConfig, props: {
48
+ regionType?: string;
49
+ fields?: Record<string, any>;
50
+ }, titleHtml: string, body: string[]): string;
51
+ /**
52
+ * What comes right after a region in the text that is read back: `'end'` nothing (end of the document, or of the parent
53
+ * when the parent writes no closing comment of its own); `'close'` the closing comment of the parent; `'other'` any
54
+ * content that is not a region opening; a NUMBER the opening of a region of that level.
55
+ */
56
+ export type Follow = "end" | "close" | "other" | number;
57
+ import type { RegionsConfig } from "./types.js";
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Text forms of a region as a CONTAINER (ADR 018 §3.7): the markdown and HTML a region block is exported as, title and
3
+ * body included. Composes the helpers of format.js (data comment, typed fields, safe JSON); pure functions, no runes.
4
+ *
5
+ * MARKDOWN: the data comment, then the title as an ATX heading of the type's level (clamped to 1..6), then the body:
6
+ *
7
+ * <!-- region:chapter {"pov":"Anna"} -->
8
+ * ## The ferryman
9
+ *
10
+ * Opening of the first section.
11
+ *
12
+ * - an EMPTY title writes the comment alone (the body follows after a blank line); except when the body starts with a
13
+ * heading of its own, which would be read as the title: then a bare heading (`##`) stands for the empty title;
14
+ * - the END of a region is deduced when the markdown is read (next opening of the same or a higher level, end of the
15
+ * enclosing region, end of the document), so the closing comment `<!-- /region -->` is written ONLY when what follows
16
+ * the region would otherwise be swallowed by it: see {@link closingNeeded}.
17
+ *
18
+ * HTML: `<section data-nabu-region="chapter" data-nabu-fields='{…}'><h2>The ferryman</h2>…body…</section>`; always read back
19
+ * exactly, nothing to infer.
20
+ *
21
+ * @import { RegionsConfig } from "./types.js";
22
+ *
23
+ * @typedef {'end' | 'close' | 'other' | number} Follow
24
+ * What comes right after a region in the text that is read back: `'end'` nothing (end of the document, or of the parent
25
+ * when the parent writes no closing comment of its own); `'close'` the closing comment of the parent; `'other'` any
26
+ * content that is not a region opening; a NUMBER the opening of a region of that level.
27
+ */
28
+
29
+ import { escapeAttr } from '../../utils/html.js';
30
+ import { CLOSE_COMMENT, encodeTypeId, fieldsToText, headingLevel, levelOfType, safeJson } from './format.js';
31
+
32
+ /** The region type of some props: the given one, else the default type. @param {RegionsConfig} config @param {{ regionType?: string }} props @returns {string} */
33
+ function typeOf(config, props) {
34
+ return typeof props.regionType === 'string' && props.regionType ? props.regionType : config.defaultType;
35
+ }
36
+
37
+ /** The heading level of a region type: its level clamped to 1..6 (the deepest declared level for an undeclared type). @param {RegionsConfig} config @param {string} regionType @returns {number} */
38
+ export function regionHeadingLevel(config, regionType) {
39
+ return headingLevel(config.types.get(regionType)?.level ?? config.maxLevel);
40
+ }
41
+
42
+ /** The text-form fields: no `title` (it is the header, not data), richtext as inline markdown. @param {RegionsConfig} config @param {string} regionType @param {Record<string, any> | undefined} fields */
43
+ function dataFields(config, regionType, fields) {
44
+ const { title: _title, ...rest } = fields ?? {};
45
+ return fieldsToText(config, regionType, rest);
46
+ }
47
+
48
+ /**
49
+ * Whether the closing comment must be written for a region of type `regionType` followed by `follow`: unless nothing follows,
50
+ * or a region of the same or a higher level opens (reading it closes this region), what follows would be read INSIDE the
51
+ * region. A region whose parent closes (`'close'`) is closed too: the parent's closing comment would close it otherwise.
52
+ * @param {RegionsConfig} config @param {string} regionType @param {Follow} follow
53
+ * @returns {boolean}
54
+ */
55
+ export function closingNeeded(config, regionType, follow) {
56
+ if (follow === 'end') return false;
57
+ if (typeof follow === 'number') return follow > levelOfType(config, regionType);
58
+ return true;
59
+ }
60
+
61
+ /**
62
+ * What follows a region in the text, from its following siblings (as markdown) and what follows its parent.
63
+ * @param {Iterable<{ region: number } | string>} after the following siblings, nearest first, read lazily: a region is `{ region: level }`, anything else its markdown
64
+ * @param {Follow} tail what follows the parent (or the document: `'end'`)
65
+ * @returns {Follow}
66
+ */
67
+ export function followOf(after, tail) {
68
+ for (const next of after) {
69
+ if (typeof next === 'string') { if (next) return 'other'; continue; }
70
+ return next.region;
71
+ }
72
+ return tail;
73
+ }
74
+
75
+ /** The type of some props: the given one, else the default type. @param {RegionsConfig} config @param {{ regionType?: string }} props @returns {string} */
76
+ export function regionTypeOf(config, props) {
77
+ return typeOf(config, props);
78
+ }
79
+
80
+ /**
81
+ * A region as markdown.
82
+ * @param {RegionsConfig} config
83
+ * @param {{ regionType?: string, fields?: Record<string, any> }} props
84
+ * @param {string} titleMarkdown the title as inline markdown
85
+ * @param {string[]} body the markdown of each body block
86
+ * @param {{ close?: boolean }} [options] `close`: write the closing comment (see {@link closingNeeded})
87
+ * @returns {string}
88
+ */
89
+ export function regionBlockToMarkdown(config, props, titleMarkdown, body, { close = false } = {}) {
90
+ const regionType = typeOf(config, props);
91
+ const comment = `<!-- region:${encodeTypeId(regionType)} ${safeJson(dataFields(config, regionType, props.fields))} -->`;
92
+ const hashes = '#'.repeat(regionHeadingLevel(config, regionType));
93
+ const blocks = body.filter(Boolean);
94
+ // An empty title is the comment alone, unless the body opens with a heading (it would be read as the title).
95
+ const heading = titleMarkdown ? `${hashes} ${titleMarkdown}` : /^ {0,3}#{1,6}(?:[ \t]|$)/.test(blocks[0] ?? '') ? hashes : '';
96
+ return [heading ? `${comment}\n${heading}` : comment, ...blocks, ...(close ? [CLOSE_COMMENT] : [])].join('\n\n');
97
+ }
98
+
99
+ /**
100
+ * A region as HTML.
101
+ * @param {RegionsConfig} config
102
+ * @param {{ regionType?: string, fields?: Record<string, any> }} props
103
+ * @param {string} titleHtml the title as inline HTML (already escaped)
104
+ * @param {string[]} body the HTML of each body block
105
+ * @returns {string}
106
+ */
107
+ export function regionBlockToHtml(config, props, titleHtml, body) {
108
+ const regionType = typeOf(config, props);
109
+ const level = regionHeadingLevel(config, regionType);
110
+ const json = safeJson(dataFields(config, regionType, props.fields));
111
+ return `<section data-nabu-region="${escapeAttr(regionType)}" data-nabu-fields='${escapeAttr(json)}'><h${level}>${titleHtml}</h${level}>${body.join('')}</section>`;
112
+ }