@sveltia/ui 0.87.3 → 0.88.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,49 @@ import { $getState as getState, $setState as setState, createState } from 'lexic
5
5
  * @import { TextNode } from 'lexical';
6
6
  */
7
7
 
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 to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
29
+ * processing instruction, a declaration or a CDATA section, which isn’t escaped.
30
+ * @see https://spec.commonmark.org/0.31.2/#raw-html
31
+ */
32
+ export const INLINE_HTML_REGEX = new RegExp(
33
+ [
34
+ // Not escaped. The lookahead comes first, so the lookbehind is only tried before a `<`
35
+ String.raw`(?=<)(?<!(?:^|[^\\])(?:\\\\)*\\)(?:`,
36
+ [
37
+ HTML_TAG_SOURCE,
38
+ // Comment
39
+ String.raw`<!-->|<!--->|<!--.*?-->`,
40
+ // Processing instruction
41
+ String.raw`<\?.*?\?>`,
42
+ // Declaration
43
+ String.raw`<![A-Za-z][^>]*>`,
44
+ // CDATA section
45
+ String.raw`<!\[CDATA\[.*?\]\]>`,
46
+ ].join('|'),
47
+ ')',
48
+ ].join(''),
49
+ );
50
+
8
51
  /**
9
52
  * Block syntax at the start of a line, after up to 3 spaces.
10
53
  */
@@ -41,6 +84,13 @@ const rawSyntaxState = createState('suiRawSyntax', {
41
84
  parse: (value) => (typeof value === 'string' ? value : ''),
42
85
  });
43
86
 
87
+ /**
88
+ * Get the Markdown a text node was imported from, if it’s raw syntax.
89
+ * @param {TextNode} node Text node.
90
+ * @returns {string | undefined} Markdown, or `undefined` if the node is not raw syntax.
91
+ */
92
+ export const getImportedRawSyntax = (node) => getState(node, rawSyntaxState) || undefined;
93
+
44
94
  /**
45
95
  * Get the Markdown of a text node imported from raw syntax.
46
96
  * @param {TextNode} node Text node.
@@ -54,7 +104,7 @@ export const getRawSyntax = (node) => {
54
104
  return undefined;
55
105
  }
56
106
 
57
- // The block syntax has no escapes, so the text is the Markdown, even if it has been edited since
107
+ // Raw syntax has no escapes, so the text is the Markdown, even if it has been edited since
58
108
  return node.getTextContent();
59
109
  };
60
110
 
@@ -86,3 +136,37 @@ export const RAW_SYNTAX = {
86
136
  },
87
137
  type: 'text-match',
88
138
  };
139
+
140
+ /**
141
+ * Regular expression to match raw HTML: a line of an HTML block after the placeholder that marks
142
+ * it, or inline HTML, like a tag. The placeholder is left out of the match, so an editor component
143
+ * that matches the line after it, which comes first, still takes precedence; see
144
+ * `removePlaceholdersFromMatch()` in `core.js`.
145
+ */
146
+ const RAW_HTML_REGEX = new RegExp(`(?<=^${HTML_BLOCK_PLACEHOLDER}).+|${INLINE_HTML_REGEX.source}`);
147
+
148
+ /**
149
+ * Import-only transformer that marks raw HTML, so it’s exported as is. Neither the Markdown syntax
150
+ * nor the escapes are read in raw HTML, so an escape added to it on export, like `\_` in an
151
+ * attribute value or `\*` in the text of an HTML block, would show up as a backslash. Lexical
152
+ * removes the escapes from the text it has imported, so the text is restored with
153
+ * `getImportedRawSyntax()` once the import is done.
154
+ * @type {TextMatchTransformer}
155
+ * @see https://spec.commonmark.org/0.31.2/#raw-html
156
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
157
+ * @see https://github.com/sveltia/sveltia-cms/issues/1105
158
+ * @see https://github.com/sveltia/sveltia-cms/issues/1106
159
+ */
160
+ export const RAW_HTML = {
161
+ dependencies: [],
162
+ /**
163
+ * Leave the export to `BLOCK_SYNTAX_ESCAPE`.
164
+ * @returns {null} Nothing.
165
+ */
166
+ export: () => null,
167
+ importRegExp: RAW_HTML_REGEX,
168
+ // Never used as a shortcut
169
+ regExp: /(?!)/,
170
+ replace: RAW_SYNTAX.replace,
171
+ type: 'text-match',
172
+ };
@@ -27,14 +27,19 @@ import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from '
27
27
  import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
28
28
  import { LEADING_INLINE_CODE } from './inline-code.js';
29
29
  import { LINK } from './link.js';
30
- import { RAW_SYNTAX } from './raw-syntax.js';
30
+ import { RAW_HTML, RAW_SYNTAX } from './raw-syntax.js';
31
31
 
32
32
  /**
33
33
  * @import { ElementTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
34
34
  */
35
35
 
36
36
  const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
37
- const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/;
37
+ /**
38
+ * Regular expression to match a delimiter row, like `| --- | :-: |`. Each cell needs a hyphen, so a
39
+ * row of empty cells, like `| | |`, is an empty header row, not a delimiter row.
40
+ * @see https://github.github.com/gfm/#delimiter-row
41
+ */
42
+ const TABLE_ROW_DIVIDER_REG_EXP = /^(?:\|[ \t]*:?-+:?[ \t]*)+\|\s?$/;
38
43
 
39
44
  /**
40
45
  * Returns the number of columns in the table.
@@ -98,6 +103,7 @@ export const createTableTransformer = (cellTransformers = []) => {
98
103
  ...cellTransformers,
99
104
  ...TRANSFORMERS.map((transformer) => (transformer === LEXICAL_LINK ? LINK : transformer)),
100
105
  LEADING_INLINE_CODE,
106
+ RAW_HTML,
101
107
  RAW_SYNTAX,
102
108
  ];
103
109
 
@@ -94,9 +94,9 @@
94
94
  let cloneElement = $state();
95
95
 
96
96
  $effect(() => {
97
- // Keep the highlighted text scrolled along with the `<textarea>`. It never scrolls when it’s
98
- // auto-resized
99
- if (!element || !cloneElement || autoResize) {
97
+ // Keep the highlighted text scrolled along with the `<textarea>`, which can also scroll when
98
+ // it’s auto-resized, if its height is capped with `max-height`
99
+ if (!element || !cloneElement) {
100
100
  return undefined;
101
101
  }
102
102
 
@@ -232,12 +232,12 @@ textarea[aria-invalid=true] {
232
232
  .clone {
233
233
  overflow: hidden;
234
234
  visibility: hidden;
235
+ scroll-behavior: auto;
235
236
  }
236
237
  .clone:not(.auto-resize) {
237
238
  position: absolute;
238
239
  inset: 0;
239
240
  min-height: 0;
240
- scroll-behavior: auto;
241
241
  }
242
242
 
243
243
  .highlighted textarea {