@sveltia/ui 0.87.2 → 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.
- package/dist/components/text-editor/constants.js +3 -2
- package/dist/components/text-editor/core.js +54 -6
- package/dist/components/text-editor/markdown.d.ts +32 -0
- package/dist/components/text-editor/markdown.js +430 -59
- 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 +3 -1
- package/dist/components/text-editor/transformers/block-escape.js +31 -2
- package/dist/components/text-editor/transformers/code.d.ts +27 -0
- package/dist/components/text-editor/transformers/code.js +95 -0
- package/dist/components/text-editor/transformers/formatted-code.d.ts +38 -0
- package/dist/components/text-editor/transformers/formatted-code.js +394 -0
- package/dist/components/text-editor/transformers/inline-code.d.ts +8 -5
- package/dist/components/text-editor/transformers/inline-code.js +171 -6
- package/dist/components/text-editor/transformers/link.d.ts +7 -0
- package/dist/components/text-editor/transformers/link.js +168 -0
- package/dist/components/text-editor/transformers/raw-syntax.d.ts +12 -0
- package/dist/components/text-editor/transformers/raw-syntax.js +88 -0
- package/dist/components/text-editor/transformers/table.js +10 -1
- package/package.json +2 -2
|
@@ -6,12 +6,10 @@ import {
|
|
|
6
6
|
BOLD_ITALIC_UNDERSCORE,
|
|
7
7
|
BOLD_STAR,
|
|
8
8
|
BOLD_UNDERSCORE,
|
|
9
|
-
CODE,
|
|
10
9
|
HEADING,
|
|
11
10
|
INLINE_CODE,
|
|
12
11
|
ITALIC_STAR,
|
|
13
12
|
ITALIC_UNDERSCORE,
|
|
14
|
-
LINK,
|
|
15
13
|
ORDERED_LIST,
|
|
16
14
|
QUOTE,
|
|
17
15
|
STRIKETHROUGH,
|
|
@@ -20,7 +18,9 @@ import {
|
|
|
20
18
|
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
21
19
|
import { createCommand } from 'lexical';
|
|
22
20
|
|
|
21
|
+
import { CODE } from './transformers/code.js';
|
|
23
22
|
import { LEADING_INLINE_CODE } from './transformers/inline-code.js';
|
|
23
|
+
import { LINK } from './transformers/link.js';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* @import { EditorThemeClasses, LexicalCommand } from 'lexical';
|
|
@@ -239,6 +239,7 @@ export const TRANSFORMER_MAP = {
|
|
|
239
239
|
],
|
|
240
240
|
strikethrough: [STRIKETHROUGH],
|
|
241
241
|
// TEXT_MATCH_TRANSFORMERS
|
|
242
|
+
// Unlike Lexical’s, it keeps the formats of the whole link text outside the link
|
|
242
243
|
link: [LINK],
|
|
243
244
|
};
|
|
244
245
|
|
|
@@ -30,7 +30,6 @@ import {
|
|
|
30
30
|
ListNode,
|
|
31
31
|
} from '@lexical/list';
|
|
32
32
|
import {
|
|
33
|
-
CODE,
|
|
34
33
|
HEADING,
|
|
35
34
|
$convertFromMarkdownString as convertFromMarkdownString,
|
|
36
35
|
$convertToMarkdownString as convertToMarkdownString,
|
|
@@ -94,9 +93,11 @@ import {
|
|
|
94
93
|
HTML_IMPORT_MAP,
|
|
95
94
|
} from './html.js';
|
|
96
95
|
import {
|
|
96
|
+
addImportPlaceholders,
|
|
97
97
|
convertSetextHeadings,
|
|
98
98
|
convertTildeFences,
|
|
99
99
|
padBlankBlockquoteLines,
|
|
100
|
+
removeImportPlaceholders,
|
|
100
101
|
splitMultilineFormatting,
|
|
101
102
|
trimBlankBlockquoteLines,
|
|
102
103
|
} from './markdown.js';
|
|
@@ -110,7 +111,10 @@ import {
|
|
|
110
111
|
import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js';
|
|
111
112
|
import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
|
|
112
113
|
import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
|
|
114
|
+
import { CODE } from './transformers/code.js';
|
|
115
|
+
import { addCodeSpanSplit } from './transformers/formatted-code.js';
|
|
113
116
|
import { HR } from './transformers/hr.js';
|
|
117
|
+
import { RAW_SYNTAX } from './transformers/raw-syntax.js';
|
|
114
118
|
import { $getSelectedAlignment } from './alignment.js';
|
|
115
119
|
import {
|
|
116
120
|
$ensureParagraphAfterTable,
|
|
@@ -998,6 +1002,8 @@ export const initEditor = ({
|
|
|
998
1002
|
componentTransformers.filter(({ type }) => type === 'text-match')
|
|
999
1003
|
),
|
|
1000
1004
|
),
|
|
1005
|
+
// Last, so the syntax of the editor’s own comes first
|
|
1006
|
+
RAW_SYNTAX,
|
|
1001
1007
|
]),
|
|
1002
1008
|
];
|
|
1003
1009
|
|
|
@@ -1154,6 +1160,37 @@ const importValue = async (editor, { languages, $import, $export, formatName })
|
|
|
1154
1160
|
return editor.read($export);
|
|
1155
1161
|
};
|
|
1156
1162
|
|
|
1163
|
+
/**
|
|
1164
|
+
* Remove the placeholders added by `addImportPlaceholders()` from the imported nodes: the text, the
|
|
1165
|
+
* Markdown of raw syntax, and the URL and title of a link.
|
|
1166
|
+
*/
|
|
1167
|
+
const removePlaceholders = () => {
|
|
1168
|
+
getRoot()
|
|
1169
|
+
.getAllTextNodes()
|
|
1170
|
+
.forEach((node) => {
|
|
1171
|
+
const text = node.getTextContent();
|
|
1172
|
+
const newText = removeImportPlaceholders(text);
|
|
1173
|
+
const link = node.getParent();
|
|
1174
|
+
|
|
1175
|
+
if (newText !== text) {
|
|
1176
|
+
node.setTextContent(newText);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
if (isLinkNode(link)) {
|
|
1180
|
+
const url = link.getURL();
|
|
1181
|
+
const title = link.getTitle();
|
|
1182
|
+
|
|
1183
|
+
if (removeImportPlaceholders(url) !== url) {
|
|
1184
|
+
link.setURL(removeImportPlaceholders(url));
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
if (title && removeImportPlaceholders(title) !== title) {
|
|
1188
|
+
link.setTitle(removeImportPlaceholders(title));
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
};
|
|
1193
|
+
|
|
1157
1194
|
/**
|
|
1158
1195
|
* Convert Markdown to Lexical nodes.
|
|
1159
1196
|
* @param {LexicalEditor} editor Editor instance.
|
|
@@ -1182,19 +1219,30 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1182
1219
|
const normalizedValue = hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
|
|
1183
1220
|
|
|
1184
1221
|
return importValue(editor, {
|
|
1185
|
-
|
|
1222
|
+
// The language is the leading word of the info string, like `js` in ```` ``` js title=x ````;
|
|
1223
|
+
// see `CODE`
|
|
1224
|
+
languages: [...normalizedValue.matchAll(/^`{3,}[ \t]*(?<lang>[\w-]+)/gm)].map(
|
|
1186
1225
|
({ groups: { lang = 'plain' } = {} }) => lang,
|
|
1187
1226
|
),
|
|
1188
1227
|
/**
|
|
1189
1228
|
* Import the Markdown.
|
|
1190
1229
|
*/
|
|
1191
1230
|
$import: () => {
|
|
1231
|
+
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
1232
|
+
const splitValue = splitMultilineFormatting(normalizedValue);
|
|
1233
|
+
// Add placeholders so Lexical reads the backslashes and the tabs in code spans right
|
|
1234
|
+
const markedValue = addImportPlaceholders(splitValue);
|
|
1235
|
+
|
|
1192
1236
|
convertFromMarkdownString(
|
|
1193
|
-
//
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
enabledTransformers,
|
|
1237
|
+
// Pad blank blockquote lines so they are not imported as literal `>` text
|
|
1238
|
+
padBlankBlockquoteLines(markedValue),
|
|
1239
|
+
// Keep the formatting around a code span after other formatting
|
|
1240
|
+
addCodeSpanSplit(enabledTransformers),
|
|
1197
1241
|
);
|
|
1242
|
+
|
|
1243
|
+
if (markedValue !== splitValue) {
|
|
1244
|
+
removePlaceholders();
|
|
1245
|
+
}
|
|
1198
1246
|
},
|
|
1199
1247
|
/**
|
|
1200
1248
|
* Export the content as Markdown.
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export function splitMultilineFormatting(value: string): string;
|
|
2
|
+
export function findCodeSpans(line: string): CodeSpan[];
|
|
3
|
+
export function addImportPlaceholders(value: string): string;
|
|
4
|
+
export function removeImportPlaceholders(text: string): string;
|
|
2
5
|
export function convertTildeFences(value: string): string;
|
|
3
6
|
export function convertSetextHeadings(value: string): string;
|
|
4
7
|
export function padBlankBlockquoteLines(value: string): string;
|
|
@@ -22,3 +25,32 @@ export type CodeBlock = {
|
|
|
22
25
|
*/
|
|
23
26
|
closed: boolean;
|
|
24
27
|
};
|
|
28
|
+
export type FormattedLine = {
|
|
29
|
+
/**
|
|
30
|
+
* Whitespace at the start of the line, outside the formatting.
|
|
31
|
+
*/
|
|
32
|
+
indent: string;
|
|
33
|
+
/**
|
|
34
|
+
* Text of the line within the formatting.
|
|
35
|
+
*/
|
|
36
|
+
text: string;
|
|
37
|
+
/**
|
|
38
|
+
* Whitespace or the backslash of a hard line break at the end of the
|
|
39
|
+
* line, outside the formatting.
|
|
40
|
+
*/
|
|
41
|
+
lineEnd: string;
|
|
42
|
+
};
|
|
43
|
+
export type CodeSpan = {
|
|
44
|
+
/**
|
|
45
|
+
* Index of the opening backticks.
|
|
46
|
+
*/
|
|
47
|
+
start: number;
|
|
48
|
+
/**
|
|
49
|
+
* Index after the closing backticks.
|
|
50
|
+
*/
|
|
51
|
+
end: number;
|
|
52
|
+
/**
|
|
53
|
+
* Number of backticks on either side.
|
|
54
|
+
*/
|
|
55
|
+
fenceLength: number;
|
|
56
|
+
};
|
|
@@ -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
|
-
|
|
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.
|