@sveltia/ui 0.80.2 → 0.80.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.
- package/dist/components/text-editor/core.js +26 -14
- package/dist/components/text-editor/floating-link-editor.svelte +6 -1
- package/dist/components/text-editor/markdown.d.ts +21 -0
- package/dist/components/text-editor/markdown.js +305 -36
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/text-editor.svelte +8 -1
- package/dist/components/text-editor/transformers/block-escape.d.ts +8 -4
- package/dist/components/text-editor/transformers/block-escape.js +111 -14
- package/dist/components/text-editor/transformers/table.js +14 -3
- package/dist/components/text-field/number-input.d.ts +2 -0
- package/dist/components/text-field/number-input.js +56 -0
- package/dist/components/text-field/number-input.svelte +12 -9
- package/dist/shiki-engine.js +3 -3
- package/package.json +9 -9
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
} from '@lexical/list';
|
|
28
28
|
import {
|
|
29
29
|
CODE,
|
|
30
|
+
HEADING,
|
|
30
31
|
$convertFromMarkdownString as convertFromMarkdownString,
|
|
31
32
|
$convertToMarkdownString as convertToMarkdownString,
|
|
32
33
|
registerMarkdownShortcuts,
|
|
@@ -88,6 +89,8 @@ import {
|
|
|
88
89
|
HTML_IMPORT_MAP,
|
|
89
90
|
} from './html.js';
|
|
90
91
|
import {
|
|
92
|
+
convertSetextHeadings,
|
|
93
|
+
convertTildeFences,
|
|
91
94
|
increaseListIndentation,
|
|
92
95
|
padBlankBlockquoteLines,
|
|
93
96
|
splitMultilineFormatting,
|
|
@@ -477,21 +480,13 @@ export const exportMarkdown = (enabledTransformers) => {
|
|
|
477
480
|
...enabledTransformers.filter(
|
|
478
481
|
(/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
|
|
479
482
|
),
|
|
480
|
-
//
|
|
483
|
+
// Tidy up the escapes in text and escape the block syntax at the start of a line. Last, so a
|
|
484
|
+
// transformer of the editor’s own, such as a link, comes first
|
|
481
485
|
BLOCK_SYNTAX_ESCAPE,
|
|
482
486
|
];
|
|
483
487
|
|
|
484
488
|
return trimBlankBlockquoteLines(
|
|
485
489
|
convertToMarkdownString(transformers)
|
|
486
|
-
// Remove unnecessary backslash for underscore and backslash characters
|
|
487
|
-
// @see https://github.com/sveltia/sveltia-cms/issues/430
|
|
488
|
-
// @see https://github.com/sveltia/sveltia-cms/issues/512
|
|
489
|
-
.replace(/\\([_\\])/g, '$1')
|
|
490
|
-
// Replace encoded spaces with regular spaces. The HTML entity can appear with a
|
|
491
|
-
// combination of bold and italic text
|
|
492
|
-
// @see https://github.com/sveltia/sveltia-cms/issues/511
|
|
493
|
-
// @see https://github.com/sveltia/sveltia-cms/issues/534
|
|
494
|
-
.replace(/ /g, ' ')
|
|
495
490
|
// Remove the line breaks left by empty paragraphs at the end, which have no content. One
|
|
496
491
|
// may be added just by clicking below a block decorator; see `$moveCaretIntoBlock()`
|
|
497
492
|
.replace(/\n+$/, ''),
|
|
@@ -1105,9 +1100,25 @@ const importValue = async (editor, { languages, $import, $export, formatName })
|
|
|
1105
1100
|
* import has been started for the same editor in the meantime, in which case this value is skipped.
|
|
1106
1101
|
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
1107
1102
|
*/
|
|
1108
|
-
export const convertMarkdownToLexical = async (editor, value, enabledTransformers) =>
|
|
1109
|
-
|
|
1110
|
-
|
|
1103
|
+
export const convertMarkdownToLexical = async (editor, value, enabledTransformers) => {
|
|
1104
|
+
// Convert the syntax Lexical doesn’t support to the one it does, if the element is enabled: tilde
|
|
1105
|
+
// code fences to backtick ones, and setext headings to ATX ones. Otherwise the syntax is kept as
|
|
1106
|
+
// text, as written. A transformer is found by its regular expression, because the transformers
|
|
1107
|
+
// can come wrapped in a `$state` proxy, which leaves a regular expression as is but not the
|
|
1108
|
+
// transformer object itself
|
|
1109
|
+
const hasCodeBlocks = enabledTransformers.some(
|
|
1110
|
+
(transformer) => 'regExpStart' in transformer && transformer.regExpStart === CODE.regExpStart,
|
|
1111
|
+
);
|
|
1112
|
+
|
|
1113
|
+
const hasHeadings = enabledTransformers.some(
|
|
1114
|
+
(transformer) => 'regExp' in transformer && transformer.regExp === HEADING.regExp,
|
|
1115
|
+
);
|
|
1116
|
+
|
|
1117
|
+
const fencedValue = hasCodeBlocks ? convertTildeFences(value) : value;
|
|
1118
|
+
const normalizedValue = hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
|
|
1119
|
+
|
|
1120
|
+
return importValue(editor, {
|
|
1121
|
+
languages: [...normalizedValue.matchAll(/^`{3,}(?<lang>[^`\n]+)\n/gm)].map(
|
|
1111
1122
|
({ groups: { lang = 'plain' } = {} }) => lang,
|
|
1112
1123
|
),
|
|
1113
1124
|
/**
|
|
@@ -1118,7 +1129,7 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1118
1129
|
// Split multiline formatting into separate lines to prevent Markdown parsing issues, then
|
|
1119
1130
|
// increase list indentation levels to prevent Markdown parsing issues, and pad blank
|
|
1120
1131
|
// blockquote lines so they are not imported as literal `>` text
|
|
1121
|
-
padBlankBlockquoteLines(increaseListIndentation(splitMultilineFormatting(
|
|
1132
|
+
padBlankBlockquoteLines(increaseListIndentation(splitMultilineFormatting(normalizedValue))),
|
|
1122
1133
|
enabledTransformers,
|
|
1123
1134
|
);
|
|
1124
1135
|
},
|
|
@@ -1129,6 +1140,7 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1129
1140
|
$export: () => exportMarkdown(enabledTransformers),
|
|
1130
1141
|
formatName: 'Markdown',
|
|
1131
1142
|
});
|
|
1143
|
+
};
|
|
1132
1144
|
|
|
1133
1145
|
/**
|
|
1134
1146
|
* Convert HTML to Lexical nodes. Inline content outside any block, like bare text, is wrapped with
|
|
@@ -545,6 +545,11 @@
|
|
|
545
545
|
</script>
|
|
546
546
|
|
|
547
547
|
{#if open && position}
|
|
548
|
+
<!--
|
|
549
|
+
The `mousedown` listener isn’t an interaction with the group: it only keeps the focus where it
|
|
550
|
+
is when the popup’s background is pressed, as the controls in it take the interactions.
|
|
551
|
+
-->
|
|
552
|
+
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
|
548
553
|
<div
|
|
549
554
|
bind:this={popupElement}
|
|
550
555
|
role="group"
|
|
@@ -605,7 +610,7 @@
|
|
|
605
610
|
class="url"
|
|
606
611
|
href={isSafeLinkURL(currentLink.url) ? currentLink.url : undefined}
|
|
607
612
|
target="_blank"
|
|
608
|
-
rel="noopener noreferrer"
|
|
613
|
+
rel="external noopener noreferrer"
|
|
609
614
|
dir="ltr"
|
|
610
615
|
>
|
|
611
616
|
{currentLink.url}
|
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
export function splitMultilineFormatting(value: string): string;
|
|
2
|
+
export function convertTildeFences(value: string): string;
|
|
3
|
+
export function convertSetextHeadings(value: string): string;
|
|
2
4
|
export function increaseListIndentation(value: string): string;
|
|
3
5
|
export function padBlankBlockquoteLines(value: string): string;
|
|
4
6
|
export function trimBlankBlockquoteLines(value: string): string;
|
|
7
|
+
export type CodeBlock = {
|
|
8
|
+
/**
|
|
9
|
+
* Index of the line with the opening fence.
|
|
10
|
+
*/
|
|
11
|
+
start: number;
|
|
12
|
+
/**
|
|
13
|
+
* Index of the line with the closing fence, or of the last line if the code
|
|
14
|
+
* block is not closed, in which case it runs to the end of the document.
|
|
15
|
+
*/
|
|
16
|
+
end: number;
|
|
17
|
+
/**
|
|
18
|
+
* Opening fence, made of three or more backticks or tildes.
|
|
19
|
+
*/
|
|
20
|
+
fence: string;
|
|
21
|
+
/**
|
|
22
|
+
* Whether the code block has a closing fence.
|
|
23
|
+
*/
|
|
24
|
+
closed: boolean;
|
|
25
|
+
};
|
|
@@ -1,17 +1,94 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @typedef {object} CodeBlock
|
|
3
|
+
* @property {number} start Index of the line with the opening fence.
|
|
4
|
+
* @property {number} end Index of the line with the closing fence, or of the last line if the code
|
|
5
|
+
* block is not closed, in which case it runs to the end of the document.
|
|
6
|
+
* @property {string} fence Opening fence, made of three or more backticks or tildes.
|
|
7
|
+
* @property {boolean} closed Whether the code block has a closing fence.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Find the fenced code blocks in the lines of a Markdown string.
|
|
12
|
+
* @param {string[]} lines Lines of a Markdown string.
|
|
13
|
+
* @returns {CodeBlock[]} Code blocks.
|
|
14
|
+
*/
|
|
15
|
+
const findCodeBlocks = (lines) => {
|
|
16
|
+
/** @type {CodeBlock[]} */
|
|
17
|
+
const blocks = [];
|
|
18
|
+
/** @type {Omit<CodeBlock, 'end' | 'closed'> | undefined} */
|
|
19
|
+
let openBlock;
|
|
20
|
+
|
|
21
|
+
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
|
+
|
|
25
|
+
if (!openBlock) {
|
|
26
|
+
if (fence) {
|
|
27
|
+
openBlock = { start: index, fence };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Like Lexical’s `CODE_END_REGEX`, a code block is only closed by a bare fence of the same
|
|
34
|
+
// character that is at least as long as the opening one, so a shorter fence within the block,
|
|
35
|
+
// like ```` ``` ```` within ```` ```` ````, is part of the code
|
|
36
|
+
if (
|
|
37
|
+
fence?.[0] === openBlock.fence[0] &&
|
|
38
|
+
fence.length >= openBlock.fence.length &&
|
|
39
|
+
!line.trim().slice(fence.length)
|
|
40
|
+
) {
|
|
41
|
+
blocks.push({ ...openBlock, end: index, closed: true });
|
|
42
|
+
openBlock = undefined;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (openBlock) {
|
|
47
|
+
blocks.push({ ...openBlock, end: lines.length - 1, closed: false });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return blocks;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Apply a function to the parts of a Markdown string outside fenced code blocks, leaving the code
|
|
55
|
+
* blocks, fences included, untouched. Each part is given with the line breaks around it, if any, so
|
|
56
|
+
* a regular expression can tell the start and the end of a line from those of the part.
|
|
3
57
|
* @param {string} value Markdown string to process.
|
|
58
|
+
* @param {(part: string) => string} callback Function that returns a processed part. It must keep
|
|
59
|
+
* the line breaks at the start and the end of the part.
|
|
4
60
|
* @returns {string} Processed Markdown string.
|
|
5
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/548
|
|
6
61
|
*/
|
|
7
|
-
|
|
8
|
-
value
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
62
|
+
const mapOutsideCodeBlocks = (value, callback) => {
|
|
63
|
+
const lines = value.split('\n');
|
|
64
|
+
/** @type {string[]} */
|
|
65
|
+
const output = [];
|
|
66
|
+
let index = 0;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Process the lines from the current index to the given one, exclusive.
|
|
70
|
+
* @param {number} end Index of the line to stop at.
|
|
71
|
+
*/
|
|
72
|
+
const flush = (end) => {
|
|
73
|
+
if (end > index) {
|
|
74
|
+
const before = index > 0 ? '\n' : '';
|
|
75
|
+
const after = end < lines.length ? '\n' : '';
|
|
76
|
+
const part = callback(`${before}${lines.slice(index, end).join('\n')}${after}`);
|
|
77
|
+
|
|
78
|
+
output.push(part.slice(before.length, part.length - after.length));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
findCodeBlocks(lines).forEach(({ start, end }) => {
|
|
83
|
+
flush(start);
|
|
84
|
+
output.push(lines.slice(start, end + 1).join('\n'));
|
|
85
|
+
index = end + 1;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
flush(lines.length);
|
|
89
|
+
|
|
90
|
+
return output.join('\n');
|
|
91
|
+
};
|
|
15
92
|
|
|
16
93
|
/**
|
|
17
94
|
* Apply a function to each line of a Markdown string, leaving the lines inside fenced code blocks
|
|
@@ -21,41 +98,233 @@ export const splitMultilineFormatting = (value) =>
|
|
|
21
98
|
* @returns {string} Processed Markdown string.
|
|
22
99
|
*/
|
|
23
100
|
const mapLinesOutsideCodeBlocks = (value, callback) => {
|
|
24
|
-
|
|
25
|
-
* Opening fence of the code block being processed, like ```` ``` ````, or an empty string when
|
|
26
|
-
* outside a code block.
|
|
27
|
-
*/
|
|
28
|
-
let openingFence = '';
|
|
101
|
+
const lines = value.split('\n');
|
|
29
102
|
|
|
30
|
-
|
|
31
|
-
.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
103
|
+
const codeLines = new Set(
|
|
104
|
+
findCodeBlocks(lines).flatMap(({ start, end }) =>
|
|
105
|
+
Array.from({ length: end - start + 1 }, (_, offset) => start + offset),
|
|
106
|
+
),
|
|
107
|
+
);
|
|
35
108
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
openingFence = fence;
|
|
39
|
-
return line;
|
|
40
|
-
}
|
|
109
|
+
return lines.map((line, index) => (codeLines.has(index) ? line : callback(line))).join('\n');
|
|
110
|
+
};
|
|
41
111
|
|
|
42
|
-
|
|
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}`);
|
|
43
163
|
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return lines.join('\n');
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Regular expression to match a setext heading underline, like `===` or `---`.
|
|
171
|
+
*/
|
|
172
|
+
const SETEXT_UNDERLINE_REGEX = /^ {0,3}(=+|-+)[ \t]*$/;
|
|
173
|
+
/**
|
|
174
|
+
* Regular expression to match an ATX heading, like `## Heading`.
|
|
175
|
+
*/
|
|
176
|
+
const ATX_HEADING_REGEX = /^ {0,3}#{1,6}(?:[ \t]|$)/;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Names of the HTML elements that start an HTML block that can interrupt a paragraph, as opposed to
|
|
180
|
+
* inline HTML, like `<span>`, which a paragraph can start with.
|
|
181
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
182
|
+
*/
|
|
183
|
+
const HTML_BLOCK_TAG_NAMES = [
|
|
184
|
+
'address',
|
|
185
|
+
'article',
|
|
186
|
+
'aside',
|
|
187
|
+
'base',
|
|
188
|
+
'basefont',
|
|
189
|
+
'blockquote',
|
|
190
|
+
'body',
|
|
191
|
+
'caption',
|
|
192
|
+
'center',
|
|
193
|
+
'col',
|
|
194
|
+
'colgroup',
|
|
195
|
+
'dd',
|
|
196
|
+
'details',
|
|
197
|
+
'dialog',
|
|
198
|
+
'dir',
|
|
199
|
+
'div',
|
|
200
|
+
'dl',
|
|
201
|
+
'dt',
|
|
202
|
+
'fieldset',
|
|
203
|
+
'figcaption',
|
|
204
|
+
'figure',
|
|
205
|
+
'footer',
|
|
206
|
+
'form',
|
|
207
|
+
'frame',
|
|
208
|
+
'frameset',
|
|
209
|
+
'h[1-6]',
|
|
210
|
+
'head',
|
|
211
|
+
'header',
|
|
212
|
+
'hr',
|
|
213
|
+
'html',
|
|
214
|
+
'iframe',
|
|
215
|
+
'legend',
|
|
216
|
+
'li',
|
|
217
|
+
'link',
|
|
218
|
+
'main',
|
|
219
|
+
'menu',
|
|
220
|
+
'menuitem',
|
|
221
|
+
'nav',
|
|
222
|
+
'noframes',
|
|
223
|
+
'ol',
|
|
224
|
+
'optgroup',
|
|
225
|
+
'option',
|
|
226
|
+
'p',
|
|
227
|
+
'param',
|
|
228
|
+
'search',
|
|
229
|
+
'section',
|
|
230
|
+
'summary',
|
|
231
|
+
'table',
|
|
232
|
+
'tbody',
|
|
233
|
+
'td',
|
|
234
|
+
'tfoot',
|
|
235
|
+
'th',
|
|
236
|
+
'thead',
|
|
237
|
+
'title',
|
|
238
|
+
'tr',
|
|
239
|
+
'track',
|
|
240
|
+
'ul',
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
/**
|
|
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.
|
|
247
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
248
|
+
*/
|
|
249
|
+
const HTML_BLOCK_START_REGEX = new RegExp(
|
|
250
|
+
`<(?:(?:script|pre|style|textarea)(?:[ \\t>]|$)|!--|\\?|![A-Za-z]|!\\[CDATA\\[|` +
|
|
251
|
+
`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$))`,
|
|
252
|
+
'i',
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Regular expression to match a line that doesn’t continue a paragraph: an ATX heading, a
|
|
257
|
+
* blockquote, a list item, a code fence, a table row, an HTML block, an indented code block, a
|
|
258
|
+
* thematic break or a setext heading underline.
|
|
259
|
+
*/
|
|
260
|
+
const NON_PARAGRAPH_LINE_REGEX = new RegExp(
|
|
261
|
+
[
|
|
262
|
+
ATX_HEADING_REGEX.source,
|
|
263
|
+
/^ {0,3}(?:>|[-+*](?:[ \t]|$)|\d{1,9}[.)](?:[ \t]|$)|`{3,}|~{3,}|\|)/.source,
|
|
264
|
+
`^ {0,3}${HTML_BLOCK_START_REGEX.source}`,
|
|
265
|
+
/^(?: {4}|\t)/.source,
|
|
266
|
+
/^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/.source,
|
|
267
|
+
SETEXT_UNDERLINE_REGEX.source,
|
|
268
|
+
].join('|'),
|
|
269
|
+
'i',
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Check if the given line is part of a paragraph.
|
|
274
|
+
* @param {string} line Line.
|
|
275
|
+
* @returns {boolean} Result.
|
|
276
|
+
*/
|
|
277
|
+
const isParagraphLine = (line) => !!line.trim() && !NON_PARAGRAPH_LINE_REGEX.test(line);
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Convert setext headings, underlined with `===` or `---`, to ATX headings, like `# Heading`,
|
|
281
|
+
* because Lexical only supports the latter. Without it, the underline is imported as text or a
|
|
282
|
+
* horizontal rule. Fenced code blocks are left untouched. To avoid misreading a horizontal rule,
|
|
283
|
+
* only a paragraph that follows a blank line, an ATX heading or the start of the document is
|
|
284
|
+
* converted; for example, the second `---` of a block like front matter, which starts with a
|
|
285
|
+
* horizontal rule, is left as is.
|
|
286
|
+
* @param {string} value Markdown string to process.
|
|
287
|
+
* @returns {string} Processed Markdown string.
|
|
288
|
+
*/
|
|
289
|
+
export const convertSetextHeadings = (value) => {
|
|
290
|
+
if (!/^ {0,3}(?:=+|-+)[ \t]*$/m.test(value)) {
|
|
291
|
+
return value;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return mapOutsideCodeBlocks(value, (part) => {
|
|
295
|
+
/** @type {string[]} */
|
|
296
|
+
const lines = [];
|
|
297
|
+
|
|
298
|
+
part.split('\n').forEach((line) => {
|
|
299
|
+
const [, underline] = line.match(SETEXT_UNDERLINE_REGEX) ?? [];
|
|
300
|
+
let start = lines.length;
|
|
301
|
+
|
|
302
|
+
while (start > 0 && isParagraphLine(lines[start - 1])) {
|
|
303
|
+
start -= 1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const previous = lines[start - 1];
|
|
44
307
|
|
|
45
|
-
// Like Lexical's `CODE_END_REGEX`, a code block is only closed by a bare fence of the same
|
|
46
|
-
// character that is at least as long as the opening one, so a shorter fence within the block,
|
|
47
|
-
// like ```` ``` ```` within ```` ```` ````, is part of the code
|
|
48
308
|
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
309
|
+
!underline ||
|
|
310
|
+
start === lines.length ||
|
|
311
|
+
(previous !== undefined && previous.trim() && !ATX_HEADING_REGEX.test(previous))
|
|
52
312
|
) {
|
|
53
|
-
|
|
313
|
+
lines.push(line);
|
|
314
|
+
|
|
315
|
+
return;
|
|
54
316
|
}
|
|
55
317
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
318
|
+
const text = lines
|
|
319
|
+
.splice(start)
|
|
320
|
+
.map((paragraphLine) => paragraphLine.trim())
|
|
321
|
+
.join(' ');
|
|
322
|
+
|
|
323
|
+
lines.push(`${underline.startsWith('=') ? '#' : '##'} ${text}`);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
return lines.join('\n');
|
|
327
|
+
});
|
|
59
328
|
};
|
|
60
329
|
|
|
61
330
|
/**
|
|
@@ -144,6 +144,13 @@
|
|
|
144
144
|
let highlighterLoadCount = $state(0);
|
|
145
145
|
|
|
146
146
|
const usePlainText = $derived(!editorStore.useRichText && !hidden);
|
|
147
|
+
/**
|
|
148
|
+
* The `dir` attribute on the `<textarea>` of the plain text mode. The rich text mode keeps the
|
|
149
|
+
* hidden `<textarea>` in sync with the editor, setting its value on every change, and the browser
|
|
150
|
+
* resolves `dir="auto"` over the whole value each time it’s set, which makes typing in a long
|
|
151
|
+
* document slow. As nobody sees the `<textarea>` meanwhile, it only gets `auto` once it’s shown.
|
|
152
|
+
*/
|
|
153
|
+
const textAreaDir = $derived(dir === 'auto' && editorStore.useRichText ? undefined : dir);
|
|
147
154
|
/**
|
|
148
155
|
* Comma-separated languages used in the fenced code blocks of the Markdown source, which are
|
|
149
156
|
* highlighted as well. It’s a string rather than an array, so the highlighter isn’t reloaded on
|
|
@@ -316,7 +323,7 @@
|
|
|
316
323
|
{useEmojiAutocomplete}
|
|
317
324
|
highlight={highlightSource}
|
|
318
325
|
{flex}
|
|
319
|
-
{
|
|
326
|
+
dir={textAreaDir}
|
|
320
327
|
hidden={editorStore.useRichText || hidden}
|
|
321
328
|
{disabled}
|
|
322
329
|
{readonly}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
export function escapeBlockSyntax(text: string): string;
|
|
2
|
+
export function unescapeText(markdown: string): string;
|
|
2
3
|
/**
|
|
3
|
-
* Export-only transformer that
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Export-only transformer that tidies up the Markdown of a text node that isn’t inline code, which
|
|
5
|
+
* Lexical exports verbatim. It removes the unnecessary escapes Lexical adds; see `unescapeText()`.
|
|
6
|
+
* It uses `*` instead of `_` for italic text within a word, like `a*b*c`, which would otherwise be
|
|
7
|
+
* exported as `a_b_c`, plain text. Last, it escapes the Markdown block syntax at the start of a
|
|
8
|
+
* block or of a line after a line break. Without it, a paragraph typed as `## Hours` with the
|
|
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.
|
|
7
11
|
* @type {TextMatchTransformer}
|
|
8
12
|
*/
|
|
9
13
|
export const BLOCK_SYNTAX_ESCAPE: TextMatchTransformer;
|