@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +531 -0
- package/LICENSE +21 -0
- package/README.md +72 -341
- package/dist/behaviors/text/RichText.svelte +24 -4
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
- 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 +95 -38
- package/dist/behaviors/text/text.behavior.svelte.d.ts +195 -55
- package/dist/behaviors/text/text.behavior.svelte.js +531 -103
- package/dist/behaviors/text/typography.extension.d.ts +2 -1
- package/dist/behaviors/text/typography.extension.js +3 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +169 -25
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +4 -7
- package/dist/blocks/block.svelte.d.ts +350 -86
- package/dist/blocks/block.svelte.js +370 -59
- package/dist/blocks/blockquote/Blockquote.svelte +1 -2
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
- package/dist/blocks/blockquote/blockquote.extension.js +14 -5
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
- package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
- package/dist/blocks/callout/Callout.svelte +6 -8
- package/dist/blocks/callout/callout.extension.d.ts +4 -1
- package/dist/blocks/callout/callout.extension.js +19 -3
- package/dist/blocks/callout/callout.svelte.d.ts +5 -3
- package/dist/blocks/callout/callout.svelte.js +11 -6
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +7 -8
- package/dist/blocks/code/code.extension.d.ts +4 -1
- package/dist/blocks/code/code.extension.js +22 -2
- package/dist/blocks/code/code.svelte.d.ts +9 -2
- package/dist/blocks/code/code.svelte.js +49 -25
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -4
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
- package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
- 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.extension.d.ts +23 -1
- package/dist/blocks/divider/divider.extension.js +85 -19
- package/dist/blocks/divider/divider.svelte.d.ts +7 -2
- package/dist/blocks/divider/divider.svelte.js +9 -4
- package/dist/blocks/file/File.svelte +13 -6
- package/dist/blocks/file/file.extension.d.ts +4 -1
- package/dist/blocks/file/file.extension.js +9 -1
- package/dist/blocks/file/file.svelte.d.ts +3 -3
- package/dist/blocks/file/file.svelte.js +20 -14
- package/dist/blocks/heading/Heading.svelte +1 -2
- package/dist/blocks/heading/heading.svelte.d.ts +23 -11
- package/dist/blocks/heading/heading.svelte.js +68 -13
- 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 +5 -2
- package/dist/blocks/image/image.extension.d.ts +4 -1
- package/dist/blocks/image/image.extension.js +9 -1
- package/dist/blocks/image/image.svelte.d.ts +17 -6
- package/dist/blocks/image/image.svelte.js +33 -11
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/list/List.svelte +1 -0
- package/dist/blocks/list/ListItem.svelte +1 -0
- 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 +49 -28
- 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 +6 -3
- package/dist/blocks/list/index.js +42 -31
- package/dist/blocks/list/list-item.svelte.d.ts +32 -18
- package/dist/blocks/list/list-item.svelte.js +99 -100
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
- package/dist/blocks/list/list.behavior.svelte.js +6 -1
- package/dist/blocks/list/list.svelte.d.ts +89 -18
- package/dist/blocks/list/list.svelte.js +129 -125
- 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/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +77 -2
- package/dist/blocks/media/media-block.svelte.js +124 -12
- package/dist/blocks/media/media.helpers.d.ts +6 -1
- package/dist/blocks/media/media.helpers.js +43 -6
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +114 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +764 -71
- package/dist/blocks/nabu.svelte.js +1753 -189
- package/dist/blocks/paragraph/Paragraph.svelte +1 -2
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -13
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
- package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
- 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 +622 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +35 -14
- package/dist/blocks/text-block.svelte.js +42 -8
- package/dist/blocks/toggle/Toggle.svelte +4 -15
- 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 -1
- package/dist/blocks/toggle/toggle.extension.js +6 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +55 -8
- package/dist/blocks/toggle/toggle.svelte.js +134 -34
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +5 -2
- package/dist/blocks/video/video.extension.d.ts +4 -1
- package/dist/blocks/video/video.extension.js +9 -1
- package/dist/blocks/video/video.svelte.d.ts +6 -5
- package/dist/blocks/video/video.svelte.js +18 -9
- 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 +321 -19
- package/dist/files/files.svelte.js +847 -43
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +3 -1
- 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 -9
- package/dist/index.js +117 -10
- package/dist/nabu-theme.css +21 -1
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +550 -86
- 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 -26
- package/dist/presets.js +35 -13
- 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 +97 -21
- package/dist/ui/FloatingToolbar.svelte +75 -27
- package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
- package/dist/ui/SlashMenu.svelte +147 -15
- package/dist/ui/SlashMenu.svelte.d.ts +20 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +7 -1
- package/dist/ui/index.js +12 -1
- package/dist/ui/slash-menu.svelte.d.ts +79 -7
- package/dist/ui/slash-menu.svelte.js +85 -51
- 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/composition.d.ts +28 -0
- package/dist/utils/composition.js +51 -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 +439 -45
- package/dist/utils/extensions.js +271 -29
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -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 +81 -3
- package/dist/utils/placeholders.js +73 -3
- package/dist/utils/selection.svelte.d.ts +48 -20
- 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 +42 -0
- package/dist/utils/words.js +147 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +56 -20
- package/dist/blocks/variable.svelte.d.ts +0 -47
- package/dist/blocks/variable.svelte.js +0 -51
- package/dist/blocks/zone/TemplateEditor.svelte +0 -308
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/Zone.svelte +0 -272
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneEditor.svelte +0 -289
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
- 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 -237
- package/dist/blocks/zone/zone.svelte.d.ts +0 -221
- package/dist/blocks/zone/zone.svelte.js +0 -608
- package/dist/overlays/context/Context.svelte +0 -182
- package/dist/overlays/context/Context.svelte.d.ts +0 -17
- package/dist/overlays/context/collect.d.ts +0 -17
- package/dist/overlays/context/collect.js +0 -33
- package/dist/overlays/context/context.extension.d.ts +0 -2
- package/dist/overlays/context/context.extension.js +0 -8
- package/dist/overlays/context/context.svelte.d.ts +0 -82
- package/dist/overlays/context/context.svelte.js +0 -159
- package/dist/overlays/context/index.d.ts +0 -3
- package/dist/overlays/context/index.js +0 -3
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown lists (paste AND `importMarkdown`), CommonMark / GFM level, as a nested List / ListItem PasteBlock tree.
|
|
3
|
+
*
|
|
4
|
+
* - markers `-` `*` `+` `1.` `1)` (up to 9 digits); a task item is `[ ]` / `[x]` / `[X]` (`[]` is an unchecked one);
|
|
5
|
+
* - nesting is by indentation, relative: an item indented MORE than the previous item is its child (2 spaces, 4 spaces
|
|
6
|
+
* or a TAB (= 4 columns) each give one level, mixed indentation is tolerated);
|
|
7
|
+
* - CommonMark list boundaries: a list ends at a change of marker at the same level, and the next items start a NEW list.
|
|
8
|
+
* The marker is the bullet character (`-` / `*` / `+`) or the ordered delimiter (`.` / `)`), so `- a` then `* b`, `1. a`
|
|
9
|
+
* then `1) b`, bulleted then numbered (P-30) are all separate lists. Adjacent lists of the SAME type are merged back by
|
|
10
|
+
* the List normalizer (nothing changes for them in the model), but a to-do list next to a bullet list stay two lists.
|
|
11
|
+
* The exporter relies on it: it alternates `-` / `*` (and `.` / `)`) between adjacent lists. An ordered list starts at
|
|
12
|
+
* its first number;
|
|
13
|
+
* - a loose list (blank lines between items) is still ONE list;
|
|
14
|
+
* - an indented line under an item (a continuation paragraph, also after a blank line) is APPENDED to the item text with
|
|
15
|
+
* a line break; so is an unindented line after a hard break, and, on import (`joinLines`), a lazy continuation line.
|
|
16
|
+
* A fenced code in an item is kept verbatim (fences included) as text of the item: an item holds text, not blocks.
|
|
17
|
+
* The exporter writes such text back as an indented fence (see {@link itemMarkdown}), which this parser reads as the
|
|
18
|
+
* same text.
|
|
19
|
+
*
|
|
20
|
+
* Pure functions, no DOM, no runes.
|
|
21
|
+
*
|
|
22
|
+
* @import { PasteBlock } from "../../utils/extensions.js";
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ITEM_RE = /^([ \t]*)([-*+]|(\d{1,9})([.)]))[ \t]+(.*)$/;
|
|
26
|
+
const THEMATIC_RE = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
27
|
+
const TASK_RE = /^\[([ xX]?)\](?:[ \t]+(.*)|$)/;
|
|
28
|
+
const FENCE_RE = /^([ \t]*)(`{3,}|~{3,})/;
|
|
29
|
+
const HARD_BREAK_RE = /(?: {2,}|\\)$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Index of the line that closes the fence opened on line `at` (a fence of the same character, at least as long), or -1.
|
|
33
|
+
* @param {string[]} lines
|
|
34
|
+
* @param {number} at
|
|
35
|
+
* @param {string} fence the opening fence (its backticks / tildes)
|
|
36
|
+
* @returns {number}
|
|
37
|
+
*/
|
|
38
|
+
function fenceEnd(lines, at, fence) {
|
|
39
|
+
for (let k = at + 1; k < lines.length; k++) {
|
|
40
|
+
const close = /^[ \t]*(`+|~+)[ \t]*$/.exec(lines[k]);
|
|
41
|
+
if (close && close[1][0] === fence[0] && close[1].length >= fence.length) return k;
|
|
42
|
+
}
|
|
43
|
+
return -1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Width of the leading whitespace of a line, in columns (a TAB goes to the next multiple of 4).
|
|
48
|
+
* @param {string} line
|
|
49
|
+
* @returns {number}
|
|
50
|
+
*/
|
|
51
|
+
function indentOf(line) {
|
|
52
|
+
let col = 0;
|
|
53
|
+
for (const ch of line) {
|
|
54
|
+
if (ch === ' ') col++;
|
|
55
|
+
else if (ch === '\t') col += 4 - (col % 4);
|
|
56
|
+
else break;
|
|
57
|
+
}
|
|
58
|
+
return col;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @param {string} line */
|
|
62
|
+
export function isListItem(line) {
|
|
63
|
+
return ITEM_RE.test(line) && !THEMATIC_RE.test(line);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether a list may start in the middle of a paragraph (CommonMark): a bullet with text, or an ordered list that starts at 1.
|
|
68
|
+
* @param {string} line
|
|
69
|
+
*/
|
|
70
|
+
export function interruptsParagraph(line) {
|
|
71
|
+
return /^ {0,3}(?:[-*+]|1[.)])[ \t]+\S/.test(line) && !THEMATIC_RE.test(line);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Number of lines of the list that starts at line `i`.
|
|
76
|
+
* @param {string[]} lines
|
|
77
|
+
* @param {number} i
|
|
78
|
+
* @param {{ joinLines?: boolean, interrupts?: (lines: string[], k: number) => boolean }} [helpers]
|
|
79
|
+
* @returns {number}
|
|
80
|
+
*/
|
|
81
|
+
export function listLength(lines, i, helpers) {
|
|
82
|
+
const n = lines.length;
|
|
83
|
+
let j = i;
|
|
84
|
+
let rootIndent = 0;
|
|
85
|
+
let lastIndent = 0;
|
|
86
|
+
let afterBlank = false; // the line was already accepted as a continuation, past a blank line
|
|
87
|
+
while (j < n) {
|
|
88
|
+
const line = lines[j];
|
|
89
|
+
if (!line.trim()) {
|
|
90
|
+
let k = j + 1;
|
|
91
|
+
while (k < n && !lines[k].trim()) k++;
|
|
92
|
+
if (k >= n) break;
|
|
93
|
+
const next = lines[k];
|
|
94
|
+
if (isListItem(next)) { j = k; continue; } // loose list
|
|
95
|
+
// an indented paragraph (or fence) under the items belongs to the last one
|
|
96
|
+
if (THEMATIC_RE.test(next) || indentOf(next) < rootIndent + 2) break;
|
|
97
|
+
afterBlank = true;
|
|
98
|
+
j = k;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// A closed fence under an item is skipped as a whole: what it holds (a `---`, a `- x`, a blank line…) is code.
|
|
102
|
+
const fence = j > i ? FENCE_RE.exec(line) : null;
|
|
103
|
+
if (fence && (afterBlank || indentOf(line) > lastIndent)) {
|
|
104
|
+
const end = fenceEnd(lines, j, fence[2]);
|
|
105
|
+
if (end >= 0) { afterBlank = false; j = end + 1; continue; }
|
|
106
|
+
}
|
|
107
|
+
if (isListItem(line)) {
|
|
108
|
+
afterBlank = false;
|
|
109
|
+
lastIndent = indentOf(line);
|
|
110
|
+
if (j === i) rootIndent = lastIndent;
|
|
111
|
+
j++;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (THEMATIC_RE.test(line)) break;
|
|
115
|
+
if (afterBlank) { afterBlank = false; j++; continue; }
|
|
116
|
+
if (j > i && indentOf(line) > lastIndent) { j++; continue; } // indented continuation
|
|
117
|
+
// an unindented line: continues the item after a hard break, or lazily on import
|
|
118
|
+
const previous = lines[j - 1];
|
|
119
|
+
if (j > i && previous.trim() && (HARD_BREAK_RE.test(previous) || (helpers?.joinLines && !helpers.interrupts?.(lines, j)))) { j++; continue; }
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
return Math.max(1, j - i);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @typedef {{ raw: boolean, s: string }} Chunk
|
|
127
|
+
* @typedef {{ indent: number, key: string, ordered: boolean, num: number, task: boolean, checked: boolean, chunks: Chunk[], previous: string, children: Item[] }} Item
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Parse the lines of a list (see {@link listLength}) into List PasteBlocks: one, or several when the marker type changes at
|
|
132
|
+
* the root.
|
|
133
|
+
* @param {string[]} lines
|
|
134
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], joinLines?: boolean, softBreak?: string }} helpers
|
|
135
|
+
* @param {(listType: string, start: unknown) => number | null} listStart normalises the `start` of an ordered list
|
|
136
|
+
* @returns {PasteBlock | PasteBlock[] | null}
|
|
137
|
+
*/
|
|
138
|
+
export function parseListLines(lines, { parseInline, joinLines, softBreak }, listStart) {
|
|
139
|
+
/** @type {Item[]} */
|
|
140
|
+
const roots = [];
|
|
141
|
+
/** @type {Item[]} */
|
|
142
|
+
const stack = [];
|
|
143
|
+
let blankBefore = false;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {Item} item
|
|
147
|
+
* @param {string} text
|
|
148
|
+
*/
|
|
149
|
+
const appendText = (item, text) => {
|
|
150
|
+
const last = item.chunks[item.chunks.length - 1];
|
|
151
|
+
if (last.raw) item.chunks.push({ raw: false, s: text });
|
|
152
|
+
else if (!last.s) last.s = text;
|
|
153
|
+
else {
|
|
154
|
+
const hard = HARD_BREAK_RE.test(item.previous);
|
|
155
|
+
// paragraphs of an item are separated by a line break, the lines of one by a space (import) or a line break
|
|
156
|
+
last.s += (hard ? '\n' : blankBefore ? '\\\n' : (softBreak ?? (joinLines ? ' ' : '\n'))) + text;
|
|
157
|
+
}
|
|
158
|
+
item.previous = text;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
for (let li = 0; li < lines.length; li++) {
|
|
162
|
+
const line = lines[li];
|
|
163
|
+
if (!line.trim()) { blankBefore = true; continue; }
|
|
164
|
+
|
|
165
|
+
const m = isListItem(line) ? ITEM_RE.exec(line) : null;
|
|
166
|
+
if (m) {
|
|
167
|
+
const indent = indentOf(m[1]);
|
|
168
|
+
while (stack.length && stack[stack.length - 1].indent >= indent) stack.pop();
|
|
169
|
+
let text = m[5];
|
|
170
|
+
const task = TASK_RE.exec(text);
|
|
171
|
+
if (task) text = task[2] ?? '';
|
|
172
|
+
/** @type {Item} */
|
|
173
|
+
const item = {
|
|
174
|
+
indent,
|
|
175
|
+
key: m[3] === undefined ? m[2] : m[4], // the marker that tells lists apart: `-` `*` `+` / `.` `)`
|
|
176
|
+
ordered: m[3] !== undefined,
|
|
177
|
+
num: m[3] === undefined ? NaN : parseInt(m[3], 10),
|
|
178
|
+
task: !!task,
|
|
179
|
+
checked: !!task && /[xX]/.test(task[1]),
|
|
180
|
+
chunks: [{ raw: false, s: text.trimEnd() ? text : '' }],
|
|
181
|
+
previous: text,
|
|
182
|
+
children: [],
|
|
183
|
+
};
|
|
184
|
+
(stack.length ? stack[stack.length - 1].children : roots).push(item);
|
|
185
|
+
stack.push(item);
|
|
186
|
+
blankBefore = false;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// A continuation line: it belongs to the deepest open item that is indented less than the line (else the last one).
|
|
191
|
+
const ind = indentOf(line);
|
|
192
|
+
let target = stack[stack.length - 1];
|
|
193
|
+
for (let k = stack.length - 1; k >= 0; k--) if (stack[k].indent < ind) { target = stack[k]; break; }
|
|
194
|
+
if (!target) { blankBefore = false; continue; }
|
|
195
|
+
|
|
196
|
+
const fence = FENCE_RE.exec(line);
|
|
197
|
+
if (fence) {
|
|
198
|
+
// verbatim, up to the closing fence (or the end): blank lines, markdown and indentation inside are kept; only the
|
|
199
|
+
// indentation of the opening fence is removed from every line (the rest, TABs included, is untouched)
|
|
200
|
+
const strip = indentOf(fence[1]);
|
|
201
|
+
const dedent = (/** @type {string} */ l) => {
|
|
202
|
+
let col = 0;
|
|
203
|
+
let k = 0;
|
|
204
|
+
while (k < l.length && col < strip) {
|
|
205
|
+
if (l[k] === ' ') col++;
|
|
206
|
+
else if (l[k] === '\t' && col + (4 - (col % 4)) <= strip) col += 4 - (col % 4);
|
|
207
|
+
else break;
|
|
208
|
+
k++;
|
|
209
|
+
}
|
|
210
|
+
return l.slice(k);
|
|
211
|
+
};
|
|
212
|
+
const body = [dedent(line)];
|
|
213
|
+
const char = fence[2][0];
|
|
214
|
+
while (li + 1 < lines.length) {
|
|
215
|
+
const next = lines[++li];
|
|
216
|
+
body.push(dedent(next));
|
|
217
|
+
const close = /^[ \t]*(`+|~+)[ \t]*$/.exec(next);
|
|
218
|
+
if (close && close[1][0] === char && close[1].length >= fence[2].length) break;
|
|
219
|
+
}
|
|
220
|
+
while (body.length > 1 && !body[body.length - 1].trim()) body.pop();
|
|
221
|
+
const last = target.chunks[target.chunks.length - 1];
|
|
222
|
+
if (!last.raw) last.s = last.s.replace(/\\$/, '');
|
|
223
|
+
target.chunks.push({ raw: true, s: body.join('\n') });
|
|
224
|
+
target.previous = '';
|
|
225
|
+
} else {
|
|
226
|
+
appendText(target, line.replace(/^[ \t]+/, ''));
|
|
227
|
+
}
|
|
228
|
+
blankBefore = false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (!roots.length) return null;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @param {Item[]} siblings
|
|
235
|
+
* @returns {PasteBlock[]}
|
|
236
|
+
*/
|
|
237
|
+
const toLists = (siblings) => {
|
|
238
|
+
/** @type {Item[][]} */
|
|
239
|
+
const groups = [];
|
|
240
|
+
for (const item of siblings) {
|
|
241
|
+
const group = groups[groups.length - 1];
|
|
242
|
+
if (group && group[0].key === item.key) group.push(item);
|
|
243
|
+
else groups.push([item]);
|
|
244
|
+
}
|
|
245
|
+
return groups.map((group) => {
|
|
246
|
+
const listType = group.some((it) => it.task) ? 'todo' : group[0].ordered ? 'ordered' : 'bullet';
|
|
247
|
+
const start = listStart(listType, group[0].num);
|
|
248
|
+
/** @type {PasteBlock[]} */
|
|
249
|
+
const children = group.map((it) => {
|
|
250
|
+
/** @type {PasteBlock} */
|
|
251
|
+
const pb = { type: 'list-item', delta: itemDelta(it), partial: false };
|
|
252
|
+
if (it.task) pb.props = { checked: it.checked };
|
|
253
|
+
if (it.children.length) pb.children = toLists(it.children);
|
|
254
|
+
return pb;
|
|
255
|
+
});
|
|
256
|
+
return { type: 'list', props: start === null ? { listType } : { listType, start }, children, partial: false };
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @param {Item} item
|
|
262
|
+
* @returns {import('loro-crdt').Delta<string>[]}
|
|
263
|
+
*/
|
|
264
|
+
const itemDelta = (item) => {
|
|
265
|
+
/** @type {import('loro-crdt').Delta<string>[]} */
|
|
266
|
+
const delta = [];
|
|
267
|
+
item.chunks.forEach((chunk, k) => {
|
|
268
|
+
if (k) delta.push({ insert: '\n' });
|
|
269
|
+
if (chunk.raw) delta.push({ insert: chunk.s });
|
|
270
|
+
else delta.push(...parseInline(chunk.s));
|
|
271
|
+
});
|
|
272
|
+
return delta.filter((op) => op.insert !== '' || op.attributes);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const lists = toLists(roots);
|
|
276
|
+
return lists.length === 1 ? lists[0] : lists;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ── Export ───────────────────────────────────────────────────────────────────────────────────────────────────
|
|
280
|
+
|
|
281
|
+
/** Bullet characters, alternated between ADJACENT lists of the same family (P-30). */
|
|
282
|
+
const BULLETS = ['-', '*'];
|
|
283
|
+
/** Ordered delimiters, alternated likewise. */
|
|
284
|
+
const DELIMITERS = ['.', ')'];
|
|
285
|
+
|
|
286
|
+
/** Bulleted (bullet, to-do) lists tell their neighbours apart by the bullet character, numbered ones by the delimiter. */
|
|
287
|
+
const familyOf = (/** @type {string | undefined} */ listType) => (listType === 'ordered' ? 'ordered' : 'bullet');
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Number of lists of the same family directly before a list: the position of the list in a run of ADJACENT lists, which
|
|
291
|
+
* picks its marker (`-` then `*` then `-`…, `.` then `)`). CommonMark ends a list at a change of marker, so this is what
|
|
292
|
+
* keeps two neighbours (a to-do list then a bullet list, two numbered lists…) from reading back as ONE list. A block
|
|
293
|
+
* that is not a list, or a list of the other family, ends the run (the marker type already differs).
|
|
294
|
+
* @param {string | undefined} listType
|
|
295
|
+
* @param {Array<{ type: string, props?: { listType?: string } }>} before the siblings before the list, in order
|
|
296
|
+
* @returns {number}
|
|
297
|
+
*/
|
|
298
|
+
export function adjacentRun(listType, before) {
|
|
299
|
+
const family = familyOf(listType);
|
|
300
|
+
let run = 0;
|
|
301
|
+
for (let k = before.length - 1; k >= 0; k--) {
|
|
302
|
+
const b = before[k];
|
|
303
|
+
if (b?.type !== 'list' || familyOf(b.props?.listType) !== family) break;
|
|
304
|
+
run++;
|
|
305
|
+
}
|
|
306
|
+
return run;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Columns taken by the marker of an item plus its space: the indentation of what belongs to the item (continuation
|
|
311
|
+
* lines, sublists) so that any CommonMark reader nests them under it. A to-do item counts the bullet only.
|
|
312
|
+
* @param {string | undefined} listType
|
|
313
|
+
* @param {number} number the number of the item (ordered lists)
|
|
314
|
+
* @returns {number}
|
|
315
|
+
*/
|
|
316
|
+
export function markerWidth(listType, number) {
|
|
317
|
+
return listType === 'ordered' ? String(number).length + 2 : 2;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Plain text of a delta (embeds ignored).
|
|
322
|
+
* @param {Array<{ insert?: unknown }>} delta
|
|
323
|
+
*/
|
|
324
|
+
const deltaText = (delta) => delta.map((op) => (typeof op.insert === 'string' ? op.insert : '')).join('');
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Part of a delta between two character offsets (marks kept).
|
|
328
|
+
* @template {{ insert?: unknown }} Op
|
|
329
|
+
* @param {Op[]} delta
|
|
330
|
+
* @param {number} from
|
|
331
|
+
* @param {number} to
|
|
332
|
+
* @returns {Op[]}
|
|
333
|
+
*/
|
|
334
|
+
function sliceDelta(delta, from, to) {
|
|
335
|
+
/** @type {Op[]} */
|
|
336
|
+
const out = [];
|
|
337
|
+
let pos = 0;
|
|
338
|
+
for (const op of delta) {
|
|
339
|
+
if (typeof op.insert !== 'string') continue;
|
|
340
|
+
const a = Math.max(from, pos);
|
|
341
|
+
const b = Math.min(to, pos + op.insert.length);
|
|
342
|
+
if (a < b) out.push({ ...op, insert: op.insert.slice(a - pos, b - pos) });
|
|
343
|
+
pos += op.insert.length;
|
|
344
|
+
}
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The text of an item as markdown. An item holds text, not blocks, so a fenced code written in it (typed, or imported from a
|
|
350
|
+
* list) is TEXT with line breaks. Escaped like any text it would be unreadable (`\`\`\`js\`), so a well-formed fence (an
|
|
351
|
+
* opener at the start of a line of the text, not the first, and its closer) is written back as an indented fence under the
|
|
352
|
+
* item, which the list parser reads as the same text, verbatim (marks inside the fence are not exported). Any other
|
|
353
|
+
* text is the inline markdown of its delta.
|
|
354
|
+
* @param {Array<{ insert?: unknown, attributes?: Record<string, any> }>} delta
|
|
355
|
+
* @param {string} pad indentation of what belongs to the item
|
|
356
|
+
* @param {(delta: any[]) => string} toMarkdown inline serializer
|
|
357
|
+
* @returns {string} the first line (after the marker) and the following ones, already indented
|
|
358
|
+
*/
|
|
359
|
+
export function itemMarkdown(delta, pad, toMarkdown) {
|
|
360
|
+
const text = deltaText(delta);
|
|
361
|
+
if (!text.includes('```') && !text.includes('~~~')) return toMarkdown(delta);
|
|
362
|
+
const lines = text.split('\n');
|
|
363
|
+
/** @type {number[]} */
|
|
364
|
+
const starts = [];
|
|
365
|
+
let at = 0;
|
|
366
|
+
for (const line of lines) { starts.push(at); at += line.length + 1; }
|
|
367
|
+
|
|
368
|
+
/** @type {Array<{ from: number, to: number }>} */
|
|
369
|
+
const regions = [];
|
|
370
|
+
for (let k = 1; k < lines.length; k++) {
|
|
371
|
+
const open = /^(?:(`{3,})[^`]*|(~{3,}).*)$/.exec(lines[k]); // an opener at the very start of the line
|
|
372
|
+
if (!open) continue;
|
|
373
|
+
const end = fenceEnd(lines, k, open[1] ?? open[2]);
|
|
374
|
+
if (end < 0) continue;
|
|
375
|
+
regions.push({ from: k, to: end });
|
|
376
|
+
k = end;
|
|
377
|
+
}
|
|
378
|
+
// An empty line next to a fence cannot be told apart from a paragraph break on the way back: keep the escaped text.
|
|
379
|
+
if (!regions.length || regions.some((r) => lines[r.from - 1] === '' || lines[r.to + 1] === '')) return toMarkdown(delta);
|
|
380
|
+
|
|
381
|
+
/** @type {string[]} */
|
|
382
|
+
const out = [];
|
|
383
|
+
/** @param {number} from first line @param {number} to last line (inclusive) @param {boolean} first */
|
|
384
|
+
const textLines = (from, to, first) => {
|
|
385
|
+
const md = toMarkdown(sliceDelta(delta, starts[from], starts[to] + lines[to].length));
|
|
386
|
+
out.push(first ? md : md.split('\n').map((l) => pad + l).join('\n'));
|
|
387
|
+
};
|
|
388
|
+
let cursor = 0;
|
|
389
|
+
for (const region of regions) {
|
|
390
|
+
if (region.from > cursor) textLines(cursor, region.from - 1, cursor === 0);
|
|
391
|
+
for (let k = region.from; k <= region.to; k++) out.push(lines[k] ? pad + lines[k] : '');
|
|
392
|
+
cursor = region.to + 1;
|
|
393
|
+
}
|
|
394
|
+
if (cursor < lines.length) textLines(cursor, lines.length - 1, false);
|
|
395
|
+
return out.join('\n');
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Environment of the renderers below: the inline serializer and the normalizer of the `start` of an ordered list (both
|
|
400
|
+
* live outside this pure module).
|
|
401
|
+
* @typedef {{ toMarkdown: (delta: any[]) => string, listStart: (listType: string, start: unknown) => number | null }} RenderEnv
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* One list item (with its sublists) as markdown.
|
|
406
|
+
* - marker: `-` / `*` (alternated, see {@link adjacentRun}), `- [ ]` / `- [x]` for a to-do item, `1.` / `1)` (the number
|
|
407
|
+
* is `start + index`);
|
|
408
|
+
* - everything that belongs to the item (continuation lines, sublists) is indented by the width of the marker;
|
|
409
|
+
* - its sublists follow, in order (an item may hold several, of different types: P-28).
|
|
410
|
+
* @param {{ type?: string, delta?: any[], props?: { checked?: boolean }, children?: any[] }} pb
|
|
411
|
+
* @param {RenderEnv} env
|
|
412
|
+
* @param {{ listType?: string, index?: number, start?: number, run?: number, indent?: string }} [ctx]
|
|
413
|
+
* @returns {string}
|
|
414
|
+
*/
|
|
415
|
+
export function renderItem(pb, env, { listType = 'bullet', index = 0, start = 1, run = 0, indent = '' } = {}) {
|
|
416
|
+
const number = start + index;
|
|
417
|
+
const bullet = BULLETS[run % 2];
|
|
418
|
+
const prefix = listType === 'ordered' ? `${number}${DELIMITERS[run % 2]}`
|
|
419
|
+
: listType === 'todo' ? `${bullet} [${pb.props?.checked ? 'x' : ' '}]` : bullet;
|
|
420
|
+
const pad = indent + ' '.repeat(markerWidth(listType, number));
|
|
421
|
+
const lines = [`${indent}${prefix} ${itemMarkdown(pb.delta || [], pad, env.toMarkdown)}`];
|
|
422
|
+
const subs = (pb.children || []).filter((child) => child?.type === 'list');
|
|
423
|
+
subs.forEach((sub, k) => {
|
|
424
|
+
const md = renderList(sub, env, { indent: pad, before: subs.slice(0, k) });
|
|
425
|
+
if (md) lines.push(md);
|
|
426
|
+
});
|
|
427
|
+
return lines.join('\n');
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* A list (its items and, recursively, their sublists) as markdown.
|
|
432
|
+
* @param {{ props?: { listType?: string, start?: unknown }, children?: any[] }} pb
|
|
433
|
+
* @param {RenderEnv} env
|
|
434
|
+
* @param {{ indent?: string, before?: Array<{ type: string, props?: { listType?: string } }> }} [ctx] `before`: the siblings before the list
|
|
435
|
+
* @returns {string}
|
|
436
|
+
*/
|
|
437
|
+
export function renderList(pb, env, { indent = '', before = [] } = {}) {
|
|
438
|
+
const listType = pb.props?.listType ?? 'bullet';
|
|
439
|
+
const start = env.listStart(listType, pb.props?.start) ?? 1;
|
|
440
|
+
const run = adjacentRun(listType, before);
|
|
441
|
+
return (pb.children || []).map((child, index) => renderItem(child, env, { listType, index, start, run, indent })).join('\n');
|
|
442
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brings the list structure back to its invariants (see the file header). Cheap when nothing
|
|
3
|
+
* structural is pending or when the document holds no list.
|
|
4
|
+
* @param {Nabu} nabu
|
|
5
|
+
* @param {{ full?: boolean }} [options] `full`: scan every list of the document instead of the scope of the pending ops
|
|
6
|
+
* found from the tree itself (the reference the scoped one is tested against: no dependence on the block registry)
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeLists(nabu: Nabu, options?: {
|
|
9
|
+
full?: boolean;
|
|
10
|
+
}): void;
|
|
11
|
+
import type { Nabu } from "../nabu.svelte.js";
|