@sveltia/ui 0.80.3 → 0.81.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.
- package/dist/components/text-editor/core.js +26 -14
- 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/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 +15 -0
- package/dist/components/text-field/number-input.js +86 -0
- package/dist/components/text-field/number-input.svelte +102 -52
- package/dist/components/text-field/number-input.svelte.d.ts +30 -2
- package/dist/locales/ar.yaml +6 -0
- package/dist/locales/bg.yaml +6 -0
- package/dist/locales/ca.yaml +6 -0
- package/dist/locales/cs.yaml +6 -0
- package/dist/locales/da.yaml +6 -0
- package/dist/locales/de.yaml +6 -0
- package/dist/locales/el.yaml +6 -0
- package/dist/locales/en-CA.yaml +6 -0
- package/dist/locales/en-GB.yaml +6 -0
- package/dist/locales/en-US.yaml +6 -0
- package/dist/locales/es-CO.yaml +6 -0
- package/dist/locales/fa.yaml +6 -0
- package/dist/locales/fi.yaml +6 -0
- package/dist/locales/fr.yaml +6 -0
- package/dist/locales/he.yaml +6 -0
- package/dist/locales/hr.yaml +6 -0
- package/dist/locales/it.yaml +6 -0
- package/dist/locales/ja.yaml +6 -0
- package/dist/locales/ko.yaml +6 -0
- package/dist/locales/nl.yaml +6 -0
- package/dist/locales/pl.yaml +6 -0
- package/dist/locales/pt-BR.yaml +6 -0
- package/dist/locales/pt-PT.yaml +6 -0
- package/dist/locales/ru.yaml +6 -0
- package/dist/locales/sk.yaml +6 -0
- package/dist/locales/sv.yaml +6 -0
- package/dist/locales/tr.yaml +6 -0
- package/dist/locales/uk.yaml +6 -0
- package/dist/locales/vi.yaml +6 -0
- package/dist/locales/zh-CN.yaml +6 -0
- package/dist/locales/zh-TW.yaml +6 -0
- package/package.json +1 -1
|
@@ -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
|
|
@@ -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
|
/**
|
|
@@ -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;
|
|
@@ -21,16 +21,103 @@ export const escapeBlockSyntax = (text) =>
|
|
|
21
21
|
.replace(/^( {0,3}\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2');
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
24
|
+
* Check if the given character is a word character, which is neither whitespace nor punctuation
|
|
25
|
+
* (or a symbol). An underscore next to one on both sides can’t open or close emphasis.
|
|
26
|
+
* @param {string | undefined} char Character.
|
|
27
|
+
* @returns {boolean} Result.
|
|
28
|
+
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
|
|
29
|
+
*/
|
|
30
|
+
const isWordChar = (char) => !!char && /[^\s\p{P}\p{S}]/u.test(char);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Remove the backslashes Lexical adds before underscores and backslashes in text, where they’re not
|
|
34
|
+
* needed, so `snake_case` and `C:\Users` aren’t exported as `snake\_case` and `C:\\Users`. Unlike
|
|
35
|
+
* a global replacement, the escapes that are needed are kept: an underscore that could open or
|
|
36
|
+
* close emphasis, like that in `\_not italic\_`, and a backslash that would otherwise escape the
|
|
37
|
+
* next character or make a hard line break. Also replace the encoded spaces Lexical outputs around
|
|
38
|
+
* formatted text, which can appear with a combination of bold and italic text.
|
|
39
|
+
* @param {string} markdown Markdown exported from a text node with its format, in which every
|
|
40
|
+
* backslash starts an escape.
|
|
41
|
+
* @returns {string} Tidied Markdown.
|
|
42
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/430
|
|
43
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/512
|
|
44
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/511
|
|
45
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/534
|
|
46
|
+
*/
|
|
47
|
+
export const unescapeText = (markdown) =>
|
|
48
|
+
markdown
|
|
49
|
+
.replace(/((?:\\_)+)|\\(.)/gs, (match, underscores, char, offset, string) => {
|
|
50
|
+
if (underscores) {
|
|
51
|
+
return isWordChar(string[offset - 1]) && isWordChar(string[offset + match.length])
|
|
52
|
+
? underscores.replaceAll('\\', '')
|
|
53
|
+
: match;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (char === '\\') {
|
|
57
|
+
const next = string[offset + 2];
|
|
58
|
+
|
|
59
|
+
// The backslash is literal unless it’s followed by ASCII punctuation, which it would
|
|
60
|
+
// escape, or a line break, which it would turn into a hard line break. The end of the node
|
|
61
|
+
// is kept escaped, as the next node may start with either
|
|
62
|
+
return next && !/[\n\r!-/:-@[-`{-~]/.test(next) ? char : match;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return match;
|
|
66
|
+
})
|
|
67
|
+
.replace(/ /g, ' ');
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if the given text node is italic and the italic text it’s part of is next to a word
|
|
71
|
+
* character, like `b` in `a*b*c`. The italic text then needs the `*` delimiter instead of `_`,
|
|
72
|
+
* which can’t be used within a word.
|
|
73
|
+
* @param {TextNode} node Text node.
|
|
74
|
+
* @returns {boolean} Result.
|
|
75
|
+
*/
|
|
76
|
+
const isItalicWithinWord = (node) => {
|
|
77
|
+
if (!node.hasFormat('italic')) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if the given node is italic text.
|
|
83
|
+
* @param {LexicalNode | null} sibling Node.
|
|
84
|
+
* @returns {sibling is TextNode} Result.
|
|
85
|
+
*/
|
|
86
|
+
const isItalicText = (sibling) => isTextNode(sibling) && sibling.hasFormat('italic');
|
|
87
|
+
let first = node;
|
|
88
|
+
let last = node;
|
|
89
|
+
|
|
90
|
+
while (isItalicText(first.getPreviousSibling())) {
|
|
91
|
+
first = /** @type {TextNode} */ (first.getPreviousSibling());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
while (isItalicText(last.getNextSibling())) {
|
|
95
|
+
last = /** @type {TextNode} */ (last.getNextSibling());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Lexical puts the delimiters inside the leading and trailing whitespace, if any
|
|
99
|
+
const firstText = first.getTextContent();
|
|
100
|
+
const lastText = last.getTextContent();
|
|
101
|
+
const before = /^\s/.test(firstText) ? ' ' : first.getPreviousSibling()?.getTextContent().at(-1);
|
|
102
|
+
const after = /\s$/.test(lastText) ? ' ' : last.getNextSibling()?.getTextContent()[0];
|
|
103
|
+
|
|
104
|
+
return isWordChar(before) || isWordChar(after);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Export-only transformer that tidies up the Markdown of a text node that isn’t inline code, which
|
|
109
|
+
* Lexical exports verbatim. It removes the unnecessary escapes Lexical adds; see `unescapeText()`.
|
|
110
|
+
* It uses `*` instead of `_` for italic text within a word, like `a*b*c`, which would otherwise be
|
|
111
|
+
* exported as `a_b_c`, plain text. Last, it escapes the Markdown block syntax at the start of a
|
|
112
|
+
* block or of a line after a line break. Without it, a paragraph typed as `## Hours` with the
|
|
113
|
+
* 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.
|
|
28
115
|
* @type {TextMatchTransformer}
|
|
29
116
|
*/
|
|
30
117
|
export const BLOCK_SYNTAX_ESCAPE = {
|
|
31
118
|
dependencies: [],
|
|
32
119
|
/**
|
|
33
|
-
* Export a text node
|
|
120
|
+
* Export a text node.
|
|
34
121
|
* @param {LexicalNode} node Node.
|
|
35
122
|
* @param {(node: ElementNode) => string} _exportChildren Function to export the children of an
|
|
36
123
|
* element, unused.
|
|
@@ -39,25 +126,35 @@ export const BLOCK_SYNTAX_ESCAPE = {
|
|
|
39
126
|
* @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
|
|
40
127
|
*/
|
|
41
128
|
export: (node, _exportChildren, exportFormat) => {
|
|
42
|
-
if (!isTextNode(node)) {
|
|
129
|
+
if (!isTextNode(node) || node.hasFormat('code')) {
|
|
43
130
|
return null;
|
|
44
131
|
}
|
|
45
132
|
|
|
133
|
+
let markdown = exportFormat(node, node.getTextContent());
|
|
134
|
+
|
|
135
|
+
if (isItalicWithinWord(node)) {
|
|
136
|
+
// Every underscore in the text is escaped, so an unescaped one is an italic delimiter
|
|
137
|
+
markdown = markdown.replace(/\\.|_/gs, (match) => (match === '_' ? '*' : match));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
markdown = unescapeText(markdown);
|
|
141
|
+
|
|
46
142
|
const parent = node.getParent();
|
|
47
143
|
const previous = node.getPreviousSibling();
|
|
48
144
|
|
|
49
145
|
// Text in a link or other inline element, in a code block, which is exported verbatim, or in a
|
|
50
|
-
// heading, whose content is only parsed for inline syntax
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
146
|
+
// heading, whose content is only parsed for inline syntax, or text after the start of a line
|
|
147
|
+
if (
|
|
148
|
+
!parent ||
|
|
149
|
+
parent.isInline() ||
|
|
150
|
+
['code', 'heading'].includes(parent.getType()) ||
|
|
151
|
+
(previous && !isLineBreakNode(previous))
|
|
152
|
+
) {
|
|
153
|
+
return markdown;
|
|
57
154
|
}
|
|
58
155
|
|
|
59
156
|
// A formatted node starts with its own syntax, such as `**`, which is left as is
|
|
60
|
-
return escapeBlockSyntax(
|
|
157
|
+
return escapeBlockSyntax(markdown);
|
|
61
158
|
},
|
|
62
159
|
// Never used to import Markdown or as a shortcut
|
|
63
160
|
regExp: /(?!)/,
|