@sveltia/ui 0.87.1 → 0.87.3

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,5 @@
1
+ import { INFO_BACKTICK } from './transformers/code.js';
2
+
1
3
  /**
2
4
  * @typedef {object} CodeBlock
3
5
  * @property {number} start Index of the line with the opening fence.
@@ -19,8 +21,9 @@ const findCodeBlocks = (lines) => {
19
21
  let openBlock;
20
22
 
21
23
  lines.forEach((line, index) => {
22
- // Like Lexical’s `CODE_START_REGEX`, allow an indented fence
23
- const fence = line.match(/^[ \t]*(`{3,}|~{3,})/)?.[1];
24
+ // Like Lexical’s `CODE_START_REGEX`, allow an indented fence. A backtick fence can’t be
25
+ // followed by a backtick on the same line; see `CODE`
26
+ const fence = line.match(/^[ \t]*(`{3,}(?=[^`]*$)|~{3,})/)?.[1];
24
27
 
25
28
  if (!openBlock) {
26
29
  if (fence) {
@@ -109,63 +112,6 @@ const mapLinesOutsideCodeBlocks = (value, callback) => {
109
112
  return lines.map((line, index) => (codeLines.has(index) ? line : callback(line))).join('\n');
110
113
  };
111
114
 
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
115
  /**
170
116
  * Regular expression to match a setext heading underline, like `===` or `---`.
171
117
  */
@@ -269,6 +215,431 @@ const NON_PARAGRAPH_LINE_REGEX = new RegExp(
269
215
  'i',
270
216
  );
271
217
 
