@aionbuilders/nabu 0.1.0-beta.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +523 -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 +199 -48
- package/dist/behaviors/text/text.behavior.svelte.js +549 -83
- 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 +31 -17
- 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 +1746 -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 +34 -13
- 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 +54 -7
- 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/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 +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 +9 -3
- package/dist/vite.js +19 -8
- package/package.json +54 -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,86 @@
|
|
|
1
|
+
/** @param {string} s */
|
|
2
|
+
export function normalizeLabel(s: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* Pull the link reference definitions (`[ref]: url "title"`, one line each) out of the markdown lines. Fenced code is
|
|
5
|
+
* left untouched, footnote definitions (`[^1]:`) are not links. The first definition of a label wins.
|
|
6
|
+
* @param {string[]} lines
|
|
7
|
+
* @returns {{ lines: string[], resolveRef: ((label: string) => LinkReference | null) | undefined }}
|
|
8
|
+
*/
|
|
9
|
+
export function extractLinkDefinitions(lines: string[]): {
|
|
10
|
+
lines: string[];
|
|
11
|
+
resolveRef: ((label: string) => LinkReference | null) | undefined;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Parse inline markdown into a sequence of text segments (a Loro delta each) and image segments: an image splits
|
|
15
|
+
* the text around it. Unsafe image URLs stay as their alt text.
|
|
16
|
+
* @param {string} text
|
|
17
|
+
* @param {InlineOptions} [options]
|
|
18
|
+
* @returns {InlineSegment[]}
|
|
19
|
+
*/
|
|
20
|
+
export function parseMarkdownSegments(text: string, options?: InlineOptions): InlineSegment[];
|
|
21
|
+
/**
|
|
22
|
+
* Parse inline markdown text into a Loro-compatible delta array:
|
|
23
|
+
* `**bold *italic* still bold**` → [bold, bold+italic, bold]; `[t](url)` → text with a `link` mark.
|
|
24
|
+
* An image becomes its alt text (use {@link parseMarkdownSegments} to keep images as blocks).
|
|
25
|
+
*
|
|
26
|
+
* @param {string} text
|
|
27
|
+
* @param {Record<string, any>} [marks] marks inherited by every run
|
|
28
|
+
* @param {InlineOptions} [options]
|
|
29
|
+
* @returns {import('loro-crdt').Delta<string>[]}
|
|
30
|
+
*/
|
|
31
|
+
export function parseMarkdownInline(text: string, marks?: Record<string, any>, options?: InlineOptions): import("loro-crdt").Delta<string>[];
|
|
32
|
+
export type Node = {
|
|
33
|
+
t: "text";
|
|
34
|
+
s: string;
|
|
35
|
+
} | {
|
|
36
|
+
t: "code";
|
|
37
|
+
s: string;
|
|
38
|
+
} | {
|
|
39
|
+
t: "br";
|
|
40
|
+
} | {
|
|
41
|
+
t: "delim";
|
|
42
|
+
ch: string;
|
|
43
|
+
len: number;
|
|
44
|
+
orig: number;
|
|
45
|
+
canOpen: boolean;
|
|
46
|
+
canClose: boolean;
|
|
47
|
+
} | {
|
|
48
|
+
t: "em";
|
|
49
|
+
mark: string;
|
|
50
|
+
children: Node[];
|
|
51
|
+
} | {
|
|
52
|
+
t: "link";
|
|
53
|
+
href: string;
|
|
54
|
+
children: Node[];
|
|
55
|
+
} | {
|
|
56
|
+
t: "image";
|
|
57
|
+
href: string;
|
|
58
|
+
children: Node[];
|
|
59
|
+
};
|
|
60
|
+
export type Item = {
|
|
61
|
+
insert: string;
|
|
62
|
+
attributes?: Record<string, any>;
|
|
63
|
+
} | {
|
|
64
|
+
image: {
|
|
65
|
+
alt: string;
|
|
66
|
+
ref: string;
|
|
67
|
+
};
|
|
68
|
+
marks: Record<string, any>;
|
|
69
|
+
};
|
|
70
|
+
export type LinkReference = {
|
|
71
|
+
href: string;
|
|
72
|
+
title?: string;
|
|
73
|
+
};
|
|
74
|
+
export type InlineOptions = {
|
|
75
|
+
resolveRef?: (label: string) => LinkReference | null;
|
|
76
|
+
};
|
|
77
|
+
export type ImageSegment = {
|
|
78
|
+
image: {
|
|
79
|
+
alt: string;
|
|
80
|
+
ref: string;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
export type TextSegment = {
|
|
84
|
+
delta: import("loro-crdt").Delta<string>[];
|
|
85
|
+
};
|
|
86
|
+
export type InlineSegment = TextSegment | ImageSegment;
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline markdown parser (CommonMark / GFM level) used by markdown paste and `importMarkdown`.
|
|
3
|
+
*
|
|
4
|
+
* Approach: one left-to-right scan builds a flat node list (text, code spans, delimiter runs, links, images…), then
|
|
5
|
+
* CommonMark's delimiter-stack algorithm pairs the emphasis delimiters; links / images are resolved when their `]` is
|
|
6
|
+
* met (inline destination, full / collapsed / shortcut reference). The tree is finally flattened to a Loro delta.
|
|
7
|
+
*
|
|
8
|
+
* Covered: `*em*` `**strong**` `***both***` `_em_` `__strong__` `~~strike~~` `<u>underline</u>`, code spans (any
|
|
9
|
+
* backtick run length), backslash escapes, hard line breaks, inline links (`[t](url "title")`, `<dest with spaces>`,
|
|
10
|
+
* balanced parentheses in URLs), reference links (`[t][ref]`, `[t][]`, `[t]`), autolinks (`<https://…>`, `<a@b.c>`),
|
|
11
|
+
* GFM bare URLs (`https://…`, `www.…`, trailing punctuation excluded), images (``, `![alt][ref]`).
|
|
12
|
+
* Emphasis flanking is shared with the typing shortcuts (`behaviors/text/markdown-syntax.js`).
|
|
13
|
+
*
|
|
14
|
+
* Every URL goes through `sanitizeUrl`: an unsafe link keeps its text and loses the link; an unsafe image keeps its
|
|
15
|
+
* alt text.
|
|
16
|
+
*
|
|
17
|
+
* Pure functions, no DOM, no runes.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { sanitizeUrl } from '../utils/html.js';
|
|
21
|
+
import { ASCII_PUNCTUATION, delimiterFlags } from '../behaviors/text/markdown-syntax.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {{ href: string, title?: string }} LinkReference
|
|
25
|
+
* @typedef {{ resolveRef?: (label: string) => LinkReference | null }} InlineOptions
|
|
26
|
+
* @typedef {{ image: { alt: string, ref: string } }} ImageSegment
|
|
27
|
+
* @typedef {{ delta: import('loro-crdt').Delta<string>[] }} TextSegment
|
|
28
|
+
* @typedef {TextSegment | ImageSegment} InlineSegment
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
// ── Link reference definitions ───────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
const DEFINITION_RE = /^ {0,3}\[((?:[^\[\]\\]|\\.)+)\]:[ \t]*(<(?:[^<>\n\\]|\\.)*>|[^\s<]\S*)(?:[ \t]+(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^()\\]|\\.)*\)))?[ \t]*$/;
|
|
34
|
+
/** An opening code fence: 3+ backticks (no backtick in the info string) or 3+ tildes. */
|
|
35
|
+
const FENCE_OPEN_RE = /^ {0,3}(`{3,}(?=[^`]*$)|~{3,})/;
|
|
36
|
+
|
|
37
|
+
/** @param {string} s */
|
|
38
|
+
export function normalizeLabel(s) {
|
|
39
|
+
return s.trim().replace(/\s+/g, ' ').toLowerCase();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {string} s */
|
|
43
|
+
function unescapeMarkdown(s) {
|
|
44
|
+
return s.replace(/\\([!-/:-@[-`{-~])/g, '$1');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Pull the link reference definitions (`[ref]: url "title"`, one line each) out of the markdown lines. Fenced code is
|
|
49
|
+
* left untouched, footnote definitions (`[^1]:`) are not links. The first definition of a label wins.
|
|
50
|
+
* @param {string[]} lines
|
|
51
|
+
* @returns {{ lines: string[], resolveRef: ((label: string) => LinkReference | null) | undefined }}
|
|
52
|
+
*/
|
|
53
|
+
export function extractLinkDefinitions(lines) {
|
|
54
|
+
/** @type {Map<string, LinkReference>} */
|
|
55
|
+
const defs = new Map();
|
|
56
|
+
/** @type {string[]} */
|
|
57
|
+
const kept = [];
|
|
58
|
+
let fence = '';
|
|
59
|
+
for (const line of lines) {
|
|
60
|
+
if (fence) {
|
|
61
|
+
// closing fence: the same character, at least as long, nothing but spaces after it
|
|
62
|
+
const close = /^ {0,3}(`+|~+)[ \t]*$/.exec(line);
|
|
63
|
+
if (close && close[1][0] === fence[0] && close[1].length >= fence.length) fence = '';
|
|
64
|
+
kept.push(line);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const open = FENCE_OPEN_RE.exec(line);
|
|
68
|
+
if (open) { fence = open[1]; kept.push(line); continue; }
|
|
69
|
+
const m = !fence && line.includes(']:') ? DEFINITION_RE.exec(line) : null;
|
|
70
|
+
if (!m || m[1].startsWith('^')) { kept.push(line); continue; }
|
|
71
|
+
const key = normalizeLabel(m[1]);
|
|
72
|
+
let dest = m[2];
|
|
73
|
+
dest = dest.startsWith('<') ? dest.slice(1, -1) : dest;
|
|
74
|
+
if (key && !defs.has(key)) defs.set(key, { href: unescapeMarkdown(dest) });
|
|
75
|
+
}
|
|
76
|
+
return { lines: kept, resolveRef: defs.size ? (label) => defs.get(normalizeLabel(label)) ?? null : undefined };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── Scanner ──────────────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
const AUTOLINK_RE = /<([A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*)>/y;
|
|
82
|
+
const EMAIL_RE = /<([A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*)>/y;
|
|
83
|
+
const UNDERLINE_TAG_RE = /<(\/?)u>/y;
|
|
84
|
+
const BARE_URL_RE = /(?:https?:\/\/[\p{L}\p{N}][^\s<]*|www\.[A-Za-z0-9][^\s<]*)/iuy;
|
|
85
|
+
const BARE_MIN_RE = /^(?:https?:\/\/[\p{L}\p{N}]|www\.[A-Za-z0-9])/iu;
|
|
86
|
+
/** A bare URL starts at the line start, after whitespace, or after one of these. */
|
|
87
|
+
const BARE_BOUNDARY = '*_~(';
|
|
88
|
+
const TRAILING_PUNCT = '?!.,:;*_~\'"';
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {{ t: 'text', s: string }
|
|
92
|
+
* | { t: 'code', s: string }
|
|
93
|
+
* | { t: 'br' }
|
|
94
|
+
* | { t: 'delim', ch: string, len: number, orig: number, canOpen: boolean, canClose: boolean }
|
|
95
|
+
* | { t: 'em', mark: string, children: Node[] }
|
|
96
|
+
* | { t: 'link', href: string, children: Node[] }
|
|
97
|
+
* | { t: 'image', href: string, children: Node[] }} Node
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Parse the destination (and ignored title) of an inline link, starting right after the `(`.
|
|
102
|
+
* @param {string} src
|
|
103
|
+
* @param {number} p
|
|
104
|
+
* @returns {{ href: string, end: number } | null}
|
|
105
|
+
*/
|
|
106
|
+
function parseDestination(src, p) {
|
|
107
|
+
const n = src.length;
|
|
108
|
+
let i = p;
|
|
109
|
+
while (i < n && /\s/.test(src[i])) i++;
|
|
110
|
+
let href = '';
|
|
111
|
+
if (src[i] === '<') {
|
|
112
|
+
i++;
|
|
113
|
+
while (i < n && src[i] !== '>') {
|
|
114
|
+
const ch = src[i];
|
|
115
|
+
if (ch === '\n' || ch === '<') return null;
|
|
116
|
+
if (ch === '\\' && ASCII_PUNCTUATION.includes(src[i + 1] ?? '')) { href += src[i + 1]; i += 2; continue; }
|
|
117
|
+
href += ch;
|
|
118
|
+
i++;
|
|
119
|
+
}
|
|
120
|
+
if (src[i] !== '>') return null;
|
|
121
|
+
i++;
|
|
122
|
+
} else {
|
|
123
|
+
let depth = 0;
|
|
124
|
+
while (i < n) {
|
|
125
|
+
const ch = src[i];
|
|
126
|
+
if (ch === '\\' && ASCII_PUNCTUATION.includes(src[i + 1] ?? '')) { href += src[i + 1]; i += 2; continue; }
|
|
127
|
+
if (/[\s\u0000-\u001f]/.test(ch)) break;
|
|
128
|
+
if (ch === '(') depth++;
|
|
129
|
+
else if (ch === ')') { if (depth === 0) break; depth--; }
|
|
130
|
+
href += ch;
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
if (depth !== 0) return null;
|
|
134
|
+
}
|
|
135
|
+
let j = i;
|
|
136
|
+
while (j < n && /\s/.test(src[j])) j++;
|
|
137
|
+
if (j > i && (src[j] === '"' || src[j] === "'" || src[j] === '(')) {
|
|
138
|
+
const close = src[j] === '(' ? ')' : src[j];
|
|
139
|
+
let k = j + 1;
|
|
140
|
+
while (k < n && src[k] !== close) k += src[k] === '\\' ? 2 : 1;
|
|
141
|
+
if (k >= n) return null;
|
|
142
|
+
j = k + 1;
|
|
143
|
+
while (j < n && /\s/.test(src[j])) j++;
|
|
144
|
+
}
|
|
145
|
+
if (src[j] !== ')') return null;
|
|
146
|
+
return { href, end: j + 1 };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Resolve the `]` at `close` into a link / image destination, or null (literal).
|
|
151
|
+
* @param {string} src
|
|
152
|
+
* @param {number} close
|
|
153
|
+
* @param {{ start: number }} opener
|
|
154
|
+
* @param {InlineOptions} opts
|
|
155
|
+
* @returns {{ href: string, end: number } | null}
|
|
156
|
+
*/
|
|
157
|
+
function resolveBracket(src, close, opener, opts) {
|
|
158
|
+
const after = close + 1;
|
|
159
|
+
if (src[after] === '(') {
|
|
160
|
+
const d = parseDestination(src, after + 1);
|
|
161
|
+
if (d) return d;
|
|
162
|
+
}
|
|
163
|
+
const resolve = opts.resolveRef;
|
|
164
|
+
if (!resolve) return null;
|
|
165
|
+
const label = src.slice(opener.start, close);
|
|
166
|
+
if (!label.trim()) return null;
|
|
167
|
+
if (src[after] === '[') {
|
|
168
|
+
const m = /^\[((?:[^\[\]\\]|\\.)*)\]/.exec(src.slice(after, after + 1002));
|
|
169
|
+
if (m) {
|
|
170
|
+
const ref = resolve(m[1].trim() ? m[1] : label);
|
|
171
|
+
return ref ? { href: ref.href, end: after + m[0].length } : null; // followed by a label: never a shortcut
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const ref = resolve(label);
|
|
175
|
+
return ref ? { href: ref.href, end: after } : null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* CommonMark's "process emphasis" on a node list: pair closing delimiters with the nearest compatible opener.
|
|
180
|
+
* Unmatched delimiters are left in place (flattened to literal text later).
|
|
181
|
+
* @param {Node[]} nodes mutated
|
|
182
|
+
*/
|
|
183
|
+
function processEmphasis(nodes) {
|
|
184
|
+
let ci = 0;
|
|
185
|
+
while (ci < nodes.length) {
|
|
186
|
+
const closer = nodes[ci];
|
|
187
|
+
if (closer.t !== 'delim' || !closer.canClose || closer.len === 0) { ci++; continue; }
|
|
188
|
+
let oi = -1;
|
|
189
|
+
for (let k = ci - 1; k >= 0; k--) {
|
|
190
|
+
const o = nodes[k];
|
|
191
|
+
if (o.t !== 'delim' || o.ch !== closer.ch || !o.canOpen || o.len === 0) continue;
|
|
192
|
+
if (o.ch === '~') { if (o.len === 2 && closer.len === 2) { oi = k; break; } continue; }
|
|
193
|
+
// "Multiple of 3" rule: an opener that can also close (or a closer that can also open) only pairs when
|
|
194
|
+
// the lengths do not sum to a multiple of 3, unless both are.
|
|
195
|
+
if (o.ch !== 'u' && (o.canClose || closer.canOpen) && (o.orig + closer.orig) % 3 === 0 && !(o.orig % 3 === 0 && closer.orig % 3 === 0)) continue;
|
|
196
|
+
oi = k;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
if (oi < 0) { ci++; continue; }
|
|
200
|
+
const opener = /** @type {Extract<Node, { t: 'delim' }>} */ (nodes[oi]);
|
|
201
|
+
const use = opener.ch === '~' ? 2 : opener.ch === 'u' ? 1 : opener.len >= 2 && closer.len >= 2 ? 2 : 1;
|
|
202
|
+
const mark = opener.ch === '~' ? 'strikethrough' : opener.ch === 'u' ? 'underline' : use === 2 ? 'bold' : 'italic';
|
|
203
|
+
const inner = nodes.slice(oi + 1, ci);
|
|
204
|
+
opener.len -= use;
|
|
205
|
+
closer.len -= use;
|
|
206
|
+
nodes.splice(oi + 1, ci - oi - 1, { t: 'em', mark, children: inner });
|
|
207
|
+
ci = oi + 2;
|
|
208
|
+
if (opener.len === 0) { nodes.splice(oi, 1); ci--; }
|
|
209
|
+
if (closer.len === 0) nodes.splice(ci, 1);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Strip GFM-excluded trailing punctuation from a bare URL candidate.
|
|
215
|
+
* @param {string} url
|
|
216
|
+
* @returns {string}
|
|
217
|
+
*/
|
|
218
|
+
function trimBareUrl(url) {
|
|
219
|
+
for (;;) {
|
|
220
|
+
const last = url[url.length - 1];
|
|
221
|
+
if (!last) return url;
|
|
222
|
+
if (TRAILING_PUNCT.includes(last)) {
|
|
223
|
+
const entity = last === ';' ? /&[A-Za-z0-9]+;$/.exec(url) : null; // `&` is not punctuation… but is not a URL end either
|
|
224
|
+
url = entity ? url.slice(0, -entity[0].length) : url.slice(0, -1);
|
|
225
|
+
} else if (last === ')') {
|
|
226
|
+
const open = (url.match(/\(/g) || []).length;
|
|
227
|
+
const close = (url.match(/\)/g) || []).length;
|
|
228
|
+
if (close > open) url = url.slice(0, -1);
|
|
229
|
+
else return url;
|
|
230
|
+
} else return url;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @param {string} src
|
|
236
|
+
* @param {InlineOptions} opts
|
|
237
|
+
* @returns {Node[]}
|
|
238
|
+
*/
|
|
239
|
+
function scan(src, opts) {
|
|
240
|
+
const n = src.length;
|
|
241
|
+
/** @type {Node[]} */
|
|
242
|
+
const nodes = [];
|
|
243
|
+
/** @type {Array<{ node: Node, image: boolean, active: boolean, start: number }>} */
|
|
244
|
+
const brackets = [];
|
|
245
|
+
let buf = '';
|
|
246
|
+
let i = 0;
|
|
247
|
+
|
|
248
|
+
const flush = () => { if (buf) { nodes.push({ t: 'text', s: buf }); buf = ''; } };
|
|
249
|
+
const skipSpaces = () => { while (src[i] === ' ' || src[i] === '\t') i++; };
|
|
250
|
+
/** Strip the spaces before a newline (from the pending buffer, or the last text node); returns how many. */
|
|
251
|
+
const stripTrailingSpaces = () => {
|
|
252
|
+
let stripped = 0;
|
|
253
|
+
const strip = (/** @type {string} */ s) => {
|
|
254
|
+
const m = / +$/.exec(s);
|
|
255
|
+
if (!m) return s;
|
|
256
|
+
stripped += m[0].length;
|
|
257
|
+
return s.slice(0, -m[0].length);
|
|
258
|
+
};
|
|
259
|
+
buf = strip(buf);
|
|
260
|
+
if (!buf) {
|
|
261
|
+
const last = nodes[nodes.length - 1];
|
|
262
|
+
if (last && last.t === 'text') last.s = strip(last.s);
|
|
263
|
+
}
|
|
264
|
+
return stripped;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
while (i < n) {
|
|
268
|
+
const c = src[i];
|
|
269
|
+
|
|
270
|
+
// A line break inside the text stays a `\n` of the block (callout / quote bodies are joined with it). Paragraph
|
|
271
|
+
// lines are joined BEFORE parsing (soft break = space, hard break = `\n`), see Paragraph.fromMarkdown.
|
|
272
|
+
if (c === '\n') {
|
|
273
|
+
stripTrailingSpaces();
|
|
274
|
+
i++;
|
|
275
|
+
skipSpaces();
|
|
276
|
+
flush();
|
|
277
|
+
nodes.push({ t: 'br' });
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (c === '\\') {
|
|
282
|
+
const next = src[i + 1];
|
|
283
|
+
if (next === '\n') { flush(); nodes.push({ t: 'br' }); i += 2; skipSpaces(); continue; }
|
|
284
|
+
if (next !== undefined && ASCII_PUNCTUATION.includes(next)) { buf += next; i += 2; continue; }
|
|
285
|
+
buf += c;
|
|
286
|
+
i++;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (c === '`') {
|
|
291
|
+
let j = i;
|
|
292
|
+
while (src[j] === '`') j++;
|
|
293
|
+
const len = j - i;
|
|
294
|
+
let found = -1;
|
|
295
|
+
for (let k = j; k < n;) {
|
|
296
|
+
const p = src.indexOf('`', k);
|
|
297
|
+
if (p < 0) break;
|
|
298
|
+
let q = p;
|
|
299
|
+
while (src[q] === '`') q++;
|
|
300
|
+
if (q - p === len) { found = p; break; }
|
|
301
|
+
k = q;
|
|
302
|
+
}
|
|
303
|
+
if (found < 0) { buf += src.slice(i, j); i = j; continue; }
|
|
304
|
+
let content = src.slice(j, found).replace(/\n/g, ' ');
|
|
305
|
+
if (content.length > 2 && content.startsWith(' ') && content.endsWith(' ') && content.trim()) content = content.slice(1, -1);
|
|
306
|
+
flush();
|
|
307
|
+
nodes.push({ t: 'code', s: content });
|
|
308
|
+
i = found + len;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (c === '*' || c === '_' || c === '~') {
|
|
313
|
+
let j = i;
|
|
314
|
+
while (src[j] === c) j++;
|
|
315
|
+
const len = j - i;
|
|
316
|
+
if (c === '~' && len !== 2) { buf += src.slice(i, j); i = j; continue; }
|
|
317
|
+
const { canOpen, canClose } = delimiterFlags(c, src[i - 1] ?? '', src[j] ?? '');
|
|
318
|
+
flush();
|
|
319
|
+
nodes.push({ t: 'delim', ch: c, len, orig: len, canOpen, canClose });
|
|
320
|
+
i = j;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (c === '[' || (c === '!' && src[i + 1] === '[')) {
|
|
325
|
+
const image = c === '!';
|
|
326
|
+
flush();
|
|
327
|
+
/** @type {Node} */
|
|
328
|
+
const node = { t: 'text', s: image ? '![' : '[' };
|
|
329
|
+
nodes.push(node);
|
|
330
|
+
i += image ? 2 : 1;
|
|
331
|
+
brackets.push({ node, image, active: true, start: i });
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (c === ']') {
|
|
336
|
+
const opener = brackets[brackets.length - 1];
|
|
337
|
+
if (!opener) { buf += c; i++; continue; }
|
|
338
|
+
brackets.pop();
|
|
339
|
+
const res = opener.active ? resolveBracket(src, i, opener, opts) : null;
|
|
340
|
+
if (!res) { buf += c; i++; continue; }
|
|
341
|
+
flush();
|
|
342
|
+
const idx = nodes.indexOf(opener.node);
|
|
343
|
+
const children = nodes.splice(idx + 1);
|
|
344
|
+
processEmphasis(children);
|
|
345
|
+
nodes[idx] = opener.image ? { t: 'image', href: res.href, children } : { t: 'link', href: res.href, children };
|
|
346
|
+
if (!opener.image) for (const b of brackets) if (!b.image) b.active = false; // links do not nest
|
|
347
|
+
i = res.end;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (c === '<') {
|
|
352
|
+
AUTOLINK_RE.lastIndex = i;
|
|
353
|
+
const auto = AUTOLINK_RE.exec(src);
|
|
354
|
+
if (auto) {
|
|
355
|
+
if (sanitizeUrl(auto[1], { kind: 'link' }) === null) buf += auto[0];
|
|
356
|
+
else { flush(); nodes.push({ t: 'link', href: auto[1], children: [{ t: 'text', s: auto[1] }] }); }
|
|
357
|
+
i += auto[0].length;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
EMAIL_RE.lastIndex = i;
|
|
361
|
+
const mail = EMAIL_RE.exec(src);
|
|
362
|
+
if (mail) {
|
|
363
|
+
flush();
|
|
364
|
+
nodes.push({ t: 'link', href: 'mailto:' + mail[1], children: [{ t: 'text', s: mail[1] }] });
|
|
365
|
+
i += mail[0].length;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
UNDERLINE_TAG_RE.lastIndex = i;
|
|
369
|
+
const tag = UNDERLINE_TAG_RE.exec(src);
|
|
370
|
+
if (tag) {
|
|
371
|
+
const closing = tag[1] === '/';
|
|
372
|
+
flush();
|
|
373
|
+
nodes.push({ t: 'delim', ch: 'u', len: 1, orig: 1, canOpen: !closing, canClose: closing });
|
|
374
|
+
i += tag[0].length;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
buf += c;
|
|
378
|
+
i++;
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (c === 'h' || c === 'H' || c === 'w' || c === 'W') {
|
|
383
|
+
const prev = src[i - 1];
|
|
384
|
+
if (prev === undefined || /\s/.test(prev) || BARE_BOUNDARY.includes(prev)) {
|
|
385
|
+
BARE_URL_RE.lastIndex = i;
|
|
386
|
+
const m = BARE_URL_RE.exec(src);
|
|
387
|
+
if (m) {
|
|
388
|
+
let url = m[0];
|
|
389
|
+
if (brackets.length) { const cut = url.indexOf(']'); if (cut >= 0) url = url.slice(0, cut); }
|
|
390
|
+
url = trimBareUrl(url);
|
|
391
|
+
if (BARE_MIN_RE.test(url)) {
|
|
392
|
+
flush();
|
|
393
|
+
nodes.push({ t: 'link', href: /^www\./i.test(url) ? 'http://' + url : url, children: [{ t: 'text', s: url }] });
|
|
394
|
+
i += url.length;
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
buf += c;
|
|
402
|
+
i++;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
flush();
|
|
406
|
+
processEmphasis(nodes);
|
|
407
|
+
return nodes;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ── Flatten ──────────────────────────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
/** @param {Node[]} nodes @returns {string} */
|
|
413
|
+
function plainText(nodes) {
|
|
414
|
+
let out = '';
|
|
415
|
+
for (const n of nodes) {
|
|
416
|
+
if (n.t === 'text' || n.t === 'code') out += n.s;
|
|
417
|
+
else if (n.t === 'br') out += ' ';
|
|
418
|
+
else if (n.t === 'delim') out += n.ch === 'u' ? '' : n.ch.repeat(n.len);
|
|
419
|
+
else out += plainText(n.children);
|
|
420
|
+
}
|
|
421
|
+
return out;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @typedef {{ insert: string, attributes?: Record<string, any> } | { image: { alt: string, ref: string }, marks: Record<string, any> }} Item
|
|
426
|
+
*/
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* @param {Node[]} nodes
|
|
430
|
+
* @param {Record<string, any>} marks
|
|
431
|
+
* @param {Item[]} out
|
|
432
|
+
*/
|
|
433
|
+
function flatten(nodes, marks, out) {
|
|
434
|
+
/** @param {string} s @param {Record<string, any>} m */
|
|
435
|
+
const text = (s, m) => { if (s) out.push(Object.keys(m).length ? { insert: s, attributes: { ...m } } : { insert: s }); };
|
|
436
|
+
for (const n of nodes) {
|
|
437
|
+
switch (n.t) {
|
|
438
|
+
case 'text': text(n.s, marks); break;
|
|
439
|
+
case 'code': text(n.s, { ...marks, code: true }); break;
|
|
440
|
+
case 'br': text('\n', marks); break;
|
|
441
|
+
case 'delim': text(n.ch === 'u' ? (n.canOpen ? '<u>' : '</u>') : n.ch.repeat(n.len), marks); break;
|
|
442
|
+
case 'em': flatten(n.children, { ...marks, [n.mark]: true }, out); break;
|
|
443
|
+
case 'link': {
|
|
444
|
+
const safe = n.href && marks.link === undefined ? sanitizeUrl(n.href, { kind: 'link' }) : null;
|
|
445
|
+
flatten(n.children, safe === null ? marks : { ...marks, link: n.href }, out);
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
case 'image': {
|
|
449
|
+
const alt = plainText(n.children);
|
|
450
|
+
if (n.href && sanitizeUrl(n.href, { kind: 'media' }) !== null) out.push({ image: { alt, ref: n.href }, marks });
|
|
451
|
+
else text(alt, marks);
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** @param {Record<string, any> | undefined} a @param {Record<string, any> | undefined} b */
|
|
459
|
+
function sameAttributes(a, b) {
|
|
460
|
+
const ka = Object.keys(a ?? {});
|
|
461
|
+
const kb = Object.keys(b ?? {});
|
|
462
|
+
return ka.length === kb.length && ka.every((k) => a?.[k] === b?.[k]);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Merge adjacent text ops with identical attributes into one.
|
|
467
|
+
* @param {Item[]} items
|
|
468
|
+
* @returns {import('loro-crdt').Delta<string>[]}
|
|
469
|
+
*/
|
|
470
|
+
function toDelta(items) {
|
|
471
|
+
/** @type {import('loro-crdt').Delta<string>[]} */
|
|
472
|
+
const delta = [];
|
|
473
|
+
for (const it of items) {
|
|
474
|
+
if (!('insert' in it) || !it.insert) continue;
|
|
475
|
+
const last = /** @type {any} */ (delta[delta.length - 1]);
|
|
476
|
+
if (last && sameAttributes(last.attributes, it.attributes)) last.insert += it.insert;
|
|
477
|
+
else delta.push(it.attributes ? { insert: it.insert, attributes: { ...it.attributes } } : { insert: it.insert });
|
|
478
|
+
}
|
|
479
|
+
return delta;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Parse inline markdown into a sequence of text segments (a Loro delta each) and image segments: an image splits
|
|
484
|
+
* the text around it. Unsafe image URLs stay as their alt text.
|
|
485
|
+
* @param {string} text
|
|
486
|
+
* @param {InlineOptions} [options]
|
|
487
|
+
* @returns {InlineSegment[]}
|
|
488
|
+
*/
|
|
489
|
+
export function parseMarkdownSegments(text, options = {}) {
|
|
490
|
+
if (!text) return [];
|
|
491
|
+
/** @type {Item[]} */
|
|
492
|
+
const items = [];
|
|
493
|
+
flatten(scan(text, options), {}, items);
|
|
494
|
+
/** @type {InlineSegment[]} */
|
|
495
|
+
const segments = [];
|
|
496
|
+
/** @type {Item[]} */
|
|
497
|
+
let run = [];
|
|
498
|
+
const closeRun = () => {
|
|
499
|
+
const delta = toDelta(run);
|
|
500
|
+
if (delta.length) segments.push({ delta });
|
|
501
|
+
run = [];
|
|
502
|
+
};
|
|
503
|
+
for (const it of items) {
|
|
504
|
+
if ('image' in it) { closeRun(); segments.push({ image: it.image }); } else run.push(it);
|
|
505
|
+
}
|
|
506
|
+
closeRun();
|
|
507
|
+
return segments;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Parse inline markdown text into a Loro-compatible delta array:
|
|
512
|
+
* `**bold *italic* still bold**` → [bold, bold+italic, bold]; `[t](url)` → text with a `link` mark.
|
|
513
|
+
* An image becomes its alt text (use {@link parseMarkdownSegments} to keep images as blocks).
|
|
514
|
+
*
|
|
515
|
+
* @param {string} text
|
|
516
|
+
* @param {Record<string, any>} [marks] marks inherited by every run
|
|
517
|
+
* @param {InlineOptions} [options]
|
|
518
|
+
* @returns {import('loro-crdt').Delta<string>[]}
|
|
519
|
+
*/
|
|
520
|
+
export function parseMarkdownInline(text, marks = {}, options = {}) {
|
|
521
|
+
if (!text) return [];
|
|
522
|
+
/** @type {Item[]} */
|
|
523
|
+
const items = [];
|
|
524
|
+
flatten(scan(text, options), marks, items);
|
|
525
|
+
return toDelta(items.map((it) => ('image' in it
|
|
526
|
+
? (it.image.alt ? { insert: it.image.alt, ...(Object.keys(it.marks).length ? { attributes: it.marks } : {}) } : { insert: '' })
|
|
527
|
+
: it)));
|
|
528
|
+
}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Uses a leftmost-match recursive strategy so nested marks work naturally:
|
|
4
|
-
* **bold *italic* still bold** → [bold, bold+italic, bold]
|
|
5
|
-
*
|
|
6
|
-
* @param {string} text
|
|
7
|
-
* @param {Record<string, true>} [marks]
|
|
8
|
-
* @returns {import('loro-crdt').Delta<string>[]}
|
|
2
|
+
* Paste of markdown typed as `text/plain`: parses it into blocks and inline marks through the rules the registered block classes declare (`markdownRules`). It also powers `importMarkdown()`.
|
|
9
3
|
*/
|
|
10
|
-
export function parseMarkdownInline(text: string, marks?: Record<string, true>): import("loro-crdt").Delta<string>[];
|
|
11
4
|
export const MarkdownPasteExtension: Extension;
|
|
12
5
|
import { Extension } from '../utils/extensions.js';
|
|
6
|
+
export { parseMarkdownInline, parseMarkdownSegments } from "./markdown-inline.js";
|