@aionbuilders/nabu 0.1.0-alpha.9 → 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 (319) hide show
  1. package/CHANGELOG.md +523 -0
  2. package/LICENSE +21 -0
  3. package/README.md +82 -144
  4. package/dist/behaviors/text/RichText.svelte +25 -2
  5. package/dist/behaviors/text/index.d.ts +2 -0
  6. package/dist/behaviors/text/index.js +2 -0
  7. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
  8. package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
  9. package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
  10. package/dist/behaviors/text/markdown-syntax.js +81 -0
  11. package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
  12. package/dist/behaviors/text/rich-text.extension.js +135 -53
  13. package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
  14. package/dist/behaviors/text/text.behavior.svelte.js +657 -55
  15. package/dist/behaviors/text/typography.extension.d.ts +7 -0
  16. package/dist/behaviors/text/typography.extension.js +25 -0
  17. package/dist/blocks/Block.svelte +5 -2
  18. package/dist/blocks/BlockList.svelte +48 -0
  19. package/dist/blocks/BlockList.svelte.d.ts +17 -0
  20. package/dist/blocks/Nabu.svelte +180 -14
  21. package/dist/blocks/Nabu.svelte.d.ts +13 -0
  22. package/dist/blocks/block.extension.d.ts +3 -0
  23. package/dist/blocks/block.extension.js +8 -9
  24. package/dist/blocks/block.svelte.d.ts +356 -68
  25. package/dist/blocks/block.svelte.js +388 -42
  26. package/dist/blocks/blockquote/Blockquote.svelte +39 -0
  27. package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
  28. package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
  29. package/dist/blocks/blockquote/blockquote.extension.js +80 -0
  30. package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
  31. package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
  32. package/dist/blocks/blockquote/index.d.ts +3 -0
  33. package/dist/blocks/blockquote/index.js +3 -0
  34. package/dist/blocks/callout/Callout.svelte +67 -0
  35. package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
  36. package/dist/blocks/callout/callout.extension.d.ts +4 -0
  37. package/dist/blocks/callout/callout.extension.js +84 -0
  38. package/dist/blocks/callout/callout.svelte.d.ts +80 -0
  39. package/dist/blocks/callout/callout.svelte.js +189 -0
  40. package/dist/blocks/callout/index.d.ts +3 -0
  41. package/dist/blocks/callout/index.js +3 -0
  42. package/dist/blocks/chunks.d.ts +49 -0
  43. package/dist/blocks/chunks.js +90 -0
  44. package/dist/blocks/code/Code.svelte +84 -0
  45. package/dist/blocks/code/Code.svelte.d.ts +11 -0
  46. package/dist/blocks/code/code.extension.d.ts +4 -0
  47. package/dist/blocks/code/code.extension.js +88 -0
  48. package/dist/blocks/code/code.svelte.d.ts +68 -0
  49. package/dist/blocks/code/code.svelte.js +245 -0
  50. package/dist/blocks/code/index.d.ts +3 -0
  51. package/dist/blocks/code/index.js +3 -0
  52. package/dist/blocks/container.utils.d.ts +85 -13
  53. package/dist/blocks/container.utils.js +338 -178
  54. package/dist/blocks/dialogue/Dialogue.svelte +8 -2
  55. package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
  56. package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
  57. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
  58. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
  59. package/dist/blocks/dialogue/index.d.ts +12 -1
  60. package/dist/blocks/dialogue/index.js +51 -35
  61. package/dist/blocks/divider/Divider.svelte +28 -0
  62. package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
  63. package/dist/blocks/divider/divider.extension.d.ts +23 -0
  64. package/dist/blocks/divider/divider.extension.js +153 -0
  65. package/dist/blocks/divider/divider.svelte.d.ts +54 -0
  66. package/dist/blocks/divider/divider.svelte.js +89 -0
  67. package/dist/blocks/divider/index.d.ts +3 -0
  68. package/dist/blocks/divider/index.js +3 -0
  69. package/dist/blocks/file/File.svelte +56 -0
  70. package/dist/blocks/file/File.svelte.d.ts +11 -0
  71. package/dist/blocks/file/file.extension.d.ts +4 -0
  72. package/dist/blocks/file/file.extension.js +27 -0
  73. package/dist/blocks/file/file.svelte.d.ts +29 -0
  74. package/dist/blocks/file/file.svelte.js +77 -0
  75. package/dist/blocks/file/index.d.ts +3 -0
  76. package/dist/blocks/file/index.js +3 -0
  77. package/dist/blocks/heading/Heading.svelte +2 -1
  78. package/dist/blocks/heading/heading.svelte.d.ts +27 -34
  79. package/dist/blocks/heading/heading.svelte.js +72 -60
  80. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
  81. package/dist/blocks/heading/index.d.ts +2 -2
  82. package/dist/blocks/heading/index.js +33 -8
  83. package/dist/blocks/image/Image.svelte +41 -0
  84. package/dist/blocks/image/Image.svelte.d.ts +11 -0
  85. package/dist/blocks/image/image.extension.d.ts +5 -0
  86. package/dist/blocks/image/image.extension.js +34 -0
  87. package/dist/blocks/image/image.svelte.d.ts +61 -0
  88. package/dist/blocks/image/image.svelte.js +114 -0
  89. package/dist/blocks/image/index.d.ts +3 -0
  90. package/dist/blocks/image/index.js +3 -0
  91. package/dist/blocks/index.d.ts +11 -1
  92. package/dist/blocks/index.js +11 -1
  93. package/dist/blocks/list/List.svelte +9 -2
  94. package/dist/blocks/list/ListItem.svelte +44 -7
  95. package/dist/blocks/list/convert.d.ts +20 -0
  96. package/dist/blocks/list/convert.js +92 -0
  97. package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
  98. package/dist/blocks/list/html-flat.d.ts +78 -0
  99. package/dist/blocks/list/html-flat.js +244 -0
  100. package/dist/blocks/list/indent.d.ts +86 -0
  101. package/dist/blocks/list/indent.js +205 -0
  102. package/dist/blocks/list/index.d.ts +7 -3
  103. package/dist/blocks/list/index.js +43 -31
  104. package/dist/blocks/list/list-item.svelte.d.ts +43 -17
  105. package/dist/blocks/list/list-item.svelte.js +143 -93
  106. package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
  107. package/dist/blocks/list/list.behavior.svelte.js +10 -5
  108. package/dist/blocks/list/list.svelte.d.ts +101 -23
  109. package/dist/blocks/list/list.svelte.js +136 -114
  110. package/dist/blocks/list/markdown.d.ts +149 -0
  111. package/dist/blocks/list/markdown.js +442 -0
  112. package/dist/blocks/list/normalize.d.ts +11 -0
  113. package/dist/blocks/list/normalize.js +396 -0
  114. package/dist/blocks/list/paste.d.ts +45 -0
  115. package/dist/blocks/list/paste.js +242 -0
  116. package/dist/blocks/list/step-back.d.ts +24 -0
  117. package/dist/blocks/list/step-back.js +87 -0
  118. package/dist/blocks/list/to-list.d.ts +7 -0
  119. package/dist/blocks/list/to-list.js +44 -0
  120. package/dist/blocks/list/todos-toggle.d.ts +20 -0
  121. package/dist/blocks/list/todos-toggle.js +48 -0
  122. package/dist/blocks/list/todos.svelte.d.ts +35 -0
  123. package/dist/blocks/list/todos.svelte.js +76 -0
  124. package/dist/blocks/media/index.d.ts +2 -0
  125. package/dist/blocks/media/index.js +2 -0
  126. package/dist/blocks/media/markdown.d.ts +12 -0
  127. package/dist/blocks/media/markdown.js +26 -0
  128. package/dist/blocks/media/media-block.svelte.d.ts +107 -0
  129. package/dist/blocks/media/media-block.svelte.js +181 -0
  130. package/dist/blocks/media/media.helpers.d.ts +24 -0
  131. package/dist/blocks/media/media.helpers.js +88 -0
  132. package/dist/blocks/megablock.svelte.d.ts +50 -19
  133. package/dist/blocks/megablock.svelte.js +115 -74
  134. package/dist/blocks/move.d.ts +23 -0
  135. package/dist/blocks/move.js +92 -0
  136. package/dist/blocks/nabu.svelte.d.ts +846 -75
  137. package/dist/blocks/nabu.svelte.js +1990 -182
  138. package/dist/blocks/paragraph/Paragraph.svelte +1 -0
  139. package/dist/blocks/paragraph/index.d.ts +2 -2
  140. package/dist/blocks/paragraph/index.js +35 -16
  141. package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
  142. package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
  143. package/dist/blocks/paragraph/table.html.d.ts +9 -0
  144. package/dist/blocks/paragraph/table.html.js +111 -0
  145. package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
  146. package/dist/blocks/paragraph/table.markdown.js +90 -0
  147. package/dist/blocks/region/Region.svelte +264 -0
  148. package/dist/blocks/region/Region.svelte.d.ts +11 -0
  149. package/dist/blocks/region/actions.d.ts +135 -0
  150. package/dist/blocks/region/actions.js +402 -0
  151. package/dist/blocks/region/close.d.ts +35 -0
  152. package/dist/blocks/region/close.js +130 -0
  153. package/dist/blocks/region/commands.d.ts +6 -0
  154. package/dist/blocks/region/commands.js +277 -0
  155. package/dist/blocks/region/create.d.ts +72 -0
  156. package/dist/blocks/region/create.js +208 -0
  157. package/dist/blocks/region/end-marker.d.ts +35 -0
  158. package/dist/blocks/region/end-marker.js +51 -0
  159. package/dist/blocks/region/facts.d.ts +69 -0
  160. package/dist/blocks/region/facts.js +152 -0
  161. package/dist/blocks/region/format.d.ts +91 -0
  162. package/dist/blocks/region/format.js +260 -0
  163. package/dist/blocks/region/index.d.ts +14 -0
  164. package/dist/blocks/region/index.js +14 -0
  165. package/dist/blocks/region/insert.d.ts +59 -0
  166. package/dist/blocks/region/insert.js +102 -0
  167. package/dist/blocks/region/messages.d.ts +254 -0
  168. package/dist/blocks/region/messages.js +131 -0
  169. package/dist/blocks/region/normalize.d.ts +7 -0
  170. package/dist/blocks/region/normalize.js +170 -0
  171. package/dist/blocks/region/region.extension.d.ts +58 -0
  172. package/dist/blocks/region/region.extension.js +119 -0
  173. package/dist/blocks/region/region.svelte.d.ts +221 -0
  174. package/dist/blocks/region/region.svelte.js +582 -0
  175. package/dist/blocks/region/regions.svelte.d.ts +141 -0
  176. package/dist/blocks/region/regions.svelte.js +276 -0
  177. package/dist/blocks/region/serialize.d.ts +57 -0
  178. package/dist/blocks/region/serialize.js +112 -0
  179. package/dist/blocks/region/types.d.ts +255 -0
  180. package/dist/blocks/region/types.js +204 -0
  181. package/dist/blocks/selection.svelte.d.ts +106 -8
  182. package/dist/blocks/selection.svelte.js +632 -74
  183. package/dist/blocks/subthread.d.ts +80 -0
  184. package/dist/blocks/subthread.js +167 -0
  185. package/dist/blocks/text-block.svelte.d.ts +70 -0
  186. package/dist/blocks/text-block.svelte.js +108 -0
  187. package/dist/blocks/toggle/Toggle.svelte +73 -0
  188. package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
  189. package/dist/blocks/toggle/index.d.ts +3 -0
  190. package/dist/blocks/toggle/index.js +3 -0
  191. package/dist/blocks/toggle/markdown.d.ts +20 -0
  192. package/dist/blocks/toggle/markdown.js +112 -0
  193. package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
  194. package/dist/blocks/toggle/toggle.extension.js +18 -0
  195. package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
  196. package/dist/blocks/toggle/toggle.svelte.js +352 -0
  197. package/dist/blocks/undo-selection.d.ts +175 -0
  198. package/dist/blocks/undo-selection.js +360 -0
  199. package/dist/blocks/video/Video.svelte +42 -0
  200. package/dist/blocks/video/Video.svelte.d.ts +11 -0
  201. package/dist/blocks/video/index.d.ts +3 -0
  202. package/dist/blocks/video/index.js +3 -0
  203. package/dist/blocks/video/video.extension.d.ts +4 -0
  204. package/dist/blocks/video/video.extension.js +27 -0
  205. package/dist/blocks/video/video.svelte.d.ts +29 -0
  206. package/dist/blocks/video/video.svelte.js +80 -0
  207. package/dist/extend.d.ts +55 -0
  208. package/dist/extend.js +88 -0
  209. package/dist/files/assets.clipboard.d.ts +77 -0
  210. package/dist/files/assets.clipboard.js +152 -0
  211. package/dist/files/files.svelte.d.ts +363 -0
  212. package/dist/files/files.svelte.js +897 -0
  213. package/dist/files/index.d.ts +3 -0
  214. package/dist/files/index.js +3 -0
  215. package/dist/files/outbox.d.ts +69 -0
  216. package/dist/files/outbox.js +48 -0
  217. package/dist/files/status.d.ts +65 -0
  218. package/dist/files/status.js +87 -0
  219. package/dist/files/store.d.ts +97 -0
  220. package/dist/files/store.js +65 -0
  221. package/dist/files/uploads.svelte.d.ts +174 -0
  222. package/dist/files/uploads.svelte.js +504 -0
  223. package/dist/index.d.ts +68 -6
  224. package/dist/index.js +117 -7
  225. package/dist/nabu-theme.css +112 -0
  226. package/dist/paste/html.paste.d.ts +74 -3
  227. package/dist/paste/html.paste.js +555 -83
  228. package/dist/paste/markdown-inline.d.ts +86 -0
  229. package/dist/paste/markdown-inline.js +528 -0
  230. package/dist/paste/markdown.paste.d.ts +2 -8
  231. package/dist/paste/markdown.paste.js +78 -94
  232. package/dist/paste/nabu.paste.d.ts +3 -0
  233. package/dist/paste/nabu.paste.js +29 -3
  234. package/dist/paste/plain-text.paste.d.ts +2 -2
  235. package/dist/paste/plain-text.paste.js +3 -3
  236. package/dist/persistence/asset-store.d.ts +51 -0
  237. package/dist/persistence/asset-store.js +103 -0
  238. package/dist/persistence/autosave.d.ts +53 -0
  239. package/dist/persistence/autosave.js +62 -0
  240. package/dist/persistence/compact.d.ts +162 -0
  241. package/dist/persistence/compact.js +141 -0
  242. package/dist/persistence/history.d.ts +77 -0
  243. package/dist/persistence/history.js +176 -0
  244. package/dist/persistence/indexeddb.d.ts +190 -4
  245. package/dist/persistence/indexeddb.js +333 -29
  246. package/dist/persistence/merge.d.ts +38 -0
  247. package/dist/persistence/merge.js +36 -0
  248. package/dist/persistence/outbox.d.ts +25 -0
  249. package/dist/persistence/outbox.js +40 -0
  250. package/dist/persistence/persistence.d.ts +70 -9
  251. package/dist/persistence/persistence.js +507 -35
  252. package/dist/persistence/recovery.d.ts +79 -0
  253. package/dist/persistence/recovery.js +79 -0
  254. package/dist/persistence/release.d.ts +34 -0
  255. package/dist/persistence/release.js +64 -0
  256. package/dist/persistence/state.svelte.d.ts +53 -0
  257. package/dist/persistence/state.svelte.js +48 -0
  258. package/dist/persistence/sync.d.ts +39 -0
  259. package/dist/persistence/sync.js +138 -0
  260. package/dist/persistence/tail.d.ts +42 -0
  261. package/dist/persistence/tail.js +103 -0
  262. package/dist/presets.d.ts +29 -24
  263. package/dist/presets.js +51 -14
  264. package/dist/themes/atelier.css +318 -0
  265. package/dist/ui/CommandChooser.svelte +153 -0
  266. package/dist/ui/CommandChooser.svelte.d.ts +13 -0
  267. package/dist/ui/CommandInvite.svelte +99 -0
  268. package/dist/ui/CommandInvite.svelte.d.ts +12 -0
  269. package/dist/ui/DragHandle.svelte +217 -0
  270. package/dist/ui/DragHandle.svelte.d.ts +11 -0
  271. package/dist/ui/FloatingToolbar.svelte +165 -0
  272. package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
  273. package/dist/ui/SlashMenu.svelte +215 -0
  274. package/dist/ui/SlashMenu.svelte.d.ts +33 -0
  275. package/dist/ui/anchor.d.ts +112 -0
  276. package/dist/ui/anchor.js +136 -0
  277. package/dist/ui/index.d.ts +10 -0
  278. package/dist/ui/index.js +15 -0
  279. package/dist/ui/slash-menu.svelte.d.ts +112 -0
  280. package/dist/ui/slash-menu.svelte.js +150 -0
  281. package/dist/utils/command-confirm.svelte.d.ts +349 -0
  282. package/dist/utils/command-confirm.svelte.js +673 -0
  283. package/dist/utils/commands.d.ts +328 -0
  284. package/dist/utils/commands.js +416 -0
  285. package/dist/utils/dom-scope.d.ts +14 -0
  286. package/dist/utils/dom-scope.js +20 -0
  287. package/dist/utils/drop.d.ts +160 -0
  288. package/dist/utils/drop.js +305 -0
  289. package/dist/utils/extensions.d.ts +488 -40
  290. package/dist/utils/extensions.js +300 -19
  291. package/dist/utils/html.d.ts +44 -0
  292. package/dist/utils/html.js +183 -0
  293. package/dist/utils/index.d.ts +4 -0
  294. package/dist/utils/index.js +4 -0
  295. package/dist/utils/keys.d.ts +120 -0
  296. package/dist/utils/keys.js +248 -0
  297. package/dist/utils/links.d.ts +22 -0
  298. package/dist/utils/links.js +34 -0
  299. package/dist/utils/placeholders.d.ts +88 -0
  300. package/dist/utils/placeholders.js +91 -0
  301. package/dist/utils/selection.svelte.d.ts +47 -19
  302. package/dist/utils/selection.svelte.js +60 -10
  303. package/dist/utils/spellcheck.d.ts +9 -0
  304. package/dist/utils/spellcheck.js +17 -0
  305. package/dist/utils/tab.d.ts +31 -0
  306. package/dist/utils/tab.js +117 -0
  307. package/dist/utils/words.d.ts +7 -0
  308. package/dist/utils/words.js +62 -0
  309. package/dist/vite.d.ts +22 -0
  310. package/dist/vite.js +34 -0
  311. package/package.json +61 -22
  312. package/dist/blocks/zone/Zone.svelte +0 -258
  313. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  314. package/dist/blocks/zone/index.d.ts +0 -2
  315. package/dist/blocks/zone/index.js +0 -2
  316. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  317. package/dist/blocks/zone/zone.extension.js +0 -221
  318. package/dist/blocks/zone/zone.svelte.d.ts +0 -112
  319. package/dist/blocks/zone/zone.svelte.js +0 -321
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Killian Di Vincenzo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,10 +1,19 @@
1
- # Nabu — Block Editor Engine
1
+ # Nabu
2
2
 