218
+ /**
219
+ * Regular expression to match a line that ends the paragraph before it: a line that doesn’t
220
+ * continue a paragraph, other than an indented line, which does.
221
+ */
222
+ const PARAGRAPH_INTERRUPTION_REGEX = new RegExp(
223
+ NON_PARAGRAPH_LINE_REGEX.source.replace(`|${/^(?: {4}|\t)/.source}`, ''),
224
+ 'i',
225
+ );
226
+
227
+ /**
228
+ * Create a regular expression to match formatting that spans one or more line breaks, like
229
+ * `**foo\nbar**`. It captures the text between the markers, line breaks included. The text of each
230
+ * line is read as CommonMark does: a backslash escapes the next character, so an escaped marker is
231
+ * part of the text, and the text can end with an escaped backslash, like `_foo\\`, which is
232
+ * followed by a soft line break, not a hard one. A line can end with whitespace or the backslash of
233
+ * a hard line break, and start with whitespace, outside its text. The markers can’t be escaped, or
234
+ * next to whitespace within the formatting. The formatting must be at the start or the end of the
235
+ * value, or next to whitespace or punctuation other than its own marker, so a marker within a word,
236
+ * like `_` in `snake_case`, is left alone. As per the CommonMark flanking rules, `*` can open and
237
+ * close formatting within a word as well, like `**foo\nbar**baz`, unless the text starts or ends
238
+ * with punctuation there. The surrounding characters are checked with lookarounds rather than
239
+ * matched with `\s+`, which would take quadratic time on a long run of whitespace not followed by a
240
+ * marker. The text of a line can’t contain the marker, and it can’t start or end with whitespace,
241
+ * so the text and the line breaks can only be matched one way, which keeps the time linear.
242
+ * @param {string} char Marker character.
243
+ * @param {number} length Number of marker characters.
244
+ * @returns {RegExp} Regular expression.
245
+ * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
246
+ */
247
+ const createMultilineFormattingRegex = (char, length) => {
248
+ const marker = `[${char}]`.repeat(length);
249
+ // A character other than the marker and a line break, or an escape. The first and the last of the
250
+ // text can’t be whitespace
251
+ const unit = String.raw`(?:\\[^\s]|[^${char}\\\n])`;
252
+ const edgeUnit = String.raw`(?:\\[^\s]|[^${char}\s\\])`;
253
+ const text = `${edgeUnit}(?:${unit}*?${edgeUnit})?`;
254
+ const boundary = String.raw`[\s\p{P}\p{S}]`;
255
+ const isWithinWordAllowed = char === '*';
256
+
257
+ const opening = isWithinWordAllowed
258
+ ? `(?:(?<=^|${boundary})${marker}|${marker}(?!${boundary}))`
259
+ : `(?<=^|${boundary})${marker}`;
260
+
261
+ const closing = isWithinWordAllowed
262
+ ? `(?:(?<!${boundary})${marker}|${marker}(?=${boundary}|$))`
263
+ : `${marker}(?=${boundary}|$)`;
264
+
265
+ const lineBreak = String.raw`(?:\\|[ \t]*)\n[ \t]*`;
266
+
267
+ return new RegExp(
268
+ String.raw`(?<![${char}\\])${opening}(${text}(?:${lineBreak}${text})+)${closing}(?![${char}])`,
269
+ 'gu',
270
+ );
271
+ };
272
+
273
+ /**
274
+ * Regular expressions to match formatting that spans line breaks, with the marker of each.
275
+ * @type {[string, RegExp][]}
276
+ */
277
+ const MULTILINE_FORMATTING_REGEXES = [
278
+ ['***', createMultilineFormattingRegex('*', 3)],
279
+ ['___', createMultilineFormattingRegex('_', 3)],
280
+ ['**', createMultilineFormattingRegex('*', 2)],
281
+ ['__', createMultilineFormattingRegex('_', 2)],
282
+ ['~~', createMultilineFormattingRegex('~', 2)],
283
+ ['*', createMultilineFormattingRegex('*', 1)],
284
+ ['_', createMultilineFormattingRegex('_', 1)],
285
+ ];
286
+
287
+ /**
288
+ * @typedef {object} FormattedLine
289
+ * @property {string} indent Whitespace at the start of the line, outside the formatting.
290
+ * @property {string} text Text of the line within the formatting.
291
+ * @property {string} lineEnd Whitespace or the backslash of a hard line break at the end of the
292
+ * line, outside the formatting.
293
+ */
294
+
295
+ /**
296
+ * Split the text of formatting that spans line breaks into lines. The text of a line doesn’t start
297
+ * or end with whitespace, and the backslash of a hard line break is the last of an odd number of
298
+ * backslashes at the end of a line, as the others escape each other.
299
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
300
+ * @returns {FormattedLine[]} Lines.
301
+ */
302
+ const splitFormattedLines = (text) =>
303
+ text.split('\n').map((line) => {
304
+ const start = line.length - line.trimStart().length;
305
+ // Not a regular expression like `[ \t]*$`, which would take quadratic time on a long run of
306
+ // whitespace within the line
307
+ let end = line.length;
308
+ let backslashes = 0;
309
+
310
+ while (end > start && /[ \t]/.test(line[end - 1])) {
311
+ end -= 1;
312
+ }
313
+
314
+ while (line[end - backslashes - 1] === '\\') {
315
+ backslashes += 1;
316
+ }
317
+
318
+ if (backslashes % 2) {
319
+ end -= 1;
320
+ }
321
+
322
+ return { indent: line.slice(0, start), text: line.slice(start, end), lineEnd: line.slice(end) };
323
+ });
324
+
325
+ /**
326
+ * Check if the text of formatting that spans line breaks has other formatting that spans line
327
+ * breaks, like `_foo\nbar_` in `**_foo\nbar_**` or `**_foo\nbar_\nbaz**`, which has to be split
328
+ * first.
329
+ * @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
330
+ * @returns {boolean} Result.
331
+ */
332
+ const hasMultilineFormatting = (text) =>
333
+ MULTILINE_FORMATTING_REGEXES.some(([, regex]) => text.search(regex) !== -1);
334
+
335
+ /**
336
+ * Split multiline formatting into separate lines to prevent Markdown parsing issues. Fenced code
337
+ * blocks are left untouched, as the formatting syntax is not parsed there. Formatting is left as is
338
+ * if a line after the first starts a block, like a heading or a list item, which ends the paragraph
339
+ * and therefore the formatting.
340
+ * @param {string} value Markdown string to process.
341
+ * @returns {string} Processed Markdown string.
342
+ * @see https://github.com/sveltia/sveltia-cms/issues/548
343
+ */
344
+ export const splitMultilineFormatting = (value) =>
345
+ mapOutsideCodeBlocks(value, (part) => {
346
+ let result = part;
347
+ let previous;
348
+
349
+ // Split the inner formatting first, so `**_foo\nbar_**` becomes `**_foo_**\n**_bar_**`, not
350
+ // `**_foo**_\n_**bar_**`. The outer one is split on the next pass. A text can’t have its own
351
+ // marker, so formatting is nested a few levels deep at most
352
+ while (result !== previous) {
353
+ previous = result;
354
+ result = MULTILINE_FORMATTING_REGEXES.reduce(
355
+ (current, [marker, regex]) =>
356
+ current.replace(regex, (match, text) => {
357
+ if (hasMultilineFormatting(text)) {
358
+ return match;
359
+ }
360
+
361
+ const lines = splitFormattedLines(text);
362
+
363
+ if (
364
+ lines
365
+ .slice(1)
366
+ .some(({ indent, text: lineText }) =>
367
+ PARAGRAPH_INTERRUPTION_REGEX.test(`${indent}${lineText}`),
368
+ )
369
+ ) {
370
+ return match;
371
+ }
372
+
373
+ return lines
374
+ .map(
375
+ ({ indent, text: lineText, lineEnd }) =>
376
+ `${indent}${marker}${lineText}${marker}${lineEnd}`,
377
+ )
378
+ .join('\n');
379
+ }),
380
+ result,
381
+ );
382
+ }
383
+
384
+ return (
385
+ result
386
+ // Backslashes and spaces are literal in a code span
387
+ .replace(
388
+ /(?<=^|[\s\p{P}\p{S}])(?<![`\\])`([^`\n]+?)\n([^`\n]+?)`(?=[\s\p{P}\p{S}]|$)(?!`)/gu,
389
+ '`$1`\n`$2`',
390
+ )
391
+ );
392
+ });
393
+
394
+ /**
395
+ * Placeholder put after an escaped backslash at the end of a line; see `addImportPlaceholders()`.
396
+ */
397
+ const ESCAPED_BACKSLASH_PLACEHOLDER = '\uE002';
398
+ /**
399
+ * Placeholder put with a period between a backslash and a formatting marker; see
400
+ * `addImportPlaceholders()`.
401
+ */
402
+ const PUNCTUATION_PLACEHOLDER = '\uE003';
403
+ /**
404
+ * Placeholder put in place of a tab in a code span; see `addImportPlaceholders()`.
405
+ */
406
+ const CODE_TAB_PLACEHOLDER = '\uE005';
407
+ /**
408
+ * Placeholder put in place of a space in a code span made only of spaces; see
409
+ * `addImportPlaceholders()`.
410
+ */
411
+ const CODE_SPACE_PLACEHOLDER = '\uE006';
412
+
413
+ /**
414
+ * @typedef {object} CodeSpan
415
+ * @property {number} start Index of the opening backticks.
416
+ * @property {number} end Index after the closing backticks.
417
+ * @property {number} fenceLength Number of backticks on either side.
418
+ */
419
+
420
+ /**
421
+ * Find the code spans in a line like Lexical does: a run of backticks that isn’t escaped opens a
422
+ * code span, which is closed by the next run of as many backticks.
423
+ * @param {string} line Line.
424
+ * @returns {CodeSpan[]} Code spans.
425
+ * @see https://spec.commonmark.org/0.31.2/#code-spans
426
+ */
427
+ export const findCodeSpans = (line) => {
428
+ const runs = [...line.matchAll(/`+/g)].map(({ 0: { length }, index }) => {
429
+ let backslashes = 0;
430
+
431
+ while (line[index - backslashes - 1] === '\\') {
432
+ backslashes += 1;
433
+ }
434
+
435
+ return { index, length, escaped: backslashes % 2 === 1 };
436
+ });
437
+
438
+ /** @type {CodeSpan[]} */
439
+ const spans = [];
440
+
441
+ for (let open = 0; open < runs.length; open += 1) {
442
+ let close = open + 1;
443
+
444
+ while (close < runs.length && runs[close].length !== runs[open].length) {
445
+ close += 1;
446
+ }
447
+
448
+ if (!runs[open].escaped && close < runs.length) {
449
+ const { index, length } = runs[close];
450
+
451
+ spans.push({ start: runs[open].index, end: index + length, fenceLength: length });
452
+ open = close;
453
+ }
454
+ }
455
+
456
+ return spans;
457
+ };
458
+
459
+ /**
460
+ * Add placeholders around the backslashes at the end of a line, so Lexical reads them as CommonMark
461
+ * does. Lexical looks for a hard line break, a backslash at the end of a line, after it has removed
462
+ * the escapes from the text, so an escaped backslash, like `foo\\`, `` `foo\` `` in a code span or
463
+ * `_foo\\_`, becomes a hard line break, losing the backslash, and `foo\\\` loses its hard line
464
+ * break. A placeholder after the escaped backslashes, or before the closing backticks of a code
465
+ * span, keeps the backslash from ending the text.
466
+ * @param {string} line Line.
467
+ * @returns {string} Processed line.
468
+ * @see https://spec.commonmark.org/0.31.2/#hard-line-breaks
469
+ */
470
+ const markLineEndBackslashes = (line) => {
471
+ if (!/[\\`][*_~]*$/.test(line)) {
472
+ return line;
473
+ }
474
+
475
+ const codeSpan = findCodeSpans(line).findLast(({ end }) => /^[*_~]*$/.test(line.slice(end)));
476
+
477
+ // Backslashes are literal in a code span
478
+ if (codeSpan) {
479
+ const index = codeSpan.end - codeSpan.fenceLength;
480
+
481
+ return line[index - 1] === '\\'
482
+ ? `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`
483
+ : line;
484
+ }
485
+
486
+ // The escaped backslashes are followed by formatting markers, if any, and the backslash of a hard
487
+ // line break, if any. They’re counted from the end rather than matched with a regular expression,
488
+ // which would take quadratic time on a long run of backslashes or markers
489
+ const index = [true, false]
490
+ .map((hasHardLineBreak) => {
491
+ let end = line.length - (hasHardLineBreak && line.endsWith('\\') ? 1 : 0);
492
+ let backslashes = 0;
493
+
494
+ while (end > 0 && '*_~'.includes(line[end - 1])) {
495
+ end -= 1;
496
+ }
497
+
498
+ while (line[end - backslashes - 1] === '\\') {
499
+ backslashes += 1;
500
+ }
501
+
502
+ return backslashes && backslashes % 2 === 0 ? end : -1;
503
+ })
504
+ .find((end) => end !== -1);
505
+
506
+ return index === undefined
507
+ ? line
508
+ : `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`;
509
+ };
510
+
511
+ /**
512
+ * Regular expression to match the place between a formatting marker run that isn’t escaped and a
513
+ * backslash: after the run, like in `_foo_\`, or before it, after an escaped backslash, like in
514
+ * `\\_foo_`.
515
+ */
516
+ const MARKER_BACKSLASH_REGEX =
517
+ // Each lookahead comes first, so its lookbehind is only tried next to a marker or a backslash
518
+ /(?=\\)(?<=(?:^|[^\\])(?:\\\\)*[*_~])|(?=[*_~])(?<=(?:^|[^\\])(?:\\\\)+)/g;
519
+
520
+ /**
521
+ * Put a period, which Lexical takes as punctuation, between a backslash and a formatting marker.
522
+ * Unlike CommonMark, Lexical doesn’t take a backslash as punctuation, so it reads the marker as
523
+ * being within a word: it doesn’t read `_foo_\` or `**foo.**\` as formatted, as the marker can’t
524
+ * close the formatting there, nor `\\_foo_`, as it can’t open it. The period gives the marker the
525
+ * same neighbor as in CommonMark. A placeholder on the backslash side lets it be removed, and keeps
526
+ * it from being escaped by the backslash.
527
+ * @param {string} line Line.
528
+ * @returns {string} Processed line.
529
+ * @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
530
+ */
531
+ const markMarkersNextToBackslashes = (line) =>
532
+ /[*_~]\\|\\[*_~]/.test(line)
533
+ ? line.replace(MARKER_BACKSLASH_REGEX, (_match, index) =>
534
+ // The period goes next to the marker
535
+ '*_~'.includes(line[index]) ? `${PUNCTUATION_PLACEHOLDER}.` : `.${PUNCTUATION_PLACEHOLDER}`,
536
+ )
537
+ : line;
538
+
539
+ /**
540
+ * Replace the tabs in the code spans of a line, and the spaces in a code span made only of spaces,
541
+ * with placeholders. Lexical turns a tab into a tab node, which has no format, so a tab in a code
542
+ * span would leave the code span, and a code span with only tabs would be exported as text. Also,
543
+ * Lexical takes a paragraph with up to 3 whitespace characters as empty, and removes it, even if
544
+ * they’re code, like `` ` ` ``. The spaces of other code spans are left alone, as one of them is
545
+ * stripped from both ends of the code span if the code isn’t all spaces.
546
+ * @param {string} line Line.
547
+ * @returns {string} Processed line.
548
+ * @see https://spec.commonmark.org/0.31.2/#code-spans
549
+ */
550
+ const markCodeSpanWhitespace = (line) => {
551
+ if (!/`[ \t]|\t/.test(line)) {
552
+ return line;
553
+ }
554
+
555
+ let result = '';
556
+ let index = 0;
557
+
558
+ findCodeSpans(line).forEach(({ start, end, fenceLength }) => {
559
+ const content = line.slice(start + fenceLength, end - fenceLength);
560
+
561
+ result += `${line.slice(index, start + fenceLength)}${
562
+ /^ +$/.test(content)
563
+ ? CODE_SPACE_PLACEHOLDER.repeat(content.length)
564
+ : content.replaceAll('\t', CODE_TAB_PLACEHOLDER)
565
+ }`;
566
+ index = end - fenceLength;
567
+ });
568
+
569
+ return result + line.slice(index);
570
+ };
571
+
572
+ /**
573
+ * Add placeholders to a Markdown string, so Lexical reads it as CommonMark does; see
574
+ * `markLineEndBackslashes()`, `markMarkersNextToBackslashes()` and `markCodeSpanWhitespace()`.
575
+ * Lines in fenced code blocks are left untouched. The placeholders are removed from the imported
576
+ * text with `removeImportPlaceholders()`.
577
+ * @param {string} value Markdown string to process.
578
+ * @returns {string} Processed Markdown string.
579
+ */
580
+ export const addImportPlaceholders = (value) =>
581
+ mapLinesOutsideCodeBlocks(value, (line) =>
582
+ markCodeSpanWhitespace(markMarkersNextToBackslashes(markLineEndBackslashes(line))),
583
+ );
584
+
585
+ /**
586
+ * Remove the placeholders added by `addImportPlaceholders()` from imported text.
587
+ * @param {string} text Text.
588
+ * @returns {string} Text without the placeholders.
589
+ */
590
+ export const removeImportPlaceholders = (text) =>
591
+ text
592
+ .replace(new RegExp(`\\.${PUNCTUATION_PLACEHOLDER}|${PUNCTUATION_PLACEHOLDER}\\.`, 'g'), '')
593
+ .replaceAll(ESCAPED_BACKSLASH_PLACEHOLDER, '')
594
+ .replaceAll(CODE_TAB_PLACEHOLDER, '\t')
595
+ .replaceAll(CODE_SPACE_PLACEHOLDER, ' ');
596
+
597
+ /**
598
+ * Convert the fenced code blocks using tildes, like `~~~`, to ones using backticks, because Lexical
599
+ * only supports the latter. The new fence is made longer than any backtick fence in the code, so
600
+ * the code block isn’t closed early. Backticks can’t be used in the info string of a backtick
601
+ * fence, so those after the language are replaced with a placeholder, which the code block
602
+ * transformer turns back into backticks, exporting a tilde fence again; see `CODE`. Other backticks
603
+ * are removed.
604
+ * @param {string} value Markdown string to process.
605
+ * @returns {string} Processed Markdown string.
606
+ */
607
+ export const convertTildeFences = (value) => {
608
+ if (!/^[ \t]*~{3,}/m.test(value)) {
609
+ return value;
610
+ }
611
+
612
+ const lines = value.split('\n');
613
+
614
+ findCodeBlocks(lines)
615
+ .filter(({ fence }) => fence.startsWith('~'))
616
+ .forEach(({ start, end, closed }) => {
617
+ const codeLines = lines.slice(start + 1, closed ? end : end + 1);
618
+
619
+ const longestFence = Math.max(
620
+ 2,
621
+ ...codeLines.map(
622
+ (line) => /** @type {RegExpMatchArray} */ (line.match(/^[ \t]*(`*)/))[1].length,
623
+ ),
624
+ );
625
+
626
+ const backticks = '`'.repeat(longestFence + 1);
627
+
628
+ // Unlike that of a tilde fence, the info string of a backtick fence can’t contain a backtick
629
+ lines[start] = lines[start].replace(
630
+ /^([ \t]*)~+([ \t]*[\w-]+)?(.*)$/,
631
+ (_, indent, language, rest) =>
632
+ `${indent}${backticks}${language ?? ''}${rest.replaceAll('`', language ? INFO_BACKTICK : '')}`,
633
+ );
634
+
635
+ if (closed) {
636
+ lines[end] = lines[end].replace(/^([ \t]*)~+[ \t]*$/, `$1${backticks}`);
637
+ }
638
+ });
639
+
640
+ return lines.join('\n');
641
+ };
642
+
272
643
  /**
273
644
  * Check if the given line is part of a paragraph.
274
645
  * @param {string} line Line.
@@ -327,24 +698,6 @@ export const convertSetextHeadings = (value) => {
327
698
  });
328
699
  };
329
700
 
330
- /**
331
- * Increase list indentation levels to prevent Markdown parsing issues.
332
- * Slate uses 2 spaces per indentation level, whereas Lexical uses 4 spaces.
333
- * This function doubles the indentation to match Lexical's expectations.
334
- * @param {string} value Markdown string to process.
335
- * @returns {string} Processed Markdown string.
336
- * @see https://github.com/sveltia/sveltia-cms/issues/549
337
- */
338
- export const increaseListIndentation = (value) => {
339
- if (!value.match(/^\s{2}(?:-|\+|\*|\d+\.)\s/m)) {
340
- return value;
341
- }
342
-
343
- return mapLinesOutsideCodeBlocks(value, (line) =>
344
- line.replace(/^(\s+)(-|\+|\*|\d+\.)/, (_match, p1, p2) => `${' '.repeat(p1.length * 2)}${p2}`),
345
- );
346
- };
347
-
348
701
  /**
349
702
  * Add a trailing space to a blank blockquote line (`>`) so Lexical treats it as a blank line within
350
703
  * the blockquote. Without the space, the line does not match Lexical's `/^>\s/` quote pattern and
@@ -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.1";
4
+ export const UI_VERSION: "0.87.3";
@@ -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.1';
6
+ export const UI_VERSION = '0.87.3';
@@ -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;
@@ -1,5 +1,8 @@
1
1
  import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
2
2
 
3
+ import { getFormatExportNode } from './inline-code.js';
4
+ import { getRawSyntax } from './raw-syntax.js';
5
+
3
6
  /**
4
7
  * @import { TextMatchTransformer } from '@lexical/markdown';
5
8
  * @import { ElementNode, LexicalNode, TextNode } from 'lexical';
@@ -104,6 +107,11 @@ const isItalicWithinWord = (node) => {
104
107
  return isWordChar(before) || isWordChar(after);
105
108
  };
106
109
 
110
+ /**
111
+ * Placeholder for the text of a raw syntax node, which Lexical doesn’t escape.
112
+ */
113
+ const RAW_TEXT = '\uE000';
114
+
107
115
  /**
108
116
  * Export-only transformer that tidies up the Markdown of a text node that isn’t inline code, which
109
117
  * Lexical exports verbatim. It removes the unnecessary escapes Lexical adds; see `unescapeText()`.
@@ -111,7 +119,9 @@ const isItalicWithinWord = (node) => {
111
119
  * exported as `a_b_c`, plain text. Last, it escapes the Markdown block syntax at the start of a
112
120
  * block or of a line after a line break. Without it, a paragraph typed as `## Hours` with the
113
121
  * Markdown shortcuts turned off, pasted as text, or imported from the escaped `\## Hours` would be
114
- * exported as is and become a heading the next time the Markdown is read.
122
+ * exported as is and become a heading the next time the Markdown is read. The text imported from
123
+ * the syntax the editor doesn’t support, like a list in a blockquote, is exported as is instead;
124
+ * see `RAW_SYNTAX`.
115
125
  * @type {TextMatchTransformer}
116
126
  */
