@sveltia/ui 0.91.0 → 0.92.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/dist/components/bottom-navigation/bottom-navigation.svelte +3 -9
- package/dist/components/bottom-navigation/bottom-navigation.svelte.d.ts +4 -39
- package/dist/components/button/button-group.svelte +2 -4
- package/dist/components/button/button-group.svelte.d.ts +3 -19
- package/dist/components/button/select-button-group.svelte +2 -13
- package/dist/components/button/select-button-group.svelte.d.ts +5 -67
- package/dist/components/calendar/calendar.d.ts +9 -0
- package/dist/components/calendar/calendar.js +43 -0
- package/dist/components/calendar/calendar.svelte +26 -27
- package/dist/components/checkbox/checkbox-group.svelte +2 -8
- package/dist/components/checkbox/checkbox-group.svelte.d.ts +4 -39
- package/dist/components/dialog/alert-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/confirmation-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/dialog.svelte +6 -7
- package/dist/components/dialog/dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/prompt-dialog.svelte +2 -2
- package/dist/components/dialog/prompt-dialog.svelte.d.ts +2 -3
- package/dist/components/disclosure/disclosure.svelte +14 -16
- package/dist/components/disclosure/disclosure.svelte.d.ts +4 -38
- package/dist/components/drawer/drawer.svelte +22 -39
- package/dist/components/drawer/drawer.svelte.d.ts +3 -24
- package/dist/components/grid/grid-body.svelte +2 -3
- package/dist/components/grid/grid-body.svelte.d.ts +3 -18
- package/dist/components/grid/grid-cell.svelte +2 -8
- package/dist/components/grid/grid-cell.svelte.d.ts +3 -22
- package/dist/components/grid/grid-col-header.svelte +2 -4
- package/dist/components/grid/grid-col-header.svelte.d.ts +3 -19
- package/dist/components/grid/grid-foot.svelte +4 -9
- package/dist/components/grid/grid-foot.svelte.d.ts +3 -22
- package/dist/components/grid/grid-head.svelte +4 -9
- package/dist/components/grid/grid-head.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row-header.svelte +2 -8
- package/dist/components/grid/grid-row-header.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row.svelte +4 -5
- package/dist/components/grid/grid-row.svelte.d.ts +3 -19
- package/dist/components/grid/grid.d.ts +10 -0
- package/dist/components/grid/grid.js +11 -0
- package/dist/components/grid/grid.svelte +6 -7
- package/dist/components/grid/grid.svelte.d.ts +5 -21
- package/dist/components/listbox/listbox.svelte +34 -15
- package/dist/components/listbox/listbox.svelte.d.ts +5 -69
- package/dist/components/listbox/option-group.svelte +2 -8
- package/dist/components/listbox/option-group.svelte.d.ts +4 -39
- package/dist/components/listbox/option.svelte +2 -1
- package/dist/components/menu/menu-item-group.svelte +2 -6
- package/dist/components/menu/menu-item-group.svelte.d.ts +3 -22
- package/dist/components/menu/menu-item.svelte +5 -1
- package/dist/components/menu/menu.svelte +2 -8
- package/dist/components/menu/menu.svelte.d.ts +4 -39
- package/dist/components/radio/radio-group.svelte +2 -17
- package/dist/components/radio/radio-group.svelte.d.ts +5 -67
- package/dist/components/radio/radio.svelte +19 -22
- package/dist/components/radio/radio.svelte.d.ts +10 -40
- package/dist/components/resizable-pane/resizable-handle.svelte +42 -49
- package/dist/components/resizable-pane/resizable-pane-group.svelte +44 -58
- package/dist/components/resizable-pane/resizable-pane-group.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/resizable-pane.svelte +2 -5
- package/dist/components/resizable-pane/resizable-pane.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/sizing.d.ts +24 -0
- package/dist/components/resizable-pane/sizing.js +83 -0
- package/dist/components/select/combobox.svelte +35 -39
- package/dist/components/select/select-tags.svelte +3 -13
- package/dist/components/select/select-tags.svelte.d.ts +5 -67
- package/dist/components/slider/slider.d.ts +5 -4
- package/dist/components/slider/slider.js +28 -23
- package/dist/components/slider/slider.svelte +64 -62
- package/dist/components/slider/slider.svelte.d.ts +4 -37
- package/dist/components/switch/switch.svelte +2 -13
- package/dist/components/switch/switch.svelte.d.ts +5 -67
- package/dist/components/table/table-body.svelte +2 -4
- package/dist/components/table/table-body.svelte.d.ts +3 -19
- package/dist/components/table/table-cell.svelte +2 -8
- package/dist/components/table/table-cell.svelte.d.ts +3 -22
- package/dist/components/table/table-col-header.svelte +2 -8
- package/dist/components/table/table-col-header.svelte.d.ts +3 -22
- package/dist/components/table/table-foot.svelte +2 -8
- package/dist/components/table/table-foot.svelte.d.ts +3 -22
- package/dist/components/table/table-head.svelte +2 -8
- package/dist/components/table/table-head.svelte.d.ts +3 -22
- package/dist/components/table/table-part.svelte +2 -4
- package/dist/components/table/table-part.svelte.d.ts +3 -19
- package/dist/components/table/table-row-header.svelte +2 -8
- package/dist/components/table/table-row-header.svelte.d.ts +3 -22
- package/dist/components/table/table-row.svelte +2 -8
- package/dist/components/table/table-row.svelte.d.ts +3 -22
- package/dist/components/table/table.svelte +2 -4
- package/dist/components/table/table.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-box.svelte +2 -4
- package/dist/components/tabs/tab-box.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-list.svelte +2 -7
- package/dist/components/tabs/tab-list.svelte.d.ts +4 -37
- package/dist/components/tabs/tab-panel.svelte +2 -8
- package/dist/components/tabs/tab-panel.svelte.d.ts +3 -22
- package/dist/components/tabs/tab-panels.svelte +2 -8
- package/dist/components/tabs/tab-panels.svelte.d.ts +3 -22
- package/dist/components/text-editor/code-editor.js +14 -7
- package/dist/components/text-editor/code-editor.svelte +14 -22
- package/dist/components/text-editor/code-editor.svelte.d.ts +5 -103
- package/dist/components/text-editor/config.d.ts +73 -0
- package/dist/components/text-editor/config.js +110 -0
- package/dist/components/text-editor/conversion/editor-components.js +4 -4
- package/dist/components/text-editor/conversion/html/export.js +1 -1
- package/dist/components/text-editor/conversion/import.js +32 -26
- package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
- package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
- package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
- package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
- package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
- package/dist/components/text-editor/conversion/markdown/index.js +55 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
- package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
- package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
- package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
- package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
- package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
- package/dist/components/text-editor/conversion/transformers/code.js +78 -16
- package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
- package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
- package/dist/components/text-editor/conversion/transformers/link.js +3 -1
- package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
- package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
- package/dist/components/text-editor/conversion/transformers/table.js +57 -16
- package/dist/components/text-editor/conversion/update.d.ts +19 -2
- package/dist/components/text-editor/conversion/update.js +63 -39
- package/dist/components/text-editor/core.d.ts +12 -51
- package/dist/components/text-editor/core.js +44 -66
- package/dist/components/text-editor/editing/raw-markdown.d.ts +3 -0
- package/dist/components/text-editor/editing/raw-markdown.js +46 -2
- package/dist/components/text-editor/lexical-root.svelte +49 -47
- package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
- package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
- package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
- package/dist/components/text-editor/shiki/highlighter.js +5 -5
- package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
- package/dist/components/text-editor/shiki/lexical.js +165 -0
- package/dist/components/text-editor/shiki/preload.js +1 -1
- package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
- package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -0
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/store.svelte.js +65 -16
- package/dist/components/text-editor/text-editor.svelte +53 -145
- package/dist/components/text-editor/text-editor.svelte.d.ts +5 -103
- package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
- package/dist/components/text-editor/toolbar/actions.js +107 -0
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
- package/dist/components/text-editor/toolbar/format-text-button.svelte +26 -24
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
- package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
- package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +16 -4
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +34 -33
- package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
- package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
- package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
- package/dist/components/text-field/date-time-input.svelte +7 -8
- package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
- package/dist/components/text-field/number-input.d.ts +0 -2
- package/dist/components/text-field/number-input.js +25 -25
- package/dist/components/text-field/number-input.svelte +18 -9
- package/dist/components/text-field/number-input.svelte.d.ts +1 -1
- package/dist/components/text-field/text-area.d.ts +2 -0
- package/dist/components/text-field/text-area.js +31 -0
- package/dist/components/text-field/text-area.svelte +12 -25
- package/dist/components/text-field/text-area.svelte.d.ts +5 -67
- package/dist/components/toast/toast.svelte +31 -8
- package/dist/components/toast/toast.svelte.d.ts +6 -6
- package/dist/components/toolbar/toolbar.svelte +2 -7
- package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
- package/dist/components/tree/tree-item.svelte +2 -6
- package/dist/components/tree/tree-item.svelte.d.ts +3 -22
- package/dist/components/tree/tree.svelte +3 -10
- package/dist/components/tree/tree.svelte.d.ts +4 -39
- package/dist/components/util/group.svelte +2 -7
- package/dist/components/util/group.svelte.d.ts +4 -37
- package/dist/components/util/modal.d.ts +9 -0
- package/dist/components/util/modal.js +30 -0
- package/dist/components/util/modal.svelte +33 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/locales/hu.yaml +176 -0
- package/dist/services/composite-widget.svelte.d.ts +76 -0
- package/dist/services/composite-widget.svelte.js +157 -0
- package/dist/services/focus.d.ts +2 -0
- package/dist/services/focus.js +68 -0
- package/dist/services/group.svelte.d.ts +17 -69
- package/dist/services/group.svelte.js +128 -391
- package/dist/services/label.d.ts +9 -0
- package/dist/services/label.js +22 -0
- package/dist/services/menu.d.ts +8 -0
- package/dist/services/menu.js +237 -0
- package/dist/services/navigation.d.ts +9 -0
- package/dist/services/navigation.js +13 -0
- package/dist/services/number.d.ts +2 -0
- package/dist/services/number.js +27 -0
- package/dist/services/pointer-drag.d.ts +14 -0
- package/dist/services/pointer-drag.js +89 -0
- package/dist/services/popup.svelte.js +123 -103
- package/dist/services/text.d.ts +3 -0
- package/dist/services/text.js +17 -0
- package/dist/services/tree.svelte.d.ts +29 -43
- package/dist/services/tree.svelte.js +58 -105
- package/dist/services/type-ahead.d.ts +10 -1
- package/dist/services/type-ahead.js +56 -34
- package/dist/typedefs.d.ts +103 -4
- package/dist/typedefs.js +60 -4
- package/package.json +2 -2
- package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
- package/dist/components/text-editor/conversion/markdown.js +0 -949
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
- package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
- package/dist/components/text-editor/toolbar/insert-component.js +0 -37
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers to process a Markdown string outside its fenced code blocks, which are left untouched.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { LIST_MARKER } from './syntax.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} CodeBlock
|
|
9
|
+
* @property {number} start Index of the line with the opening fence.
|
|
10
|
+
* @property {number} end Index of the line with the closing fence, or of the last line of the code
|
|
11
|
+
* block if it is not closed, in which case it runs to the end of the document, or to the end of the
|
|
12
|
+
* list item it starts in.
|
|
13
|
+
* @property {string} fence Opening fence, made of three or more backticks or tildes.
|
|
14
|
+
* @property {boolean} closed Whether the code block has a closing fence.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Regular expression to match the fence at the start of a line, after the indentation and the
|
|
19
|
+
* markers of list items, if any, which are captured with it. A backtick fence can’t be followed by
|
|
20
|
+
* a backtick on the same line; see `CODE`.
|
|
21
|
+
*/
|
|
22
|
+
const LINE_FENCE_REGEX = new RegExp(
|
|
23
|
+
`^([ \\t]*(?:${LIST_MARKER}[ \\t]+)*)(\`{3,}(?=[^\`]*$)|~{3,})`,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Find the fenced code blocks in the lines of a Markdown string, including those starting right
|
|
28
|
+
* after the marker of a list item, like `- ~~~`, whose closing fence is indented to the content
|
|
29
|
+
* of the item.
|
|
30
|
+
* @param {string[]} lines Lines of a Markdown string.
|
|
31
|
+
* @returns {CodeBlock[]} Code blocks.
|
|
32
|
+
*/
|
|
33
|
+
export const findCodeBlocks = (lines) => {
|
|
34
|
+
/** @type {CodeBlock[]} */
|
|
35
|
+
const blocks = [];
|
|
36
|
+
/** @type {Omit<CodeBlock, 'end' | 'closed'> | undefined} */
|
|
37
|
+
let openBlock;
|
|
38
|
+
/**
|
|
39
|
+
* Indentation of the content of the list item the open code block starts in, if any.
|
|
40
|
+
* @type {number | undefined}
|
|
41
|
+
*/
|
|
42
|
+
let contentIndent;
|
|
43
|
+
|
|
44
|
+
lines.forEach((line, index) => {
|
|
45
|
+
// Like Lexical’s `CODE_START_REGEX`, allow an indented fence, and also one after the marker of
|
|
46
|
+
// a list item
|
|
47
|
+
const [, prefix = '', fence] = line.match(LINE_FENCE_REGEX) ?? [];
|
|
48
|
+
|
|
49
|
+
// A code block in a list item ends with the item, at a line indented less than its content
|
|
50
|
+
if (
|
|
51
|
+
openBlock &&
|
|
52
|
+
contentIndent !== undefined &&
|
|
53
|
+
line.trim() &&
|
|
54
|
+
/** @type {RegExpMatchArray} */ (line.match(/^[ \t]*/))[0].length < contentIndent
|
|
55
|
+
) {
|
|
56
|
+
blocks.push({ ...openBlock, end: index - 1, closed: false });
|
|
57
|
+
openBlock = undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!openBlock) {
|
|
61
|
+
if (fence) {
|
|
62
|
+
openBlock = { start: index, fence };
|
|
63
|
+
contentIndent = prefix.trim() ? prefix.length : undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Like Lexical’s `CODE_END_REGEX`, a code block is only closed by a bare fence of the same
|
|
70
|
+
// character that is at least as long as the opening one, so a shorter fence within the block,
|
|
71
|
+
// like ```` ``` ```` within ```` ```` ````, is part of the code
|
|
72
|
+
if (
|
|
73
|
+
fence?.[0] === openBlock.fence[0] &&
|
|
74
|
+
fence.length >= openBlock.fence.length &&
|
|
75
|
+
!line.trim().slice(fence.length)
|
|
76
|
+
) {
|
|
77
|
+
blocks.push({ ...openBlock, end: index, closed: true });
|
|
78
|
+
openBlock = undefined;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (openBlock) {
|
|
83
|
+
blocks.push({ ...openBlock, end: lines.length - 1, closed: false });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return blocks;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Code blocks found in the lines of a Markdown string, keyed by the array of lines.
|
|
91
|
+
* @type {WeakMap<string[], CodeBlock[]>}
|
|
92
|
+
*/
|
|
93
|
+
const codeBlockCache = new WeakMap();
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get the fenced code blocks in the lines of a Markdown string, like {@link findCodeBlocks}, but
|
|
97
|
+
* only scan the lines once for the same array, like the one Lexical passes to each transformer of
|
|
98
|
+
* an import, or the one {@link splitLines} returns for the same string. The array must not be
|
|
99
|
+
* changed afterwards.
|
|
100
|
+
* @param {string[]} lines Lines of a Markdown string.
|
|
101
|
+
* @returns {CodeBlock[]} Code blocks.
|
|
102
|
+
*/
|
|
103
|
+
export const getCodeBlocks = (lines) => {
|
|
104
|
+
let blocks = codeBlockCache.get(lines);
|
|
105
|
+
|
|
106
|
+
if (!blocks) {
|
|
107
|
+
blocks = findCodeBlocks(lines);
|
|
108
|
+
codeBlockCache.set(lines, blocks);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return blocks;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Markdown string last split by {@link splitLines}, with its lines, kept only while
|
|
116
|
+
* {@link withLineCache} runs, so the last document isn’t held in memory afterwards.
|
|
117
|
+
* @type {{ value: string, lines: string[] } | undefined}
|
|
118
|
+
*/
|
|
119
|
+
let lastSplit;
|
|
120
|
+
/**
|
|
121
|
+
* Number of {@link withLineCache} calls running, which may be nested.
|
|
122
|
+
*/
|
|
123
|
+
let lineCacheDepth = 0;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Run the given steps of an import with the lines of the Markdown cached: {@link splitLines}
|
|
127
|
+
* returns the same array for the same string as the last time, so {@link getCodeBlocks} doesn’t
|
|
128
|
+
* scan it again when a step leaves the string as is. The cache is cleared once the steps are done.
|
|
129
|
+
* @template T
|
|
130
|
+
* @param {() => T} steps Function that runs the steps.
|
|
131
|
+
* @returns {T} Result of the steps.
|
|
132
|
+
*/
|
|
133
|
+
export const withLineCache = (steps) => {
|
|
134
|
+
lineCacheDepth += 1;
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
return steps();
|
|
138
|
+
} finally {
|
|
139
|
+
lineCacheDepth -= 1;
|
|
140
|
+
|
|
141
|
+
if (!lineCacheDepth) {
|
|
142
|
+
lastSplit = undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Split a Markdown string into lines. Within {@link withLineCache}, the same array is returned for
|
|
149
|
+
* the same string as the last time. The array must not be changed.
|
|
150
|
+
* @internal
|
|
151
|
+
* @param {string} value Markdown string.
|
|
152
|
+
* @returns {string[]} Lines.
|
|
153
|
+
*/
|
|
154
|
+
export const splitLines = (value) => {
|
|
155
|
+
if (!lineCacheDepth) {
|
|
156
|
+
return value.split('\n');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (lastSplit?.value !== value) {
|
|
160
|
+
lastSplit = { value, lines: value.split('\n') };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return lastSplit.lines;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @typedef {object} OpeningFence
|
|
168
|
+
* @property {string} indent Whitespace before the fence.
|
|
169
|
+
* @property {string} fence Fence, made of three or more backticks or tildes.
|
|
170
|
+
* @property {string} info Rest of the line after the fence, the info string with the whitespace
|
|
171
|
+
* around it.
|
|
172
|
+
* @property {string | undefined} language Leading word of the info string, if any.
|
|
173
|
+
* @property {string} meta Rest of the info string after the language, if any, trimmed. Empty
|
|
174
|
+
* without a language, as the info string is then part of the code.
|
|
175
|
+
* @property {RegExpMatchArray} match Match of the fence, the info string up to the end of the
|
|
176
|
+
* language and the whitespace after it, which Lexical’s `CODE` transformer takes as `startMatch`:
|
|
177
|
+
* the indented fence is the first group, and the language the second.
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Regular expression to match the opening fence of a code block and the language of its info
|
|
182
|
+
* string, if any; see {@link parseOpeningFence}.
|
|
183
|
+
*/
|
|
184
|
+
const OPENING_FENCE_REGEX = /^([ \t]*(?:`{3,}|~{3,}))[ \t]*([\w-][^\s`]*)?[ \t]*/;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Parse the opening fence of a code block and its info string. Like Lexical, the language is the
|
|
188
|
+
* leading word of the info string, and the rest is metadata. Unlike Lexical’s, the word runs to the
|
|
189
|
+
* next whitespace or backtick, so a language with symbols, like `c#` or `c++`, isn’t cut off. Like
|
|
190
|
+
* Lexical’s, it must start with a word character; otherwise, like `{.js}`, the text is part of the
|
|
191
|
+
* code.
|
|
192
|
+
* @param {string} line Line.
|
|
193
|
+
* @returns {OpeningFence | undefined} Opening fence, or `undefined` if the line doesn’t start with
|
|
194
|
+
* one, including a backtick fence followed by a backtick, which can’t be in its info string.
|
|
195
|
+
* @see https://spec.commonmark.org/0.31.2/#fenced-code-blocks
|
|
196
|
+
*/
|
|
197
|
+
export const parseOpeningFence = (line) => {
|
|
198
|
+
const match = line.match(OPENING_FENCE_REGEX);
|
|
199
|
+
|
|
200
|
+
if (!match) {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const [prefix, indentedFence, language] = match;
|
|
205
|
+
const fence = indentedFence.trimStart();
|
|
206
|
+
const info = line.slice(indentedFence.length);
|
|
207
|
+
|
|
208
|
+
if (fence.startsWith('`') && info.includes('`')) {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
indent: indentedFence.slice(0, -fence.length),
|
|
214
|
+
fence,
|
|
215
|
+
info,
|
|
216
|
+
language,
|
|
217
|
+
meta: language ? line.slice(prefix.length).trim() : '',
|
|
218
|
+
match,
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Apply a function to the parts of a Markdown string outside fenced code blocks, leaving the code
|
|
224
|
+
* blocks, fences included, untouched. Each part is given with the line breaks around it, if any, so
|
|
225
|
+
* a regular expression can tell the start and the end of a line from those of the part.
|
|
226
|
+
* @param {string} value Markdown string to process.
|
|
227
|
+
* @param {(part: string) => string} callback Function that returns a processed part. It must keep
|
|
228
|
+
* the line breaks at the start and the end of the part.
|
|
229
|
+
* @returns {string} Processed Markdown string.
|
|
230
|
+
*/
|
|
231
|
+
export const mapOutsideCodeBlocks = (value, callback) => {
|
|
232
|
+
const lines = splitLines(value);
|
|
233
|
+
/** @type {string[]} */
|
|
234
|
+
const output = [];
|
|
235
|
+
let index = 0;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Process the lines from the current index to the given one, exclusive.
|
|
239
|
+
* @param {number} end Index of the line to stop at.
|
|
240
|
+
*/
|
|
241
|
+
const flush = (end) => {
|
|
242
|
+
if (end > index) {
|
|
243
|
+
const before = index > 0 ? '\n' : '';
|
|
244
|
+
const after = end < lines.length ? '\n' : '';
|
|
245
|
+
const part = callback(`${before}${lines.slice(index, end).join('\n')}${after}`);
|
|
246
|
+
|
|
247
|
+
output.push(part.slice(before.length, part.length - after.length));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
getCodeBlocks(lines).forEach(({ start, end }) => {
|
|
252
|
+
flush(start);
|
|
253
|
+
output.push(lines.slice(start, end + 1).join('\n'));
|
|
254
|
+
index = end + 1;
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
flush(lines.length);
|
|
258
|
+
|
|
259
|
+
return output.join('\n');
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Apply a function to each line of a Markdown string, leaving the lines inside fenced code blocks
|
|
264
|
+
* untouched.
|
|
265
|
+
* @param {string} value Markdown string to process.
|
|
266
|
+
* @param {(line: string) => string} callback Function that returns a processed line.
|
|
267
|
+
* @returns {string} Processed Markdown string.
|
|
268
|
+
*/
|
|
269
|
+
export const mapLinesOutsideCodeBlocks = (value, callback) => {
|
|
270
|
+
const lines = splitLines(value);
|
|
271
|
+
|
|
272
|
+
const codeLines = new Set(
|
|
273
|
+
getCodeBlocks(lines).flatMap(({ start, end }) =>
|
|
274
|
+
Array.from({ length: end - start + 1 }, (_, offset) => start + offset),
|
|
275
|
+
),
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
return lines.map((line, index) => (codeLines.has(index) ? line : callback(line))).join('\n');
|
|
279
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function findCodeSpans(line: string): CodeSpan[];
|
|
2
|
+
export function mapCodeSpans(text: string, { outside, inside }: {
|
|
3
|
+
outside?: ((text: string) => string) | undefined;
|
|
4
|
+
inside?: ((codeSpan: string, span: CodeSpan) => string) | undefined;
|
|
5
|
+
}): string;
|
|
6
|
+
export type CodeSpan = {
|
|
7
|
+
/**
|
|
8
|
+
* Index of the opening backticks.
|
|
9
|
+
*/
|
|
10
|
+
start: number;
|
|
11
|
+
/**
|
|
12
|
+
* Index after the closing backticks.
|
|
13
|
+
*/
|
|
14
|
+
end: number;
|
|
15
|
+
/**
|
|
16
|
+
* Number of backticks on either side.
|
|
17
|
+
*/
|
|
18
|
+
fenceLength: number;
|
|
19
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the code spans in Markdown, as Lexical reads them.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { countBackslashesBefore } from './syntax.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} CodeSpan
|
|
9
|
+
* @property {number} start Index of the opening backticks.
|
|
10
|
+
* @property {number} end Index after the closing backticks.
|
|
11
|
+
* @property {number} fenceLength Number of backticks on either side.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Find the code spans in a line like Lexical does: a run of backticks that isn’t escaped opens a
|
|
16
|
+
* code span, which is closed by the next run of as many backticks.
|
|
17
|
+
* @param {string} line Line.
|
|
18
|
+
* @returns {CodeSpan[]} Code spans.
|
|
19
|
+
* @see https://spec.commonmark.org/0.31.2/#code-spans
|
|
20
|
+
*/
|
|
21
|
+
export const findCodeSpans = (line) => {
|
|
22
|
+
const runs = [...line.matchAll(/`+/g)].map(({ 0: { length }, index }) => ({
|
|
23
|
+
index,
|
|
24
|
+
length,
|
|
25
|
+
escaped: countBackslashesBefore(line, index) % 2 === 1,
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
/** @type {CodeSpan[]} */
|
|
29
|
+
const spans = [];
|
|
30
|
+
|
|
31
|
+
for (let open = 0; open < runs.length; open += 1) {
|
|
32
|
+
let close = open + 1;
|
|
33
|
+
|
|
34
|
+
while (close < runs.length && runs[close].length !== runs[open].length) {
|
|
35
|
+
close += 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!runs[open].escaped && close < runs.length) {
|
|
39
|
+
const { index, length } = runs[close];
|
|
40
|
+
|
|
41
|
+
spans.push({ start: runs[open].index, end: index + length, fenceLength: length });
|
|
42
|
+
open = close;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return spans;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Apply functions to the code spans of a text and to the text outside them, like {@link
|
|
51
|
+
* findCodeSpans} finds them.
|
|
52
|
+
* @param {string} text Text.
|
|
53
|
+
* @param {object} callbacks Functions to apply.
|
|
54
|
+
* @param {(text: string) => string} [callbacks.outside] Function that returns a processed piece of
|
|
55
|
+
* text between code spans. The text is kept as is by default.
|
|
56
|
+
* @param {(codeSpan: string, span: CodeSpan) => string} [callbacks.inside] Function that returns a
|
|
57
|
+
* processed code span, given with its backticks. The code span is kept as is by default.
|
|
58
|
+
* @returns {string} Processed text.
|
|
59
|
+
*/
|
|
60
|
+
export const mapCodeSpans = (text, { outside = (piece) => piece, inside = (span) => span }) => {
|
|
61
|
+
let result = '';
|
|
62
|
+
let index = 0;
|
|
63
|
+
|
|
64
|
+
findCodeSpans(text).forEach((span) => {
|
|
65
|
+
result +=
|
|
66
|
+
outside(text.slice(index, span.start)) + inside(text.slice(span.start, span.end), span);
|
|
67
|
+
index = span.end;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return result + outside(text.slice(index));
|
|
71
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function splitMultilineFormatting(value: string): string;
|
|
2
|
+
export type FormattedLine = {
|
|
3
|
+
/**
|
|
4
|
+
* Blockquote markers at the start of the line, outside the formatting.
|
|
5
|
+
* Always empty for the first line.
|
|
6
|
+
*/
|
|
7
|
+
quote: string;
|
|
8
|
+
/**
|
|
9
|
+
* Blockquote markers and whitespace at the start of the line, outside the
|
|
10
|
+
* formatting.
|
|
11
|
+
*/
|
|
12
|
+
indent: string;
|
|
13
|
+
/**
|
|
14
|
+
* Text of the line within the formatting.
|
|
15
|
+
*/
|
|
16
|
+
text: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whitespace or the backslash of a hard line break at the end of the
|
|
19
|
+
* line, outside the formatting.
|
|
20
|
+
*/
|
|
21
|
+
lineEnd: string;
|
|
22
|
+
};
|