@sveltia/ui 0.87.2 → 0.87.4

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.
@@ -1,3 +1,10 @@
1
+ import { INFO_BACKTICK } from './transformers/code.js';
2
+ import {
3
+ HTML_BLOCK_PLACEHOLDER,
4
+ HTML_TAG_SOURCE,
5
+ INLINE_HTML_REGEX,
6
+ } from './transformers/raw-syntax.js';
7
+
1
8
  /**
2
9
  * @typedef {object} CodeBlock
3
10
  * @property {number} start Index of the line with the opening fence.
@@ -19,8 +26,9 @@ const findCodeBlocks = (lines) => {
19
26
  let openBlock;
20
27
 
21
28
  lines.forEach((line, index) => {
22
- // Like Lexical’s `CODE_START_REGEX`, allow an indented fence
23
- const fence = line.match(/^[ \t]*(`{3,}|~{3,})/)?.[1];
29
+ // Like Lexical’s `CODE_START_REGEX`, allow an indented fence. A backtick fence can’t be
30
+ // followed by a backtick on the same line; see `CODE`
31
+ const fence = line.match(/^[ \t]*(`{3,}(?=[^`]*$)|~{3,})/)?.[1];
24
32
 
25
33
  if (!openBlock) {
26
34
  if (fence) {
@@ -109,63 +117,6 @@ const mapLinesOutsideCodeBlocks = (value, callback) => {
109
117
  return lines.map((line, index) => (codeLines.has(index) ? line : callback(line))).join('\n');
110
118
  };
111
119
 
112
- /**
113
- * Split multiline formatting into separate lines to prevent Markdown parsing issues. Fenced code
114
- * blocks are left untouched, as the formatting syntax is not parsed there.
115
- * @param {string} value Markdown string to process.
116
- * @returns {string} Processed Markdown string.
117
- * @see https://github.com/sveltia/sveltia-cms/issues/548
118
- */
119
- export const splitMultilineFormatting = (value) =>
120
- mapOutsideCodeBlocks(value, (part) =>
121
- part
122
- // The surrounding whitespace is checked with lookarounds rather than matched with `\s+`,
123
- // which would take quadratic time on a long run of whitespace not followed by a formatting
124
- // marker
125
- .replace(/(?<=\s)_([^_\n]+?)\n([^_\n]+?)_(?=\s)/g, '_$1_\n_$2_')
126
- .replace(/(?<=\s)\*\*([^*\n]+?)\n([^*\n]+?)\*\*(?=\s)/g, '**$1**\n**$2**')
127
- .replace(/(?<=\s)~~([^~\n]+?)\n([^~\n]+?)~~(?=\s)/g, '~~$1~~\n~~$2~~')
128
- .replace(/(?<=\s)`([^`\n]+?)\n([^`\n]+?)`(?=\s)/g, '`$1`\n`$2`'),
129
- );
130
-
131
- /**
132
- * Convert the fenced code blocks using tildes, like `~~~`, to ones using backticks, because Lexical
133
- * only supports the latter. The new fence is made longer than any backtick fence in the code, so
134
- * the code block isn’t closed early.
135
- * @param {string} value Markdown string to process.
136
- * @returns {string} Processed Markdown string.
137
- */
138
- export const convertTildeFences = (value) => {
139
- if (!/^[ \t]*~{3,}/m.test(value)) {
140
- return value;
141
- }
142
-
143
- const lines = value.split('\n');
144
-
145
- findCodeBlocks(lines)
146
- .filter(({ fence }) => fence.startsWith('~'))
147
- .forEach(({ start, end, closed }) => {
148
- const codeLines = lines.slice(start + 1, closed ? end : end + 1);
149
-
150
- const longestFence = Math.max(
151
- 2,
152
- ...codeLines.map(
153
- (line) => /** @type {RegExpMatchArray} */ (line.match(/^[ \t]*(`*)/))[1].length,
154
- ),
155
- );
156
-
157
- const backticks = '`'.repeat(longestFence + 1);
158
-
159
- lines[start] = lines[start].replace(/^([ \t]*)~+/, `$1${backticks}`);
160
-
161
- if (closed) {
162
- lines[end] = lines[end].replace(/^([ \t]*)~+[ \t]*$/, `$1${backticks}`);
163
- }
164
- });
165
-
166
- return lines.join('\n');
167
- };
168
-
169
120
  /**
170
121
  * Regular expression to match a setext heading underline, like `===` or `---`.
171
122
  */
@@ -241,14 +192,52 @@ const HTML_BLOCK_TAG_NAMES = [
241
192
  ];
242
193
 
243
194
  /**
244
- * Regular expression to match the start of an HTML block that can interrupt a paragraph: a raw
245
- * text element, a comment, a processing instruction, a declaration, a CDATA section or a block
246
- * element.
195
+ * Start and end conditions of the HTML blocks that can interrupt a paragraph: a raw text element, a
196
+ * comment, a processing instruction, a declaration, a CDATA section or a block element. The start
197
+ * is what follows the `<` at the start of the line. The end of the block is the line that matches
198
+ * the end condition, or the line before a blank line if there’s none.
199
+ * @type {[string, RegExp | null][]}
247
200
  * @see https://spec.commonmark.org/0.31.2/#html-blocks
248
201
  */
202
+ const HTML_BLOCK_CONDITION_SOURCES = [
203
+ ['(?:script|pre|style|textarea)(?:[ \\t>]|$)', /<\/(?:pre|script|style|textarea)>/i],
204
+ ['!--', /-->/],
205
+ ['\\?', /\?>/],
206
+ ['![A-Za-z]', />/],
207
+ ['!\\[CDATA\\[', /\]\]>/],
208
+ [`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$)`, null],
209
+ ];
210
+
211
+ /**
212
+ * Regular expression to match the start of an HTML block that can interrupt a paragraph; see
213
+ * {@link HTML_BLOCK_CONDITION_SOURCES}.
214
+ */
249
215
  const HTML_BLOCK_START_REGEX = new RegExp(
250
- `<(?:(?:script|pre|style|textarea)(?:[ \\t>]|$)|!--|\\?|![A-Za-z]|!\\[CDATA\\[|` +
251
- `/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$))`,
216
+ `<(?:${HTML_BLOCK_CONDITION_SOURCES.map(([start]) => start).join('|')})`,
217
+ 'i',
218
+ );
219
+
220
+ /**
221
+ * Start and end conditions of the HTML blocks that can interrupt a paragraph, with the start as a
222
+ * regular expression to match a line; see {@link HTML_BLOCK_CONDITION_SOURCES}.
223
+ * @type {[RegExp, RegExp | null][]}
224
+ */
225
+ const HTML_BLOCK_CONDITIONS = HTML_BLOCK_CONDITION_SOURCES.map(([start, end]) => [
226
+ new RegExp(`^ {0,3}<(?:${start})`, 'i'),
227
+ end,
228
+ ]);
229
+
230
+ /**
231
+ * Global version of `INLINE_HTML_REGEX`.
232
+ */
233
+ const GLOBAL_INLINE_HTML_REGEX = new RegExp(INLINE_HTML_REGEX, 'g');
234
+
235
+ /**
236
+ * Regular expression to match a line that starts an HTML block that can’t interrupt a paragraph.
237
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
238
+ */
239
+ const STANDALONE_HTML_TAG_REGEX = new RegExp(
240
+ `^ {0,3}(?!</?(?:pre|script|style|textarea)\\b)(?:${HTML_TAG_SOURCE})[ \\t]*$`,
252
241
  'i',
253
242
  );
254
243
 
@@ -269,6 +258,529 @@ const NON_PARAGRAPH_LINE_REGEX = new RegExp(
269
258
  'i',
270
259
  );
271
260
 
261
+ /**
262
+ * Regular expression to match a line that ends the paragraph before it: a line that doesn’t
263
+ * continue a paragraph, other than an indented line, which does.
264
+ */
265
+ const PARAGRAPH_INTERRUPTION_REGEX = new RegExp(
266
+ NON_PARAGRAPH_LINE_REGEX.source.replace(`|${/^(?: {4}|\t)/.source}`, ''),
267
+ 'i',
268
+ );
269
+
270
+ /**
271
+ * Create a regular expression to match formatting that spans one or more line breaks, like
272
+ * `**foo\nbar**`. It captures the text between the markers, line breaks included. The text of each
273
+ * line is read as CommonMark does: a backslash escapes the next character, so an escaped marker is
274
+ * part of the text, and the text can end with an escaped backslash, like `_foo\\`, which is
275
+ * followed by a soft line break, not a hard one. A line can end with whitespace or the backslash of
276
+ * a hard line break, and start with whitespace, outside its text. The markers can’t be escaped, or
277
+ * next to whitespace within the formatting. The formatting must be at the start or the end of the
278
+ * value, or next to whitespace or punctuation other than its own marker, so a marker within a word,
279
+ * like `_` in `snake_case`, is left alone. As per the CommonMark flanking rules, `*` can open and
280
+ * close formatting within a word as well, like `**foo\nbar**baz`, unless the text starts or ends
281
+ * with punctuation there. The surrounding characters are checked with lookarounds rather than
282
+ * matched with `\s+`, which would take quadratic time on a long run of whitespace not followed by a
283
+ * marker. The text of a line can’t contain the marker, and it can’t start or end with whitespace,
284
+ * so the text and the line breaks can only be matched one way, which keeps the time linear.
285
+ * @param {string} char Marker character.
286
+ * @param {number} length Number of marker characters.
287
+ * @returns {RegExp} Regular expression.
288
+ * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
289
+ */
290
+ const createMultilineFormattingRegex = (char, length) => {
291
+ const marker = `[${char}]`.repeat(length);
292
+ // A character other than the marker and a line break, or an escape. The first and the last of the
293
+ // text can’t be whitespace
294
+ const unit = String.raw`(?:\\[^\s]|[^${char}\\\n])`;
295
+ const edgeUnit = String.raw`(?:\\[^\s]|[^${char}\s\\])`;
296
+ const text = `${edgeUnit}(?:${unit}*?${edgeUnit})?`;
297
+ const boundary = String.raw`[\s\p{P}\p{S}]`;
298
+ const isWithinWordAllowed = char === '*';
299
+
300
+ const opening = isWithinWordAllowed
301
+ ? `(?:(?<=^|${boundary})${marker}|${marker}(?!${boundary}))`
302
+ : `(?<=^|${boundary})${marker}`;
303
+
304
+ const closing = isWithinWordAllowed
305
+ ? `(?:(?<!${boundary})${marker}|${marker}(?=${boundary}|$))`
306
+ : `${marker}(?=${boundary}|$)`;
307
+
308
+ const lineBreak = String.raw`(?:\\|[ \t]*)\n[ \t]*`;
309
+
310
+ return new RegExp(
311
+ String.raw`(?<![${char}\\])${opening}(${text}(?:${lineBreak}${text})+)${closing}(?![${char}])`,
312
+ 'gu',
313
+ );
314
+ };
315
+
316
+ /**
317
+ * Regular expressions to match formatting that spans line breaks, with the marker of each.
318
+ * @type {[string, RegExp][]}
319
+ */
320
+ const MULTILINE_FORMATTING_REGEXES = [
321
+ ['***', createMultilineFormattingRegex('*', 3)],
322
+ ['___', createMultilineFormattingRegex('_', 3)],
323
+ ['**', createMultilineFormattingRegex('*', 2)],
324
+ ['__', createMultilineFormattingRegex('_', 2)],
325
+ ['~~', createMultilineFormattingRegex('~', 2)],
326
+ ['*', createMultilineFormattingRegex('*', 1)],
327
+ ['_', createMultilineFormattingRegex('_', 1)],
328
+ ];
329
+
330
+ /**
331
+ * @typedef {object} FormattedLine
332
+ * @property {string} indent Whitespace at the start of the line, outside the formatting.
333
+ * @property {string} text Text of the line within the formatting.
334
+ * @property {string} lineEnd Whitespace or the backslash of a hard line break at the end of the
335
+ * line, outside the formatting.
336
+ */
337
+
338
+ /**
339
+ * Split the text of formatting that spans line breaks into lines. The text of a line doesn’t start
340
+ * or end with whitespace, and the backslash of a hard line break is the last of an odd number of
341
+ * backslashes at the end of a line, as the others escape each other.
342
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
343
+ * @returns {FormattedLine[]} Lines.
344
+ */
345
+ const splitFormattedLines = (text) =>
346
+ text.split('\n').map((line) => {
347
+ const start = line.length - line.trimStart().length;
348
+ // Not a regular expression like `[ \t]*$`, which would take quadratic time on a long run of
349
+ // whitespace within the line
350
+ let end = line.length;
351
+ let backslashes = 0;
352
+
353
+ while (end > start && /[ \t]/.test(line[end - 1])) {
354
+ end -= 1;
355
+ }
356
+
357
+ while (line[end - backslashes - 1] === '\\') {
358
+ backslashes += 1;
359
+ }
360
+
361
+ if (backslashes % 2) {
362
+ end -= 1;
363
+ }
364
+
365
+ return { indent: line.slice(0, start), text: line.slice(start, end), lineEnd: line.slice(end) };
366
+ });
367
+
368
+ /**
369
+ * Check if the text of formatting that spans line breaks has other formatting that spans line
370
+ * breaks, like `_foo\nbar_` in `**_foo\nbar_**` or `**_foo\nbar_\nbaz**`, which has to be split
371
+ * first.
372
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
373
+ * @returns {boolean} Result.
374
+ */
375
+ const hasMultilineFormatting = (text) =>
376
+ MULTILINE_FORMATTING_REGEXES.some(([, regex]) => text.search(regex) !== -1);
377
+
378
+ /**
379
+ * Split multiline formatting into separate lines to prevent Markdown parsing issues. Fenced code
380
+ * blocks are left untouched, as the formatting syntax is not parsed there. Formatting is left as is
381
+ * if a line after the first starts a block, like a heading or a list item, which ends the paragraph
382
+ * and therefore the formatting.
383
+ * @param {string} value Markdown string to process.
384
+ * @returns {string} Processed Markdown string.
385
+ * @see https://github.com/sveltia/sveltia-cms/issues/548
386
+ */
387
+ export const splitMultilineFormatting = (value) =>
388
+ mapOutsideCodeBlocks(value, (part) => {
389
+ let result = part;
390
+ let previous;
391
+
392
+ // Split the inner formatting first, so `**_foo\nbar_**` becomes `**_foo_**\n**_bar_**`, not
393
+ // `**_foo**_\n_**bar_**`. The outer one is split on the next pass. A text can’t have its own
394
+ // marker, so formatting is nested a few levels deep at most
395
+ while (result !== previous) {
396
+ previous = result;
397
+ result = MULTILINE_FORMATTING_REGEXES.reduce(
398
+ (current, [marker, regex]) =>
399
+ current.replace(regex, (match, text) => {
400
+ if (hasMultilineFormatting(text)) {
401
+ return match;
402
+ }
403
+
404
+ const lines = splitFormattedLines(text);
405
+
406
+ // A line with only markers, like `**` in `_**\n**_` of `**_(a)_**\n**_(b)_**`, means
407
+ // the match runs from the closing marker of one line to the opening marker of the next,
408
+ // which isn’t formatting. Splitting it would add markers that match again, without end
409
+ if (
410
+ lines.some(({ text: lineText }) => /^[*_~]+$/.test(lineText)) ||
411
+ lines
412
+ .slice(1)
413
+ .some(({ indent, text: lineText }) =>
414
+ PARAGRAPH_INTERRUPTION_REGEX.test(`${indent}${lineText}`),
415
+ )
416
+ ) {
417
+ return match;
418
+ }
419
+
420
+ return lines
421
+ .map(
422
+ ({ indent, text: lineText, lineEnd }) =>
423
+ `${indent}${marker}${lineText}${marker}${lineEnd}`,
424
+ )
425
+ .join('\n');
426
+ }),
427
+ result,
428
+ );
429
+ }
430
+
431
+ return (
432
+ result
433
+ // Backslashes and spaces are literal in a code span
434
+ .replace(
435
+ /(?<=^|[\s\p{P}\p{S}])(?<![`\\])`([^`\n]+?)\n([^`\n]+?)`(?=[\s\p{P}\p{S}]|$)(?!`)/gu,
436
+ '`$1`\n`$2`',
437
+ )
438
+ );
439
+ });
440
+
441
+ /**
442
+ * Placeholder put after an escaped backslash at the end of a line; see `addImportPlaceholders()`.
443
+ */
444
+ const ESCAPED_BACKSLASH_PLACEHOLDER = '\uE002';
445
+ /**
446
+ * Placeholder put with a period between a backslash and a formatting marker; see
447
+ * `addImportPlaceholders()`.
448
+ */
449
+ const PUNCTUATION_PLACEHOLDER = '\uE003';
450
+ /**
451
+ * Placeholder put in place of a tab in a code span; see `addImportPlaceholders()`.
452
+ */
453
+ const CODE_TAB_PLACEHOLDER = '\uE005';
454
+ /**
455
+ * Placeholder put in place of a space in a code span made only of spaces; see
456
+ * `addImportPlaceholders()`.
457
+ */
458
+ const CODE_SPACE_PLACEHOLDER = '\uE006';
459
+
460
+ /**
461
+ * @typedef {object} CodeSpan
462
+ * @property {number} start Index of the opening backticks.
463
+ * @property {number} end Index after the closing backticks.
464
+ * @property {number} fenceLength Number of backticks on either side.
465
+ */
466
+
467
+ /**
468
+ * Find the code spans in a line like Lexical does: a run of backticks that isn’t escaped opens a
469
+ * code span, which is closed by the next run of as many backticks.
470
+ * @param {string} line Line.
471
+ * @returns {CodeSpan[]} Code spans.
472
+ * @see https://spec.commonmark.org/0.31.2/#code-spans
473
+ */
474
+ export const findCodeSpans = (line) => {
475
+ const runs = [...line.matchAll(/`+/g)].map(({ 0: { length }, index }) => {
476
+ let backslashes = 0;
477
+
478
+ while (line[index - backslashes - 1] === '\\') {
479
+ backslashes += 1;
480
+ }
481
+
482
+ return { index, length, escaped: backslashes % 2 === 1 };
483
+ });
484
+
485
+ /** @type {CodeSpan[]} */
486
+ const spans = [];
487
+
488
+ for (let open = 0; open < runs.length; open += 1) {
489
+ let close = open + 1;
490
+
491
+ while (close < runs.length && runs[close].length !== runs[open].length) {
492
+ close += 1;
493
+ }
494
+
495
+ if (!runs[open].escaped && close < runs.length) {
496
+ const { index, length } = runs[close];
497
+
498
+ spans.push({ start: runs[open].index, end: index + length, fenceLength: length });
499
+ open = close;
500
+ }
501
+ }
502
+
503
+ return spans;
504
+ };
505
+
506
+ /**
507
+ * Add placeholders around the backslashes at the end of a line, so Lexical reads them as CommonMark
508
+ * does. Lexical looks for a hard line break, a backslash at the end of a line, after it has removed
509
+ * the escapes from the text, so an escaped backslash, like `foo\\`, `` `foo\` `` in a code span or
510
+ * `_foo\\_`, becomes a hard line break, losing the backslash, and `foo\\\` loses its hard line
511
+ * break. A placeholder after the escaped backslashes, or before the closing backticks of a code
512
+ * span, keeps the backslash from ending the text.
513
+ * @param {string} line Line.
514
+ * @returns {string} Processed line.
515
+ * @see https://spec.commonmark.org/0.31.2/#hard-line-breaks
516
+ */
517
+ const markLineEndBackslashes = (line) => {
518
+ if (!/[\\`][*_~]*$/.test(line)) {
519
+ return line;
520
+ }
521
+
522
+ const codeSpan = findCodeSpans(line).findLast(({ end }) => /^[*_~]*$/.test(line.slice(end)));
523
+
524
+ // Backslashes are literal in a code span
525
+ if (codeSpan) {
526
+ const index = codeSpan.end - codeSpan.fenceLength;
527
+
528
+ return line[index - 1] === '\\'
529
+ ? `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`
530
+ : line;
531
+ }
532
+
533
+ // The escaped backslashes are followed by formatting markers, if any, and the backslash of a hard
534
+ // line break, if any. They’re counted from the end rather than matched with a regular expression,
535
+ // which would take quadratic time on a long run of backslashes or markers
536
+ const index = [true, false]
537
+ .map((hasHardLineBreak) => {
538
+ let end = line.length - (hasHardLineBreak && line.endsWith('\\') ? 1 : 0);
539
+ let backslashes = 0;
540
+
541
+ while (end > 0 && '*_~'.includes(line[end - 1])) {
542
+ end -= 1;
543
+ }
544
+
545
+ while (line[end - backslashes - 1] === '\\') {
546
+ backslashes += 1;
547
+ }
548
+
549
+ return backslashes && backslashes % 2 === 0 ? end : -1;
550
+ })
551
+ .find((end) => end !== -1);
552
+
553
+ return index === undefined
554
+ ? line
555
+ : `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`;
556
+ };
557
+
558
+ /**
559
+ * Regular expression to match the place between a formatting marker run that isn’t escaped and a
560
+ * backslash: after the run, like in `_foo_\`, or before it, after an escaped backslash, like in
561
+ * `\\_foo_`.
562
+ */
563
+ const MARKER_BACKSLASH_REGEX =
564
+ // Each lookahead comes first, so its lookbehind is only tried next to a marker or a backslash
565
+ /(?=\\)(?<=(?:^|[^\\])(?:\\\\)*[*_~])|(?=[*_~])(?<=(?:^|[^\\])(?:\\\\)+)/g;
566
+
567
+ /**
568
+ * Put a period, which Lexical takes as punctuation, between a backslash and a formatting marker.
569
+ * Unlike CommonMark, Lexical doesn’t take a backslash as punctuation, so it reads the marker as
570
+ * being within a word: it doesn’t read `_foo_\` or `**foo.**\` as formatted, as the marker can’t
571
+ * close the formatting there, nor `\\_foo_`, as it can’t open it. The period gives the marker the
572
+ * same neighbor as in CommonMark. A placeholder on the backslash side lets it be removed, and keeps
573
+ * it from being escaped by the backslash.
574
+ * @param {string} line Line.
575
+ * @returns {string} Processed line.
576
+ * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
577
+ */
578
+ const markMarkersNextToBackslashes = (line) =>
579
+ /[*_~]\\|\\[*_~]/.test(line)
580
+ ? line.replace(MARKER_BACKSLASH_REGEX, (_match, index) =>
581
+ // The period goes next to the marker
582
+ '*_~'.includes(line[index]) ? `${PUNCTUATION_PLACEHOLDER}.` : `.${PUNCTUATION_PLACEHOLDER}`,
583
+ )
584
+ : line;
585
+
586
+ /**
587
+ * Regular expression to match the place between a formatting marker and a punctuation character or
588
+ * a symbol other than ASCII, like an en dash or a curly quote.
589
+ */
590
+ const MARKER_UNICODE_PUNCTUATION_REGEX =
591
+ /(?<=[*_~])(?=(?![\0-\x7F])[\p{P}\p{S}])|(?<=(?![\0-\x7F])[\p{P}\p{S}])(?=[*_~])/gu;
592
+
593
+ /**
594
+ * Put a period, which Lexical takes as punctuation, between a formatting marker and a punctuation
595
+ * character or a symbol other than ASCII. Unlike CommonMark, Lexical only takes ASCII characters as
596
+ * punctuation, so it reads the marker as being next to a letter: it doesn’t read `**[a](u)**` in
597
+ * `–**[a](u)**` or `“**[a](u)**”` as bold, as the marker can’t open or close the formatting there.
598
+ * The period gives the marker the same neighbor as in CommonMark. A placeholder on the other side
599
+ * lets it be removed.
600
+ * @param {string} line Line.
601
+ * @returns {string} Processed line.
602
+ * @see https://spec.commonmark.org/0.31.2/#unicode-punctuation-character
603
+ * @see https://github.com/sveltia/sveltia-cms/issues/1107
604
+ */
605
+ const markMarkersNextToUnicodePunctuation = (line) =>
606
+ /[^\0-\x7F]/.test(line)
607
+ ? line.replace(MARKER_UNICODE_PUNCTUATION_REGEX, (_match, index) =>
608
+ // The period goes next to the marker
609
+ '*_~'.includes(line[index - 1])
610
+ ? `.${PUNCTUATION_PLACEHOLDER}`
611
+ : `${PUNCTUATION_PLACEHOLDER}.`,
612
+ )
613
+ : line;
614
+
615
+ /**
616
+ * Replace the tabs in the code spans of a line, and the spaces in a code span made only of spaces,
617
+ * with placeholders. Lexical turns a tab into a tab node, which has no format, so a tab in a code
618
+ * span would leave the code span, and a code span with only tabs would be exported as text. Also,
619
+ * Lexical takes a paragraph with up to 3 whitespace characters as empty, and removes it, even if
620
+ * they’re code, like `` ` ` ``. The spaces of other code spans are left alone, as one of them is
621
+ * stripped from both ends of the code span if the code isn’t all spaces.
622
+ * @param {string} line Line.
623
+ * @returns {string} Processed line.
624
+ * @see https://spec.commonmark.org/0.31.2/#code-spans
625
+ */
626
+ const markCodeSpanWhitespace = (line) => {
627
+ if (!/`[ \t]|\t/.test(line)) {
628
+ return line;
629
+ }
630
+
631
+ let result = '';
632
+ let index = 0;
633
+
634
+ findCodeSpans(line).forEach(({ start, end, fenceLength }) => {
635
+ const content = line.slice(start + fenceLength, end - fenceLength);
636
+
637
+ result += `${line.slice(index, start + fenceLength)}${
638
+ /^ +$/.test(content)
639
+ ? CODE_SPACE_PLACEHOLDER.repeat(content.length)
640
+ : content.replaceAll('\t', CODE_TAB_PLACEHOLDER)
641
+ }`;
642
+ index = end - fenceLength;
643
+ });
644
+
645
+ return result + line.slice(index);
646
+ };
647
+
648
+ /**
649
+ * Mark the lines of the HTML blocks in a part of a Markdown string with a placeholder at the start,
650
+ * so they’re imported as raw HTML, without the Markdown syntax being read; see `RAW_HTML`. Their
651
+ * tabs, and those in the inline HTML of the other lines, are replaced with a placeholder, as
652
+ * Lexical would otherwise split the raw HTML into separate nodes around a tab node.
653
+ * @param {string} part Part of a Markdown string outside fenced code blocks.
654
+ * @returns {string} Processed part.
655
+ * @see https://spec.commonmark.org/0.31.2/#html-blocks
656
+ * @see https://github.com/sveltia/sveltia-cms/issues/1106
657
+ */
658
+ const markHtml = (part) => {
659
+ /**
660
+ * End condition of the current HTML block: a regular expression, `null` for a blank line, or
661
+ * `undefined` if the line is not in an HTML block.
662
+ * @type {RegExp | null | undefined}
663
+ */
664
+ let end;
665
+ let isInParagraph = false;
666
+
667
+ return part
668
+ .split('\n')
669
+ .map((line) => {
670
+ const isBlankLine = !line.trim();
671
+
672
+ if (end === null && isBlankLine) {
673
+ end = undefined;
674
+ }
675
+
676
+ if (end === undefined) {
677
+ const condition = HTML_BLOCK_CONDITIONS.find(([start]) => start.test(line));
678
+
679
+ if (condition) {
680
+ [, end] = condition;
681
+ } else if (!isInParagraph && STANDALONE_HTML_TAG_REGEX.test(line)) {
682
+ end = null;
683
+ } else {
684
+ isInParagraph = !isBlankLine;
685
+
686
+ return line.includes('\t')
687
+ ? line.replace(GLOBAL_INLINE_HTML_REGEX, (tag) =>
688
+ tag.replaceAll('\t', CODE_TAB_PLACEHOLDER),
689
+ )
690
+ : line;
691
+ }
692
+ }
693
+
694
+ if (end?.test(line)) {
695
+ end = undefined;
696
+ }
697
+
698
+ isInParagraph = false;
699
+
700
+ return `${HTML_BLOCK_PLACEHOLDER}${line.replaceAll('\t', CODE_TAB_PLACEHOLDER)}`;
701
+ })
702
+ .join('\n');
703
+ };
704
+
705
+ /**
706
+ * Add placeholders to a Markdown string, so Lexical reads it as CommonMark does; see
707
+ * `markLineEndBackslashes()`, `markMarkersNextToBackslashes()`,
708
+ * `markMarkersNextToUnicodePunctuation()` and `markCodeSpanWhitespace()`.
709
+ * Lines in fenced code blocks are left untouched. The placeholders are removed from the imported
710
+ * text with `removeImportPlaceholders()`.
711
+ * @param {string} value Markdown string to process.
712
+ * @returns {string} Processed Markdown string.
713
+ */
714
+ export const addImportPlaceholders = (value) =>
715
+ mapLinesOutsideCodeBlocks(
716
+ value.includes('<') ? mapOutsideCodeBlocks(value, markHtml) : value,
717
+ (line) =>
718
+ markCodeSpanWhitespace(
719
+ markMarkersNextToUnicodePunctuation(
720
+ markMarkersNextToBackslashes(markLineEndBackslashes(line)),
721
+ ),
722
+ ),
723
+ );
724
+
725
+ /**
726
+ * Remove the placeholders added by `addImportPlaceholders()` from imported text.
727
+ * @param {string} text Text.
728
+ * @returns {string} Text without the placeholders.
729
+ */
730
+ export const removeImportPlaceholders = (text) =>
731
+ text
732
+ .replace(new RegExp(`\\.${PUNCTUATION_PLACEHOLDER}|${PUNCTUATION_PLACEHOLDER}\\.`, 'g'), '')
733
+ .replaceAll(ESCAPED_BACKSLASH_PLACEHOLDER, '')
734
+ .replaceAll(HTML_BLOCK_PLACEHOLDER, '')
735
+ .replaceAll(CODE_TAB_PLACEHOLDER, '\t')
736
+ .replaceAll(CODE_SPACE_PLACEHOLDER, ' ');
737
+
738
+ /**
739
+ * Convert the fenced code blocks using tildes, like `~~~`, to ones using backticks, because Lexical
740
+ * only supports the latter. The new fence is made longer than any backtick fence in the code, so
741
+ * the code block isn’t closed early. Backticks can’t be used in the info string of a backtick
742
+ * fence, so those after the language are replaced with a placeholder, which the code block
743
+ * transformer turns back into backticks, exporting a tilde fence again; see `CODE`. Other backticks
744
+ * are removed.
745
+ * @param {string} value Markdown string to process.
746
+ * @returns {string} Processed Markdown string.
747
+ */
748
+ export const convertTildeFences = (value) => {
749
+ if (!/^[ \t]*~{3,}/m.test(value)) {
750
+ return value;
751
+ }
752
+
753
+ const lines = value.split('\n');
754
+
755
+ findCodeBlocks(lines)
756
+ .filter(({ fence }) => fence.startsWith('~'))
757
+ .forEach(({ start, end, closed }) => {
758
+ const codeLines = lines.slice(start + 1, closed ? end : end + 1);
759
+
760
+ const longestFence = Math.max(
761
+ 2,
762
+ ...codeLines.map(
763
+ (line) => /** @type {RegExpMatchArray} */ (line.match(/^[ \t]*(`*)/))[1].length,
764
+ ),
765
+ );
766
+
767
+ const backticks = '`'.repeat(longestFence + 1);
768
+
769
+ // Unlike that of a tilde fence, the info string of a backtick fence can’t contain a backtick
770
+ lines[start] = lines[start].replace(
771
+ /^([ \t]*)~+([ \t]*[\w-]+)?(.*)$/,
772
+ (_, indent, language, rest) =>
773
+ `${indent}${backticks}${language ?? ''}${rest.replaceAll('`', language ? INFO_BACKTICK : '')}`,
774
+ );
775
+
776
+ if (closed) {
777
+ lines[end] = lines[end].replace(/^([ \t]*)~+[ \t]*$/, `$1${backticks}`);
778
+ }
779
+ });
780
+
781
+ return lines.join('\n');
782
+ };
783
+
272
784
  /**
273
785
  * Check if the given line is part of a paragraph.
274
786
  * @param {string} line Line.
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
3
3
  */
4
- export const UI_VERSION: "0.87.2";
4
+ export const UI_VERSION: "0.87.4";
@@ -3,4 +3,4 @@
3
3
  /**
4
4
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
5
5
  */
6
- export const UI_VERSION = '0.87.2';
6
+ export const UI_VERSION = '0.87.4';
@@ -7,7 +7,9 @@ export function unescapeText(markdown: string): string;
7
7
  * exported as `a_b_c`, plain text. Last, it escapes the Markdown block syntax at the start of a
8
8
  * block or of a line after a line break. Without it, a paragraph typed as `## Hours` with the
9
9
  * Markdown shortcuts turned off, pasted as text, or imported from the escaped `\## Hours` would be
10
- * exported as is and become a heading the next time the Markdown is read.
10
+ * exported as is and become a heading the next time the Markdown is read. The text imported from
11
+ * the syntax the editor doesn’t support, like a list in a blockquote, is exported as is instead;
12
+ * see `RAW_SYNTAX`.
11
13
  * @type {TextMatchTransformer}
12
14
  */
13
15
  export const BLOCK_SYNTAX_ESCAPE: TextMatchTransformer;