117
127
  export const BLOCK_SYNTAX_ESCAPE = {
@@ -130,13 +140,32 @@ export const BLOCK_SYNTAX_ESCAPE = {
130
140
  return null;
131
141
  }
132
142
 
133
- let markdown = exportFormat(node, node.getTextContent());
143
+ const text = node.getTextContent();
144
+
145
+ // Lexical exports formatted whitespace, like the bold space in `**_a_ [b](u)**`, as is, but it
146
+ // drops the closing syntax due after it, before a link for example, leaving `**_a_ **[b](u)**`.
147
+ // Export a placeholder instead to get the closing syntax, which goes before the whitespace
148
+ if (node.getFormat() && /^\s+$/.test(text)) {
149
+ return `${exportFormat(getFormatExportNode(node), RAW_TEXT).replace(RAW_TEXT, '')}${text}`;
150
+ }
151
+
152
+ const rawSyntax = getRawSyntax(node);
153
+
154
+ // Raw syntax is exported as is, but with the syntax of its format, if any
155
+ let markdown = exportFormat(
156
+ getFormatExportNode(node),
157
+ rawSyntax === undefined ? text : RAW_TEXT,
158
+ );
134
159
 
135
160
  if (isItalicWithinWord(node)) {
136
161
  // Every underscore in the text is escaped, so an unescaped one is an italic delimiter
137
162
  markdown = markdown.replace(/\\.|_/gs, (match) => (match === '_' ? '*' : match));
138
163
  }
139
164
 
165
+ if (rawSyntax !== undefined) {
166
+ return markdown.replace(RAW_TEXT, () => rawSyntax);
167
+ }
168
+
140
169
  markdown = unescapeText(markdown);
141
170
 
142
171
  const parent = node.getParent();
@@ -0,0 +1,27 @@
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
+ /**
13
+ * Code block transformer that, unlike Lexical’s, doesn’t take a line with a backtick after the
14
+ * opening backtick fence for a code block. In CommonMark, the info string of a backtick fence
15
+ * can’t contain a backtick, so such a line is a paragraph starting with inline code, like a code
16
+ * span of three backticks, which the editor exports with a fence of four backticks. Lexical would
17
+ * turn it into a code block the next time the Markdown is read. It also reads the info string as
18
+ * CommonMark does when Lexical doesn’t: the language after spaces, like ```` ``` js ````, and the
19
+ * 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.
23
+ * @type {MultilineElementTransformer}
24
+ * @see https://spec.commonmark.org/0.31.2/#fenced-code-blocks
25
+ */
26
+ export const CODE: MultilineElementTransformer;
27
+ import type { MultilineElementTransformer } from '@lexical/markdown';