@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. package/CHANGELOG.md +531 -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 +195 -55
  12. package/dist/behaviors/text/text.behavior.svelte.js +531 -103
  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 +32 -18
  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 +1753 -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 +35 -14
  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 +55 -8
  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/composition.d.ts +28 -0
  253. package/dist/utils/composition.js +51 -0
  254. package/dist/utils/dom-scope.d.ts +14 -0
  255. package/dist/utils/dom-scope.js +20 -0
  256. package/dist/utils/drop.d.ts +160 -0
  257. package/dist/utils/drop.js +305 -0
  258. package/dist/utils/extensions.d.ts +439 -45
  259. package/dist/utils/extensions.js +271 -29
  260. package/dist/utils/html.d.ts +44 -0
  261. package/dist/utils/html.js +183 -0
  262. package/dist/utils/index.d.ts +3 -0
  263. package/dist/utils/index.js +3 -0
  264. package/dist/utils/keys.d.ts +120 -0
  265. package/dist/utils/keys.js +248 -0
  266. package/dist/utils/links.d.ts +22 -0
  267. package/dist/utils/links.js +34 -0
  268. package/dist/utils/placeholders.d.ts +81 -3
  269. package/dist/utils/placeholders.js +73 -3
  270. package/dist/utils/selection.svelte.d.ts +48 -20
  271. package/dist/utils/selection.svelte.js +60 -10
  272. package/dist/utils/spellcheck.d.ts +9 -0
  273. package/dist/utils/spellcheck.js +17 -0
  274. package/dist/utils/tab.d.ts +31 -0
  275. package/dist/utils/tab.js +117 -0
  276. package/dist/utils/words.d.ts +42 -0
  277. package/dist/utils/words.js +147 -0
  278. package/dist/vite.d.ts +9 -3
  279. package/dist/vite.js +19 -8
  280. package/package.json +56 -20
  281. package/dist/blocks/variable.svelte.d.ts +0 -47
  282. package/dist/blocks/variable.svelte.js +0 -51
  283. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  284. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/Zone.svelte +0 -272
  286. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  288. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  290. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  291. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  292. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  293. package/dist/blocks/zone/index.d.ts +0 -2
  294. package/dist/blocks/zone/index.js +0 -2
  295. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  296. package/dist/blocks/zone/zone.extension.js +0 -237
  297. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  298. package/dist/blocks/zone/zone.svelte.js +0 -608
  299. package/dist/overlays/context/Context.svelte +0 -182
  300. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  301. package/dist/overlays/context/collect.d.ts +0 -17
  302. package/dist/overlays/context/collect.js +0 -33
  303. package/dist/overlays/context/context.extension.d.ts +0 -2
  304. package/dist/overlays/context/context.extension.js +0 -8
  305. package/dist/overlays/context/context.svelte.d.ts +0 -82
  306. package/dist/overlays/context/context.svelte.js +0 -159
  307. package/dist/overlays/context/index.d.ts +0 -3
  308. package/dist/overlays/context/index.js +0 -3
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Can a delimiter run of `ch` (`*`, `_`, `~`) OPEN / CLOSE emphasis, given the character right before and right after
3
+ * the whole run ('' at the text edges)? CommonMark flanking, whitespace half only (the same as the typing shortcuts):
4
+ * an opener is followed by a non-whitespace char, a closer preceded by one. `_` additionally never opens or closes
5
+ * INTRAWORD (`snake_case_name` stays literal); `*` and `~` may (`a*b*c`).
6
+ * @param {string} ch
7
+ * @param {string} prev char before the run ('' at the start)
8
+ * @param {string} next char after the run ('' at the end)
9
+ * @returns {{ canOpen: boolean, canClose: boolean }}
10
+ */
11
+ export function delimiterFlags(ch: string, prev: string, next: string): {
12
+ canOpen: boolean;
13
+ canClose: boolean;
14
+ };
15
+ /**
16
+ * Escape the markdown-significant characters of a plain text run so that parsing it back gives the same text.
17
+ * Only what can start something is escaped: `\ * ` [ ]`, a `_` that could be a delimiter (not intraword), a `~`
18
+ * next to another `~`, a `<` that would open an autolink / `<u>` / `<details>` tag.
19
+ * @param {string} text
20
+ * @returns {string}
21
+ */
22
+ export function escapeMarkdownText(text: string): string;
23
+ /**
24
+ * Escape what would turn a block's first line into another block when read back (`# `, `> `, `- `, `1. `, `-- `,
25
+ * `---`). Applied to the start of the exported text of a block.
26
+ * @param {string} md already inline-escaped markdown
27
+ * @returns {string}
28
+ */
29
+ export function escapeBlockStart(md: string): string;
30
+ /**
31
+ * Markdown inline syntax rules shared by everything that reads or writes inline markdown: the typing shortcuts
32
+ * (`markdown-shortcuts.extension.js`), the markdown paste / import parser (`paste/markdown-inline.js`) and the
33
+ * exporter (`deltaToMarkdown`). One place, so typing `*a*`, pasting `*a*` and exporting italic `a` agree.
34
+ *
35
+ * Pure functions, no DOM, no runes.
36
+ */
37
+ /** Regex class body of a "word" character (letter, digit, underscore): an `_` touching one is intraword. */
38
+ export const WORD: "\\p{L}\\p{N}_";
39
+ /** ASCII punctuation: the characters a backslash can escape (CommonMark). */
40
+ export const ASCII_PUNCTUATION: "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
41
+ export function isWordChar(c: string | undefined): boolean;
42
+ export function isSpaceOrEdge(c: string | undefined): boolean;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Markdown inline syntax rules shared by everything that reads or writes inline markdown: the typing shortcuts
3
+ * (`markdown-shortcuts.extension.js`), the markdown paste / import parser (`paste/markdown-inline.js`) and the
4
+ * exporter (`deltaToMarkdown`). One place, so typing `*a*`, pasting `*a*` and exporting italic `a` agree.
5
+ *
6
+ * Pure functions, no DOM, no runes.
7
+ */
8
+
9
+ /** Regex class body of a "word" character (letter, digit, underscore): an `_` touching one is intraword. */
10
+ export const WORD = '\\p{L}\\p{N}_';
11
+
12
+ const WORD_RE = new RegExp(`[${WORD}]`, 'u');
13
+ const SPACE_RE = /\s/u;
14
+
15
+ /** ASCII punctuation: the characters a backslash can escape (CommonMark). */
16
+ export const ASCII_PUNCTUATION = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
17
+
18
+ /** @param {string | undefined} c */
19
+ export const isWordChar = (c) => !!c && WORD_RE.test(c);
20
+
21
+ /** Whitespace, or the edge of the text (nothing there): CommonMark treats the line edges as whitespace. */
22
+ export const isSpaceOrEdge = (/** @type {string | undefined} */ c) => !c || SPACE_RE.test(c);
23
+
24
+ /**
25
+ * Can a delimiter run of `ch` (`*`, `_`, `~`) OPEN / CLOSE emphasis, given the character right before and right after
26
+ * the whole run ('' at the text edges)? CommonMark flanking, whitespace half only (the same as the typing shortcuts):
27
+ * an opener is followed by a non-whitespace char, a closer preceded by one. `_` additionally never opens or closes
28
+ * INTRAWORD (`snake_case_name` stays literal); `*` and `~` may (`a*b*c`).
29
+ * @param {string} ch
30
+ * @param {string} prev char before the run ('' at the start)
31
+ * @param {string} next char after the run ('' at the end)
32
+ * @returns {{ canOpen: boolean, canClose: boolean }}
33
+ */
34
+ export function delimiterFlags(ch, prev, next) {
35
+ const left = !isSpaceOrEdge(next);
36
+ const right = !isSpaceOrEdge(prev);
37
+ if (ch === '_') return { canOpen: left && !isWordChar(prev), canClose: right && !isWordChar(next) };
38
+ return { canOpen: left, canClose: right };
39
+ }
40
+
41
+ /**
42
+ * A `<` that the inline parser would read as markup: `<u>`, `</u>`, an autolink `<scheme:…>` or `<a@b.c>`; and the tags of
43
+ * the toggle construct (`<details>`, `</summary>`…): a line of text must never read back as the start / end of a toggle.
44
+ */
45
+ const MARKUP_START = /^<(?:\/?u>|\/?(?:details|summary)(?![A-Za-z0-9])|[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*>|[^\s<>@]+@[^\s<>]+>)/i;
46
+
47
+ /**
48
+ * Escape the markdown-significant characters of a plain text run so that parsing it back gives the same text.
49
+ * Only what can start something is escaped: `\ * ` [ ]`, a `_` that could be a delimiter (not intraword), a `~`
50
+ * next to another `~`, a `<` that would open an autolink / `<u>` / `<details>` tag.
51
+ * @param {string} text
52
+ * @returns {string}
53
+ */
54
+ export function escapeMarkdownText(text) {
55
+ let out = '';
56
+ for (let i = 0; i < text.length; i++) {
57
+ const c = text[i];
58
+ if (c === '\\' || c === '*' || c === '`' || c === '[' || c === ']') out += '\\' + c;
59
+ else if (c === '_') out += isWordChar(text[i - 1]) && isWordChar(text[i + 1]) ? c : '\\_';
60
+ else if (c === '~') out += text[i - 1] === '~' || text[i + 1] === '~' ? '\\~' : c;
61
+ else if (c === '<') out += MARKUP_START.test(text.slice(i)) ? '\\<' : c;
62
+ else out += c;
63
+ }
64
+ return out;
65
+ }
66
+
67
+ /**
68
+ * Escape what would turn a block's first line into another block when read back (`# `, `> `, `- `, `1. `, `-- `,
69
+ * `---`). Applied to the start of the exported text of a block.
70
+ * @param {string} md already inline-escaped markdown
71
+ * @returns {string}
72
+ */
73
+ export function escapeBlockStart(md) {
74
+ const indent = /^[ \t]*/.exec(md)?.[0] ?? '';
75
+ const body = md.slice(indent.length);
76
+ if (/^#{1,6}(?:\s|$)/.test(body) || body.startsWith('>')) return indent + '\\' + body;
77
+ if (/^(?:[-+](?=\s)|-{2,}(?=\s|$))/.test(body)) return indent + '\\' + body;
78
+ const ordered = /^(\d+)([.)])(?=\s)/.exec(body);
79
+ if (ordered) return indent + ordered[1] + '\\' + body.slice(ordered[1].length);
80
+ return md;
81
+ }
@@ -1,2 +1,5 @@
1
+ /**
2
+ * The inline marks (bold, italic, underline, code, strikethrough) and links: the `mark:*` and `link:*` actions and their keyboard shortcuts.
3
+ */
1
4
  export const RichTextExtension: Extension;
2
5
  import { Extension } from '../../utils/extensions.js';
@@ -4,14 +4,9 @@
4
4
 
5
5
  import { Extension } from '../../utils/extensions.js';
6
6
  import { TextBehavior } from './text.behavior.svelte.js';
7
-
8
- /** @type {Record<string, string>} */
9
- const MARK_SHORTCUTS = {
10
- b: 'bold',
11
- i: 'italic',
12
- u: 'underline',
13
- e: 'code',
14
- };
7
+ import { sanitizeUrl } from '../../utils/html.js';
8
+ import { livePoints, placePoints } from '../../blocks/undo-selection.js';
9
+ import { tick } from 'svelte';
15
10
 
16
11
  /**
17
12
  * Collect all selected blocks with a TextBehavior and a non-collapsed selection.
@@ -32,17 +27,28 @@ function collectMarkTargets(nabu) {
32
27
  return targets;
33
28
  }
34
29
 
30
+ /**
31
+ * Run a mark mutation as one transaction and leave the selection EXACTLY on the same characters (anchor, focus and so the
32
+ * direction). A mark rewrites the spans of the text, so the DOM point of an offset changes (a plain text node becomes
33
+ * text / `<strong>` / text): the selection is kept as MODEL points (block + text offset) and put back on the new DOM once it
34
+ * is rendered. Never derive a model offset from a DOM point.
35
+ * @param {Nabu} nabu
36
+ * @param {() => void} mutate
37
+ */
38
+ async function markTransaction(nabu, mutate) {
39
+ const kept = livePoints(nabu);
40
+ await nabu.transact(mutate);
41
+ if (!kept) return;
42
+ await tick();
43
+ placePoints(nabu, kept);
44
+ }
45
+
35
46
  /**
36
47
  * Toggle a mark on all selected text targets.
37
48
  * @param {Nabu} nabu
38
49
  * @param {string} markName
39
50
  */
40
51
  async function toggleMark(nabu, markName) {
41
- const startBlock = nabu.selection.startBlock;
42
- const endBlock = nabu.selection.endBlock;
43
- const startFocusData = startBlock?.focus(null, true);
44
- const endFocusData = endBlock?.focus(null, true);
45
-
46
52
  const targets = collectMarkTargets(nabu);
47
53
  if (!targets.length) {
48
54
  // Collapsed cursor: toggle the pending stored mark for the next typed char.
@@ -51,7 +57,7 @@ async function toggleMark(nabu, markName) {
51
57
  }
52
58
 
53
59
  const isFullyActive = targets.every(({ behavior, sel }) => behavior.isMarkActive(markName, sel));
54
- await nabu.transact(() => {
60
+ await markTransaction(nabu, () => {
55
61
  for (const { behavior, sel } of targets) {
56
62
  if (isFullyActive) {
57
63
  behavior.removeMark(markName, sel);
@@ -60,13 +66,6 @@ async function toggleMark(nabu, markName) {
60
66
  }
61
67
  }
62
68
  });
63
-
64
- if (startBlock && endBlock) {
65
- nabu.focus({
66
- from: { block: startBlock, offset: startFocusData?.start?.offset ?? 0 },
67
- to: { block: endBlock, offset: endFocusData?.end?.offset ?? 0 }
68
- });
69
- }
70
69
  return true;
71
70
  }
72
71
 
@@ -79,7 +78,7 @@ async function toggleMark(nabu, markName) {
79
78
  function applyMark(nabu, markName, value = true) {
80
79
  const targets = collectMarkTargets(nabu);
81
80
  if (!targets.length) return;
82
- return nabu.transact(() => {
81
+ return markTransaction(nabu, () => {
83
82
  for (const { behavior, sel } of targets) {
84
83
  behavior.applyMark(markName, value, sel);
85
84
  }
@@ -94,7 +93,7 @@ function applyMark(nabu, markName, value = true) {
94
93
  function removeMark(nabu, markName) {
95
94
  const targets = collectMarkTargets(nabu);
96
95
  if (!targets.length) return;
97
- return nabu.transact(() => {
96
+ return markTransaction(nabu, () => {
98
97
  for (const { behavior, sel } of targets) {
99
98
  behavior.removeMark(markName, sel);
100
99
  }
@@ -102,27 +101,75 @@ function removeMark(nabu, markName) {
102
101
  }
103
102
 
104
103
  /**
104
+ * The mark shortcuts are COMMANDS (`mark:bold`…, keys below, replaceable through the `keymap` option). This legacy hook only
105
+ * runs when no command took the key: the browser's own Ctrl/Cmd+B / I / U would then format the text natively
106
+ * (`beforeinput` `formatBold`…), so a shortcut a `keymap` removed or moved stays dead instead of coming back through the
107
+ * native path.
105
108
  * @param {Nabu} nabu
106
109
  * @param {KeyboardEvent} event
107
110
  */
108
111
  function onKeyDown(nabu, event) {
109
- const ctrl = event.ctrlKey || event.metaKey;
110
- if (!ctrl) return;
112
+ if (!(event.ctrlKey || event.metaKey) || event.shiftKey || event.altKey) return;
113
+ const key = typeof event.key === 'string' ? event.key.toLowerCase() : '';
114
+ if (key !== 'b' && key !== 'i' && key !== 'u') return;
115
+ event.preventDefault();
116
+ return nabu.BREAK;
117
+ }
111
118
 
112
- let markName = null;
113
- if (!event.shiftKey) {
114
- markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
115
- } else if (event.key.toLowerCase() === 'x') {
116
- markName = 'strikethrough';
117
- }
119
+ /** `beforeinput` format types (browser / OS formatting commands) -> the mark they toggle. Sub/superscript have no mark here: ignored. */
120
+ const FORMAT_INPUT_MARKS = /** @type {Record<string, string>} */ ({
121
+ formatBold: 'bold',
122
+ formatItalic: 'italic',
123
+ formatUnderline: 'underline',
124
+ formatStrikeThrough: 'strikethrough',
125
+ });
118
126
 
119
- if (!markName) return;
127
+ /** Marks `formatRemove` clears (links are content, not formatting: they stay). */
128
+ const FORMAT_REMOVE_MARKS = ['bold', 'italic', 'underline', 'strikethrough', 'code'];
120
129
 
130
+ /**
131
+ * Native formatting commands (Cmd+B from the OS menu, "Format > Bold", Touch Bar, accessibility APIs…) arrive as
132
+ * `beforeinput` events of type `formatBold` etc. They go through the same actions as the keyboard shortcuts.
133
+ * @param {Nabu} nabu
134
+ * @param {InputEvent} event
135
+ */
136
+ function onBeforeInput(nabu, event) {
137
+ const type = event.inputType;
138
+ if (typeof type !== 'string' || !type.startsWith('format')) return;
121
139
  event.preventDefault();
122
- nabu.exec('mark:toggle', { mark: markName });
140
+ const mark = FORMAT_INPUT_MARKS[type];
141
+ if (mark) {
142
+ nabu.exec('mark:toggle', { mark });
143
+ } else if (type === 'formatRemove') {
144
+ const targets = collectMarkTargets(nabu);
145
+ if (!targets.length) {
146
+ if (nabu.selection.anchorBlock?.behaviors?.has('text')) {
147
+ nabu.storedMarks = Object.fromEntries(FORMAT_REMOVE_MARKS.map((name) => [name, false]));
148
+ }
149
+ } else {
150
+ markTransaction(nabu, () => {
151
+ for (const { behavior, sel } of targets) for (const name of FORMAT_REMOVE_MARKS) behavior.removeMark(name, sel);
152
+ });
153
+ }
154
+ }
123
155
  return nabu.BREAK;
124
156
  }
125
157
 
158
+ /**
159
+ * A link href that may enter the model: http(s), mailto, tel, relative, `#fragment`. Anything else
160
+ * (`javascript:`, `data:`, `vbscript:`…) is refused with a warning. Returns the href, or null.
161
+ * @param {any} nabu @param {unknown} href @returns {string | null}
162
+ */
163
+ function safeHref(nabu, href) {
164
+ if (!href) return null;
165
+ const safe = sanitizeUrl(href, { kind: 'link' });
166
+ if (safe === null) nabu.warn('link: refused unsafe href', href);
167
+ return safe;
168
+ }
169
+
170
+ /**
171
+ * The inline marks (bold, italic, underline, code, strikethrough) and links: the `mark:*` and `link:*` actions and their keyboard shortcuts.
172
+ */
126
173
  export const RichTextExtension = new Extension('rich-text', {
127
174
  // Declare all mark attributes so Loro's applyDelta can handle them correctly.
128
175
  // Marks are exact (expand: 'none') — they don't auto-extend at boundaries.
@@ -143,8 +190,9 @@ export const RichTextExtension = new Extension('rich-text', {
143
190
  'mark:remove': (nabu, data) => removeMark(nabu, data?.mark),
144
191
  // Links — the 'link' mark stores the href as its value
145
192
  'link:set': (nabu, data) => {
146
- if (!data?.href) return;
147
- return applyMark(nabu, 'link', data.href);
193
+ const href = safeHref(nabu, data?.href);
194
+ if (!href) return;
195
+ return applyMark(nabu, 'link', href);
148
196
  },
149
197
  'link:remove': (nabu) => removeMark(nabu, 'link'),
150
198
  'link:toggle': (nabu, data) => {
@@ -152,8 +200,9 @@ export const RichTextExtension = new Extension('rich-text', {
152
200
  if (!targets.length) return;
153
201
  const allHaveLink = targets.every(({ behavior, sel }) => behavior.isMarkActive('link', sel));
154
202
  if (allHaveLink) return removeMark(nabu, 'link');
155
- if (!data?.href) return;
156
- return applyMark(nabu, 'link', data.href);
203
+ const href = safeHref(nabu, data?.href);
204
+ if (!href) return;
205
+ return applyMark(nabu, 'link', href);
157
206
  },
158
207
  // Aliases
159
208
  'bold': (nabu) => toggleMark(nabu, 'bold'),
@@ -162,5 +211,13 @@ export const RichTextExtension = new Extension('rich-text', {
162
211
  'code': (nabu) => toggleMark(nabu, 'code'),
163
212
  'strikethrough': (nabu) => toggleMark(nabu, 'strikethrough'),
164
213
  },
165
- hooks: { onKeyDown }
214
+ // Keyboard shortcuts (ADR 017). Also actions of the bus: `nabu.exec('mark:bold')`.
215
+ commands: {
216
+ 'mark:bold': { label: 'Bold', keys: ['Mod-b'], run: (nabu) => { toggleMark(nabu, 'bold'); } },
217
+ 'mark:italic': { label: 'Italic', keys: ['Mod-i'], run: (nabu) => { toggleMark(nabu, 'italic'); } },
218
+ 'mark:underline': { label: 'Underline', keys: ['Mod-u'], run: (nabu) => { toggleMark(nabu, 'underline'); } },
219
+ 'mark:code': { label: 'Code', keys: ['Mod-e'], run: (nabu) => { toggleMark(nabu, 'code'); } },
220
+ 'mark:strikethrough': { label: 'Strikethrough', keys: ['Mod-Shift-x'], run: (nabu) => { toggleMark(nabu, 'strikethrough'); } },
221
+ },
222
+ hooks: { onKeyDown, onBeforeInput }
166
223
  });