@sveltia/ui 0.91.0 → 0.92.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/dist/components/bottom-navigation/bottom-navigation.svelte +3 -9
  2. package/dist/components/bottom-navigation/bottom-navigation.svelte.d.ts +4 -39
  3. package/dist/components/button/button-group.svelte +2 -4
  4. package/dist/components/button/button-group.svelte.d.ts +3 -19
  5. package/dist/components/button/select-button-group.svelte +2 -13
  6. package/dist/components/button/select-button-group.svelte.d.ts +5 -67
  7. package/dist/components/calendar/calendar.d.ts +9 -0
  8. package/dist/components/calendar/calendar.js +43 -0
  9. package/dist/components/calendar/calendar.svelte +26 -27
  10. package/dist/components/checkbox/checkbox-group.svelte +2 -8
  11. package/dist/components/checkbox/checkbox-group.svelte.d.ts +4 -39
  12. package/dist/components/dialog/alert-dialog.svelte.d.ts +2 -3
  13. package/dist/components/dialog/confirmation-dialog.svelte.d.ts +2 -3
  14. package/dist/components/dialog/dialog.svelte +6 -7
  15. package/dist/components/dialog/dialog.svelte.d.ts +2 -3
  16. package/dist/components/dialog/prompt-dialog.svelte +2 -2
  17. package/dist/components/dialog/prompt-dialog.svelte.d.ts +2 -3
  18. package/dist/components/disclosure/disclosure.svelte +14 -16
  19. package/dist/components/disclosure/disclosure.svelte.d.ts +4 -38
  20. package/dist/components/drawer/drawer.svelte +22 -39
  21. package/dist/components/drawer/drawer.svelte.d.ts +3 -24
  22. package/dist/components/grid/grid-body.svelte +2 -3
  23. package/dist/components/grid/grid-body.svelte.d.ts +3 -18
  24. package/dist/components/grid/grid-cell.svelte +2 -8
  25. package/dist/components/grid/grid-cell.svelte.d.ts +3 -22
  26. package/dist/components/grid/grid-col-header.svelte +2 -4
  27. package/dist/components/grid/grid-col-header.svelte.d.ts +3 -19
  28. package/dist/components/grid/grid-foot.svelte +4 -9
  29. package/dist/components/grid/grid-foot.svelte.d.ts +3 -22
  30. package/dist/components/grid/grid-head.svelte +4 -9
  31. package/dist/components/grid/grid-head.svelte.d.ts +3 -22
  32. package/dist/components/grid/grid-row-header.svelte +2 -8
  33. package/dist/components/grid/grid-row-header.svelte.d.ts +3 -22
  34. package/dist/components/grid/grid-row.svelte +4 -5
  35. package/dist/components/grid/grid-row.svelte.d.ts +3 -19
  36. package/dist/components/grid/grid.d.ts +10 -0
  37. package/dist/components/grid/grid.js +11 -0
  38. package/dist/components/grid/grid.svelte +6 -7
  39. package/dist/components/grid/grid.svelte.d.ts +5 -21
  40. package/dist/components/listbox/listbox.svelte +34 -15
  41. package/dist/components/listbox/listbox.svelte.d.ts +5 -69
  42. package/dist/components/listbox/option-group.svelte +2 -8
  43. package/dist/components/listbox/option-group.svelte.d.ts +4 -39
  44. package/dist/components/listbox/option.svelte +2 -1
  45. package/dist/components/menu/menu-item-group.svelte +2 -6
  46. package/dist/components/menu/menu-item-group.svelte.d.ts +3 -22
  47. package/dist/components/menu/menu-item.svelte +5 -1
  48. package/dist/components/menu/menu.svelte +2 -8
  49. package/dist/components/menu/menu.svelte.d.ts +4 -39
  50. package/dist/components/radio/radio-group.svelte +2 -17
  51. package/dist/components/radio/radio-group.svelte.d.ts +5 -67
  52. package/dist/components/radio/radio.svelte +19 -22
  53. package/dist/components/radio/radio.svelte.d.ts +10 -40
  54. package/dist/components/resizable-pane/resizable-handle.svelte +42 -49
  55. package/dist/components/resizable-pane/resizable-pane-group.svelte +44 -58
  56. package/dist/components/resizable-pane/resizable-pane-group.svelte.d.ts +3 -19
  57. package/dist/components/resizable-pane/resizable-pane.svelte +2 -5
  58. package/dist/components/resizable-pane/resizable-pane.svelte.d.ts +3 -19
  59. package/dist/components/resizable-pane/sizing.d.ts +24 -0
  60. package/dist/components/resizable-pane/sizing.js +83 -0
  61. package/dist/components/select/combobox.svelte +35 -39
  62. package/dist/components/select/select-tags.svelte +3 -13
  63. package/dist/components/select/select-tags.svelte.d.ts +5 -67
  64. package/dist/components/slider/slider.d.ts +5 -4
  65. package/dist/components/slider/slider.js +28 -23
  66. package/dist/components/slider/slider.svelte +64 -62
  67. package/dist/components/slider/slider.svelte.d.ts +4 -37
  68. package/dist/components/switch/switch.svelte +2 -13
  69. package/dist/components/switch/switch.svelte.d.ts +5 -67
  70. package/dist/components/table/table-body.svelte +2 -4
  71. package/dist/components/table/table-body.svelte.d.ts +3 -19
  72. package/dist/components/table/table-cell.svelte +2 -8
  73. package/dist/components/table/table-cell.svelte.d.ts +3 -22
  74. package/dist/components/table/table-col-header.svelte +2 -8
  75. package/dist/components/table/table-col-header.svelte.d.ts +3 -22
  76. package/dist/components/table/table-foot.svelte +2 -8
  77. package/dist/components/table/table-foot.svelte.d.ts +3 -22
  78. package/dist/components/table/table-head.svelte +2 -8
  79. package/dist/components/table/table-head.svelte.d.ts +3 -22
  80. package/dist/components/table/table-part.svelte +2 -4
  81. package/dist/components/table/table-part.svelte.d.ts +3 -19
  82. package/dist/components/table/table-row-header.svelte +2 -8
  83. package/dist/components/table/table-row-header.svelte.d.ts +3 -22
  84. package/dist/components/table/table-row.svelte +2 -8
  85. package/dist/components/table/table-row.svelte.d.ts +3 -22
  86. package/dist/components/table/table.svelte +2 -4
  87. package/dist/components/table/table.svelte.d.ts +3 -19
  88. package/dist/components/tabs/tab-box.svelte +2 -4
  89. package/dist/components/tabs/tab-box.svelte.d.ts +3 -19
  90. package/dist/components/tabs/tab-list.svelte +2 -7
  91. package/dist/components/tabs/tab-list.svelte.d.ts +4 -37
  92. package/dist/components/tabs/tab-panel.svelte +2 -8
  93. package/dist/components/tabs/tab-panel.svelte.d.ts +3 -22
  94. package/dist/components/tabs/tab-panels.svelte +2 -8
  95. package/dist/components/tabs/tab-panels.svelte.d.ts +3 -22
  96. package/dist/components/text-editor/code-editor.js +14 -7
  97. package/dist/components/text-editor/code-editor.svelte +14 -22
  98. package/dist/components/text-editor/code-editor.svelte.d.ts +5 -103
  99. package/dist/components/text-editor/config.d.ts +73 -0
  100. package/dist/components/text-editor/config.js +110 -0
  101. package/dist/components/text-editor/conversion/editor-components.js +4 -4
  102. package/dist/components/text-editor/conversion/html/export.js +1 -1
  103. package/dist/components/text-editor/conversion/import.js +32 -26
  104. package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
  105. package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
  106. package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
  107. package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
  108. package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
  109. package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
  110. package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
  111. package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
  112. package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
  113. package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
  114. package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
  115. package/dist/components/text-editor/conversion/markdown/index.js +55 -0
  116. package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
  117. package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
  118. package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
  119. package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
  120. package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
  121. package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
  122. package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
  123. package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
  124. package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
  125. package/dist/components/text-editor/conversion/transformers/code.js +78 -16
  126. package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
  127. package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
  128. package/dist/components/text-editor/conversion/transformers/link.js +3 -1
  129. package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
  130. package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
  131. package/dist/components/text-editor/conversion/transformers/table.js +57 -16
  132. package/dist/components/text-editor/conversion/update.d.ts +19 -2
  133. package/dist/components/text-editor/conversion/update.js +63 -39
  134. package/dist/components/text-editor/core.d.ts +12 -51
  135. package/dist/components/text-editor/core.js +44 -66
  136. package/dist/components/text-editor/editing/raw-markdown.d.ts +3 -0
  137. package/dist/components/text-editor/editing/raw-markdown.js +46 -2
  138. package/dist/components/text-editor/lexical-root.svelte +49 -47
  139. package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
  140. package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
  141. package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
  142. package/dist/components/text-editor/shiki/highlighter.js +5 -5
  143. package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
  144. package/dist/components/text-editor/shiki/lexical.js +165 -0
  145. package/dist/components/text-editor/shiki/preload.js +1 -1
  146. package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
  147. package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -0
  148. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  149. package/dist/components/text-editor/shiki/version.js +1 -1
  150. package/dist/components/text-editor/store.svelte.js +65 -16
  151. package/dist/components/text-editor/text-editor.svelte +53 -145
  152. package/dist/components/text-editor/text-editor.svelte.d.ts +5 -103
  153. package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
  154. package/dist/components/text-editor/toolbar/actions.js +107 -0
  155. package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
  156. package/dist/components/text-editor/toolbar/format-text-button.svelte +26 -24
  157. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
  158. package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
  159. package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
  160. package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
  161. package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
  162. package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
  163. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +16 -4
  164. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
  165. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +34 -33
  166. package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
  167. package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
  168. package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
  169. package/dist/components/text-field/date-time-input.svelte +7 -8
  170. package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
  171. package/dist/components/text-field/number-input.d.ts +0 -2
  172. package/dist/components/text-field/number-input.js +25 -25
  173. package/dist/components/text-field/number-input.svelte +18 -9
  174. package/dist/components/text-field/number-input.svelte.d.ts +1 -1
  175. package/dist/components/text-field/text-area.d.ts +2 -0
  176. package/dist/components/text-field/text-area.js +31 -0
  177. package/dist/components/text-field/text-area.svelte +12 -25
  178. package/dist/components/text-field/text-area.svelte.d.ts +5 -67
  179. package/dist/components/toast/toast.svelte +31 -8
  180. package/dist/components/toast/toast.svelte.d.ts +6 -6
  181. package/dist/components/toolbar/toolbar.svelte +2 -7
  182. package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
  183. package/dist/components/tree/tree-item.svelte +2 -6
  184. package/dist/components/tree/tree-item.svelte.d.ts +3 -22
  185. package/dist/components/tree/tree.svelte +3 -10
  186. package/dist/components/tree/tree.svelte.d.ts +4 -39
  187. package/dist/components/util/group.svelte +2 -7
  188. package/dist/components/util/group.svelte.d.ts +4 -37
  189. package/dist/components/util/modal.d.ts +9 -0
  190. package/dist/components/util/modal.js +30 -0
  191. package/dist/components/util/modal.svelte +16 -12
  192. package/dist/index.d.ts +1 -1
  193. package/dist/index.js +1 -1
  194. package/dist/locales/hu.yaml +176 -0
  195. package/dist/services/composite-widget.svelte.d.ts +76 -0
  196. package/dist/services/composite-widget.svelte.js +157 -0
  197. package/dist/services/focus.d.ts +2 -0
  198. package/dist/services/focus.js +68 -0
  199. package/dist/services/group.svelte.d.ts +17 -69
  200. package/dist/services/group.svelte.js +128 -391
  201. package/dist/services/label.d.ts +9 -0
  202. package/dist/services/label.js +22 -0
  203. package/dist/services/menu.d.ts +8 -0
  204. package/dist/services/menu.js +237 -0
  205. package/dist/services/navigation.d.ts +9 -0
  206. package/dist/services/navigation.js +13 -0
  207. package/dist/services/number.d.ts +2 -0
  208. package/dist/services/number.js +27 -0
  209. package/dist/services/pointer-drag.d.ts +14 -0
  210. package/dist/services/pointer-drag.js +89 -0
  211. package/dist/services/popup.svelte.js +123 -103
  212. package/dist/services/text.d.ts +3 -0
  213. package/dist/services/text.js +17 -0
  214. package/dist/services/tree.svelte.d.ts +29 -43
  215. package/dist/services/tree.svelte.js +58 -105
  216. package/dist/services/type-ahead.d.ts +10 -1
  217. package/dist/services/type-ahead.js +56 -34
  218. package/dist/typedefs.d.ts +103 -4
  219. package/dist/typedefs.js +60 -4
  220. package/package.json +1 -1
  221. package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
  222. package/dist/components/text-editor/conversion/markdown.js +0 -949
  223. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
  224. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
  225. package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
  226. 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
+ };