@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
@@ -1,64 +1,7 @@
1
1
  import { Extension } from '../utils/extensions.js';
2
+ import { parseMarkdownInline, parseMarkdownSegments, extractLinkDefinitions } from './markdown-inline.js';
2
3
 
3
- // ── Inline mark patterns — ordered by specificity (longest patterns first) ───
4
- // Each entry: { re (non-global, used for leftmost-match search), marks }
5
- const INLINE_PATTERNS = [
6
- { re: /\*\*\*(.+?)\*\*\*/s, marks: { bold: true, italic: true } },
7
- { re: /\*\*(.+?)\*\*/s, marks: { bold: true } },
8
- { re: /\*(.+?)\*/s, marks: { italic: true } },
9
- { re: /~~(.+?)~~/s, marks: { strikethrough: true } },
10
- { re: /`([^`]+)`/, marks: { code: true } },
11
- ];
12
-
13
- /**
14
- * Parse inline markdown text into a Loro-compatible delta array.
15
- * Uses a leftmost-match recursive strategy so nested marks work naturally:
16
- * **bold *italic* still bold** → [bold, bold+italic, bold]
17
- *
18
- * @param {string} text
19
- * @param {Record<string, true>} [marks]
20
- * @returns {import('loro-crdt').Delta<string>[]}
21
- */
22
- export function parseMarkdownInline(text, marks = {}) {
23
- if (!text) return [];
24
-
25
- // Find the leftmost (earliest index) pattern match across all patterns
26
- let earliest = /** @type {RegExpExecArray | null} */ (null);
27
- let earliestMarks = /** @type {Record<string, true>} */ ({});
28
-
29
- for (const pat of INLINE_PATTERNS) {
30
- const m = pat.re.exec(text);
31
- if (m && (earliest === null || m.index < earliest.index)) {
32
- earliest = m;
33
- earliestMarks = pat.marks;
34
- }
35
- }
36
-
37
- if (!earliest) {
38
- // No marks — emit plain text (with inherited marks)
39
- return [
40
- Object.keys(marks).length
41
- ? { insert: text, attributes: { ...marks } }
42
- : { insert: text }
43
- ];
44
- }
45
-
46
- const ops = /** @type {import('loro-crdt').Delta<string>[]} */ ([]);
47
-
48
- // Plain text before the match
49
- if (earliest.index > 0) {
50
- ops.push(...parseMarkdownInline(text.slice(0, earliest.index), marks));
51
- }
52
-
53
- // The matched span — recurse with merged marks
54
- ops.push(...parseMarkdownInline(earliest[1], { ...marks, ...earliestMarks }));
55
-
56
- // Text after the match
57
- const after = text.slice(earliest.index + earliest[0].length);
58
- if (after) ops.push(...parseMarkdownInline(after, marks));
59
-
60
- return ops;
61
- }
4
+ export { parseMarkdownInline, parseMarkdownSegments } from './markdown-inline.js';
62
5
 
63
6
  /**
64
7
  * Build the ordered markdown ruleset from all BlockClasses registered in nabu.
@@ -71,7 +14,7 @@ function buildMarkdownRuleset(nabu) {
71
14
  /** @type {Array<{ BlockClass: any, rule: any }>} */
72
15
  const ruleset = [];
73
16
  for (const [, BlockClass] of nabu.registry) {
74
- const rules = BlockClass.markdownRules;
17
+ const rules = /** @type {{ markdownRules?: unknown }} */ (BlockClass).markdownRules;
75
18
  if (!Array.isArray(rules)) continue;
76
19
  for (const rule of rules) ruleset.push({ BlockClass, rule });
77
20
  }
@@ -79,6 +22,9 @@ function buildMarkdownRuleset(nabu) {
79
22
  return ruleset;
80
23
  }
81
24
 
25
+ /**
26
+ * Paste of markdown typed as `text/plain`: parses it into blocks and inline marks through the rules the registered block classes declare (`markdownRules`). It also powers `importMarkdown()`.
27
+ */
82
28
  export const MarkdownPasteExtension = new Extension('markdown-paste', {
83
29
  pasteInterpreters: [
84
30
  {
@@ -87,49 +33,87 @@ export const MarkdownPasteExtension = new Extension('markdown-paste', {
87
33
  /**
88
34
  * @param {string} raw
89
35
  * @param {import('../blocks/nabu.svelte.js').Nabu} nabu
36
+ * @param {import('../utils/extensions.js').PasteInterpretOptions} [options]
90
37
  * @returns {import('../utils/extensions.js').PasteFragment | null}
91
38
  */
92
- interpret(raw, nabu) {
39
+ interpret(raw, nabu, options) {
93
40
  try {
94
- const lines = raw.split(/\r?\n/);
41
+ // BOM stripped, CRLF / CR normalised to LF. Link reference definitions (`[ref]: url`) are collected
42
+ // first and produce no block.
43
+ const { lines, resolveRef } = extractLinkDefinitions(raw.replace(/^\uFEFF/, '').split(/\r\n|\r|\n/));
95
44
  const ruleset = buildMarkdownRuleset(nabu);
96
45
  if (!ruleset.length) return null;
97
46
 
98
- const helpers = { parseInline: parseMarkdownInline };
99
-
100
- /** @type {import('../utils/extensions.js').PasteBlock[]} */
101
- const blocks = [];
102
- let i = 0;
103
-
104
- while (i < lines.length) {
105
- // Skip blank lines between blocks
106
- if (!lines[i].trim()) { i++; continue; }
107
-
108
- let matched = false;
109
- for (const { BlockClass, rule } of ruleset) {
110
- // Normalize detect: regex or function
111
- if (rule.detect instanceof RegExp) rule.detect.lastIndex = 0;
112
- const detects = rule.detect instanceof RegExp
113
- ? rule.detect.test(lines[i])
114
- : rule.detect(lines[i], lines, i);
115
-
116
- if (!detects) continue;
117
-
118
- const count = rule.consume(lines, i);
119
- const consumed = lines.slice(i, i + count);
120
- const pb = BlockClass.fromMarkdown(consumed, helpers, rule);
121
-
122
- if (pb) {
123
- blocks.push(pb);
124
- i += count;
125
- matched = true;
126
- break;
47
+ const inlineOptions = { resolveRef };
48
+ const joinLines = !!options?.joinLines;
49
+ // The rules that start a block in the middle of a paragraph (CommonMark): a fence, a heading, a quote, a list…
50
+ const interrupters = ruleset.filter(({ rule }) => rule.interruptsParagraph);
51
+
52
+ /**
53
+ * The helpers a rule receives. `overrides` change the parsing mode for a nested parse (`parseBlocks`).
54
+ * @param {{ joinLines?: boolean, softBreak?: string }} overrides
55
+ * @returns {any}
56
+ */
57
+ const makeHelpers = (overrides) => {
58
+ /** @type {any} */
59
+ const helpers = {
60
+ parseInline: (/** @type {string} */ text) => parseMarkdownInline(text, {}, inlineOptions),
61
+ parseInlineSegments: (/** @type {string} */ text) => parseMarkdownSegments(text, inlineOptions),
62
+ joinLines,
63
+ ...overrides,
64
+ };
65
+ // Does line `k` start a block that ends the paragraph above it?
66
+ helpers.interrupts = (/** @type {string[]} */ ls, /** @type {number} */ k) => interrupters.some(({ rule }) =>
67
+ rule.interruptsParagraph === true ? detects(rule, ls, k, helpers) : rule.interruptsParagraph(ls[k], ls, k, helpers));
68
+ // Parse lines as a document of their own (the content of a quote…).
69
+ helpers.parseBlocks = (/** @type {string[]} */ ls, /** @type {object} */ over = {}) => parseBlocks(ls, makeHelpers({ ...overrides, ...over }));
70
+ return helpers;
71
+ };
72
+
73
+ /** @returns {boolean} */
74
+ const detects = (/** @type {any} */ rule, /** @type {string[]} */ ls, /** @type {number} */ k, /** @type {any} */ helpers) => {
75
+ // Normalize detect: regex or function
76
+ if (rule.detect instanceof RegExp) { rule.detect.lastIndex = 0; return rule.detect.test(ls[k]); }
77
+ return !!rule.detect(ls[k], ls, k, helpers);
78
+ };
79
+
80
+ /**
81
+ * @param {string[]} ls
82
+ * @param {any} helpers
83
+ * @returns {import('../utils/extensions.js').PasteBlock[]}
84
+ */
85
+ const parseBlocks = (ls, helpers) => {
86
+ /** @type {import('../utils/extensions.js').PasteBlock[]} */
87
+ const blocks = [];
88
+ let i = 0;
89
+
90
+ while (i < ls.length) {
91
+ // Skip blank lines between blocks
92
+ if (!ls[i].trim()) { i++; continue; }
93
+
94
+ let matched = false;
95
+ for (const { BlockClass, rule } of ruleset) {
96
+ if (!detects(rule, ls, i, helpers)) continue;
97
+
98
+ const count = Math.max(1, rule.consume(ls, i, helpers));
99
+ const consumed = ls.slice(i, i + count);
100
+ const pb = BlockClass.fromMarkdown(consumed, helpers, rule);
101
+
102
+ if (pb) {
103
+ if (Array.isArray(pb)) blocks.push(...pb); // a paragraph split around inline images, a table, several lists
104
+ else blocks.push(pb);
105
+ i += count;
106
+ matched = true;
107
+ break;
108
+ }
127
109
  }
128
- }
129
110
 
130
- if (!matched) i++; // safety: skip unrecognized line
131
- }
111
+ if (!matched) i++; // safety: skip unrecognized line
112
+ }
113
+ return blocks;
114
+ };
132
115
 
116
+ const blocks = parseBlocks(lines, makeHelpers({}));
133
117
  return blocks.length ? { blocks } : null;
134
118
  } catch {
135
119
  return null;
@@ -1,2 +1,5 @@
1
+ /**
2
+ * Paste of `application/x-nabu+json`, the format Nabu writes on copy and cut: copying between Nabu editors keeps the block structure, marks and files.
3
+ */
1
4
  export const NabuPasteExtension: Extension;
2
5
  import { Extension } from '../utils/extensions.js';
@@ -1,15 +1,41 @@
1
- import { Extension } from '../utils/extensions.js';
1
+ import { Extension, CLIPBOARD_FORMAT_VERSION } from '../utils/extensions.js';
2
2
 
3
+ /**
4
+ * The version of a fragment: the `version` field (a number, or its string form "1"); a fragment without one is
5
+ * a version-1 fragment (the format shipped without it). NaN for anything else.
6
+ * @param {any} fragment
7
+ * @returns {number}
8
+ */
9
+ const versionOf = (fragment) => {
10
+ const v = fragment?.version;
11
+ if (v === undefined || v === null) return 1;
12
+ return typeof v === 'number' || typeof v === 'string' ? Number(v) : NaN;
13
+ };
14
+
15
+ /**
16
+ * Paste of `application/x-nabu+json`, the format Nabu writes on copy and cut: copying between Nabu editors keeps the block structure, marks and files.
17
+ */
3
18
  export const NabuPasteExtension = new Extension('nabu-paste', {
4
19
  pasteInterpreters: [
5
20
  {
6
21
  format: 'application/x-nabu+json',
7
22
  priority: 100,
8
- /** @param {string} raw */
9
- interpret(raw) {
23
+ /**
24
+ * Accepts format version 1 (written as `version: 1`, or without `version`). A fragment from a NEWER
25
+ * format is not mis-parsed: the interpreter declines (`null` + `nabu.warn`), and the paste falls back
26
+ * to the next interpreter (`text/html`, then `text/plain`) of the same clipboard.
27
+ * @param {string} raw
28
+ * @param {import('../blocks/nabu.svelte.js').Nabu} [nabu]
29
+ */
30
+ interpret(raw, nabu) {
10
31
  try {
11
32
  const fragment = JSON.parse(raw);
12
33
  if (!fragment?.blocks?.length) return null;
34
+ const version = versionOf(fragment);
35
+ if (version !== CLIPBOARD_FORMAT_VERSION) {
36
+ nabu?.warn(`paste: unsupported application/x-nabu+json version "${fragment.version}" (this editor reads version ${CLIPBOARD_FORMAT_VERSION}): falling back to the next format`);
37
+ return null;
38
+ }
13
39
  return fragment;
14
40
  } catch {
15
41
  return null;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Interprète text/plain comme du texte pur.
3
- * - Double saut de ligne (\n\n) → blocs séparés
4
- * - Saut simple (\n) → soft break dans le même bloc
3
+ * - Chaque ligne non vide → un bloc (une ligne = un bloc)
4
+ * - Les lignes vides ne sont que des séparateurs (aucun paragraphe vide)
5
5
  * Priorité 0 (fallback de dernier recours).
6
6
  */
7
7
  export const PlainTextPasteExtension: Extension;
@@ -2,8 +2,8 @@ import { Extension } from '../utils/extensions.js';
2
2
 
3
3
  /**
4
4
  * Interprète text/plain comme du texte pur.
5
- * - Double saut de ligne (\n\n) → blocs séparés
6
- * - Saut simple (\n) → soft break dans le même bloc
5
+ * - Chaque ligne non vide → un bloc (une ligne = un bloc)
6
+ * - Les lignes vides ne sont que des séparateurs (aucun paragraphe vide)
7
7
  * Priorité 0 (fallback de dernier recours).
8
8
  */
9
9
  export const PlainTextPasteExtension = new Extension('plain-text-paste', {
@@ -16,7 +16,7 @@ export const PlainTextPasteExtension = new Extension('plain-text-paste', {
16
16
  if (!raw) return null;
17
17
 
18
18
  const normalized = raw.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
19
- const paragraphs = normalized.split(/\n\n+/).filter(Boolean);
19
+ const paragraphs = normalized.split('\n').filter(line => line.trim());
20
20
  if (paragraphs.length === 0) return null;
21
21
 
22
22
  if (paragraphs.length === 1) {
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @typedef {import('../files/store.js').AssetStore} AssetStore
3
+ * @typedef {import('../files/store.js').AssetMeta} AssetMeta
4
+ */
5
+ /**
6
+ * The {@link AssetStore} of a persisted editor (ADR 014 §3.3): database `nabu`, object store `assets` (`{ hash, blob, mime,
7
+ * name, size, savedAt }`), SHARED by every document of the origin — the same image used in two documents is stored once.
8
+ * A second object store, `docRefs` (`{ docId, hashes }`), records which files each document references; the persisted
9
+ * editor refreshes it on every save, and the cleanup (compaction, `clearPersistence()`) only deletes a file no document references.
10
+ *
11
+ * Bytes are stored as `Blob`s (the browser keeps them on disk, not in memory).
12
+ * @implements {AssetStore}
13
+ */
14
+ export class IndexedDBAssetStore implements AssetStore {
15
+ /** @param {string} hash @returns {Promise<boolean>} */
16
+ has(hash: string): Promise<boolean>;
17
+ /** @param {string} hash @returns {Promise<Blob | null>} */
18
+ get(hash: string): Promise<Blob | null>;
19
+ /** @param {string} hash @param {Blob} blob @param {AssetMeta} meta @returns {Promise<void>} */
20
+ put(hash: string, blob: Blob, meta: AssetMeta): Promise<void>;
21
+ /** @param {string} hash @returns {Promise<void>} */
22
+ delete(hash: string): Promise<void>;
23
+ /** @returns {AsyncGenerator<string>} */
24
+ keys(): AsyncGenerator<string>;
25
+ /** @param {string} hash @returns {Promise<AssetMeta | null>} */
26
+ stat(hash: string): Promise<AssetMeta | null>;
27
+ /**
28
+ * When the file was last stored or re-added (`touch`), `null` if unknown (no record, or a record without `savedAt`).
29
+ * @param {string} hash @returns {Promise<number | null>}
30
+ */
31
+ savedAt(hash: string): Promise<number | null>;
32
+ /**
33
+ * A file that is already stored is added again (deduplication): refresh its `savedAt`, so that a release in another tab keeps
34
+ * it during the grace period even though that tab does not see the new reference yet. One transaction.
35
+ * @param {string} hash @returns {Promise<void>}
36
+ */
37
+ touch(hash: string): Promise<void>;
38
+ /**
39
+ * Record the files `docId` references (replaces the previous list).
40
+ * @param {string} docId @param {Iterable<string>} hashes @returns {Promise<void>}
41
+ */
42
+ setDocRefs(docId: string, hashes: Iterable<string>): Promise<void>;
43
+ /** @param {string} docId @returns {Promise<Set<string>>} */
44
+ getDocRefs(docId: string): Promise<Set<string>>;
45
+ /** @param {string} docId @returns {Promise<void>} */
46
+ clearDocRefs(docId: string): Promise<void>;
47
+ /** Every file referenced by at least one document. @returns {Promise<Set<string>>} */
48
+ referencedHashes(): Promise<Set<string>>;
49
+ }
50
+ export type AssetStore = import("../files/store.js").AssetStore;
51
+ export type AssetMeta = import("../files/store.js").AssetMeta;
@@ -0,0 +1,103 @@
1
+ import { ASSETS_STORE, DOC_REFS_STORE, withStore, openDB } from './indexeddb.js';
2
+
3
+ /**
4
+ * @typedef {import('../files/store.js').AssetStore} AssetStore
5
+ * @typedef {import('../files/store.js').AssetMeta} AssetMeta
6
+ */
7
+
8
+ /**
9
+ * The {@link AssetStore} of a persisted editor (ADR 014 §3.3): database `nabu`, object store `assets` (`{ hash, blob, mime,
10
+ * name, size, savedAt }`), SHARED by every document of the origin — the same image used in two documents is stored once.
11
+ * A second object store, `docRefs` (`{ docId, hashes }`), records which files each document references; the persisted
12
+ * editor refreshes it on every save, and the cleanup (compaction, `clearPersistence()`) only deletes a file no document references.
13
+ *
14
+ * Bytes are stored as `Blob`s (the browser keeps them on disk, not in memory).
15
+ * @implements {AssetStore}
16
+ */
17
+ export class IndexedDBAssetStore {
18
+ /** @param {string} hash @returns {Promise<boolean>} */
19
+ async has(hash) {
20
+ return (await withStore(ASSETS_STORE, 'readonly', (s) => s.count(hash))) > 0;
21
+ }
22
+
23
+ /** @param {string} hash @returns {Promise<Blob | null>} */
24
+ async get(hash) {
25
+ const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
26
+ return record?.blob ?? null;
27
+ }
28
+
29
+ /** @param {string} hash @param {Blob} blob @param {AssetMeta} meta @returns {Promise<void>} */
30
+ async put(hash, blob, meta) {
31
+ await withStore(ASSETS_STORE, 'readwrite', (s) => s.put({ hash, blob, mime: meta.mime, name: meta.name, size: meta.size, savedAt: Date.now() }));
32
+ }
33
+
34
+ /** @param {string} hash @returns {Promise<void>} */
35
+ async delete(hash) {
36
+ await withStore(ASSETS_STORE, 'readwrite', (s) => s.delete(hash));
37
+ }
38
+
39
+ /** @returns {AsyncGenerator<string>} */
40
+ async *keys() {
41
+ const all = /** @type {IDBValidKey[]} */ (await withStore(ASSETS_STORE, 'readonly', (s) => s.getAllKeys()));
42
+ for (const key of all) yield String(key);
43
+ }
44
+
45
+ /** @param {string} hash @returns {Promise<AssetMeta | null>} */
46
+ async stat(hash) {
47
+ const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
48
+ return record ? { mime: record.mime, name: record.name, size: record.size } : null;
49
+ }
50
+
51
+ /**
52
+ * When the file was last stored or re-added (`touch`), `null` if unknown (no record, or a record without `savedAt`).
53
+ * @param {string} hash @returns {Promise<number | null>}
54
+ */
55
+ async savedAt(hash) {
56
+ const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
57
+ return typeof record?.savedAt === 'number' ? record.savedAt : null;
58
+ }
59
+
60
+ /**
61
+ * A file that is already stored is added again (deduplication): refresh its `savedAt`, so that a release in another tab keeps
62
+ * it during the grace period even though that tab does not see the new reference yet. One transaction.
63
+ * @param {string} hash @returns {Promise<void>}
64
+ */
65
+ async touch(hash) {
66
+ const db = await openDB();
67
+ await new Promise((resolve, reject) => {
68
+ const tx = db.transaction(ASSETS_STORE, 'readwrite');
69
+ const store = tx.objectStore(ASSETS_STORE);
70
+ const get = store.get(hash);
71
+ get.onsuccess = () => { if (get.result) store.put({ ...get.result, savedAt: Date.now() }); };
72
+ tx.oncomplete = () => { db.close(); resolve(undefined); };
73
+ tx.onabort = tx.onerror = () => { db.close(); reject(tx.error); };
74
+ });
75
+ }
76
+
77
+ // ── docRefs: which files each document references ─────────────────────────────────────────
78
+
79
+ /**
80
+ * Record the files `docId` references (replaces the previous list).
81
+ * @param {string} docId @param {Iterable<string>} hashes @returns {Promise<void>}
82
+ */
83
+ async setDocRefs(docId, hashes) {
84
+ await withStore(DOC_REFS_STORE, 'readwrite', (s) => s.put({ docId, hashes: [...hashes], savedAt: Date.now() }));
85
+ }
86
+
87
+ /** @param {string} docId @returns {Promise<Set<string>>} */
88
+ async getDocRefs(docId) {
89
+ const record = await withStore(DOC_REFS_STORE, 'readonly', (s) => s.get(docId));
90
+ return new Set(record?.hashes ?? []);
91
+ }
92
+
93
+ /** @param {string} docId @returns {Promise<void>} */
94
+ async clearDocRefs(docId) {
95
+ await withStore(DOC_REFS_STORE, 'readwrite', (s) => s.delete(docId));
96
+ }
97
+
98
+ /** Every file referenced by at least one document. @returns {Promise<Set<string>>} */
99
+ async referencedHashes() {
100
+ const records = /** @type {Array<{ hashes: string[] }>} */ (await withStore(DOC_REFS_STORE, 'readonly', (s) => s.getAll()));
101
+ return new Set(records.flatMap((r) => r.hashes));
102
+ }
103
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Normalise the `maxWait` option: a positive number of ms, never shorter than `debounce` (like lodash: the maximum wait cannot be
3
+ * shorter than the wait). `0` / `Infinity` / a non-number other than `undefined` disable it (the old, trailing-only behaviour);
4
+ * `undefined` gives {@link DEFAULT_MAX_WAIT}.
5
+ * @param {unknown} maxWait
6
+ * @param {number} debounce
7
+ * @returns {number} ms, or `Infinity` (no maximum)
8
+ */
9
+ export function resolveMaxWait(maxWait: unknown, debounce: number): number;
10
+ /**
11
+ * @typedef {object} Autosave
12
+ * @property {() => void} touch a change happened: (re)arm the timer
13
+ * @property {() => void} cancel drop the pending save (nothing is saved by this scheduler)
14
+ * @property {() => boolean} pending whether a save is scheduled and has not started yet
15
+ */
16
+ /**
17
+ * Debounce with a maximum wait: `touch()` re-arms a timer of `debounce` ms, but never later than `maxWait` ms after the FIRST
18
+ * `touch()` of the current burst, so a long uninterrupted burst of changes is still saved every `maxWait` ms. Once `run` is
19
+ * called, the next `touch()` starts a new burst. Pure (injectable clock and timers) so it can be unit tested.
20
+ *
21
+ * @param {object} options
22
+ * @param {number} options.debounce ms of quiet before a save
23
+ * @param {number} [options.maxWait] ms; `Infinity` / omitted: no maximum
24
+ * @param {() => void} options.run the save (must not throw: report its own errors)
25
+ * @param {() => number} [options.now]
26
+ * @param {(fn: () => void, ms: number) => any} [options.setTimer]
27
+ * @param {(handle: any) => void} [options.clearTimer]
28
+ * @returns {Autosave}
29
+ */
30
+ export function createAutosave({ debounce, maxWait, run, now, setTimer, clearTimer }: {
31
+ debounce: number;
32
+ maxWait?: number | undefined;
33
+ run: () => void;
34
+ now?: (() => number) | undefined;
35
+ setTimer?: ((fn: () => void, ms: number) => any) | undefined;
36
+ clearTimer?: ((handle: any) => void) | undefined;
37
+ }): Autosave;
38
+ /** Default `maxWait` of `createPersistedEditor` (ms): during a continuous burst of edits, a save happens at least this often. */
39
+ export const DEFAULT_MAX_WAIT: 3000;
40
+ export type Autosave = {
41
+ /**
42
+ * a change happened: (re)arm the timer
43
+ */
44
+ touch: () => void;
45
+ /**
46
+ * drop the pending save (nothing is saved by this scheduler)
47
+ */
48
+ cancel: () => void;
49
+ /**
50
+ * whether a save is scheduled and has not started yet
51
+ */
52
+ pending: () => boolean;
53
+ };
@@ -0,0 +1,62 @@
1
+ /** Default `maxWait` of `createPersistedEditor` (ms): during a continuous burst of edits, a save happens at least this often. */
2
+ export const DEFAULT_MAX_WAIT = 3000;
3
+
4
+ /**
5
+ * Normalise the `maxWait` option: a positive number of ms, never shorter than `debounce` (like lodash: the maximum wait cannot be
6
+ * shorter than the wait). `0` / `Infinity` / a non-number other than `undefined` disable it (the old, trailing-only behaviour);
7
+ * `undefined` gives {@link DEFAULT_MAX_WAIT}.
8
+ * @param {unknown} maxWait
9
+ * @param {number} debounce
10
+ * @returns {number} ms, or `Infinity` (no maximum)
11
+ */
12
+ export function resolveMaxWait(maxWait, debounce) {
13
+ if (maxWait === undefined) return Math.max(DEFAULT_MAX_WAIT, debounce);
14
+ if (typeof maxWait !== 'number' || !(maxWait > 0) || !Number.isFinite(maxWait)) return Infinity;
15
+ return Math.max(maxWait, debounce);
16
+ }
17
+
18
+ /**
19
+ * @typedef {object} Autosave
20
+ * @property {() => void} touch a change happened: (re)arm the timer
21
+ * @property {() => void} cancel drop the pending save (nothing is saved by this scheduler)
22
+ * @property {() => boolean} pending whether a save is scheduled and has not started yet
23
+ */
24
+
25
+ /**
26
+ * Debounce with a maximum wait: `touch()` re-arms a timer of `debounce` ms, but never later than `maxWait` ms after the FIRST
27
+ * `touch()` of the current burst, so a long uninterrupted burst of changes is still saved every `maxWait` ms. Once `run` is
28
+ * called, the next `touch()` starts a new burst. Pure (injectable clock and timers) so it can be unit tested.
29
+ *
30
+ * @param {object} options
31
+ * @param {number} options.debounce ms of quiet before a save
32
+ * @param {number} [options.maxWait] ms; `Infinity` / omitted: no maximum
33
+ * @param {() => void} options.run the save (must not throw: report its own errors)
34
+ * @param {() => number} [options.now]
35
+ * @param {(fn: () => void, ms: number) => any} [options.setTimer]
36
+ * @param {(handle: any) => void} [options.clearTimer]
37
+ * @returns {Autosave}
38
+ */
39
+ export function createAutosave({ debounce, maxWait = Infinity, run, now = Date.now, setTimer = setTimeout, clearTimer = clearTimeout }) {
40
+ /** @type {any} */
41
+ let timer = null;
42
+ /** When the current burst started (first `touch` since the last save). @type {number | null} */
43
+ let burstStart = null;
44
+
45
+ const fire = () => { timer = null; burstStart = null; run(); };
46
+
47
+ return {
48
+ touch() {
49
+ const t = now();
50
+ if (burstStart === null) burstStart = t;
51
+ if (timer !== null) clearTimer(timer);
52
+ const remaining = burstStart + maxWait - t;
53
+ timer = setTimer(fire, Math.max(0, Math.min(debounce, remaining)));
54
+ },
55
+ cancel() {
56
+ if (timer !== null) clearTimer(timer);
57
+ timer = null;
58
+ burstStart = null;
59
+ },
60
+ pending: () => timer !== null,
61
+ };
62
+ }