@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,154 @@
1
+ /**
2
+ * Convert the block syntax Lexical doesn’t support, like tilde fences and setext headings, to the
3
+ * one it does, and keep blank blockquote lines.
4
+ */
5
+
6
+ import { INFO_BACKTICK } from '../placeholder-chars.js';
7
+ import { ATX_HEADING_REGEX, isParagraphLine, SETEXT_UNDERLINE_REGEX } from './block-structure.js';
8
+ import {
9
+ findCodeBlocks,
10
+ mapLinesOutsideCodeBlocks,
11
+ mapOutsideCodeBlocks,
12
+ parseOpeningFence,
13
+ } from './code-blocks.js';
14
+
15
+ /**
16
+ * @import { OpeningFence } from './code-blocks.js';
17
+ */
18
+
19
+ /**
20
+ * Convert the fenced code blocks using tildes, like `~~~`, to ones using backticks, because Lexical
21
+ * only supports the latter. The new fence is made longer than any backtick fence in the code, so
22
+ * the code block isn’t closed early. Backticks can’t be used in the info string of a backtick
23
+ * fence, so those after the language are replaced with a placeholder, which the code block
24
+ * transformer turns back into backticks, exporting a tilde fence again; see `CODE`. Other backticks
25
+ * are removed.
26
+ * @param {string} value Markdown string to process.
27
+ * @returns {string} Processed Markdown string.
28
+ */
29
+ export const convertTildeFences = (value) => {
30
+ if (!/^[ \t]*~{3,}/m.test(value)) {
31
+ return value;
32
+ }
33
+
34
+ const lines = value.split('\n');
35
+
36
+ findCodeBlocks(lines)
37
+ // A code block in a list item, like `- ~~~`, is left as is, as it’s imported as the text of
38
+ // the item; see `CODE`
39
+ .filter(({ start, fence }) => fence.startsWith('~') && /^[ \t]*~/.test(lines[start]))
40
+ .forEach(({ start, end, closed }) => {
41
+ const codeLines = lines.slice(start + 1, closed ? end : end + 1);
42
+
43
+ const longestFence = Math.max(
44
+ 2,
45
+ ...codeLines.map(
46
+ (line) => /** @type {RegExpMatchArray} */ (line.match(/^[ \t]*(`*)/))[1].length,
47
+ ),
48
+ );
49
+
50
+ const backticks = '`'.repeat(longestFence + 1);
51
+
52
+ const { indent, info, language } = /** @type {OpeningFence} */ (
53
+ parseOpeningFence(lines[start])
54
+ );
55
+
56
+ // Unlike that of a tilde fence, the info string of a backtick fence can’t contain a backtick.
57
+ // The language can’t contain one either, so only the metadata after it is replaced
58
+ lines[start] = `${indent}${backticks}${info.replaceAll('`', language ? INFO_BACKTICK : '')}`;
59
+
60
+ if (closed) {
61
+ lines[end] = lines[end].replace(/^([ \t]*)~+[ \t]*$/, `$1${backticks}`);
62
+ }
63
+ });
64
+
65
+ return lines.join('\n');
66
+ };
67
+
68
+ /**
69
+ * Multiline version of `SETEXT_UNDERLINE_REGEX`, which matches at the start of any line.
70
+ */
71
+ const SETEXT_UNDERLINE_MULTILINE_REGEX = new RegExp(SETEXT_UNDERLINE_REGEX.source, 'm');
72
+
73
+ /**
74
+ * Convert setext headings, underlined with `===` or `---`, to ATX headings, like `# Heading`,
75
+ * because Lexical only supports the latter. Without it, the underline is imported as text or a
76
+ * horizontal rule. Fenced code blocks are left untouched. To avoid misreading a horizontal rule,
77
+ * only a paragraph that follows a blank line, an ATX heading or the start of the document is
78
+ * converted; for example, the second `---` of a block like front matter, which starts with a
79
+ * horizontal rule, is left as is.
80
+ * @param {string} value Markdown string to process.
81
+ * @returns {string} Processed Markdown string.
82
+ */
83
+ export const convertSetextHeadings = (value) => {
84
+ if (!SETEXT_UNDERLINE_MULTILINE_REGEX.test(value)) {
85
+ return value;
86
+ }
87
+
88
+ return mapOutsideCodeBlocks(value, (part) => {
89
+ /** @type {string[]} */
90
+ const lines = [];
91
+ // Index of the first of the paragraph lines at the end of `lines`, tracked as the lines are
92
+ // added rather than found by walking back, which would take quadratic time on a long paragraph
93
+ let start = 0;
94
+
95
+ part.split('\n').forEach((line) => {
96
+ const previous = lines[start - 1];
97
+
98
+ if (
99
+ !SETEXT_UNDERLINE_REGEX.test(line) ||
100
+ start === lines.length ||
101
+ (previous !== undefined && previous.trim() && !ATX_HEADING_REGEX.test(previous))
102
+ ) {
103
+ lines.push(line);
104
+
105
+ if (!isParagraphLine(line)) {
106
+ start = lines.length;
107
+ }
108
+
109
+ return;
110
+ }
111
+
112
+ const text = lines
113
+ .splice(start)
114
+ .map((paragraphLine) => paragraphLine.trim())
115
+ .join(' ');
116
+
117
+ lines.push(`${line.trimStart().startsWith('=') ? '#' : '##'} ${text}`);
118
+ start = lines.length;
119
+ });
120
+
121
+ return lines.join('\n');
122
+ });
123
+ };
124
+
125
+ /**
126
+ * Add a trailing space to a blank blockquote line (`>`) so Lexical treats it as a blank line within
127
+ * the blockquote. Without the space, the line does not match Lexical's `/^>\s/` quote pattern and
128
+ * is imported as literal `>` text, which is then exported as `> >`, an empty nested blockquote.
129
+ * @param {string} value Markdown string to process.
130
+ * @returns {string} Processed Markdown string.
131
+ * @see https://github.com/sveltia/sveltia-cms/issues/995
132
+ */
133
+ export const padBlankBlockquoteLines = (value) => {
134
+ if (!/^>$/m.test(value)) {
135
+ return value;
136
+ }
137
+
138
+ return mapLinesOutsideCodeBlocks(value, (line) => (line === '>' ? '> ' : line));
139
+ };
140
+
141
+ /**
142
+ * Remove the trailing space Lexical outputs for a blank blockquote line (`> `) so the line
143
+ * round-trips as `>`, the canonical form.
144
+ * @param {string} value Markdown string to process.
145
+ * @returns {string} Processed Markdown string.
146
+ * @see https://github.com/sveltia/sveltia-cms/issues/995
147
+ */
148
+ export const trimBlankBlockquoteLines = (value) => {
149
+ if (!/^> $/m.test(value)) {
150
+ return value;
151
+ }
152
+
153
+ return mapLinesOutsideCodeBlocks(value, (line) => (line === '> ' ? '>' : line));
154
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Regular expression to match a setext heading underline, like `===` or `---`.
3
+ */
4
+ export const SETEXT_UNDERLINE_REGEX: RegExp;
5
+ /**
6
+ * Regular expression to match an ATX heading, like `## Heading`.
7
+ */
8
+ export const ATX_HEADING_REGEX: RegExp;
9
+ /**
10
+ * Regular expression to match a table row, which starts with a pipe.
11
+ */
12
+ export const TABLE_ROW_LINE_REGEX: RegExp;
13
+ export function isParagraphLine(line: string): boolean;
14
+ export function endsParagraph(line: string): boolean;
15
+ export function interruptsParagraph(line: string): boolean;
16
+ export function findHtmlBlockLines(lines: string[]): boolean[];
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Regular expressions and helpers to read the block structure of a Markdown string, like the
3
+ * headings, the HTML blocks and the lines that end a paragraph.
4
+ */
5
+
6
+ import {
7
+ ATX_HEADING_MARKER,
8
+ CODE_FENCE,
9
+ HTML_TAG_SOURCE,
10
+ LIST_MARKER,
11
+ MARKER_END,
12
+ SETEXT_UNDERLINE,
13
+ THEMATIC_BREAK,
14
+ } from './syntax.js';
15
+
16
+ /**
17
+ * Regular expression to match a setext heading underline, like `===` or `---`.
18
+ */
19
+ export const SETEXT_UNDERLINE_REGEX = new RegExp(`^ {0,3}${SETEXT_UNDERLINE}`);
20
+ /**
21
+ * Regular expression to match an ATX heading, like `## Heading`.
22
+ */
23
+ export const ATX_HEADING_REGEX = new RegExp(`^ {0,3}${ATX_HEADING_MARKER}${MARKER_END}`);
24
+
25
+ /**
26
+ * Regular expression to match a thematic break, like `***` or `- - -`.
27
+ */
28
+ const THEMATIC_BREAK_REGEX = new RegExp(`^ {0,3}${THEMATIC_BREAK}`);
29
+
30
+ /**
31
+ * Names of the HTML elements that start an HTML block that can interrupt a paragraph, as opposed to
32
+ * inline HTML, like `<span>`, which a paragraph can start with.
33
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
34
+ */
35
+ const HTML_BLOCK_TAG_NAMES = [
36
+ 'address',
37
+ 'article',
38
+ 'aside',
39
+ 'base',
40
+ 'basefont',
41
+ 'blockquote',
42
+ 'body',
43
+ 'caption',
44
+ 'center',
45
+ 'col',
46
+ 'colgroup',
47
+ 'dd',
48
+ 'details',
49
+ 'dialog',
50
+ 'dir',
51
+ 'div',
52
+ 'dl',
53
+ 'dt',
54
+ 'fieldset',
55
+ 'figcaption',
56
+ 'figure',
57
+ 'footer',
58
+ 'form',
59
+ 'frame',
60
+ 'frameset',
61
+ 'h[1-6]',
62
+ 'head',
63
+ 'header',
64
+ 'hr',
65
+ 'html',
66
+ 'iframe',
67
+ 'legend',
68
+ 'li',
69
+ 'link',
70
+ 'main',
71
+ 'menu',
72
+ 'menuitem',
73
+ 'nav',
74
+ 'noframes',
75
+ 'ol',
76
+ 'optgroup',
77
+ 'option',
78
+ 'p',
79
+ 'param',
80
+ 'search',
81
+ 'section',
82
+ 'summary',
83
+ 'table',
84
+ 'tbody',
85
+ 'td',
86
+ 'tfoot',
87
+ 'th',
88
+ 'thead',
89
+ 'title',
90
+ 'tr',
91
+ 'track',
92
+ 'ul',
93
+ ];
94
+
95
+ /**
96
+ * Start and end conditions of the HTML blocks that can interrupt a paragraph: a raw text element, a
97
+ * comment, a processing instruction, a declaration, a CDATA section or a block element. The start
98
+ * is what follows the `<` at the start of the line. The end of the block is the line that matches
99
+ * the end condition, or the line before a blank line if there’s none.
100
+ * @type {[string, RegExp | null][]}
101
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
102
+ */
103
+ const HTML_BLOCK_CONDITION_SOURCES = [
104
+ ['(?:script|pre|style|textarea)(?:[ \\t>]|$)', /<\/(?:pre|script|style|textarea)>/i],
105
+ ['!--', /-->/],
106
+ ['\\?', /\?>/],
107
+ ['![A-Za-z]', />/],
108
+ ['!\\[CDATA\\[', /\]\]>/],
109
+ [`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$)`, null],
110
+ ];
111
+
112
+ /**
113
+ * Regular expression to match the start of an HTML block that can interrupt a paragraph; see
114
+ * {@link HTML_BLOCK_CONDITION_SOURCES}.
115
+ */
116
+ const HTML_BLOCK_START_REGEX = new RegExp(
117
+ `<(?:${HTML_BLOCK_CONDITION_SOURCES.map(([start]) => start).join('|')})`,
118
+ 'i',
119
+ );
120
+
121
+ /**
122
+ * Start and end conditions of the HTML blocks that can interrupt a paragraph, with the start as a
123
+ * regular expression to match a line; see {@link HTML_BLOCK_CONDITION_SOURCES}.
124
+ * @type {[RegExp, RegExp | null][]}
125
+ */
126
+ const HTML_BLOCK_CONDITIONS = HTML_BLOCK_CONDITION_SOURCES.map(([start, end]) => [
127
+ new RegExp(`^ {0,3}<(?:${start})`, 'i'),
128
+ end,
129
+ ]);
130
+
131
+ /**
132
+ * Regular expression to match a line that starts an HTML block that can’t interrupt a paragraph.
133
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
134
+ */
135
+ const STANDALONE_HTML_TAG_REGEX = new RegExp(
136
+ `^ {0,3}(?!</?(?:pre|script|style|textarea)\\b)(?:${HTML_TAG_SOURCE})[ \\t]*$`,
137
+ 'i',
138
+ );
139
+
140
+ /**
141
+ * Regular expression to match a line that doesn’t continue a paragraph: an ATX heading, a
142
+ * blockquote, a list item, a code fence, a table row, an HTML block, an indented code block, a
143
+ * thematic break or a setext heading underline.
144
+ */
145
+ const NON_PARAGRAPH_LINE_REGEX = new RegExp(
146
+ [
147
+ ATX_HEADING_REGEX.source,
148
+ `^ {0,3}(?:>|${LIST_MARKER}${MARKER_END}|${CODE_FENCE}|\\|)`,
149
+ `^ {0,3}${HTML_BLOCK_START_REGEX.source}`,
150
+ /^(?: {4}|\t)/.source,
151
+ THEMATIC_BREAK_REGEX.source,
152
+ SETEXT_UNDERLINE_REGEX.source,
153
+ ].join('|'),
154
+ 'i',
155
+ );
156
+
157
+ /**
158
+ * Regular expression to match a table row, which starts with a pipe.
159
+ */
160
+ export const TABLE_ROW_LINE_REGEX = /^ {0,3}\|/;
161
+
162
+ /**
163
+ * Regular expression to match a line that is a block on its own, so the line after it isn’t in a
164
+ * paragraph: an ATX heading or a thematic break. A setext heading underline is one as well, but
165
+ * only after a paragraph line.
166
+ */
167
+ const SINGLE_LINE_BLOCK_REGEX = new RegExp(
168
+ [ATX_HEADING_REGEX.source, THEMATIC_BREAK_REGEX.source].join('|'),
169
+ );
170
+
171
+ /**
172
+ * Regular expression to match a line that ends the paragraph before it: a line that doesn’t
173
+ * continue a paragraph, other than an indented line, which does.
174
+ */
175
+ const PARAGRAPH_INTERRUPTION_REGEX = new RegExp(
176
+ NON_PARAGRAPH_LINE_REGEX.source.replace(`|${/^(?: {4}|\t)/.source}`, ''),
177
+ 'i',
178
+ );
179
+
180
+ /**
181
+ * Check if the given line is paragraph text: a line that isn’t blank and doesn’t start another
182
+ * block, or continue one like an indented code block.
183
+ * @param {string} line Line.
184
+ * @returns {boolean} Result.
185
+ */
186
+ export const isParagraphLine = (line) => !!line.trim() && !NON_PARAGRAPH_LINE_REGEX.test(line);
187
+
188
+ /**
189
+ * Check if a paragraph can’t continue after the given line: a blank line, or a block on its own,
190
+ * like a heading or a thematic break.
191
+ * @param {string} line Line.
192
+ * @returns {boolean} Result.
193
+ */
194
+ export const endsParagraph = (line) => !line.trim() || SINGLE_LINE_BLOCK_REGEX.test(line);
195
+
196
+ /**
197
+ * Check if the given line can’t continue the paragraph before it: a blank line, or a line that
198
+ * starts another block, like a list item, a heading or a table row. An indented line continues it.
199
+ * @param {string} line Line.
200
+ * @returns {boolean} Result.
201
+ */
202
+ export const interruptsParagraph = (line) =>
203
+ !line.trim() || PARAGRAPH_INTERRUPTION_REGEX.test(line);
204
+
205
+ /**
206
+ * Find the lines of a part of a Markdown string that are in an HTML block, which is raw HTML,
207
+ * without any Markdown syntax.
208
+ * @param {string[]} lines Lines of a part of a Markdown string outside fenced code blocks.
209
+ * @returns {boolean[]} Whether each line is in an HTML block.
210
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
211
+ */
212
+ export const findHtmlBlockLines = (lines) => {
213
+ /**
214
+ * End condition of the current HTML block: a regular expression, `null` for a blank line, or
215
+ * `undefined` if the line is not in an HTML block.
216
+ * @type {RegExp | null | undefined}
217
+ */
218
+ let end;
219
+ let isInParagraph = false;
220
+
221
+ return lines.map((line) => {
222
+ const isBlankLine = !line.trim();
223
+
224
+ if (end === null && isBlankLine) {
225
+ end = undefined;
226
+ }
227
+
228
+ if (end === undefined) {
229
+ const condition = HTML_BLOCK_CONDITIONS.find(([start]) => start.test(line));
230
+
231
+ if (condition) {
232
+ [, end] = condition;
233
+ } else if (!isInParagraph && STANDALONE_HTML_TAG_REGEX.test(line)) {
234
+ end = null;
235
+ } else {
236
+ // A heading or a thematic break doesn’t leave a paragraph open, so a standalone tag after
237
+ // it starts an HTML block. A line like `===` is a heading underline only after a
238
+ // paragraph line, and paragraph text otherwise
239
+ isInParagraph =
240
+ !endsParagraph(line) && !(isInParagraph && SETEXT_UNDERLINE_REGEX.test(line));
241
+
242
+ return false;
243
+ }
244
+ }
245
+
246
+ if (end?.test(line)) {
247
+ end = undefined;
248
+ }
249
+
250
+ isInParagraph = false;
251
+
252
+ return true;
253
+ });
254
+ };
@@ -0,0 +1,57 @@
1
+ export function findCodeBlocks(lines: string[]): CodeBlock[];
2
+ export function getCodeBlocks(lines: string[]): CodeBlock[];
3
+ export function withLineCache<T>(steps: () => T): T;
4
+ export function splitLines(value: string): string[];
5
+ export function parseOpeningFence(line: string): OpeningFence | undefined;
6
+ export function mapOutsideCodeBlocks(value: string, callback: (part: string) => string): string;
7
+ export function mapLinesOutsideCodeBlocks(value: string, callback: (line: string) => string): string;
8
+ export type CodeBlock = {
9
+ /**
10
+ * Index of the line with the opening fence.
11
+ */
12
+ start: number;
13
+ /**
14
+ * Index of the line with the closing fence, or of the last line of the code
15
+ * block if it is not closed, in which case it runs to the end of the document, or to the end of the
16
+ * list item it starts in.
17
+ */
18
+ end: number;
19
+ /**
20
+ * Opening fence, made of three or more backticks or tildes.
21
+ */
22
+ fence: string;
23
+ /**
24
+ * Whether the code block has a closing fence.
25
+ */
26
+ closed: boolean;
27
+ };
28
+ export type OpeningFence = {
29
+ /**
30
+ * Whitespace before the fence.
31
+ */
32
+ indent: string;
33
+ /**
34
+ * Fence, made of three or more backticks or tildes.
35
+ */
36
+ fence: string;
37
+ /**
38
+ * Rest of the line after the fence, the info string with the whitespace
39
+ * around it.
40
+ */
41
+ info: string;
42
+ /**
43
+ * Leading word of the info string, if any.
44
+ */
45
+ language: string | undefined;
46
+ /**
47
+ * Rest of the info string after the language, if any, trimmed. Empty
48
+ * without a language, as the info string is then part of the code.
49
+ */
50
+ meta: string;
51
+ /**
52
+ * Match of the fence, the info string up to the end of the
53
+ * language and the whitespace after it, which Lexical’s `CODE` transformer takes as `startMatch`:
54
+ * the indented fence is the first group, and the language the second.
55
+ */
56
+ match: RegExpMatchArray;
57
+ };