3
- > **Alpha** — API in active development. Breaking changes expected.
3
+ Nabu is a modular, local-first block editor for **Svelte 5**. It combines the cross-block selection of document editors (Google Docs, ProseMirror) with the structural flexibility of block editors (Notion), on a single `contenteditable` and a CRDT core ([Loro](https://loro.dev)). You pick the blocks and behaviors you need from a list of extensions, from a `<textarea>` replacement to a full document editor, and save to the browser with one call.
4
4
 
5
- Nabu is a modular, local-first block editor engine for **Svelte 5**. It combines the intuitive cross-block selection of document editors (Google Docs, ProseMirror) with the structural flexibility of block editors (Notion), built on a **Single ContentEditable** architecture and a **CRDT core** (Loro).
5
+ **Website, documentation and live demos: [nabu.aion.builders](https://nabu.aion.builders)**
6
6
 
7
- ## Installation
7
+ > **Status: public beta** (`0.1.0`). The editing core is complete and tested, but the API may still change before 1.0. Breaking changes are listed in the [changelog](https://nabu.aion.builders/changelog) ([CHANGELOG.md](CHANGELOG.md)). Known gaps (mobile keyboards, Firefox and Safari, tables, real-time collaboration between machines) are listed honestly on the [limits page](https://nabu.aion.builders/architecture/limits).
8
+
9
+ ### Versioning (0.x)
10
+
11
+ - A `0.` version means the API can still change. 1.0.0 will mark the stability commitment.
12
+ - Breaking changes arrive only in a minor version (0.2.0, 0.3.0, ...), each with a migration note in the changelog.
13
+ - Patch versions (0.1.x) are fixes only: they keep the public API and the stable `@aionbuilders/nabu/extend` contract.
14
+ - `nabu.internals` is unstable and may change in any version.
15
+
16
+ ## Install
8
17
 
9
18
  ```bash
10
19
  npm install @aionbuilders/nabu
@@ -12,189 +21,118 @@ npm install @aionbuilders/nabu
12
21
  bun add @aionbuilders/nabu
13
22
  ```
14
23
 
15
- ## Required: Vite Setup
16
-
17
- Nabu depends on `loro-crdt`, which ships as a WebAssembly module. You must configure your Vite bundler to handle it.
18
-
19
- **1. Install the required Vite plugins:**
24
+ ### Requirements
20
25
 
21
- ```bash
22
- npm install -D vite-plugin-wasm vite-plugin-top-level-await
23
- ```
24
-
25
- **2. Add them to your `vite.config.js`:**
26
+ - **Svelte 5.20 or newer**.
27
+ - A Vite setup (SvelteKit, or Svelte + Vite) that can load WebAssembly. Loro ships as a WebAssembly module loaded with top-level `await`, so the build needs `vite-plugin-wasm` and `build.target: 'esnext'`. The bundled helper does both:
26
28
 
27
29
  ```js
30
+ // vite.config.js
28
31
  import { defineConfig } from 'vite';
29
32
  import { sveltekit } from '@sveltejs/kit/vite'; // or svelte()
30
- import wasm from 'vite-plugin-wasm';
31
- import topLevelAwait from 'vite-plugin-top-level-await';
33
+ import { nabu } from '@aionbuilders/nabu/vite';
32
34
 
33
35
  export default defineConfig({
34
- plugins: [
35
- sveltekit(),
36
- wasm(),
37
- topLevelAwait()
38
- ]
36
+ plugins: [sveltekit(), nabu()]
39
37
  });
40
38
  ```
41
39
 
42
- Without this setup, Nabu will fail to load.
40
+ Without this setup, Nabu fails to load. Why `esnext`, the manual equivalent and common errors: [Installation](https://nabu.aion.builders/docs/getting-started/installation).
43
41
 
44
- ## Basic Usage
45
-
46
- **With a preset (recommended):**
42
+ ## Minimal example
47
43
 
48
44
  ```svelte
49
45
  <script>
50
- import { NabuEditor, createFullEditor } from '@aionbuilders/nabu';
51
-
52
- const engine = createFullEditor();
53
- </script>
54
-
55
- <NabuEditor {engine} />
56
- ```
57
-
58
- **Manual setup:**
46
+ import { NabuEditor, createDocumentEditor } from '@aionbuilders/nabu';
59
47
 
60
- ```svelte
61
- <script>
62
- import {
63
- Nabu,
64
- NabuEditor,
65
- ParagraphExtension,
66
- HeadingExtension,
67
- ListExtension,
68
- ListItemExtension,
69
- DialogueExtension,
70
- RichTextExtension
71
- } from '@aionbuilders/nabu';
72
-
73
- const engine = new Nabu({
74
- extensions: [
75
- ParagraphExtension,
76
- HeadingExtension,
77
- ListExtension,
78
- ListItemExtension,
79
- DialogueExtension,
80
- RichTextExtension
81
- ]
82
- });
48
+ const engine = createDocumentEditor();
83
49
  </script>
84
50
 
85
51
  <NabuEditor {engine} />
86
52
  ```
87
53
 
88
- ## Presets
54
+ Then read and write the content from the engine:
89
55
 
90
- Nabu ships with ready-to-use presets for common use cases.
56
+ ```js test
57
+ import { createDocumentEditor } from '@aionbuilders/nabu';
91
58
 
92
- ### Preset arrays
59
+ const engine = createDocumentEditor();
60
+ await engine.importMarkdown('# Hello\n\nSome **bold** text.');
93
61
 
94
- Composable — spread and extend them with your own extensions:
95
-
96
- ```js
97
- import { FullPreset, createEditor } from '@aionbuilders/nabu';
98
-
99
- const engine = createEditor({
100
- preset: FullPreset,
101
- extensions: [MyCustomExtension]
102
- });
62
+ expect(engine.serialize('markdown')).toBe('# Hello\n\nSome **bold** text.');
63
+ expect(engine.serialize('json').blocks).toHaveLength(2);
103
64
  ```
104
65
 
105
- | Preset | Includes |
106
- |---|---|
107
- | `MinimalPreset` | Paragraph only |
108
- | `TextPreset` | Paragraph + rich text marks |
109
- | `DocumentPreset` | Paragraph + Headings + rich text |
110
- | `FullPreset` | All built-in blocks + rich text (including Dialogue) |
66
+ Add the optional UI from `@aionbuilders/nabu/ui`, and import a theme once:
111
67
 
112
- ### Factory functions
68
+ ```svelte
69
+ <script>
70
+ import '@aionbuilders/nabu/nabu-theme.css';
71
+ import { NabuEditor, createDocumentEditor } from '@aionbuilders/nabu';
72
+ import { FloatingToolbar, SlashMenu, DragHandle } from '@aionbuilders/nabu/ui';
113
73
 
114
- One-liner shortcuts for the most common cases:
74
+ const engine = createDocumentEditor();
75
+ </script>
115
76
 
116
- ```js
117
- import {
118
- createMinimalEditor, // paragraph only — textarea replacement
119
- createTextEditor, // paragraph + bold/italic/...
120
- createDocumentEditor, // paragraph + headings + rich text
121
- createFullEditor, // everything
122
- createEditor // generic — takes any preset
123
- } from '@aionbuilders/nabu';
124
-
125
- const engine = createFullEditor();
126
-
127
- // Add custom extensions on top of a preset
128
- const engine = createTextEditor({ extensions: [MyExtension] });
77
+ <FloatingToolbar {engine} />
78
+ <SlashMenu {engine} />
79
+ <DragHandle {engine} />
80
+ <NabuEditor {engine} label="Notes" />
129
81
  ```
130
82
 
131
- ## Extensions
132
-
133
- All block types and behaviors are provided as extensions. Pass them to the `Nabu` constructor:
134
-
135
- | Extension | Provides | Keyboard shortcuts |
136
- |---|---|---|
137
- | `ParagraphExtension` | `paragraph` block | — |
138
- | `HeadingExtension` | `heading` block (h1–h6) | `# ` → h1, `## ` → h2, ... |
139
- | `ListExtension` | `list` container (bullet/ordered) | — |
140
- | `ListItemExtension` | `list-item` block | `Tab` indent, `Shift+Tab` unindent |
141
- | `DialogueExtension` | `dialogue` block | `-- ` → dialogue, `Backspace` at start → paragraph |
142
- | `RichTextExtension` | Inline marks (bold, italic...) | `Ctrl+B/I/U/E`, `Ctrl+Shift+X` |
143
-
144
- ### Typographic substitutions
83
+ Save to the browser (IndexedDB) with one call, and show a document as a viewer with `readonly`:
145
84
 
146
- When `RichTextExtension` or any text-bearing block is active, Nabu automatically applies typographic substitutions as you type:
147
-
148
- | Input | Output | Description |
149
- |---|---|---|
150
- | `--` + space | `— ` | Em-dash (French dialogue convention) |
85
+ ```svelte
86
+ <script>
87
+ import { NabuEditor, createPersistedEditor } from '@aionbuilders/nabu';
151
88
 
152
- ## Programmatic API
89
+ let engine = $state(null);
90
+ createPersistedEditor({ docId: 'my-notes' }).then((created) => (engine = created));
91
+ </script>
153
92
 
154
- ```js
155
- // Insert blocks
156
- engine.insert('paragraph', { text: 'Hello' });
157
- engine.insert('heading', { level: 1, text: 'Title' });
158
- engine.insert('list', { listType: 'bullet' });
159
- engine.insert('dialogue', { text: 'Tu viens ce soir ?' });
93
+ {#if engine}
94
+ <NabuEditor {engine} label="Notes" />
95
+ <!-- or, as a read-only viewer: <NabuEditor {engine} readonly /> -->
96
+ {/if}
97
+ ```
160
98
 
161
- // Undo / Redo
162
- engine.undo();
163
- engine.redo();
99
+ ## Entry points
164
100
 
165
- // Serialize
166
- const markdown = engine.serialize('markdown');
167
- // dialogue blocks serialize as: — Tu viens ce soir ?
101
+ The public API is these entry points; everything else is internal and may change without notice.
168
102
 
169
- const json = engine.serialize('json');
170
- ```
103
+ | Import | What it is for |
104
+ |---|---|
105
+ | `@aionbuilders/nabu` | Using the editor: `NabuEditor`, the factories and presets, the built-in extensions, `createPersistedEditor`, `Files`, `Regions`, `Todos`, `deltaToMarkdown`... |
106
+ | `@aionbuilders/nabu/ui` | The optional UI: `FloatingToolbar`, `SlashMenu`, `DragHandle`, `CommandInvite`, `CommandChooser` |
107
+ | `@aionbuilders/nabu/extend` | Writing your own blocks and extensions: `extension`, `Block`, `TextBlock`, `MegaBlock`, `TextBehavior`, `RichText`, key-chord helpers |
108
+ | `@aionbuilders/nabu/vite` | The Vite helper `nabu()` shown above |
171
109
 
172
- ## Architecture
110
+ ## Presets
173
111
 
174
- ### Single ContentEditable
175
- The entire editor lives in a single `contenteditable` root. This enables seamless cross-block selection, native copy-paste, and consistent browser performance.
112
+ A preset is an array of extensions, and each has a factory.
176
113
 
177
- ### CRDT Core (Loro)
178
- All state is stored in a [Loro](https://loro.dev) document — a WASM-based CRDT. This provides:
179
- - **Local-first**: offline editing out of the box
180
- - **Collaboration-ready**: conflict-free concurrent edits
181
- - **Rich text**: native delta/mark support via `LoroText`
114
+ | Preset | Factory | Contains | Tab |
115
+ |---|---|---|---|
116
+ | `MinimalPreset` | `createMinimalEditor()` | Paragraphs; plain text, HTML and Nabu paste; `BlockExtension` | leaves the editor |
117
+ | `TextPreset` | `createTextEditor()` | Minimal + rich text marks, typography, inline markdown shortcuts, markdown paste | leaves the editor |
118
+ | `DocumentPreset` | `createDocumentEditor()` | Text + headings, blockquote, divider, code, callout, toggle, image, video, file. **No lists** | stays in the editor |
119
+ | `FullPreset` | `createFullEditor()` | Document + lists (bullet, numbered, to-do), dialogue, regions | stays in the editor |
182
120
 
183
- ### Extension System
184
- Every block type is an extension. You can build custom block types by extending the `Block` or `MegaBlock` base classes and registering them via the `extension()` helper.
121
+ `createEditor({ preset, extensions })` is the generic factory: any preset, plus your own extensions. Details, the exact extension lists and how to add or remove extensions: [Presets](https://nabu.aion.builders/docs/getting-started/presets).
185
122
 
186
- ### Svelte 5 Runes
187
- Nabu uses `$state` and `$derived` throughout for fine-grained reactivity — only modified blocks re-render.
123
+ ## Documentation
188
124
 
189
- ---
125
+ Everything is on the website: **[nabu.aion.builders/docs](https://nabu.aion.builders/docs)**.
190
126
 
191
- ## Status
127
+ - [Installation](https://nabu.aion.builders/docs/getting-started/installation)
128
+ - [Your first editor](https://nabu.aion.builders/docs/getting-started/first-editor): mounting, reading and writing content, listening to changes
129
+ - [Presets](https://nabu.aion.builders/docs/getting-started/presets)
130
+ - Guides: [Extensions](https://nabu.aion.builders/docs/guides/extensions) (including the stable `/extend` contract), [Commands and keymap](https://nabu.aion.builders/docs/guides/commands-and-keymap), [Persistence and sync](https://nabu.aion.builders/docs/guides/persistence-and-sync), [Regions](https://nabu.aion.builders/docs/guides/regions), [Accessibility](https://nabu.aion.builders/docs/guides/accessibility), [Theming](https://nabu.aion.builders/docs/guides/theming)
131
+ - Recipes, such as the [read-only viewer](https://nabu.aion.builders/docs/recipes/read-only-viewer)
132
+ - [API reference](https://nabu.aion.builders/api), [architecture](https://nabu.aion.builders/architecture) and [live labs](https://nabu.aion.builders/labs)
192
133
 
193
- This is an **alpha release**. The core editing experience is functional, but some features are still in development:
134
+ In the repository: [architecture decisions](docs/adr), [design notes](docs/design), [tests](tests/README.md).
194
135
 
195
- - [ ] Persistence (IndexedDB)
196
- - [ ] Floating toolbar UI
197
- - [ ] Slash command menu
198
- - [ ] Link support
136
+ ## License
199
137
 
200
- Contributions and feedback welcome.
138
+ MIT, as declared in `package.json`.
@@ -1,4 +1,7 @@
1
1
  <script>
2
+ import { sanitizeUrl } from '../../utils/html.js';
3
+ import { linkAttributes } from '../../utils/links.js';
4
+
2
5
  /** @type {{ delta: import('loro-crdt').Delta<string>[] }} */
3
6
  let { delta } = $props();
4
7
 
@@ -13,16 +16,32 @@
13
16
  ].filter(Boolean).join(' ');
14
17
  }
15
18
 
19
+ // Only a link to ANOTHER site opens in a new tab; the origin is read once (not known while rendering on the server: see links.js).
20
+ const origin = typeof location === 'undefined' ? undefined : location.origin;
21
+
16
22
  const segments = $derived(delta.length ? delta : [{ insert: '\n' }]);
23
+
24
+ // A trailing "\n" does not open a visible line by itself (white-space: pre-wrap): the new line only exists once something
25
+ // follows the break. A filler <br> after it (ProseMirror's "trailing hack") gives that last, empty line a line box, so the
26
+ // caret sits on it right away. The filler is pure rendering: it holds no text, so offsets / Range.toString / the
27
+ // TreeWalker over text nodes ignore it; it is not in the model nor the clipboard. An EMPTY delta renders a lone "\n"
28
+ // text node instead (one line already) — no filler there.
29
+ const lastInsert = $derived(delta.length ? delta[delta.length - 1].insert : '');
30
+ const needsFiller = $derived(typeof lastInsert === 'string' && lastInsert.endsWith('\n'));
17
31
  </script>
18
32
 
19
33
  {#each segments as op}
20
- {#if op.attributes && Object.values(op.attributes).some(Boolean)}
34
+ {@const link = /** @type {any} */ (op.attributes)?.link}
35
+ {@const href = link ? sanitizeUrl(link, { kind: 'link' }) : null}
36
+ {#if href !== null}
37
+ {@const attrs = linkAttributes(href, origin)}
38
+ <a {href} class="nabu-link {getClasses(op.attributes ?? {})}" target={attrs.target} rel={attrs.rel}>{op.insert}</a>
39
+ {:else if op.attributes && Object.values(op.attributes).some(Boolean)}
21
40
  <span class={getClasses(op.attributes)}>{op.insert}</span>
22
41
  {:else}
23
42
  {op.insert}
24
43
  {/if}
25
- {/each}
44
+ {/each}{#if needsFiller}<br data-nabu-filler>{/if}
26
45
 
27
46
  <style>
28
47
  :global(.nabu-bold) { font-weight: bold; }
@@ -30,4 +49,8 @@
30
49
  :global(.nabu-underline) { text-decoration: underline; }
31
50
  :global(.nabu-strikethrough) { text-decoration: line-through; }
32
51
  :global(.nabu-code) { font-family: monospace; background: rgba(135, 131, 120, 0.15); border-radius: 3px; padding: 0.1em 0.3em; }
52
+ :global(.nabu-link) { color: var(--nb-link, #0b57d0); text-decoration: underline; cursor: pointer; } /* fallback 6.4:1 on white (AA text: 4.5:1); the base theme sets --nb-link for both schemes */
53
+ @media (prefers-color-scheme: dark) {
54
+ :global(.nabu-link) { color: var(--nb-link, #8ab4f8); } /* fallback 7.9:1 on #1e1e1e, 9.9:1 on black */
55
+ }
33
56
  </style>
@@ -1,3 +1,5 @@
1
1
  export * from "./text.behavior.svelte.js";
2
2
  export { RichTextExtension } from "./rich-text.extension.js";
3
+ export { TypographyExtension } from "./typography.extension.js";
4
+ export { MarkdownShortcutsExtension } from "./markdown-shortcuts.extension.js";
3
5
  export { default as RichText } from "./RichText.svelte";
@@ -1,3 +1,5 @@
1
1
  export * from './text.behavior.svelte.js';
2
2
  export { RichTextExtension } from './rich-text.extension.js';
3
+ export { TypographyExtension } from './typography.extension.js';
4
+ export { MarkdownShortcutsExtension } from './markdown-shortcuts.extension.js';
3
5
  export { default as RichText } from './RichText.svelte';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Inline markdown shortcuts while typing: `**bold**`, `_italic_`, `` `code` ``, `~~strike~~` turn into marks as the closing delimiter is typed.
3
+ */
4
+ export const MarkdownShortcutsExtension: Extension;
5
+ import { Extension } from '../../utils/extensions.js';
@@ -0,0 +1,79 @@
1
+ import { Extension } from '../../utils/extensions.js';
2
+ import { TextBehavior } from './text.behavior.svelte.js';
3
+ import { WORD } from './markdown-syntax.js';
4
+
5
+ // Flanking rules (CommonMark, simplified): an opening delimiter must be followed by a non-whitespace
6
+ // char and a closing one preceded by a non-whitespace char, so `2 * 3 * 4` and `* not *` stay literal.
7
+ // Only the whitespace half of CommonMark's flanking rules is implemented (no punctuation special case).
8
+ // `_` / `__` additionally never open or close INTRAWORD (`snake_case_name` stays literal); `*`, `~~`
9
+ // are allowed intraword (`a*b*c` → italic b, as in CommonMark / GFM). The paste / import parser applies the same
10
+ // rules (`markdown-syntax.js` `delimiterFlags`), so typing `*a*` and pasting `*a*` agree.
11
+
12
+ /**
13
+ * Inner content of a delimited span: no `d`, non-whitespace first AND last char (a single char is fine).
14
+ * @param {string} d the delimiter character
15
+ */
16
+ const inner = (d) => `[^${d}\\s](?:[^${d}]*[^${d}\\s])?`;
17
+
18
+ /**
19
+ * Build an inputRule that, when `closeChar` is typed and `pattern` matches the
20
+ * text before the cursor, strips the markdown delimiters and applies mark(s)
21
+ * to the inner content. `pattern` MUST capture the bare content in group 1, and
22
+ * match[0] MUST be exactly the delimiters+content to delete (no extra chars).
23
+ * @param {RegExp} pattern
24
+ * @param {string} closeChar the closing delimiter character being typed
25
+ * @param {string | string[]} marks mark name(s) to apply
26
+ * @param {{ noIntrawordClose?: boolean }} [opts] `noIntrawordClose`: refuse to close when the char
27
+ * right after the cursor is a letter/digit (the closing `_` would be intraword)
28
+ * @returns {import('../../utils/extensions.js').InputRule}
29
+ */
30
+ function markRule(pattern, closeChar, marks, opts = {}) {
31
+ const names = Array.isArray(marks) ? marks : [marks];
32
+ return {
33
+ pattern,
34
+ handler(nabu, block, match, sel) {
35
+ if (sel.data !== closeChar) return;
36
+ const behavior = block.behaviors.get('text');
37
+ if (!(behavior instanceof TextBehavior)) return;
38
+ if (opts.noIntrawordClose && /[\p{L}\p{N}]/u.test(behavior.text.charAt(sel.from))) return;
39
+ const full = match[0]; // delimiters + content present before the cursor
40
+ const inner = match[1]; // bare content
41
+ if (!inner) return;
42
+ const start = sel.from - full.length;
43
+ behavior.delete({ index: start, length: full.length });
44
+ behavior.insert(start, inner);
45
+ for (const name of names) {
46
+ behavior.applyMark(name, true, { from: start, to: start + inner.length, isCollapsed: false });
47
+ }
48
+ return true;
49
+ }
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Inline markdown shortcuts while typing: `**bold**`, `_italic_`, `` `code` ``, `~~strike~~` turn into marks as the closing delimiter is typed.
55
+ */
56
+ export const MarkdownShortcutsExtension = new Extension('markdown-shortcuts', {
57
+ // ORDER MATTERS: most-specific (longest) delimiters first.
58
+ // The `(?<!d)` lookbehinds make an opener the START of its delimiter run: `**…*` must not fire
59
+ // inside `***x*` (typing `***x***`: the 2nd closing `*` used to bold `x*` and leave a stray `*`).
60
+ inputRules: [
61
+ // ***bold-italic*** (asterisks) — typed final '*' after '***x**'
62
+ markRule(new RegExp(`(?<!\\*)\\*\\*\\*(${inner('*')})\\*\\*$`, 'u'), '*', ['bold', 'italic']),
63
+ // **bold** — typed final '*' after '**x*'
64
+ markRule(new RegExp(`(?<!\\*)\\*\\*(${inner('*')})\\*$`, 'u'), '*', 'bold'),
65
+ // *italic* — typed final '*' after '*x' (not preceded by '*')
66
+ markRule(new RegExp(`(?<!\\*)\\*(${inner('*')})$`, 'u'), '*', 'italic'),
67
+ // ___bold-italic___ (underscores, not intraword) — typed final '_' after '___x__'
68
+ markRule(new RegExp(`(?<![${WORD}])___(${inner('_')})__$`, 'u'), '_', ['bold', 'italic'], { noIntrawordClose: true }),
69
+ // __bold__ — typed final '_' after '__x_'
70
+ markRule(new RegExp(`(?<![${WORD}])__(${inner('_')})_$`, 'u'), '_', 'bold', { noIntrawordClose: true }),
71
+ // _italic_ — typed final '_' after '_x' (opener preceded by a non-word char or the block start)
72
+ markRule(new RegExp(`(?<![${WORD}])_(${inner('_')})$`, 'u'), '_', 'italic', { noIntrawordClose: true }),
73
+ // `code` — typed final '`' after '`x'. Code spans keep inner spaces (`a b`), but need
74
+ // at least one non-space char and don't open inside a longer backtick run.
75
+ markRule(/(?<!`)`([^`]*[^`\s][^`]*)$/, '`', 'code'),
76
+ // ~~strike~~ — typed final '~' after '~~x~'
77
+ markRule(new RegExp(`(?<!~)~~(${inner('~')})~$`, 'u'), '~', 'strikethrough'),
78
+ ],
79
+ });
@@ -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';