@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.
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 +33 -14
  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 +2 -2
  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,451 @@
1
+ /**
2
+ * Split the formatting, code spans and links that span line breaks into separate lines, as
3
+ * Lexical reads Markdown line by line.
4
+ */
5
+
6
+ import {
7
+ findHtmlBlockLines,
8
+ endsParagraph,
9
+ interruptsParagraph,
10
+ TABLE_ROW_LINE_REGEX,
11
+ } from './block-structure.js';
12
+ import { mapOutsideCodeBlocks } from './code-blocks.js';
13
+ import { findCodeSpans, mapCodeSpans } from './code-spans.js';
14
+ import {
15
+ countBackslashesBefore,
16
+ UNESCAPED_LOOKBEHIND,
17
+ WHITESPACE_OR_PUNCTUATION,
18
+ } from './syntax.js';
19
+
20
+ /**
21
+ * @import { CodeSpan } from './code-spans.js';
22
+ */
23
+
24
+ /**
25
+ * Get the blockquote markers at the start of a line, like `> >` in `> > foo`, without the
26
+ * whitespace after the last one.
27
+ * @param {string} line Line.
28
+ * @returns {string} Markers, like `> >`, or an empty string if the line isn’t in a blockquote.
29
+ */
30
+ const getBlockquotePrefix = (line) => /^(?: {0,3}>)*/.exec(line)?.[0] ?? '';
31
+
32
+ /**
33
+ * Create a regular expression to match formatting that spans one or more line breaks, like
34
+ * `**foo\nbar**`. It captures the text between the markers, line breaks included. The text of each
35
+ * line is read as CommonMark does: a backslash escapes the next character, so an escaped marker is
36
+ * part of the text, and the text can end with an escaped backslash, like `_foo\\`, which is
37
+ * followed by a soft line break, not a hard one. A line can end with whitespace or the backslash of
38
+ * a hard line break, and start with whitespace and blockquote markers, like `> ` in
39
+ * `> **foo\n> bar**`, outside its text. The blockquote markers are matched as many as there are, so
40
+ * the text of a line can’t start with one, and the line can only be matched one way. The markers
41
+ * can’t be escaped, or next to whitespace within the formatting. The formatting must be at the
42
+ * start or the end of the value, or next to whitespace or punctuation other than its own marker, so
43
+ * a marker within a word, like `_` in `snake_case`, is left alone. As per the CommonMark flanking
44
+ * rules, `*` can open and close formatting within a word as well, like `**foo\nbar**baz`, unless
45
+ * the text starts or ends with punctuation there. The surrounding characters are checked with
46
+ * lookarounds rather than matched with `\s+`, which would take quadratic time on a long run of
47
+ * whitespace not followed by a marker. The text of a line can’t contain the marker, and it can’t
48
+ * start or end with whitespace, so the text and the line breaks can only be matched one way, which
49
+ * keeps the time linear.
50
+ * @param {string} char Marker character.
51
+ * @param {number} length Number of marker characters.
52
+ * @returns {RegExp} Regular expression.
53
+ * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
54
+ */
55
+ const createMultilineFormattingRegex = (char, length) => {
56
+ const marker = `[${char}]`.repeat(length);
57
+ // A character other than the marker and a line break, or an escape. The first and the last of the
58
+ // text can’t be whitespace
59
+ const unit = String.raw`(?:\\[^\s]|[^${char}\\\n])`;
60
+ const edgeUnit = String.raw`(?:\\[^\s]|[^${char}\s\\])`;
61
+ const text = `${edgeUnit}(?:${unit}*?${edgeUnit})?`;
62
+ const boundary = WHITESPACE_OR_PUNCTUATION;
63
+ const isWithinWordAllowed = char === '*';
64
+
65
+ const opening = isWithinWordAllowed
66
+ ? `(?:(?<=^|${boundary})${marker}|${marker}(?!${boundary}))`
67
+ : `(?<=^|${boundary})${marker}`;
68
+
69
+ const closing = isWithinWordAllowed
70
+ ? `(?:(?<!${boundary})${marker}|${marker}(?=${boundary}|$))`
71
+ : `${marker}(?=${boundary}|$)`;
72
+
73
+ const lineBreak = String.raw`(?:\\|[ \t]*)\n(?: {0,3}>)*(?! {0,3}>)[ \t]*`;
74
+
75
+ return new RegExp(
76
+ String.raw`(?<![${char}\\])${opening}(${text}(?:${lineBreak}${text})+)${closing}(?![${char}])`,
77
+ 'gu',
78
+ );
79
+ };
80
+
81
+ /**
82
+ * Regular expressions to match formatting that spans line breaks, with the marker of each.
83
+ * @type {[string, RegExp][]}
84
+ */
85
+ const MULTILINE_FORMATTING_REGEXES = [
86
+ ['***', createMultilineFormattingRegex('*', 3)],
87
+ ['___', createMultilineFormattingRegex('_', 3)],
88
+ ['**', createMultilineFormattingRegex('*', 2)],
89
+ ['__', createMultilineFormattingRegex('_', 2)],
90
+ ['~~', createMultilineFormattingRegex('~', 2)],
91
+ ['*', createMultilineFormattingRegex('*', 1)],
92
+ ['_', createMultilineFormattingRegex('_', 1)],
93
+ ];
94
+
95
+ /**
96
+ * Get the number of blockquote markers in a prefix, which is the nesting level of the blockquote.
97
+ * @param {string} prefix Prefix returned by {@link getBlockquotePrefix}.
98
+ * @returns {number} Level, or `0` if the line isn’t in a blockquote.
99
+ */
100
+ const getBlockquoteLevel = (prefix) => prefix.split('>').length - 1;
101
+
102
+ /**
103
+ * @typedef {object} FormattedLine
104
+ * @property {string} quote Blockquote markers at the start of the line, outside the formatting.
105
+ * Always empty for the first line.
106
+ * @property {string} indent Blockquote markers and whitespace at the start of the line, outside the
107
+ * formatting.
108
+ * @property {string} text Text of the line within the formatting.
109
+ * @property {string} lineEnd Whitespace or the backslash of a hard line break at the end of the
110
+ * line, outside the formatting.
111
+ */
112
+
113
+ /**
114
+ * Split the text of formatting that spans line breaks into lines. The text of a line doesn’t start
115
+ * or end with whitespace, a line after the first can start with blockquote markers, and the
116
+ * backslash of a hard line break is the last of an odd number of backslashes at the end of a line,
117
+ * as the others escape each other.
118
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
119
+ * @returns {FormattedLine[]} Lines.
120
+ */
121
+ const splitFormattedLines = (text) =>
122
+ text.split('\n').map((line, index) => {
123
+ const quote = index ? getBlockquotePrefix(line) : '';
124
+ const start = line.length - line.slice(quote.length).trimStart().length;
125
+ // Not a regular expression like `[ \t]*$`, which would take quadratic time on a long run of
126
+ // whitespace within the line
127
+ let end = line.length;
128
+
129
+ while (end > start && /[ \t]/.test(line[end - 1])) {
130
+ end -= 1;
131
+ }
132
+
133
+ if (countBackslashesBefore(line, end) % 2) {
134
+ end -= 1;
135
+ }
136
+
137
+ return {
138
+ quote,
139
+ indent: line.slice(0, start),
140
+ text: line.slice(start, end),
141
+ lineEnd: line.slice(end),
142
+ };
143
+ });
144
+
145
+ /**
146
+ * Check if a line after the first one of a paragraph is in the same blockquote: it has the same
147
+ * blockquote markers, or none, as a lazy continuation line. Lines in a nested or another blockquote
148
+ * aren’t in the same paragraph.
149
+ * @param {number} level Blockquote level of the first line of the paragraph.
150
+ * @param {string} prefix Blockquote prefix of the line.
151
+ * @returns {boolean} Result.
152
+ * @see https://spec.commonmark.org/0.31.2/#lazy-continuation-line
153
+ */
154
+ const isInSameBlockquote = (level, prefix) => !prefix || getBlockquoteLevel(prefix) === level;
155
+
156
+ /**
157
+ * Check if the text of formatting that spans line breaks has other formatting that spans line
158
+ * breaks, like `_foo\nbar_` in `**_foo\nbar_**` or `**_foo\nbar_\nbaz**`, which has to be split
159
+ * first.
160
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
161
+ * @returns {boolean} Result.
162
+ */
163
+ const hasMultilineFormatting = (text) =>
164
+ MULTILINE_FORMATTING_REGEXES.some(([, regex]) => text.search(regex) !== -1);
165
+
166
+ /**
167
+ * Regular expression to match a link whose text spans one or more line breaks, like
168
+ * `[foo\nbar](url)`, but not an image. The text can’t have brackets, so a `[` followed by many
169
+ * lines isn’t matched in quadratic time.
170
+ */
171
+ const MULTILINE_LINK_REGEX = new RegExp(
172
+ String.raw`(?<!(?:^|[^\\])(?:\\\\)*[\\!])\[([^[\]\n]*\n[^[\]]*)\]` +
173
+ String.raw`\(([^()\s]*(?:[ \t]+"[^"\n]*")?)\)`,
174
+ 'g',
175
+ );
176
+
177
+ /**
178
+ * Split a link whose text spans one or more line breaks into one per line, like
179
+ * `[foo\nbar](url)` into `[foo](url)\n[bar](url)`, as Lexical can’t read a link across a line
180
+ * break, and so the formatting around it can be split into separate lines as well. The whitespace
181
+ * at the start and the end of a line goes outside the link text. A link is left as is if a line
182
+ * has no text, or ends with the backslash of a hard line break. An image is left as is, as it
183
+ * would be duplicated, and so is anything in a code span, or a link with a code span that spans a
184
+ * line break, which can’t be split.
185
+ * @param {string} text Paragraph, or text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
186
+ * @returns {string} Processed text.
187
+ */
188
+ const splitMultilineLinks = (text) => {
189
+ if (!text.includes('[')) {
190
+ return text;
191
+ }
192
+
193
+ const codeSpans = findCodeSpans(text);
194
+
195
+ return text.replace(MULTILINE_LINK_REGEX, (match, linkText, target, offset) => {
196
+ const matchEnd = offset + match.length;
197
+
198
+ if (
199
+ codeSpans.some(
200
+ ({ start, end }) =>
201
+ start < matchEnd &&
202
+ end > offset &&
203
+ (start < offset || end > matchEnd || text.slice(start, end).includes('\n')),
204
+ )
205
+ ) {
206
+ return match;
207
+ }
208
+
209
+ const lines = splitFormattedLines(linkText);
210
+ const lastLine = lines[lines.length - 1];
211
+
212
+ // The whitespace at the start and the end of the link text is part of it
213
+ lines[0] = { ...lines[0], indent: '', text: `${lines[0].indent}${lines[0].text}` };
214
+ lines[lines.length - 1] = {
215
+ ...lastLine,
216
+ text: `${lastLine.text}${lastLine.lineEnd}`,
217
+ lineEnd: '',
218
+ };
219
+
220
+ if (
221
+ lines.some(({ text: lineText }) => !/\S/.test(lineText)) ||
222
+ lines.some(({ lineEnd }) => lineEnd.includes('\\'))
223
+ ) {
224
+ return match;
225
+ }
226
+
227
+ return lines
228
+ .map(({ indent, text: lineText, lineEnd }) => `${indent}[${lineText}](${target})${lineEnd}`)
229
+ .join('\n');
230
+ });
231
+ };
232
+
233
+ /**
234
+ * Regular expression to match the character before or after a code span that can be split into
235
+ * lines, which isn’t a word character, or the start or the end of the paragraph.
236
+ */
237
+ const SPAN_BOUNDARY_REGEX = new RegExp(`^$|${WHITESPACE_OR_PUNCTUATION}`, 'u');
238
+
239
+ /**
240
+ * Split a code span whose code spans one or more line breaks into one per line; see
241
+ * {@link splitMultilineSpans}.
242
+ * @param {string} paragraph Paragraph.
243
+ * @param {CodeSpan} span Code span in the paragraph.
244
+ * @returns {string} Code spans, one per line, or the code span as is if it can’t be split.
245
+ */
246
+ const splitCodeSpan = (paragraph, { start, end, fenceLength }) => {
247
+ const codeSpan = paragraph.slice(start, end);
248
+ let code = codeSpan.slice(fenceLength, -fenceLength);
249
+
250
+ if (
251
+ !code.includes('\n') ||
252
+ !SPAN_BOUNDARY_REGEX.test(paragraph.slice(start - 1, start)) ||
253
+ !SPAN_BOUNDARY_REGEX.test(paragraph.slice(end, end + 1))
254
+ ) {
255
+ return codeSpan;
256
+ }
257
+
258
+ // Like CommonMark, strip a space from both ends of the code if it starts and ends with one, like
259
+ // in `` ` foo\nbar ` ``, as it would otherwise be kept at the end of a single line
260
+ if (code.startsWith(' ') && code.endsWith(' ')) {
261
+ code = code.slice(1, -1);
262
+ }
263
+
264
+ // The blockquote markers and the indentation of a line after the first aren’t part of the code,
265
+ // so they go before the opening backticks
266
+ const codeLines = code.split('\n').map((line, index) => {
267
+ const indent = index ? (/^(?: {0,3}>)*[ \t]*/.exec(line)?.[0] ?? '') : '';
268
+
269
+ return { indent, text: line.slice(indent.length) };
270
+ });
271
+
272
+ // A line without code, like the first one of `` `\nfoo` ``, can’t be a code span
273
+ if (codeLines.some(({ text }) => !/\S/.test(text))) {
274
+ return codeSpan;
275
+ }
276
+
277
+ const fence = '`'.repeat(fenceLength);
278
+
279
+ return codeLines
280
+ .map(({ indent, text }) => {
281
+ // A backtick next to the fence would become part of it, and a space at both ends would be
282
+ // stripped, so the code is padded with a space on both sides, which is stripped from it
283
+ // instead, like in ``` `` `foo` `` ```
284
+ const padding = /^`|`$|^ [^]* $/.test(text) ? ' ' : '';
285
+
286
+ return `${indent}${fence}${padding}${text}${padding}${fence}`;
287
+ })
288
+ .join('\n');
289
+ };
290
+
291
+ /**
292
+ * Split a code span whose code spans one or more line breaks into one per line, like
293
+ * `` `foo\nbar` `` into `` `foo`\n`bar` ``, or ``` ``foo\nbar`` ``` into ``` ``foo``\n``bar`` ```,
294
+ * so the formatting around it can be split into separate lines as well, then do the same with a
295
+ * link with {@link splitMultilineLinks}. Backslashes and spaces are literal in a code span. The
296
+ * backticks are paired as Lexical pairs them, within a paragraph, so the closing backtick of a code
297
+ * span isn’t taken as the opening one of another, like in table rows such as
298
+ * `` | `**a**` | b |\n| `*c*` | d | ``, which would get stray backticks otherwise. A code span or a
299
+ * link can’t span a blank line or the start of another block, like a list item, a heading or a
300
+ * table row.
301
+ * @param {string} part Part of a Markdown string outside fenced code blocks.
302
+ * @returns {string} Processed part.
303
+ * @see https://github.com/sveltia/sveltia-cms/issues/1168
304
+ */
305
+ const splitMultilineSpans = (part) => {
306
+ if (!/[`[]/.test(part)) {
307
+ return part;
308
+ }
309
+
310
+ const lines = part.split('\n');
311
+ // An HTML block is raw HTML, which is kept as is, so each of its lines is left on its own
312
+ const htmlBlockLines = part.includes('<') ? findHtmlBlockLines(lines) : [];
313
+ // The lines of a paragraph in a blockquote are read without the blockquote markers
314
+ const quotes = lines.map(getBlockquotePrefix);
315
+ const contents = lines.map((line, index) => line.slice(quotes[index].length));
316
+ /** @type {string[][]} */
317
+ const paragraphs = [];
318
+ let level = 0;
319
+
320
+ lines.forEach((line, index) => {
321
+ const previous = contents[index - 1];
322
+
323
+ if (
324
+ index === 0 ||
325
+ htmlBlockLines[index] ||
326
+ htmlBlockLines[index - 1] ||
327
+ endsParagraph(previous) ||
328
+ interruptsParagraph(contents[index]) ||
329
+ !isInSameBlockquote(level, quotes[index]) ||
330
+ // Lexical reads a table row as a line of its own, so a code span can’t continue after it
331
+ TABLE_ROW_LINE_REGEX.test(previous)
332
+ ) {
333
+ paragraphs.push([line]);
334
+ level = getBlockquoteLevel(quotes[index]);
335
+ } else {
336
+ paragraphs[paragraphs.length - 1].push(line);
337
+ }
338
+ });
339
+
340
+ return paragraphs
341
+ .map((paragraphLines) => {
342
+ if (paragraphLines.length === 1) {
343
+ return paragraphLines[0];
344
+ }
345
+
346
+ const paragraph = paragraphLines.join('\n');
347
+
348
+ return splitMultilineLinks(
349
+ mapCodeSpans(paragraph, {
350
+ /**
351
+ * Split a code span of the paragraph.
352
+ * @param {string} _codeSpan Code span, with its backticks.
353
+ * @param {CodeSpan} span Position of the code span.
354
+ * @returns {string} Processed code span.
355
+ */
356
+ inside: (_codeSpan, span) => splitCodeSpan(paragraph, span),
357
+ }),
358
+ );
359
+ })
360
+ .join('\n');
361
+ };
362
+
363
+ /**
364
+ * Regular expression to match an inline link or a full reference link whose text spans one or more
365
+ * line breaks. The first run can’t have a line break, so a `[` followed by many lines isn’t matched
366
+ * in quadratic time. A bracket after an escaped backslash, like `\\[`, isn’t escaped.
367
+ */
368
+ const HAS_MULTILINE_LINK_TEXT_REGEX = new RegExp(
369
+ String.raw`${UNESCAPED_LOOKBEHIND}\[[^[\]\n]*\n[^[\]]*\](?=[([])`,
370
+ );
371
+
372
+ /**
373
+ * Check if the text of formatting that spans line breaks has a line break within a code span or a
374
+ * link text, which {@link splitMultilineSpans} and {@link splitMultilineLinks} couldn’t split.
375
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
376
+ * @returns {boolean} Result.
377
+ */
378
+ const hasLineBreakInSpan = (text) =>
379
+ findCodeSpans(text).some(({ start, end }) => text.slice(start, end).includes('\n')) ||
380
+ HAS_MULTILINE_LINK_TEXT_REGEX.test(text);
381
+
382
+ /**
383
+ * Split multiline formatting into separate lines to prevent Markdown parsing issues. Fenced code
384
+ * blocks are left untouched, as the formatting syntax is not parsed there. Formatting is left as is
385
+ * if a line after the first starts a block, like a heading or a list item, which ends the paragraph
386
+ * and therefore the formatting.
387
+ * @param {string} value Markdown string to process.
388
+ * @returns {string} Processed Markdown string.
389
+ * @see https://github.com/sveltia/sveltia-cms/issues/548
390
+ */
391
+ export const splitMultilineFormatting = (value) =>
392
+ mapOutsideCodeBlocks(value, (part) => {
393
+ let result = splitMultilineSpans(part);
394
+ let previous;
395
+
396
+ // Split the inner formatting first, so `**_foo\nbar_**` becomes `**_foo_**\n**_bar_**`, not
397
+ // `**_foo**_\n_**bar_**`. The outer one is split on the next pass. A text can’t have its own
398
+ // marker, so formatting is nested a few levels deep at most
399
+ while (result !== previous) {
400
+ previous = result;
401
+ result = MULTILINE_FORMATTING_REGEXES.reduce(
402
+ (current, [marker, regex]) =>
403
+ current.replace(regex, (match, formattedText, offset) => {
404
+ if (hasMultilineFormatting(formattedText)) {
405
+ return match;
406
+ }
407
+
408
+ const text = splitMultilineLinks(formattedText);
409
+
410
+ // A line break left within a code span or a link text can’t be split there, as the
411
+ // markers would become part of the code or the link
412
+ if (hasLineBreakInSpan(text)) {
413
+ return match;
414
+ }
415
+
416
+ const lines = splitFormattedLines(text);
417
+
418
+ // The formatting can only continue on a line in the same blockquote as the first one
419
+ const level = getBlockquoteLevel(
420
+ getBlockquotePrefix(current.slice(current.lastIndexOf('\n', offset - 1) + 1, offset)),
421
+ );
422
+
423
+ // A line with only markers, like `**` in `_**\n**_` of `**_(a)_**\n**_(b)_**`, means
424
+ // the match runs from the closing marker of one line to the opening marker of the next,
425
+ // which isn’t formatting. Splitting it would add markers that match again, without end
426
+ if (
427
+ lines.some(({ text: lineText }) => /^[*_~]+$/.test(lineText)) ||
428
+ lines
429
+ .slice(1)
430
+ .some(
431
+ ({ quote, indent, text: lineText }) =>
432
+ !isInSameBlockquote(level, quote) ||
433
+ interruptsParagraph(`${indent.slice(quote.length)}${lineText}`),
434
+ )
435
+ ) {
436
+ return match;
437
+ }
438
+
439
+ return lines
440
+ .map(
441
+ ({ indent, text: lineText, lineEnd }) =>
442
+ `${indent}${marker}${lineText}${marker}${lineEnd}`,
443
+ )
444
+ .join('\n');
445
+ }),
446
+ result,
447
+ );
448
+ }
449
+
450
+ return result;
451
+ });
@@ -0,0 +1,10 @@
1
+ export { trimBlankBlockquoteLines } from "./block-conversion.js";
2
+ export { removeImportPlaceholders } from "./placeholders.js";
3
+ export function convertBlockSyntax(value: string, { hasCodeBlocks, hasHeadings }: {
4
+ hasCodeBlocks: boolean;
5
+ hasHeadings: boolean;
6
+ }): string;
7
+ export function toLexicalMarkdown(value: string): {
8
+ markdown: string;
9
+ hasPlaceholders: boolean;
10
+ };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Markdown processing of the rich text editor: the Markdown is prepared before it’s imported, so
3
+ * Lexical reads it as CommonMark does, and the placeholders added for that are removed afterwards.
4
+ */
5
+
6
+ import {
7
+ convertSetextHeadings,
8
+ convertTildeFences,
9
+ padBlankBlockquoteLines,
10
+ } from './block-conversion.js';
11
+ import { withLineCache } from './code-blocks.js';
12
+ import { splitMultilineFormatting } from './formatting.js';
13
+ import { addImportPlaceholders } from './placeholders.js';
14
+
15
+ export { trimBlankBlockquoteLines } from './block-conversion.js';
16
+ export { removeImportPlaceholders } from './placeholders.js';
17
+
18
+ /**
19
+ * Convert the block syntax Lexical doesn’t support to the one it does, if the element is enabled:
20
+ * tilde code fences to backtick ones, and setext headings to ATX ones. Otherwise the syntax is kept
21
+ * as text, as written.
22
+ * @param {string} value Markdown.
23
+ * @param {object} features Enabled features.
24
+ * @param {boolean} features.hasCodeBlocks Whether code blocks are supported.
25
+ * @param {boolean} features.hasHeadings Whether headings are supported.
26
+ * @returns {string} Converted Markdown.
27
+ */
28
+ export const convertBlockSyntax = (value, { hasCodeBlocks, hasHeadings }) =>
29
+ withLineCache(() => {
30
+ const fencedValue = hasCodeBlocks ? convertTildeFences(value) : value;
31
+
32
+ return hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
33
+ });
34
+
35
+ /**
36
+ * Get the Markdown Lexical reads as CommonMark does, from Markdown converted with
37
+ * {@link convertBlockSyntax}. Formatting that spans line breaks is split into one per line first,
38
+ * as Lexical reads Markdown line by line, so the placeholders that follow are added to the final
39
+ * lines: they mark the backslashes, the tabs in code spans and raw HTML. Blank blockquote lines
40
+ * are then padded, so they are not imported as literal `>` text. The placeholders are removed from
41
+ * the imported text with `removeImportPlaceholders()`.
42
+ * @param {string} value Markdown.
43
+ * @returns {{ markdown: string, hasPlaceholders: boolean }} Markdown to import, and whether any
44
+ * placeholder has been added.
45
+ */
46
+ export const toLexicalMarkdown = (value) =>
47
+ withLineCache(() => {
48
+ const splitValue = splitMultilineFormatting(value);
49
+ const markedValue = addImportPlaceholders(splitValue);
50
+
51
+ return {
52
+ markdown: padBlankBlockquoteLines(markedValue),
53
+ hasPlaceholders: markedValue !== splitValue,
54
+ };
55
+ });
@@ -0,0 +1,2 @@
1
+ export function addImportPlaceholders(value: string): string;
2
+ export function removeImportPlaceholders(text: string): string;