@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
@@ -1,5 +1,14 @@
1
1
  import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
2
2
 
3
+ import {
4
+ ATX_HEADING_MARKER,
5
+ isWordChar,
6
+ MARKER_END,
7
+ ORDERED_LIST_DELIMITER,
8
+ ORDERED_LIST_NUMBER,
9
+ SETEXT_UNDERLINE,
10
+ } from '../markdown/syntax.js';
11
+ import { RAW_TEXT_PLACEHOLDER } from '../placeholder-chars.js';
3
12
  import { getFormatExportNode } from './inline-code.js';
4
13
  import { getRawSyntax } from './raw-syntax.js';
5
14
 
@@ -8,6 +17,24 @@ import { getRawSyntax } from './raw-syntax.js';
8
17
  * @import { ElementNode, LexicalNode, TextNode } from 'lexical';
9
18
  */
10
19
 
20
+ /**
21
+ * Regular expression to match the block syntax at the start of a line that is escaped with a
22
+ * backslash before its first character: an ATX heading, a bullet list item, a blockquote, a setext
23
+ * heading underline or a thematic break. A `*` bullet is left out, as Lexical already escapes it. A
24
+ * single `-` is a list item rather than a setext heading underline, which comes after it.
25
+ */
26
+ const BLOCK_SYNTAX_REGEX = new RegExp(
27
+ `^( {0,3})(${ATX_HEADING_MARKER}${MARKER_END}|[-+]${MARKER_END}|>|${SETEXT_UNDERLINE})`,
28
+ );
29
+
30
+ /**
31
+ * Regular expression to match an ordered list item at the start of a line, whose number can’t be
32
+ * escaped, unlike the delimiter after it.
33
+ */
34
+ const ORDERED_LIST_ITEM_REGEX = new RegExp(
35
+ `^( {0,3}${ORDERED_LIST_NUMBER})(${ORDERED_LIST_DELIMITER})${MARKER_END}`,
36
+ );
37
+
11
38
  /**
12
39
  * Escape the Markdown block syntax at the start of a line, so text that only looks like a heading,
13
40
  * a list item, a blockquote, a setext heading underline or a thematic break stays text when the
@@ -17,20 +44,7 @@ import { getRawSyntax } from './raw-syntax.js';
17
44
  * @returns {string} Escaped Markdown.
18
45
  */
19
46
  export const escapeBlockSyntax = (text) =>
