@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.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +82 -144
- package/dist/behaviors/text/RichText.svelte +25 -2
- package/dist/behaviors/text/index.d.ts +2 -0
- package/dist/behaviors/text/index.js +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +135 -53
- package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
- package/dist/behaviors/text/text.behavior.svelte.js +657 -55
- package/dist/behaviors/text/typography.extension.d.ts +7 -0
- package/dist/behaviors/text/typography.extension.js +25 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +180 -14
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +8 -9
- package/dist/blocks/block.svelte.d.ts +356 -68
- package/dist/blocks/block.svelte.js +388 -42
- package/dist/blocks/blockquote/Blockquote.svelte +39 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
- package/dist/blocks/blockquote/blockquote.extension.js +80 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +67 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +4 -0
- package/dist/blocks/callout/callout.extension.js +84 -0
- package/dist/blocks/callout/callout.svelte.d.ts +80 -0
- package/dist/blocks/callout/callout.svelte.js +189 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +84 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +4 -0
- package/dist/blocks/code/code.extension.js +88 -0
- package/dist/blocks/code/code.svelte.d.ts +68 -0
- package/dist/blocks/code/code.svelte.js +245 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -2
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
- package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/Divider.svelte +28 -0
- package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
- package/dist/blocks/divider/divider.extension.d.ts +23 -0
- package/dist/blocks/divider/divider.extension.js +153 -0
- package/dist/blocks/divider/divider.svelte.d.ts +54 -0
- package/dist/blocks/divider/divider.svelte.js +89 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +56 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +4 -0
- package/dist/blocks/file/file.extension.js +27 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +77 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +2 -1
- package/dist/blocks/heading/heading.svelte.d.ts +27 -34
- package/dist/blocks/heading/heading.svelte.js +72 -60
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +41 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +5 -0
- package/dist/blocks/image/image.extension.js +34 -0
- package/dist/blocks/image/image.svelte.d.ts +61 -0
- package/dist/blocks/image/image.svelte.js +114 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +11 -1
- package/dist/blocks/index.js +11 -1
- package/dist/blocks/list/List.svelte +9 -2
- package/dist/blocks/list/ListItem.svelte +44 -7
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +7 -3
- package/dist/blocks/list/index.js +43 -31
- package/dist/blocks/list/list-item.svelte.d.ts +43 -17
- package/dist/blocks/list/list-item.svelte.js +143 -93
- package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
- package/dist/blocks/list/list.behavior.svelte.js +10 -5
- package/dist/blocks/list/list.svelte.d.ts +101 -23
- package/dist/blocks/list/list.svelte.js +136 -114
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/list/todos.svelte.d.ts +35 -0
- package/dist/blocks/list/todos.svelte.js +76 -0
- package/dist/blocks/media/index.d.ts +2 -0
- package/dist/blocks/media/index.js +2 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +107 -0
- package/dist/blocks/media/media-block.svelte.js +181 -0
- package/dist/blocks/media/media.helpers.d.ts +24 -0
- package/dist/blocks/media/media.helpers.js +88 -0
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +115 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +846 -75
- package/dist/blocks/nabu.svelte.js +1990 -182
- package/dist/blocks/paragraph/Paragraph.svelte +1 -0
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -16
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
- package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +632 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +70 -0
- package/dist/blocks/text-block.svelte.js +108 -0
- package/dist/blocks/toggle/Toggle.svelte +73 -0
- package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
- package/dist/blocks/toggle/index.d.ts +3 -0
- package/dist/blocks/toggle/index.js +3 -0
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
- package/dist/blocks/toggle/toggle.extension.js +18 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
- package/dist/blocks/toggle/toggle.svelte.js +352 -0
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +42 -0
- package/dist/blocks/video/Video.svelte.d.ts +11 -0
- package/dist/blocks/video/index.d.ts +3 -0
- package/dist/blocks/video/index.js +3 -0
- package/dist/blocks/video/video.extension.d.ts +4 -0
- package/dist/blocks/video/video.extension.js +27 -0
- package/dist/blocks/video/video.svelte.d.ts +29 -0
- package/dist/blocks/video/video.svelte.js +80 -0
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +363 -0
- package/dist/files/files.svelte.js +897 -0
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +3 -0
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -6
- package/dist/index.js +117 -7
- package/dist/nabu-theme.css +112 -0
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +555 -83
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -24
- package/dist/presets.js +51 -14
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +217 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +165 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
- package/dist/ui/SlashMenu.svelte +215 -0
- package/dist/ui/SlashMenu.svelte.d.ts +33 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/slash-menu.svelte.d.ts +112 -0
- package/dist/ui/slash-menu.svelte.js +150 -0
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +488 -40
- package/dist/utils/extensions.js +300 -19
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +88 -0
- package/dist/utils/placeholders.js +91 -0
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +34 -0
- package/package.json +61 -22
- package/dist/blocks/zone/Zone.svelte +0 -258
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -221
- package/dist/blocks/zone/zone.svelte.d.ts +0 -112
- package/dist/blocks/zone/zone.svelte.js +0 -321
package/dist/utils/extensions.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @typedef {Object} PasteBlock
|
|
7
7
|
* @property {string} type
|
|
8
|
-
* @property {'start' | 'end' | 'both' | false} partial
|
|
8
|
+
* @property {'start' | 'end' | 'both' | false} [partial] absent = false (a whole block)
|
|
9
9
|
* @property {Record<string, any>} [props]
|
|
10
10
|
* @property {import('loro-crdt').Delta<string>[]} [delta]
|
|
11
11
|
* @property {PasteBlock[]} [children]
|
|
@@ -13,64 +13,345 @@
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @typedef {Object} PasteFragment
|
|
16
|
+
* @property {boolean} [external] the content comes from outside the editor (the HTML of a web page, Docs…) rather than from
|
|
17
|
+
* Nabu itself: `files.importRemote` applies to its remote media URLs
|
|
16
18
|
* @property {PasteBlock[]} blocks
|
|
19
|
+
* @property {Record<string, import('../files/files.svelte.js').EmbeddedAsset>} [assets] bytes (base64) of the
|
|
20
|
+
* `nabu-asset:` refs used by `blocks` (see docs/design/clipboard-format.md)
|
|
17
21
|
*/
|
|
18
22
|
|
|
19
23
|
/**
|
|
20
24
|
* @typedef {Object} PasteInterpreter
|
|
21
25
|
* @property {string} format - MIME type géré ('application/x-nabu+json', 'text/html', 'text/plain')
|
|
22
26
|
* @property {number} [priority] - Plus haut = appelé en premier (default: 0)
|
|
23
|
-
* @property {(raw: string, nabu: Nabu) => PasteFragment | null} interpret
|
|
27
|
+
* @property {(raw: string, nabu: Nabu, options?: PasteInterpretOptions) => PasteFragment | null} interpret
|
|
28
|
+
* @property {(raw: string) => boolean} [outranksFiles] - Files on the clipboard normally win over the text payloads. Return
|
|
29
|
+
* true when THIS payload is worth more than the files that come with it (a spreadsheet range is put on the clipboard as
|
|
30
|
+
* a table AND as an image of it).
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {Object} PasteFileHandler
|
|
35
|
+
* What an extension does with a FILE pasted from the clipboard (a screenshot, a file copied from the OS): it makes a block
|
|
36
|
+
* of it. The engine reads the bytes, stores them through `Files` (in the document, or sent to `files.onBeforePersist` with the
|
|
37
|
+
* optimistic `pending` block) and inserts the blocks at the caret, in the order of the files, as ONE undo step. The engine
|
|
38
|
+
* names no block type: whatever an extension accepts is its own business.
|
|
39
|
+
* @property {number} [priority] - Higher is asked first (default 0). The first handler that accepts a file takes it.
|
|
40
|
+
* @property {(file: File, nabu: Nabu) => boolean} accepts - Whether this handler takes `file` (by MIME type, name…)
|
|
41
|
+
* @property {(file: File, nabu: Nabu) => PasteBlock | null} block - The block the file becomes: `type` and the props other
|
|
42
|
+
* than `ref` (`name`, `alt`…), which the engine sets to the stored file. `null` declines the file.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A rule of a block class's `static htmlRules`: which HTML elements the block reads on paste. The rules of every registered
|
|
47
|
+
* block form one ruleset, tried by descending `priority` (default 0); the first whose `selector` (and `filter`) matches wins and
|
|
48
|
+
* its class's `static fromHTML(el, helpers, rule, siblings)` makes the block. Extra fields are the rule's own: the class reads them
|
|
49
|
+
* back from `rule`.
|
|
50
|
+
* @typedef {{
|
|
51
|
+
* selector: string,
|
|
52
|
+
* priority?: number,
|
|
53
|
+
* filter?: (el: Element) => boolean,
|
|
54
|
+
* embed?: boolean,
|
|
55
|
+
* props?: Record<string, any>,
|
|
56
|
+
* [field: string]: any,
|
|
57
|
+
* }} HtmlRule
|
|
58
|
+
* `selector` is a CSS selector tested with `el.matches()`. `priority`: higher is tried first (a specific block beats a generic
|
|
59
|
+
* fallback). `filter`: an extra test once the selector matches. `embed`: the element may sit inside the inline content of another
|
|
60
|
+
* block and is then lifted out as a block of its own. `props`: props the rule asks `fromHTML` to set.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* What a block's `static fromHTML` receives to read an element.
|
|
65
|
+
* @typedef {Object} HtmlHelpers
|
|
66
|
+
* @property {(el: Element) => import('loro-crdt').Delta<string>[]} parseInline - The inline content of an element, as a Loro delta
|
|
67
|
+
* @property {(el: Element) => PasteBlock | null} parseBlock - The first block an element makes
|
|
68
|
+
* @property {(el: Element) => PasteBlock[]} parseBlocks - Every block an element makes
|
|
69
|
+
* @property {(el: Element) => PasteBlock[]} parseChildren - The blocks made by each child of an element
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* What a block's `static fromMarkdown` receives to read its lines (and what `markdownRules` callbacks receive).
|
|
74
|
+
* @typedef {Object} MarkdownHelpers
|
|
75
|
+
* @property {(text: string) => import('loro-crdt').Delta<string>[]} parseInline - Inline markdown to a Loro delta
|
|
76
|
+
* @property {(text: string) => any[]} parseInlineSegments - Inline markdown to segments (text, images...)
|
|
77
|
+
* @property {boolean} joinLines - CommonMark semantics: consecutive lines join into one paragraph (`importMarkdown`); false on paste
|
|
78
|
+
* @property {string} [softBreak] - What replaces a soft line break in a nested parse
|
|
79
|
+
* @property {(lines: string[], index: number) => boolean} interrupts - Whether the line at `index` starts a block that ends the paragraph above it
|
|
80
|
+
* @property {(lines: string[], overrides?: { joinLines?: boolean, softBreak?: string }) => PasteBlock[]} [parseBlocks] - Parse lines as a document of their own (the body of a quote...)
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A rule of a block class's `static markdownRules`: how a block starts and how many lines it takes. The rules of every registered
|
|
85
|
+
* block form one ruleset, tried by descending `priority` (default 0) at each line; the first whose `detect` matches and whose class's
|
|
86
|
+
* `static fromMarkdown(lines, helpers, rule)` returns a block (or several) takes the lines `consume` counted. Extra fields are the
|
|
87
|
+
* rule's own.
|
|
88
|
+
* @typedef {{
|
|
89
|
+
* detect: RegExp | ((line: string, lines: string[], index: number, helpers: MarkdownHelpers) => boolean),
|
|
90
|
+
* consume: (lines: string[], index: number, helpers: MarkdownHelpers) => number,
|
|
91
|
+
* priority?: number,
|
|
92
|
+
* interruptsParagraph?: boolean | ((line: string, lines: string[], index: number, helpers: MarkdownHelpers) => boolean),
|
|
93
|
+
* [field: string]: any,
|
|
94
|
+
* }} MarkdownRule
|
|
95
|
+
* `detect`: does the block start at this line. `consume`: how many lines it takes from `index` (at least 1). `priority`: higher is
|
|
96
|
+
* tried first. `interruptsParagraph`: the block may start in the middle of a paragraph (a fence, a heading, a quote...); `true`
|
|
97
|
+
* means "whenever `detect` matches".
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The second argument of a block's `static toMarkdown(pb, ctx)` / `static toHtml(pb, ctx)`: the clipboard and `serialize` call it
|
|
102
|
+
* once per block, with helpers to serialize children without knowing their type. Other fields are the caller's own (a list passes its `listType`, `index`...).
|
|
103
|
+
* @typedef {{
|
|
104
|
+
* recurse: (child: PasteBlock, ctx?: Record<string, any>) => string,
|
|
105
|
+
* recurseAll?: (children: PasteBlock[], ctx?: Record<string, any>) => string[],
|
|
106
|
+
* before?: PasteBlock[],
|
|
107
|
+
* after?: PasteBlock[],
|
|
108
|
+
* [field: string]: any,
|
|
109
|
+
* }} SerializeContext
|
|
110
|
+
* `recurse`: serialize one child block. `recurseAll` (markdown only): serialize siblings in order, telling each its neighbours.
|
|
111
|
+
* `before` / `after` (markdown only): the siblings around this block.
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {Object} PasteInterpretOptions
|
|
116
|
+
* @property {boolean} [joinLines] - false/absent (paste): one non-blank line = one block.
|
|
117
|
+
* true (`importMarkdown`): CommonMark semantics, consecutive lines join into one paragraph.
|
|
118
|
+
* Forwarded to block markdown rules as `helpers.joinLines` (and 3rd arg of `consume`).
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @typedef {Object} InputRule
|
|
123
|
+
* A declarative text-replacement rule applied while typing: when the text before the caret matches `pattern`, `handler` runs
|
|
124
|
+
* (the markdown shortcuts and the typography substitutions are input rules).
|
|
125
|
+
* @property {RegExp} pattern - Testé sur le texte AVANT le curseur (avec l'inputType "insertText")
|
|
126
|
+
* @property {string} [inputType] - Défaut: "insertText"
|
|
127
|
+
* @property {(nabu: Nabu, block: Block, match: RegExpMatchArray, sel: {from: number, to: number, isCollapsed: boolean, data: string}) => boolean | void} handler
|
|
128
|
+
* - `sel.data` est le texte en cours d'insertion (event.data).
|
|
129
|
+
* - Retourne true pour BREAK. NE PAS appeler commit() dans le handler.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @typedef {Object} SplitData
|
|
134
|
+
* What `onSplit` receives besides the block: where the text is cut, and the tail that moves to the new block.
|
|
135
|
+
* @property {number} offset caret offset in the block's text
|
|
136
|
+
* @property {import('loro-crdt').Delta<string>[]} delta the text from `offset` to the end, as a Loro delta
|
|
24
137
|
*/
|
|
25
138
|
|
|
26
139
|
/**
|
|
27
140
|
* @typedef {{
|
|
28
|
-
* onInit:
|
|
29
|
-
* onBlockCreate:
|
|
30
|
-
* onBlockDelete:
|
|
31
|
-
* onBlockUpdate:
|
|
32
|
-
* onBeforeTransaction:
|
|
33
|
-
* onAfterTransaction:
|
|
34
|
-
* onSplit:
|
|
35
|
-
* onBeforeInput:
|
|
36
|
-
*
|
|
37
|
-
*
|
|
141
|
+
* onInit(nabu: Nabu): void,
|
|
142
|
+
* onBlockCreate(nabu: Nabu, block: Block): void,
|
|
143
|
+
* onBlockDelete(nabu: Nabu, block: Block): void,
|
|
144
|
+
* onBlockUpdate(nabu: Nabu, block: Block): void,
|
|
145
|
+
* onBeforeTransaction(nabu: Nabu, ...args: any[]): void,
|
|
146
|
+
* onAfterTransaction(nabu: Nabu, ...args: any[]): void,
|
|
147
|
+
* onSplit(nabu: Nabu, block: Block, event: Event | null, data: SplitData): { block: Block } | void,
|
|
148
|
+
* onBeforeInput(nabu: Nabu, event: InputEvent, block: Block): symbol | boolean | void,
|
|
149
|
+
* onKeyDown(nabu: Nabu, event: KeyboardEvent, block: Block): symbol | boolean | void,
|
|
150
|
+
* onBeforeMerge(nabu: Nabu, survivor: Block, victim: Block): symbol | void,
|
|
151
|
+
* onInput(nabu: Nabu, event: InputEvent, block: Block): void,
|
|
152
|
+
* [hook: string]: (nabu: Nabu, ...args: any[]) => any,
|
|
153
|
+
* }} ExtensionHooks
|
|
154
|
+
* The hooks an extension can register (`ExtensionInit.hooks`). A hook returns `nabu.BREAK` (or, for `onSplit`, an object)
|
|
155
|
+
* to stop the engine from going on; the others return nothing. Any other name is a custom hook, run with `nabu.trigger(name, ...args)`.
|
|
156
|
+
* Declared as methods, so a handler may narrow the block parameter (`(nabu, block: Paragraph) => ...`).
|
|
157
|
+
*
|
|
158
|
+
* `onBlockCreate`, `onBlockDelete`, `onBlockUpdate`, `onAfterTransaction` and `onInput` are declared for forward compatibility:
|
|
159
|
+
* the engine does not call them yet.
|
|
38
160
|
*/
|
|
39
161
|
|
|
40
162
|
/**
|
|
41
163
|
* @typedef {Object} ExtensionInit
|
|
164
|
+
* What an extension is made of: a block class and its component, hooks, serializers, actions, commands, paste interpreters and
|
|
165
|
+
* file handlers, input rules, slash menu entries and Loro text styles. Every field is optional; pass it to `extension(name, init)`.
|
|
42
166
|
* @property {string} [name]
|
|
43
167
|
* @property {import('svelte').Component} [component]
|
|
44
168
|
* @property {typeof Block} [block]
|
|
169
|
+
* @property {boolean} [defaultBlock]
|
|
170
|
+
* Marks `block` as the editor's DEFAULT text block (the one created when the engine needs an
|
|
171
|
+
* empty text block of its own: a caret below a trailing void block, …). Last one wins.
|
|
45
172
|
* @property {Partial<ExtensionHooks>} [hooks]
|
|
46
173
|
* @property {Record<string, (nabu: Nabu) => any>} [serializers]
|
|
47
174
|
* @property {Record<string, (nabu: Nabu, data: any, topic: string) => any>} [actions]
|
|
175
|
+
* Handlers of the action bus, by topic (`nabu.exec(topic, data)`). COLLISION RULE: several extensions may declare the same topic and
|
|
176
|
+
* ALL their handlers run (that is how an extension observes another one's topic), so give a topic of your own a distinctive name
|
|
177
|
+
* (`namespace:verb`): two extensions that each mean something different by `code` would both fire. Built-in topics: `mark:*`, `block:*`, `list:*`...
|
|
178
|
+
* @property {Record<string, import('./commands.js').CommandDef>} [commands]
|
|
179
|
+
* Commands (ADR 017): `{ [id]: { label, keys?, when?, scope?, run(nabu, ctx, data?), confirm? } }`. Each one is also an action of the
|
|
180
|
+
* bus (`nabu.exec(id, data)`); the engine resolves their `keys` at keydown, before the legacy `onKeyDown` hooks.
|
|
181
|
+
* @property {(nabu: Nabu, ctx: import('./commands.js').CommandContext) => Record<string, any> | void} [commandContext]
|
|
182
|
+
* Contributes FACTS to the context given to the `when` / `run` / `label` of commands (e.g. `{ atStartOfRegion: true }`).
|
|
183
|
+
* Called once per candidate key press; keep it cheap. It cannot replace the engine's own fields.
|
|
48
184
|
* @property {PasteInterpreter[]} [pasteInterpreters]
|
|
185
|
+
* @property {PasteFileHandler[]} [pasteFileHandlers]
|
|
186
|
+
* Make a block of a file pasted from the clipboard (see {@link PasteFileHandler}).
|
|
187
|
+
* @property {InputRule[]} [inputRules]
|
|
188
|
+
* Declarative text-replacement rules applied while typing (e.g. markdown
|
|
189
|
+
* shortcuts, smart typography). Tested against the text before the cursor.
|
|
190
|
+
* @property {import('../ui/slash-menu.svelte.js').SlashCommand[]} [slashCommands]
|
|
191
|
+
* Entries this extension adds to the slash menu (collected in `nabu.slashCommands`; the menu has no entry of its own, so a
|
|
192
|
+
* block type absent from the editor is never offered). The menu sorts them by `group`, `order`, then declaration order,
|
|
193
|
+
* whatever the load order of the extensions. A `when(engine, block)` limits an entry to the places where it applies (the
|
|
194
|
+
* block holding the caret); without it the entry is always offered.
|
|
49
195
|
* @property {Record<string, { expand: 'after' | 'before' | 'both' | 'none' }>} [loroTextStyles]
|
|
50
196
|
* Loro text style configuration required by this extension.
|
|
51
197
|
* Passed to `LoroDoc.configTextStyle()` during Nabu initialization so that
|
|
52
198
|
* `applyDelta` with these attributes works correctly.
|
|
53
199
|
*/
|
|
54
200
|
|
|
201
|
+
/**
|
|
202
|
+
* A named bundle of editor behaviour (see {@link ExtensionInit}); the built-in blocks and behaviours are extensions. Build one with
|
|
203
|
+
* `extension(name, init)` and pass it to `createEditor({ extensions })`.
|
|
204
|
+
*/
|
|
55
205
|
export class Extension {
|
|
56
206
|
/** @param {string} name @param {ExtensionInit} init */
|
|
57
207
|
constructor(name, init = {}) {
|
|
208
|
+
/** The extension's name (unique in an editor). */
|
|
58
209
|
this.name = name;
|
|
210
|
+
/** The Svelte component that renders the extension's block. */
|
|
59
211
|
this.component = init.component;
|
|
212
|
+
/** The block class this extension registers. */
|
|
60
213
|
this.block = init.block;
|
|
61
|
-
/**
|
|
214
|
+
/** Whether `block` is the editor's default text block. */
|
|
215
|
+
this.defaultBlock = !!init.defaultBlock;
|
|
216
|
+
/**
|
|
217
|
+
* The hooks of the extension, by name (see {@link ExtensionHooks}).
|
|
218
|
+
* @type {Partial<ExtensionHooks>}
|
|
219
|
+
*/
|
|
62
220
|
this.hooks = init.hooks || {};
|
|
63
|
-
/**
|
|
221
|
+
/**
|
|
222
|
+
* Serializers by format name, run by `nabu.serialize(format)`.
|
|
223
|
+
* @type {Record<string, (nabu: Nabu) => any>}
|
|
224
|
+
*/
|
|
64
225
|
this.serializers = init.serializers || {};
|
|
65
|
-
/**
|
|
226
|
+
/**
|
|
227
|
+
* Handlers of the action bus by topic (`nabu.exec(topic, data)`); several extensions may declare the same topic and all run.
|
|
228
|
+
* @type {Record<string, (nabu: Nabu, data: any, topic: string) => any>}
|
|
229
|
+
*/
|
|
66
230
|
this.actions = init.actions || {};
|
|
67
|
-
/**
|
|
231
|
+
/**
|
|
232
|
+
* The commands this extension declares, by id (see {@link CommandDef}).
|
|
233
|
+
* @type {Record<string, import('./commands.js').CommandDef>}
|
|
234
|
+
*/
|
|
235
|
+
this.commands = init.commands || {};
|
|
236
|
+
/**
|
|
237
|
+
* Contributes facts to the context given to command `when` / `run` functions.
|
|
238
|
+
* @type {ExtensionInit['commandContext']}
|
|
239
|
+
*/
|
|
240
|
+
this.commandContext = init.commandContext;
|
|
241
|
+
/**
|
|
242
|
+
* Interpreters of clipboard formats (see {@link PasteInterpreter}).
|
|
243
|
+
* @type {PasteInterpreter[]}
|
|
244
|
+
*/
|
|
68
245
|
this.pasteInterpreters = init.pasteInterpreters || [];
|
|
69
|
-
/**
|
|
246
|
+
/**
|
|
247
|
+
* Handlers that make a block of a pasted file (see {@link PasteFileHandler}).
|
|
248
|
+
* @type {PasteFileHandler[]}
|
|
249
|
+
*/
|
|
250
|
+
this.pasteFileHandlers = init.pasteFileHandlers || [];
|
|
251
|
+
/**
|
|
252
|
+
* Text-replacement rules applied while typing (see {@link InputRule}).
|
|
253
|
+
* @type {InputRule[]}
|
|
254
|
+
*/
|
|
255
|
+
this.inputRules = init.inputRules || [];
|
|
256
|
+
/**
|
|
257
|
+
* Entries this extension adds to the slash menu.
|
|
258
|
+
* @type {import('../ui/slash-menu.svelte.js').SlashCommand[]}
|
|
259
|
+
*/
|
|
260
|
+
this.slashCommands = init.slashCommands || [];
|
|
261
|
+
/**
|
|
262
|
+
* Loro text style configuration this extension needs for its marks.
|
|
263
|
+
* @type {Record<string, { expand: 'after' | 'before' | 'both' | 'none' }>}
|
|
264
|
+
*/
|
|
70
265
|
this.loroTextStyles = init.loroTextStyles || {};
|
|
71
266
|
}
|
|
72
267
|
}
|
|
73
268
|
|
|
74
269
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Create an extension: the unit that bundles a block type, its component, hooks, commands, actions, paste handlers and slash entries.
|
|
272
|
+
* Shorthand for `new Extension(name, init)`.
|
|
273
|
+
* @param {string} name @param {ExtensionInit} init
|
|
274
|
+
*/
|
|
275
|
+
export const extension = (name, init) => new Extension(name, init);
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Current version of the `application/x-nabu+json` clipboard format (see docs/design/clipboard-format.md).
|
|
279
|
+
* Written on copy as `version`; the Nabu paste interpreter accepts it (and fragments without a version).
|
|
280
|
+
*/
|
|
281
|
+
export const CLIPBOARD_FORMAT_VERSION = 1;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Degrades the blocks whose type is NOT registered in the receiving editor (a fragment copied from a richer
|
|
285
|
+
* editor, an old document, a third-party extension that is not loaded): nothing is dropped.
|
|
286
|
+
* - the block becomes a block of `fallbackType` (the editor's default text block) with its text (`delta`) and
|
|
287
|
+
* `partial` kept; its props are dropped (they belong to the unknown type);
|
|
288
|
+
* - its children are kept as normal blocks RIGHT AFTER it, at the same level (an unknown container has no
|
|
289
|
+
* body to hold them), recursively degraded;
|
|
290
|
+
* - the children of a known block are degraded in place.
|
|
291
|
+
* Pure: returns new objects, never mutates the input; the engine names no block type (`isKnown` and
|
|
292
|
+
* `fallbackType` come from the registry / `defaultBlockType`).
|
|
293
|
+
* @param {PasteBlock[]} blocks
|
|
294
|
+
* @param {(type: string) => boolean} isKnown
|
|
295
|
+
* @param {string | null | undefined} fallbackType the default text block type; falsy = nothing to degrade to
|
|
296
|
+
* @returns {PasteBlock[]}
|
|
297
|
+
*/
|
|
298
|
+
export function degradeUnknownBlocks(blocks, isKnown, fallbackType) {
|
|
299
|
+
if (!Array.isArray(blocks) || !fallbackType) return blocks;
|
|
300
|
+
/** @param {PasteBlock[]} list @returns {PasteBlock[]} */
|
|
301
|
+
const walk = (list) => list.flatMap((pb) => {
|
|
302
|
+
if (!pb || typeof pb !== 'object') return [pb];
|
|
303
|
+
const kids = Array.isArray(pb.children) && pb.children.length ? walk(pb.children) : null;
|
|
304
|
+
if (isKnown(pb.type)) {
|
|
305
|
+
const { children: _drop, ...rest } = pb;
|
|
306
|
+
return [kids ? { ...rest, children: kids } : rest];
|
|
307
|
+
}
|
|
308
|
+
const { children: _c, props: _p, ...rest } = pb;
|
|
309
|
+
/** @type {PasteBlock} */
|
|
310
|
+
const own = { ...rest, type: fallbackType, delta: Array.isArray(pb.delta) ? pb.delta : [], partial: kids ? (pb.partial === 'start' || pb.partial === 'both' ? 'start' : false) : (pb.partial ?? false) };
|
|
311
|
+
return kids ? [own, ...kids] : [own];
|
|
312
|
+
});
|
|
313
|
+
return walk(blocks);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Version written by `serialize('json')` (document export). `importJSON` reads this one (a missing version is read as it). */
|
|
317
|
+
export const JSON_FORMAT_VERSION = '1';
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Whether `importJSON` can read a document export of this `version` (see {@link JSON_FORMAT_VERSION}). A missing version is
|
|
321
|
+
* read as the current one; the number `1` and the string `'1'` are the same version.
|
|
322
|
+
* @param {unknown} version
|
|
323
|
+
* @returns {boolean}
|
|
324
|
+
*/
|
|
325
|
+
export function isSupportedJsonVersion(version) {
|
|
326
|
+
return version === undefined || version === null || String(version) === JSON_FORMAT_VERSION;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Convert a `serialize('json')` document export back into PasteBlocks.
|
|
331
|
+
* The json format stores text as Slate-like runs (`{text, ...marks}`) or, for a code block, as a plain string;
|
|
332
|
+
* PasteBlocks store Loro deltas (`{insert, attributes}`). Top-level keys other than `id` / `type` / `props` / `content` /
|
|
333
|
+
* `children` are read as props (the `checked` of a to-do item in the first exports sat next to `type`).
|
|
334
|
+
* @param {Array<{type: string, props?: Record<string, any>, content?: Array<{text: string, [mark: string]: any}> | string, children?: any[], [key: string]: any}>} blocks
|
|
335
|
+
* @returns {PasteBlock[]}
|
|
336
|
+
*/
|
|
337
|
+
export function jsonToPasteBlocks(blocks) {
|
|
338
|
+
return (Array.isArray(blocks) ? blocks : []).filter(b => b && typeof b === 'object' && typeof b.type === 'string').map(b => {
|
|
339
|
+
const { id: _id, type, props, content, children, ...extra } = b;
|
|
340
|
+
/** @type {PasteBlock} */
|
|
341
|
+
const pb = { type, partial: false };
|
|
342
|
+
const merged = { ...extra, ...(props && typeof props === 'object' ? props : {}) };
|
|
343
|
+
if (Object.keys(merged).length) pb.props = merged;
|
|
344
|
+
if (typeof content === 'string') {
|
|
345
|
+
if (content) pb.delta = [{ insert: content }];
|
|
346
|
+
} else if (Array.isArray(content)) {
|
|
347
|
+
pb.delta = content.filter(run => run && typeof run.text === 'string').map(run => {
|
|
348
|
+
const { text, ...marks } = run;
|
|
349
|
+
return Object.keys(marks).length
|
|
350
|
+
? { insert: text, attributes: marks }
|
|
351
|
+
: { insert: text };
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
if (Array.isArray(children)) pb.children = jsonToPasteBlocks(children);
|
|
355
|
+
return pb;
|
|
356
|
+
});
|
|
357
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape text for use as HTML text content OR inside a quoted attribute value (& < > " ').
|
|
3
|
+
* @param {unknown} text
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
export function escapeHtml(text: unknown): string;
|
|
7
|
+
/**
|
|
8
|
+
* Escape a value for a quoted HTML attribute. Same table as {@link escapeHtml}; kept as a distinct name so the
|
|
9
|
+
* call sites say what they mean.
|
|
10
|
+
* @param {unknown} value
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export function escapeAttr(value: unknown): string;
|
|
14
|
+
/**
|
|
15
|
+
* Return `url` when it is safe to put in an `href` / `src`, otherwise `null`.
|
|
16
|
+
*
|
|
17
|
+
* - `kind: 'link'` (default): http, https, mailto, tel, relative URLs, `#fragment`.
|
|
18
|
+
* - `kind: 'media'`: http, https, relative, `blob:`, `data:image/*`, `data:video/*`, and Nabu's internal
|
|
19
|
+
* `nabu-asset:` scheme (see Files).
|
|
20
|
+
*
|
|
21
|
+
* Everything else (`javascript:`, `vbscript:`, `data:text/html`, `file:`…) is refused, including obfuscated forms:
|
|
22
|
+
* surrounding / embedded whitespace and control characters (`java\nscript:`), mixed case, and HTML-entity-encoded
|
|
23
|
+
* schemes (`javascript:`). The URL is returned UNCHANGED (callers still have to escape it for the attribute).
|
|
24
|
+
*
|
|
25
|
+
* @param {unknown} url
|
|
26
|
+
* @param {{ kind?: 'link' | 'media' }} [options]
|
|
27
|
+
* @returns {string | null}
|
|
28
|
+
*/
|
|
29
|
+
export function sanitizeUrl(url: unknown, { kind }?: {
|
|
30
|
+
kind?: "link" | "media";
|
|
31
|
+
}): string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Sanitize the link / media URLs of PasteBlocks (the fragment format shared by paste, import and the clipboard):
|
|
34
|
+
* an unsafe `link` mark is dropped from the delta (the text stays), an unsafe `ref` / `src` / `href` / `url`
|
|
35
|
+
* prop is emptied. Returns NEW objects where something changed; never mutates the input.
|
|
36
|
+
* @template {{ delta?: any[], props?: Record<string, any>, children?: any[] }} B
|
|
37
|
+
* @param {B[]} blocks
|
|
38
|
+
* @returns {B[]}
|
|
39
|
+
*/
|
|
40
|
+
export function sanitizePasteBlocks<B extends {
|
|
41
|
+
delta?: any[];
|
|
42
|
+
props?: Record<string, any>;
|
|
43
|
+
children?: any[];
|
|
44
|
+
}>(blocks: B[]): B[];
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML / URL safety helpers — the ONE place that decides what may be written into markup or an `href` / `src`.
|
|
3
|
+
*
|
|
4
|
+
* Used by every HTML serializer (clipboard `text/html`, export), by the link / media rendering, and by the
|
|
5
|
+
* paste / import pipelines (defense in depth: the model, the render and the output are each guarded).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const HTML_ESCAPES = /** @type {Record<string, string>} */ ({
|
|
9
|
+
'&': '&',
|
|
10
|
+
'<': '<',
|
|
11
|
+
'>': '>',
|
|
12
|
+
'"': '"',
|
|
13
|
+
"'": ''',
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Escape text for use as HTML text content OR inside a quoted attribute value (& < > " ').
|
|
18
|
+
* @param {unknown} text
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export function escapeHtml(text) {
|
|
22
|
+
return String(text ?? '').replace(/[&<>"']/g, (c) => HTML_ESCAPES[c]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Escape a value for a quoted HTML attribute. Same table as {@link escapeHtml}; kept as a distinct name so the
|
|
27
|
+
* call sites say what they mean.
|
|
28
|
+
* @param {unknown} value
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
export function escapeAttr(value) {
|
|
32
|
+
return escapeHtml(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Schemes allowed in a LINK (`<a href>`). Relative URLs and `#fragment` are always allowed. */
|
|
36
|
+
const LINK_SCHEMES = new Set(['http', 'https', 'mailto', 'tel']);
|
|
37
|
+
/** Schemes allowed for MEDIA (`src` of img / video, `href` of a file chip). `data:` is handled separately. */
|
|
38
|
+
const MEDIA_SCHEMES = new Set(['http', 'https', 'blob', 'nabu-asset']);
|
|
39
|
+
/** `data:` URLs accepted for media only: images and videos, never HTML / script / text. */
|
|
40
|
+
const DATA_MEDIA = /^data:(?:image|video)\/[a-z0-9][a-z0-9.+-]*[;,]/i;
|
|
41
|
+
|
|
42
|
+
/** @type {Record<string, string>} */
|
|
43
|
+
const NAMED_ENTITIES = {
|
|
44
|
+
colon: ':', tab: '\t', newline: '\n', amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", sol: '/', lpar: '(', rpar: ')',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Decode the HTML character references that could hide a URL scheme (`javascript:`, `:`, `	`…).
|
|
49
|
+
* Decodes repeatedly (double-encoding) a bounded number of times.
|
|
50
|
+
* @param {string} s
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
function decodeEntities(s) {
|
|
54
|
+
let prev;
|
|
55
|
+
let out = s;
|
|
56
|
+
for (let i = 0; i < 4; i++) {
|
|
57
|
+
prev = out;
|
|
58
|
+
out = out.replace(/&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|([a-zA-Z]{2,8}));?/g, (m, dec, hex, name) => {
|
|
59
|
+
if (dec !== undefined || hex !== undefined) {
|
|
60
|
+
const cp = dec !== undefined ? parseInt(dec, 10) : parseInt(hex, 16);
|
|
61
|
+
try { return String.fromCodePoint(cp); } catch { return ''; }
|
|
62
|
+
}
|
|
63
|
+
const named = NAMED_ENTITIES[name.toLowerCase()];
|
|
64
|
+
return named !== undefined ? named : m;
|
|
65
|
+
});
|
|
66
|
+
if (out === prev) break;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Strip what a browser URL parser ignores or tolerates inside / around a scheme: ASCII control chars, whitespace
|
|
73
|
+
* (including unicode spaces, zero-width characters and BOM).
|
|
74
|
+
* @param {string} s
|
|
75
|
+
* @returns {string}
|
|
76
|
+
*/
|
|
77
|
+
function stripInvisible(s) {
|
|
78
|
+
return s.replace(/[\u0000-\u0020\u007f-\u00a0\u00ad\u1680\u180e\u2000-\u200f\u2028-\u202f\u205f-\u2064\u3000\ufeff]/g, '');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Scheme of a (cleaned) URL, lower-cased, or `null` when it is relative / a fragment.
|
|
83
|
+
* @param {string} cleaned
|
|
84
|
+
* @returns {string | null}
|
|
85
|
+
*/
|
|
86
|
+
function schemeOf(cleaned) {
|
|
87
|
+
const m = /^([a-z][a-z0-9+.-]*):/i.exec(cleaned);
|
|
88
|
+
return m ? m[1].toLowerCase() : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {string} cleaned whitespace/control-stripped URL
|
|
93
|
+
* @param {'link' | 'media'} kind
|
|
94
|
+
* @returns {boolean}
|
|
95
|
+
*/
|
|
96
|
+
function isAllowed(cleaned, kind) {
|
|
97
|
+
const scheme = schemeOf(cleaned);
|
|
98
|
+
if (scheme === null) return true; // relative URL, `/path`, `?q`, `#frag`, `//host`
|
|
99
|
+
if (kind === 'media') return MEDIA_SCHEMES.has(scheme) || (scheme === 'data' && DATA_MEDIA.test(cleaned));
|
|
100
|
+
return LINK_SCHEMES.has(scheme);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Return `url` when it is safe to put in an `href` / `src`, otherwise `null`.
|
|
105
|
+
*
|
|
106
|
+
* - `kind: 'link'` (default): http, https, mailto, tel, relative URLs, `#fragment`.
|
|
107
|
+
* - `kind: 'media'`: http, https, relative, `blob:`, `data:image/*`, `data:video/*`, and Nabu's internal
|
|
108
|
+
* `nabu-asset:` scheme (see Files).
|
|
109
|
+
*
|
|
110
|
+
* Everything else (`javascript:`, `vbscript:`, `data:text/html`, `file:`…) is refused, including obfuscated forms:
|
|
111
|
+
* surrounding / embedded whitespace and control characters (`java\nscript:`), mixed case, and HTML-entity-encoded
|
|
112
|
+
* schemes (`javascript:`). The URL is returned UNCHANGED (callers still have to escape it for the attribute).
|
|
113
|
+
*
|
|
114
|
+
* @param {unknown} url
|
|
115
|
+
* @param {{ kind?: 'link' | 'media' }} [options]
|
|
116
|
+
* @returns {string | null}
|
|
117
|
+
*/
|
|
118
|
+
export function sanitizeUrl(url, { kind = 'link' } = {}) {
|
|
119
|
+
if (typeof url !== 'string') return null;
|
|
120
|
+
const raw = url;
|
|
121
|
+
const cleaned = stripInvisible(raw);
|
|
122
|
+
if (!cleaned) return null;
|
|
123
|
+
// Both the literal form and its entity-decoded form must pass (the decoded one is what a browser would act
|
|
124
|
+
// on if this string ever went through an HTML parser).
|
|
125
|
+
if (!isAllowed(cleaned, kind)) return null;
|
|
126
|
+
if (cleaned.includes('&')) {
|
|
127
|
+
const decoded = stripInvisible(decodeEntities(raw));
|
|
128
|
+
if (!decoded || !isAllowed(decoded, kind)) return null;
|
|
129
|
+
}
|
|
130
|
+
// A backslash or percent-encoded scheme separator before the first `/` is not a scheme for browsers, but
|
|
131
|
+
// `%6Aavascript:` must not be mistaken for relative either: decode %XX once for the scheme check.
|
|
132
|
+
if (cleaned.includes('%')) {
|
|
133
|
+
let pct = cleaned;
|
|
134
|
+
try { pct = stripInvisible(decodeURIComponent(cleaned)); } catch { /* malformed escape: keep literal */ }
|
|
135
|
+
if (!isAllowed(pct, kind)) return null;
|
|
136
|
+
}
|
|
137
|
+
return raw;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Sanitize the link / media URLs of PasteBlocks (the fragment format shared by paste, import and the clipboard):
|
|
142
|
+
* an unsafe `link` mark is dropped from the delta (the text stays), an unsafe `ref` / `src` / `href` / `url`
|
|
143
|
+
* prop is emptied. Returns NEW objects where something changed; never mutates the input.
|
|
144
|
+
* @template {{ delta?: any[], props?: Record<string, any>, children?: any[] }} B
|
|
145
|
+
* @param {B[]} blocks
|
|
146
|
+
* @returns {B[]}
|
|
147
|
+
*/
|
|
148
|
+
export function sanitizePasteBlocks(blocks) {
|
|
149
|
+
if (!Array.isArray(blocks)) return blocks;
|
|
150
|
+
return blocks.map((pb) => {
|
|
151
|
+
if (!pb || typeof pb !== 'object') return pb;
|
|
152
|
+
let out = pb;
|
|
153
|
+
if (Array.isArray(pb.delta)) {
|
|
154
|
+
let changed = false;
|
|
155
|
+
const delta = pb.delta.map((op) => {
|
|
156
|
+
const link = op?.attributes?.link;
|
|
157
|
+
if (link === undefined || link === null || link === false) return op;
|
|
158
|
+
if (typeof link === 'string' && sanitizeUrl(link, { kind: 'link' }) !== null) return op;
|
|
159
|
+
changed = true;
|
|
160
|
+
const { link: _drop, ...attributes } = op.attributes;
|
|
161
|
+
return Object.keys(attributes).length ? { ...op, attributes } : { insert: op.insert };
|
|
162
|
+
});
|
|
163
|
+
if (changed) out = { ...out, delta };
|
|
164
|
+
}
|
|
165
|
+
if (pb.props && typeof pb.props === 'object') {
|
|
166
|
+
let props = null;
|
|
167
|
+
for (const key of ['ref', 'src', 'href', 'url']) {
|
|
168
|
+
const v = pb.props[key];
|
|
169
|
+
const kind = key === 'ref' || key === 'src' ? 'media' : 'link';
|
|
170
|
+
if (typeof v === 'string' && v && sanitizeUrl(v, { kind }) === null) {
|
|
171
|
+
props ??= { ...pb.props };
|
|
172
|
+
props[key] = '';
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (props) out = { ...out, props };
|
|
176
|
+
}
|
|
177
|
+
if (Array.isArray(pb.children) && pb.children.length) {
|
|
178
|
+
const children = sanitizePasteBlocks(pb.children);
|
|
179
|
+
if (children.some((c, i) => c !== pb.children?.[i])) out = { ...out, children };
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
});
|
|
183
|
+
}
|
package/dist/utils/index.d.ts
CHANGED