20
- text
21
- // ATX heading, bullet list item, blockquote, setext heading underline or thematic break
22
- .replace(/^( {0,3})(#{1,6}(?=[ \t]|$)|[-+](?=[ \t]|$)|>|=+[ \t]*$|-{2,}[ \t]*$)/, '$1\\$2')
23
- // Ordered list item, whose number can’t be escaped, unlike the delimiter after it
24
- .replace(/^( {0,3}\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2');
25
-
26
- /**
27
- * Check if the given character is a word character, which is neither whitespace nor punctuation
28
- * (or a symbol). An underscore next to one on both sides can’t open or close emphasis.
29
- * @param {string | undefined} char Character.
30
- * @returns {boolean} Result.
31
- * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
32
- */
33
- const isWordChar = (char) => !!char && /[^\s\p{P}\p{S}]/u.test(char);
47
+ text.replace(BLOCK_SYNTAX_REGEX, '$1\\$2').replace(ORDERED_LIST_ITEM_REGEX, '$1\\$2');
34
48
 
35
49
  /**
36
50
  * Remove the backslashes Lexical adds before underscores and backslashes in text, where they’re not
@@ -111,6 +125,20 @@ export const unescapeCharacterReferences = (markdown, { isInLink, isAtLineEnd })
111
125
  return isNeeded ? `\\${char}` : char;
112
126
  });
113
127
 
128
+ /**
129
+ * Escape the `&` that would start a named character reference in text, like that in `©`.
130
+ * Lexical imports a named reference as text and exports the text as is, so the text, typed by the
131
+ * user or imported from the escaped `\©`, would become `©` the next time the Markdown is read
132
+ * by another renderer. A named reference in the Markdown is imported as raw syntax instead, so it’s
133
+ * exported as is; see `RAW_HTML`.
134
+ * @param {string} markdown Markdown exported from a text node with its format, in which every
135
+ * backslash starts an escape.
136
+ * @returns {string} Escaped Markdown.
137
+ * @see https://spec.commonmark.org/0.31.2/#entity-and-numeric-character-references
138
+ */
139
+ export const escapeNamedCharacterReferences = (markdown) =>
140
+ markdown.replace(/&(?=[A-Za-z][A-Za-z\d]*;)/g, '\\&');
141
+
114
142
  /**
115
143
  * Check if the given text node is italic and the italic text it’s part of is next to a word
116
144
  * character, like `b` in `a*b*c`. The italic text then needs the `*` delimiter instead of `_`,
@@ -149,11 +177,6 @@ const isItalicWithinWord = (node) => {
149
177
  return isWordChar(before) || isWordChar(after);
150
178
  };
151
179
 
152
- /**
153
- * Placeholder for the text of a raw syntax node, which Lexical doesn’t escape.
154
- */
155
- const RAW_TEXT = '\uE000';
156
-
157
180
  /**
158
181
  * Export-only transformer that tidies up the Markdown of a text node that isn’t inline code, which
159
182
  * Lexical exports verbatim. It removes the unnecessary escapes Lexical adds; see `unescapeText()`.
@@ -188,7 +211,9 @@ export const BLOCK_SYNTAX_ESCAPE = {
188
211
  // drops the closing syntax due after it, before a link for example, leaving `**_a_ **[b](u)**`.
189
212
  // Export a placeholder instead to get the closing syntax, which goes before the whitespace
190
213
  if (node.getFormat() && /^\s+$/.test(text)) {
191
- return `${exportFormat(getFormatExportNode(node), RAW_TEXT).replace(RAW_TEXT, '')}${text}`;
214
+ const syntax = exportFormat(getFormatExportNode(node), RAW_TEXT_PLACEHOLDER);
215
+
216
+ return `${syntax.replace(RAW_TEXT_PLACEHOLDER, '')}${text}`;
192
217
  }
193
218
 
194
219
  const rawSyntax = getRawSyntax(node);
@@ -196,7 +221,7 @@ export const BLOCK_SYNTAX_ESCAPE = {
196
221
  // Raw syntax is exported as is, but with the syntax of its format, if any
197
222
  let markdown = exportFormat(
198
223
  getFormatExportNode(node),
199
- rawSyntax === undefined ? text : RAW_TEXT,
224
+ rawSyntax === undefined ? text : RAW_TEXT_PLACEHOLDER,
200
225
  );
201
226
 
202
227
  if (isItalicWithinWord(node)) {
@@ -205,17 +230,19 @@ export const BLOCK_SYNTAX_ESCAPE = {
205
230
  }
206
231
 
207
232
  if (rawSyntax !== undefined) {
208
- return markdown.replace(RAW_TEXT, () => rawSyntax);
233
+ return markdown.replace(RAW_TEXT_PLACEHOLDER, () => rawSyntax);
209
234
  }
210
235
 
211
236
  const parent = node.getParent();
212
237
  const previous = node.getPreviousSibling();
213
238
  const next = node.getNextSibling();
214
239
 
215
- markdown = unescapeCharacterReferences(unescapeText(markdown), {
216
- isInLink: !!parent?.isInline(),
217
- isAtLineEnd: !next || isLineBreakNode(next),
218
- });
240
+ markdown = escapeNamedCharacterReferences(
241
+ unescapeCharacterReferences(unescapeText(markdown), {
242
+ isInLink: !!parent?.isInline(),
243
+ isAtLineEnd: !next || isLineBreakNode(next),
244
+ }),
245
+ );
219
246
 
220
247
  // Text in a link or other inline element, in a code block, which is exported verbatim, or in a
221
248
  // heading, whose content is only parsed for inline syntax, or text after the start of a line
@@ -1,14 +1,3 @@
1
- /**
2
- * @import { MultilineElementTransformer } from '@lexical/markdown';
3
- * @import { ElementNode, LexicalNode } from 'lexical';
4
- */
5
- /**
6
- * Placeholder for a backtick in the info string of a code block fenced with tildes, which is
7
- * converted to one fenced with backticks before the Markdown is imported; see
8
- * `convertTildeFences()`. Unlike that of a tilde fence, the info string of a backtick fence can’t
9
- * contain a backtick.
10
- */
11
- export const INFO_BACKTICK: "\uE001";
12
1
  /**
13
2
  * Code block transformer that, unlike Lexical’s, doesn’t take a line with a backtick after the
14
3
  * opening backtick fence for a code block. In CommonMark, the info string of a backtick fence
@@ -17,9 +6,9 @@ export const INFO_BACKTICK: "\uE001";
17
6
  * turn it into a code block the next time the Markdown is read. It also reads the info string as
18
7
  * CommonMark does when Lexical doesn’t: the language after spaces, like ```` ``` js ````, and the
19
8
  * metadata after the language of a code block without a closing fence, like
20
- * ```` ```js title=x ````, which Lexical would add to the code. Last, it exports a code block with
21
- * a backtick in its info string, imported from a tilde fence, with a tilde fence again. The rest is
22
- * Lexical’s.
9
+ * ```` ```js title=x ````, which Lexical would add to the code. It keeps the blank lines at the end
10
+ * of the code as well, which Lexical removes. Last, it exports a code block with a backtick in its
11
+ * info string, imported from a tilde fence, with a tilde fence again. The rest is Lexical’s.
23
12
  * @type {MultilineElementTransformer}
24
13
  * @see https://spec.commonmark.org/0.31.2/#fenced-code-blocks
25
14
  */
@@ -1,23 +1,63 @@
1
+ import { $isCodeNode as isCodeNode } from '@lexical/code-core';
1
2
  import { CODE as LEXICAL_CODE } from '@lexical/markdown';
3
+ import { $isTextNode as isTextNode } from 'lexical';
4
+
5
+ import { getCodeBlocks, parseOpeningFence } from '../markdown/code-blocks.js';
6
+ import { INFO_BACKTICK } from '../placeholder-chars.js';
2
7
 
3
8
  /**
4
9
  * @import { MultilineElementTransformer } from '@lexical/markdown';
5
10
  * @import { ElementNode, LexicalNode } from 'lexical';
11
+ * @import { OpeningFence } from '../markdown/code-blocks.js';
6
12
  */
7
13
 
8
14
  /**
9
- * Placeholder for a backtick in the info string of a code block fenced with tildes, which is
10
- * converted to one fenced with backticks before the Markdown is imported; see
11
- * `convertTildeFences()`. Unlike that of a tilde fence, the info string of a backtick fence can’t
12
- * contain a backtick.
15
+ * Check if the given line is within a code block starting in a list item, after its opening fence,
16
+ * like `- ~~~`.
17
+ * @param {string[]} lines Lines of the Markdown.
18
+ * @param {number} index Index of the line.
19
+ * @returns {boolean} Result.
13
20
  */
14
- export const INFO_BACKTICK = '\uE001';
21
+ const isInListItemCodeBlock = (lines, index) =>
22
+ // Lexical passes the same array of lines for every fence line of an import, so the lines are only
23
+ // scanned once per import rather than once per fence line
24
+ getCodeBlocks(lines).some(
25
+ ({ start, end }) => start < index && index <= end && !/^[ \t]*[`~]/.test(lines[start]),
26
+ );
15
27
 
16
28
  /**
17
- * Regular expression to match the opening fence of a code block and its info string, if any. Like
18
- * Lexical, the language is the leading word of the info string, and the rest is metadata.
29
+ * For each line of the Markdown being imported, the length of the longest line made only of
30
+ * backticks, which can close a code block, from that line to the end. Lexical passes the same array
31
+ * of lines for every fence line of an import, so it’s only computed once per import. Unlike the
32
+ * closing fence of `getCodeBlocks()`, it’s Lexical’s, which can’t be followed by whitespace.
33
+ * @type {WeakMap<string[], number[]>}
19
34
  */
20
- const OPENING_FENCE_REGEX = /^([ \t]*`{3,})[ \t]*([\w-]+)?[ \t]*/;
35
+ const closingFenceLengthCache = new WeakMap();
36
+
37
+ /**
38
+ * Check if a code block opened with the given fence has a closing fence after the given line.
39
+ * @param {string[]} lines Lines of the Markdown.
40
+ * @param {number} index Index of the line with the opening fence.
41
+ * @param {string} fence Opening fence.
42
+ * @returns {boolean} Result.
43
+ */
44
+ const hasClosingFence = (lines, index, fence) => {
45
+ let lengths = closingFenceLengthCache.get(lines);
46
+
47
+ if (!lengths) {
48
+ lengths = Array.from({ length: lines.length + 1 }, () => 0);
49
+
50
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
51
+ const length = lines[i].match(/^[ \t]*(`{3,})$/)?.[1].length ?? 0;
52
+
53
+ lengths[i] = Math.max(length, lengths[i + 1]);
54
+ }
55
+
56
+ closingFenceLengthCache.set(lines, lengths);
57
+ }
58
+
59
+ return lengths[index + 1] >= fence.length;
60
+ };
21
61
 
22
62
  /**
23
63
  * Code block transformer that, unlike Lexical’s, doesn’t take a line with a backtick after the
@@ -27,9 +67,9 @@ const OPENING_FENCE_REGEX = /^([ \t]*`{3,})[ \t]*([\w-]+)?[ \t]*/;
27
67
  * turn it into a code block the next time the Markdown is read. It also reads the info string as
28
68
  * CommonMark does when Lexical doesn’t: the language after spaces, like ```` ``` js ````, and the
29
69
  * metadata after the language of a code block without a closing fence, like
30
- * ```` ```js title=x ````, which Lexical would add to the code. Last, it exports a code block with
31
- * a backtick in its info string, imported from a tilde fence, with a tilde fence again. The rest is
32
- * Lexical’s.
70
+ * ```` ```js title=x ````, which Lexical would add to the code. It keeps the blank lines at the end
71
+ * of the code as well, which Lexical removes. Last, it exports a code block with a backtick in its
72
+ * info string, imported from a tilde fence, with a tilde fence again. The rest is Lexical’s.
33
73
  * @type {MultilineElementTransformer}
34
74
  * @see https://spec.commonmark.org/0.31.2/#fenced-code-blocks
35
75
  */
@@ -74,13 +114,21 @@ export const CODE = {
74
114
  * the index of its last line.
75
115
  */
76
116
  handleImportAfterStartMatch: ({ lines, startLineIndex, ...args }) => {
77
- const openingLine = lines[startLineIndex];
78
- const startMatch = /** @type {RegExpMatchArray} */ (openingLine.match(OPENING_FENCE_REGEX));
79
- const fence = startMatch[1].trim();
80
- const closingFenceRegex = new RegExp(`^[ \\t]*\`{${fence.length},}$`);
117
+ // The editor doesn’t support a code block in a list item, like `- ~~~`, whose lines are
118
+ // imported as the text of the item. Its closing fence, indented to the content of the item,
119
+ // or a fence within it is part of that text, not the opening fence of another code block
120
+ if (isInListItemCodeBlock(lines, startLineIndex)) {
121
+ return null;
122
+ }
123
+
124
+ // The language runs to the next whitespace, unlike Lexical’s; see `parseOpeningFence()`
125
+ const { fence, match: startMatch } = /** @type {OpeningFence} */ (
126
+ parseOpeningFence(lines[startLineIndex])
127
+ );
128
+
81
129
  // Lexical only reads the metadata of a code block with a closing fence, so close it at the end
82
130
  // of the Markdown if it isn’t closed
83
- const isClosed = lines.slice(startLineIndex + 1).some((line) => closingFenceRegex.test(line));
131
+ const isClosed = hasClosingFence(lines, startLineIndex, fence);
84
132
 
85
133
  const result = LEXICAL_CODE.handleImportAfterStartMatch?.({
86
134
  ...args,
@@ -89,6 +137,20 @@ export const CODE = {
89
137
  startMatch,
90
138
  });
91
139
 
140
+ // Lexical removes the blank lines at the end of the code, which are part of it in CommonMark,
141
+ // so add them back, unless the code is blank. Those of a code block without a closing fence
142
+ // are left out, as they’re rather the end of the document
143
+ if (isClosed && result?.[0] && result[1] > startLineIndex) {
144
+ const codeLines = lines.slice(startLineIndex + 1, result[1]);
145
+ const blankLineCount = codeLines.length - codeLines.findLastIndex((line) => !!line) - 1;
146
+ const codeNode = args.rootNode.getLastChild();
147
+ const textNode = isCodeNode(codeNode) ? codeNode.getLastChild() : null;
148
+
149
+ if (blankLineCount && isTextNode(textNode) && textNode.getTextContent()) {
150
+ textNode.setTextContent(`${textNode.getTextContent()}${'\n'.repeat(blankLineCount)}`);
151
+ }
152
+ }
153
+
92
154
  return result && [result[0], Math.min(result[1], lines.length - 1)];
93
155
  },
94
156
  regExpStart: new RegExp(`${LEXICAL_CODE.regExpStart.source}(?=[^\`]*$)`),
@@ -1,4 +1,5 @@
1
- import { findCodeSpans } from '../markdown.js';
1
+ import { findCodeSpans } from '../markdown/code-spans.js';
2
+ import { countBackslashesBefore } from '../markdown/syntax.js';
2
3
 
3
4
  /**
4
5
  * @import { TextFormatTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
@@ -30,22 +31,6 @@ const WHITESPACE = /\s/;
30
31
  */
31
32
  const PUNCTUATION = /[!"#$%&'()*+,\-./:;<=>?@[\]^_`{|}~]/;
32
33
 
33
- /**
34
- * Check if the character at the given index is escaped with a backslash.
35
- * @param {string} text Text.
36
- * @param {number} index Index.
37
- * @returns {boolean} Result.
38
- */
39
- const isEscaped = (text, index) => {
40
- let count = 0;
41
-
42
- while (text[index - count - 1] === '\\') {
43
- count += 1;
44
- }
45
-
46
- return count % 2 === 1;
47
- };
48
-
49
34
  /**
50
35
  * Check if a delimiter run is left-flanking or right-flanking, like Lexical does.
51
36
  * @param {string} text Text.
@@ -117,7 +102,7 @@ const findDelimiters = (text, chars, codeSpans) => {
117
102
 
118
103
  if (span && span.start <= index) {
119
104
  index = span.end;
120
- } else if (!chars.has(char) || isEscaped(text, index)) {
105
+ } else if (!chars.has(char) || countBackslashesBefore(text, index) % 2 === 1) {
121
106
  index += 1;
122
107
  } else {
123
108
  let length = 1;
@@ -5,6 +5,8 @@ import {
5
5
  $isTextNode as isTextNode,
6
6
  } from 'lexical';
7
7
 
8
+ import { WHITESPACE_CODE_PLACEHOLDER } from '../placeholder-chars.js';
9
+
8
10
  /**
9
11
  * @import { TextMatchTransformer } from '@lexical/markdown';
10
12
  * @import { ElementNode, LexicalNode, TextNode } from 'lexical';
@@ -52,7 +54,7 @@ export const getFormatExportNode = (node) => {
52
54
  * Get a placeholder instead of the whitespace.
53
55
  * @returns {string} Placeholder.
54
56
  */
55
- value: () => '\uE004',
57
+ value: () => WHITESPACE_CODE_PLACEHOLDER,
56
58
  },
57
59
  })
58
60
  : target;
@@ -2,6 +2,8 @@ import { $isLinkNode as isLinkNode } from '@lexical/link';
2
2
  import { LINK as LEXICAL_LINK } from '@lexical/markdown';
3
3
  import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
4
4
 
5
+ import { WHITESPACE_OR_PUNCTUATION_REGEX } from '../markdown/syntax.js';
6
+
5
7
  /**
6
8
  * @import { TextMatchTransformer } from '@lexical/markdown';
7
9
  * @import { ElementNode, LexicalNode, TextFormatType, TextNode } from 'lexical';
@@ -81,7 +83,7 @@ const getOuterFormats = (node) => {
81
83
  // Lexical puts the delimiters of the text inside its whitespace, which keeps them apart
82
84
  return formats.some((format) => sibling.hasFormat(format))
83
85
  ? /\s/.test(char)
84
- : /[\s\p{P}\p{S}]/u.test(char);
86
+ : WHITESPACE_OR_PUNCTUATION_REGEX.test(char);
85
87
  };
86
88
 
87
89
  return formats.length &&
@@ -1,22 +1,3 @@
1
- /**
2
- * @import { TextMatchTransformer } from '@lexical/markdown';
3
- * @import { TextNode } from 'lexical';
4
- */
5
- /**
6
- * Placeholder put at the start of a line of an HTML block; see `addImportPlaceholders()`.
7
- */
8
- export const HTML_BLOCK_PLACEHOLDER: "\uE007";
9
- /**
10
- * Regular expression source to match an HTML open tag or closing tag.
11
- * @see https://spec.commonmark.org/0.31.2/#raw-html
12
- */
13
- export const HTML_TAG_SOURCE: string;
14
- /**
15
- * Regular expression to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
16
- * processing instruction, a declaration or a CDATA section, which isn’t escaped.
17
- * @see https://spec.commonmark.org/0.31.2/#raw-html
18
- */
19
- export const INLINE_HTML_REGEX: RegExp;
20
1
  export function getImportedRawSyntax(node: TextNode): string | undefined;
21
2
  export function getRawSyntax(node: TextNode): string | undefined;
22
3
  /**
@@ -1,110 +1,38 @@
1
1
  import { $getState as getState, $setState as setState, createState } from 'lexical';
2
2
 
3
+ import {
4
+ ATX_HEADING_MARKER,
5
+ AUTOLINK_REGEX,
6
+ CODE_FENCE,
7
+ FOOTNOTE_REFERENCE_REGEX,
8
+ INLINE_HTML_REGEX,
9
+ LIST_MARKER,
10
+ MARKER_END,
11
+ NAMED_CHARACTER_REFERENCE_REGEX,
12
+ SETEXT_UNDERLINE,
13
+ THEMATIC_BREAK,
14
+ } from '../markdown/syntax.js';
15
+ import { HTML_BLOCK_PLACEHOLDER } from '../placeholder-chars.js';
16
+
3
17
  /**
4
18
  * @import { TextMatchTransformer } from '@lexical/markdown';
5
19
  * @import { TextNode } from 'lexical';
6
20
  */
7
21
 
8
- /**
9
- * Placeholder put at the start of a line of an HTML block; see `addImportPlaceholders()`.
10
- */
11
- export const HTML_BLOCK_PLACEHOLDER = '\uE007';
12
-
13
- /**
14
- * Whitespace within an HTML tag, including the placeholder of a tab; see `addImportPlaceholders()`.
15
- */
16
- const TAG_SPACE = '[ \\t\\uE005]';
17
-
18
- /**
19
- * Regular expression source to match an HTML open tag or closing tag.
20
- * @see https://spec.commonmark.org/0.31.2/#raw-html
21
- */
22
- export const HTML_TAG_SOURCE =
23
- `<[A-Za-z][A-Za-z0-9-]*(?:${TAG_SPACE}+[A-Za-z_:][\\w.:-]*` +
24
- `(?:${TAG_SPACE}*=${TAG_SPACE}*(?:[^\\s"'=<>\`\\uE005]+|'[^'\\n]*'|"[^"\\n]*"))?)*` +
25
- `${TAG_SPACE}*/?>|</[A-Za-z][A-Za-z0-9-]*${TAG_SPACE}*>`;
26
-
27
- /**
28
- * Regular expression source to match the place before a `<` that isn’t escaped. The lookahead comes
29
- * first, so the lookbehind is only tried before a `<`.
30
- */
31
- const UNESCAPED_LT_SOURCE = String.raw`(?=<)(?<!(?:^|[^\\])(?:\\\\)*\\)`;
32
-
33
- /**
34
- * Regular expression to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
35
- * processing instruction, a declaration or a CDATA section, which isn’t escaped.
36
- * @see https://spec.commonmark.org/0.31.2/#raw-html
37
- */
38
- export const INLINE_HTML_REGEX = new RegExp(
39
- [
40
- UNESCAPED_LT_SOURCE,
41
- '(?:',
42
- [
43
- HTML_TAG_SOURCE,
44
- // Comment
45
- String.raw`<!-->|<!--->|<!--.*?-->`,
46
- // Processing instruction
47
- String.raw`<\?.*?\?>`,
48
- // Declaration
49
- String.raw`<![A-Za-z][^>]*>`,
50
- // CDATA section
51
- String.raw`<!\[CDATA\[.*?\]\]>`,
52
- ].join('|'),
53
- ')',
54
- ].join(''),
55
- );
56
-
57
- /**
58
- * Regular expression source to match a URI scheme that runs a script or embeds content, in any
59
- * case, as a scheme is case-insensitive.
60
- */
61
- const UNSAFE_SCHEME_SOURCE = `(?:${['javascript', 'vbscript', 'data']
62
- .map((scheme) => scheme.replace(/[a-z]/g, (char) => `[${char}${char.toUpperCase()}]`))
63
- .join('|')})`;
64
-
65
- /**
66
- * Regular expression to match an autolink, like `<https://example.com>` or `<foo@example.com>`,
67
- * which isn’t escaped.
68
- * @see https://spec.commonmark.org/0.31.2/#autolinks
69
- */
70
- const AUTOLINK_REGEX = new RegExp(
71
- [
72
- UNESCAPED_LT_SOURCE,
73
- '(?:',
74
- [
75
- // URI, other than one with a scheme that runs a script or embeds content, which is left to
76
- // be escaped as text
77
- String.raw`<(?!${UNSAFE_SCHEME_SOURCE}:)[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*>`,
78
- // Email address
79
- String.raw`<[\w.!#$%&'*+/=?^\x60{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?` +
80
- String.raw`(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*>`,
81
- ].join('|'),
82
- ')',
83
- ].join(''),
84
- );
85
-
86
- /**
87
- * Regular expression to match a footnote reference, like `[^1]`, which isn’t escaped. Footnotes
88
- * aren’t supported, but they’re read by GitHub and many other renderers.
89
- * @see https://github.github.com/gfm/
90
- */
91
- const FOOTNOTE_REFERENCE_REGEX = /(?<!(?:^|[^\\])(?:\\\\)*\\)\[\^[^\s[\]]+\]/;
92
-
93
22
  /**
94
23
  * Block syntax at the start of a line, after up to 3 spaces.
95
24
  */
96
25
  const BLOCK_SYNTAX = [
97
26
  // Thematic break, which comes first as it can start like a bullet list item
98
- /([-*_])(?:[ \t]*\1){2,}[ \t]*$/,
27
+ THEMATIC_BREAK,
28
+ // ATX heading, bullet list item or ordered list item, which comes before a setext heading
29
+ // underline, as a single `-` is a list item
30
+ `(?:${ATX_HEADING_MARKER}|${LIST_MARKER})${MARKER_END}`,
99
31
  // Setext heading underline
100
- /(?:=+|-{2,})[ \t]*$/,
101
- // ATX heading, bullet list item or ordered list item
102
- /(?:#{1,6}|[-+*]|\d{1,9}[.)])(?=[ \t]|$)/,
32
+ SETEXT_UNDERLINE,
103
33
  // Blockquote or code fence
104
- />|`{3,}|~{3,}/,
105
- ]
106
- .map(({ source }) => source)
107
- .join('|');
34
+ `>|${CODE_FENCE}`,
35
+ ].join('|');
108
36
 
109
37
  /**
110
38
  * Label of a link reference definition or a footnote definition, like `[foo]:` in
@@ -204,6 +132,7 @@ const RAW_HTML_REGEX = new RegExp(
204
132
  INLINE_HTML_REGEX.source,
205
133
  AUTOLINK_REGEX.source,
206
134
  FOOTNOTE_REFERENCE_REGEX.source,
135
+ NAMED_CHARACTER_REFERENCE_REGEX.source,
207
136
  ].join('|'),
208
137
  );
209
138