@sveltia/ui 0.87.3 → 0.87.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/text-editor/core.d.ts +1 -0
- package/dist/components/text-editor/core.js +175 -6
- package/dist/components/text-editor/markdown.js +149 -8
- 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/raw-syntax.d.ts +33 -0
- package/dist/components/text-editor/transformers/raw-syntax.js +85 -1
- package/dist/components/text-editor/transformers/table.js +8 -2
- package/dist/shiki-engine.js +5 -5
- package/package.json +6 -6
|
@@ -5,6 +5,7 @@ export function getSelectionTypes(): TextEditorSelectionState;
|
|
|
5
5
|
export function exportMarkdown(enabledTransformers: Transformer[]): string;
|
|
6
6
|
export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string, format?: TextEditorValueFormat): string;
|
|
7
7
|
export function getComponentMarkdown({ node, createNode, transformer, createMarkdown }: TextEditorComponent): string;
|
|
8
|
+
export function removePlaceholdersFromMatch(transformer: Transformer): Transformer;
|
|
8
9
|
export function initEditor({ enabledButtons, components, useMarkdownShortcuts, isCodeEditor, defaultLanguage, format, }: TextEditorConfig): InitEditorResult;
|
|
9
10
|
export function loadCodeHighlighter(lang: string): Promise<void>;
|
|
10
11
|
export function convertMarkdownToLexical(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<string | undefined>;
|
|
@@ -114,7 +114,13 @@ import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
|
|
|
114
114
|
import { CODE } from './transformers/code.js';
|
|
115
115
|
import { addCodeSpanSplit } from './transformers/formatted-code.js';
|
|
116
116
|
import { HR } from './transformers/hr.js';
|
|
117
|
-
import {
|
|
117
|
+
import {
|
|
118
|
+
getImportedRawSyntax,
|
|
119
|
+
HTML_BLOCK_PLACEHOLDER,
|
|
120
|
+
INLINE_HTML_REGEX,
|
|
121
|
+
RAW_HTML,
|
|
122
|
+
RAW_SYNTAX,
|
|
123
|
+
} from './transformers/raw-syntax.js';
|
|
118
124
|
import { $getSelectedAlignment } from './alignment.js';
|
|
119
125
|
import {
|
|
120
126
|
$ensureParagraphAfterTable,
|
|
@@ -904,6 +910,160 @@ const registerTabIndentation = (editor) =>
|
|
|
904
910
|
};
|
|
905
911
|
});
|
|
906
912
|
|
|
913
|
+
/**
|
|
914
|
+
* Remove the placeholders added by `addImportPlaceholders()` from a regular expression match.
|
|
915
|
+
* @param {RegExpMatchArray} match Match.
|
|
916
|
+
* @returns {RegExpMatchArray} Match without the placeholders, with the same index and input.
|
|
917
|
+
*/
|
|
918
|
+
const cleanMatch = (match) => {
|
|
919
|
+
/**
|
|
920
|
+
* Remove the placeholders from a captured string, if any.
|
|
921
|
+
* @param {string | undefined} value Captured string.
|
|
922
|
+
* @returns {string | undefined} Cleaned string.
|
|
923
|
+
*/
|
|
924
|
+
const clean = (value) => (value === undefined ? value : removeImportPlaceholders(value));
|
|
925
|
+
|
|
926
|
+
return /** @type {RegExpMatchArray} */ (
|
|
927
|
+
Object.assign(match.map(clean), {
|
|
928
|
+
index: match.index,
|
|
929
|
+
input: match.input,
|
|
930
|
+
groups: match.groups
|
|
931
|
+
? Object.fromEntries(
|
|
932
|
+
Object.entries(match.groups).map(([key, value]) => [key, clean(value)]),
|
|
933
|
+
)
|
|
934
|
+
: undefined,
|
|
935
|
+
})
|
|
936
|
+
);
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* Make a regular expression match the text after the placeholder at the start of a line of an HTML
|
|
941
|
+
* block, if any, so the pattern of an editor component, like `^<aside>`, still matches the line.
|
|
942
|
+
* @param {RegExp} regExp Regular expression without the `g` flag.
|
|
943
|
+
* @param {boolean} isLineStart Whether Lexical takes the match as the start of the line and removes
|
|
944
|
+
* it by its length, like that of an element transformer, in which case the match includes the
|
|
945
|
+
* placeholder. Otherwise its index is moved past the placeholder.
|
|
946
|
+
* @returns {RegExp} Regular expression.
|
|
947
|
+
*/
|
|
948
|
+
const skipHtmlBlockPlaceholder = (regExp, isLineStart) =>
|
|
949
|
+
// Lexical matches the text with `String.prototype.match()`, which calls this method
|
|
950
|
+
Object.assign(new RegExp(regExp), {
|
|
951
|
+
/**
|
|
952
|
+
* Match the text after the placeholder at its start, if any.
|
|
953
|
+
* @param {string} text Text.
|
|
954
|
+
* @returns {RegExpMatchArray | null} Match.
|
|
955
|
+
*/
|
|
956
|
+
[Symbol.match]: (text) => {
|
|
957
|
+
const offset = text.startsWith(HTML_BLOCK_PLACEHOLDER) ? 1 : 0;
|
|
958
|
+
const match = text.slice(offset).match(regExp);
|
|
959
|
+
|
|
960
|
+
if (!match || !offset) {
|
|
961
|
+
return match;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
match.input = text;
|
|
965
|
+
|
|
966
|
+
if (isLineStart && match.index === 0) {
|
|
967
|
+
match[0] = `${HTML_BLOCK_PLACEHOLDER}${match[0]}`;
|
|
968
|
+
} else {
|
|
969
|
+
// The pattern has no `g` flag, so the match has an index
|
|
970
|
+
/** @type {number} */ (match.index) += offset;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
return match;
|
|
974
|
+
},
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Remove the placeholders added by `addImportPlaceholders()` from an argument of a transformer’s
|
|
979
|
+
* `replace()`: a regular expression match, or the lines of a multiline element.
|
|
980
|
+
* @param {any} arg Argument.
|
|
981
|
+
* @returns {any} Argument without the placeholders.
|
|
982
|
+
*/
|
|
983
|
+
const cleanReplaceArg = (arg) => {
|
|
984
|
+
if (!Array.isArray(arg)) {
|
|
985
|
+
return arg;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
if ('index' in arg) {
|
|
989
|
+
return cleanMatch(/** @type {RegExpMatchArray} */ (arg));
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
return arg.every((item) => typeof item === 'string')
|
|
993
|
+
? arg.map((line) => removeImportPlaceholders(line))
|
|
994
|
+
: arg;
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* Make an editor component’s transformer import the Markdown without the placeholders added by
|
|
999
|
+
* `addImportPlaceholders()`, which `removePlaceholders()` can’t remove from the properties of the
|
|
1000
|
+
* component node it creates, like the alt text of an image. The line of an HTML block starts with
|
|
1001
|
+
* a placeholder, so the patterns of a component, like `^<aside>`, are matched after it.
|
|
1002
|
+
* @param {Transformer} transformer Transformer.
|
|
1003
|
+
* @returns {Transformer} Transformer.
|
|
1004
|
+
*/
|
|
1005
|
+
export const removePlaceholdersFromMatch = (transformer) => {
|
|
1006
|
+
/** @type {any} */
|
|
1007
|
+
const {
|
|
1008
|
+
type,
|
|
1009
|
+
handleImportAfterStartMatch,
|
|
1010
|
+
importRegExp,
|
|
1011
|
+
regExp,
|
|
1012
|
+
regExpStart,
|
|
1013
|
+
regExpEnd,
|
|
1014
|
+
replace,
|
|
1015
|
+
} = transformer;
|
|
1016
|
+
|
|
1017
|
+
/** @type {Record<string, any>} */
|
|
1018
|
+
const overrides = {};
|
|
1019
|
+
|
|
1020
|
+
if (handleImportAfterStartMatch) {
|
|
1021
|
+
/**
|
|
1022
|
+
* Import the lines without the placeholders.
|
|
1023
|
+
* @param {{ lines: string[] }} args Arguments.
|
|
1024
|
+
* @returns {any} Result.
|
|
1025
|
+
*/
|
|
1026
|
+
overrides.handleImportAfterStartMatch = (args) =>
|
|
1027
|
+
handleImportAfterStartMatch({
|
|
1028
|
+
...args,
|
|
1029
|
+
lines: args.lines.map((line) => removeImportPlaceholders(line)),
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
if (importRegExp) {
|
|
1034
|
+
overrides.importRegExp = skipHtmlBlockPlaceholder(importRegExp, false);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// The regular expression of an element is matched against the start of a line on import
|
|
1038
|
+
if (type === 'element' && regExp) {
|
|
1039
|
+
overrides.regExp = skipHtmlBlockPlaceholder(regExp, true);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
if (regExpStart) {
|
|
1043
|
+
overrides.regExpStart = skipHtmlBlockPlaceholder(regExpStart, true);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
if (regExpEnd instanceof RegExp) {
|
|
1047
|
+
overrides.regExpEnd = skipHtmlBlockPlaceholder(regExpEnd, true);
|
|
1048
|
+
} else if (regExpEnd?.regExp) {
|
|
1049
|
+
overrides.regExpEnd = {
|
|
1050
|
+
...regExpEnd,
|
|
1051
|
+
regExp: skipHtmlBlockPlaceholder(regExpEnd.regExp, true),
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
if (replace) {
|
|
1056
|
+
/**
|
|
1057
|
+
* Import the match and the lines without the placeholders.
|
|
1058
|
+
* @param {...any} args Arguments.
|
|
1059
|
+
* @returns {any} Result.
|
|
1060
|
+
*/
|
|
1061
|
+
overrides.replace = (...args) => replace(...args.map(cleanReplaceArg));
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
return /** @type {Transformer} */ ({ ...transformer, ...overrides });
|
|
1065
|
+
};
|
|
1066
|
+
|
|
907
1067
|
/**
|
|
908
1068
|
* Initialize the Lexical editor.
|
|
909
1069
|
* @param {TextEditorConfig} config Editor configuration.
|
|
@@ -980,11 +1140,16 @@ export const initEditor = ({
|
|
|
980
1140
|
},
|
|
981
1141
|
});
|
|
982
1142
|
|
|
983
|
-
const componentTransformers = components.map(({ transformer }) =>
|
|
1143
|
+
const componentTransformers = components.map(({ transformer }) =>
|
|
1144
|
+
removePlaceholdersFromMatch(transformer),
|
|
1145
|
+
);
|
|
984
1146
|
|
|
985
1147
|
/** @type {Transformer[]} */
|
|
986
1148
|
const enabledTransformers = [
|
|
987
1149
|
...componentTransformers,
|
|
1150
|
+
// Raw HTML comes before the editor’s own syntax, so a line of an HTML block that starts like a
|
|
1151
|
+
// link, for example, is still raw HTML. Only an editor component takes precedence
|
|
1152
|
+
...(isCodeEditor ? [] : [RAW_HTML]),
|
|
988
1153
|
...new Set(
|
|
989
1154
|
Object.entries(TRANSFORMER_MAP)
|
|
990
1155
|
.filter(([button]) => enabledButtons.includes(/** @type {TextEditorNodeType} */ (button)))
|
|
@@ -1162,14 +1327,16 @@ const importValue = async (editor, { languages, $import, $export, formatName })
|
|
|
1162
1327
|
|
|
1163
1328
|
/**
|
|
1164
1329
|
* 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.
|
|
1330
|
+
* Markdown of raw syntax, and the URL and title of a link. The text of raw syntax is restored from
|
|
1331
|
+
* the Markdown it was imported from, as Lexical has removed its escapes, which are not escapes in
|
|
1332
|
+
* raw HTML; see `RAW_HTML`.
|
|
1166
1333
|
*/
|
|
1167
1334
|
const removePlaceholders = () => {
|
|
1168
1335
|
getRoot()
|
|
1169
1336
|
.getAllTextNodes()
|
|
1170
1337
|
.forEach((node) => {
|
|
1171
1338
|
const text = node.getTextContent();
|
|
1172
|
-
const newText = removeImportPlaceholders(text);
|
|
1339
|
+
const newText = removeImportPlaceholders(getImportedRawSyntax(node) ?? text);
|
|
1173
1340
|
const link = node.getParent();
|
|
1174
1341
|
|
|
1175
1342
|
if (newText !== text) {
|
|
@@ -1230,7 +1397,8 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1230
1397
|
$import: () => {
|
|
1231
1398
|
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
1232
1399
|
const splitValue = splitMultilineFormatting(normalizedValue);
|
|
1233
|
-
// Add placeholders so Lexical reads the backslashes
|
|
1400
|
+
// Add placeholders so Lexical reads the backslashes, the tabs in code spans and raw HTML
|
|
1401
|
+
// right
|
|
1234
1402
|
const markedValue = addImportPlaceholders(splitValue);
|
|
1235
1403
|
|
|
1236
1404
|
convertFromMarkdownString(
|
|
@@ -1240,7 +1408,8 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1240
1408
|
addCodeSpanSplit(enabledTransformers),
|
|
1241
1409
|
);
|
|
1242
1410
|
|
|
1243
|
-
|
|
1411
|
+
// The text of raw HTML is restored as well. The lines of an HTML block have a placeholder
|
|
1412
|
+
if (markedValue !== splitValue || INLINE_HTML_REGEX.test(markedValue)) {
|
|
1244
1413
|
removePlaceholders();
|
|
1245
1414
|
}
|
|
1246
1415
|
},
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { INFO_BACKTICK } from './transformers/code.js';
|
|
2
|
+
import {
|
|
3
|
+
HTML_BLOCK_PLACEHOLDER,
|
|
4
|
+
HTML_TAG_SOURCE,
|
|
5
|
+
INLINE_HTML_REGEX,
|
|
6
|
+
} from './transformers/raw-syntax.js';
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* @typedef {object} CodeBlock
|
|
@@ -187,14 +192,52 @@ const HTML_BLOCK_TAG_NAMES = [
|
|
|
187
192
|
];
|
|
188
193
|
|
|
189
194
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
195
|
+
* Start and end conditions of the HTML blocks that can interrupt a paragraph: a raw text element, a
|
|
196
|
+
* comment, a processing instruction, a declaration, a CDATA section or a block element. The start
|
|
197
|
+
* is what follows the `<` at the start of the line. The end of the block is the line that matches
|
|
198
|
+
* the end condition, or the line before a blank line if there’s none.
|
|
199
|
+
* @type {[string, RegExp | null][]}
|
|
193
200
|
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
194
201
|
*/
|
|
202
|
+
const HTML_BLOCK_CONDITION_SOURCES = [
|
|
203
|
+
['(?:script|pre|style|textarea)(?:[ \\t>]|$)', /<\/(?:pre|script|style|textarea)>/i],
|
|
204
|
+
['!--', /-->/],
|
|
205
|
+
['\\?', /\?>/],
|
|
206
|
+
['![A-Za-z]', />/],
|
|
207
|
+
['!\\[CDATA\\[', /\]\]>/],
|
|
208
|
+
[`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$)`, null],
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Regular expression to match the start of an HTML block that can interrupt a paragraph; see
|
|
213
|
+
* {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
214
|
+
*/
|
|
195
215
|
const HTML_BLOCK_START_REGEX = new RegExp(
|
|
196
|
-
`<(
|
|
197
|
-
|
|
216
|
+
`<(?:${HTML_BLOCK_CONDITION_SOURCES.map(([start]) => start).join('|')})`,
|
|
217
|
+
'i',
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Start and end conditions of the HTML blocks that can interrupt a paragraph, with the start as a
|
|
222
|
+
* regular expression to match a line; see {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
223
|
+
* @type {[RegExp, RegExp | null][]}
|
|
224
|
+
*/
|
|
225
|
+
const HTML_BLOCK_CONDITIONS = HTML_BLOCK_CONDITION_SOURCES.map(([start, end]) => [
|
|
226
|
+
new RegExp(`^ {0,3}<(?:${start})`, 'i'),
|
|
227
|
+
end,
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Global version of `INLINE_HTML_REGEX`.
|
|
232
|
+
*/
|
|
233
|
+
const GLOBAL_INLINE_HTML_REGEX = new RegExp(INLINE_HTML_REGEX, 'g');
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Regular expression to match a line that starts an HTML block that can’t interrupt a paragraph.
|
|
237
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
238
|
+
*/
|
|
239
|
+
const STANDALONE_HTML_TAG_REGEX = new RegExp(
|
|
240
|
+
`^ {0,3}(?!</?(?:pre|script|style|textarea)\\b)(?:${HTML_TAG_SOURCE})[ \\t]*$`,
|
|
198
241
|
'i',
|
|
199
242
|
);
|
|
200
243
|
|
|
@@ -360,7 +403,11 @@ export const splitMultilineFormatting = (value) =>
|
|
|
360
403
|
|
|
361
404
|
const lines = splitFormattedLines(text);
|
|
362
405
|
|
|
406
|
+
// A line with only markers, like `**` in `_**\n**_` of `**_(a)_**\n**_(b)_**`, means
|
|
407
|
+
// the match runs from the closing marker of one line to the opening marker of the next,
|
|
408
|
+
// which isn’t formatting. Splitting it would add markers that match again, without end
|
|
363
409
|
if (
|
|
410
|
+
lines.some(({ text: lineText }) => /^[*_~]+$/.test(lineText)) ||
|
|
364
411
|
lines
|
|
365
412
|
.slice(1)
|
|
366
413
|
.some(({ indent, text: lineText }) =>
|
|
@@ -536,6 +583,35 @@ const markMarkersNextToBackslashes = (line) =>
|
|
|
536
583
|
)
|
|
537
584
|
: line;
|
|
538
585
|
|
|
586
|
+
/**
|
|
587
|
+
* Regular expression to match the place between a formatting marker and a punctuation character or
|
|
588
|
+
* a symbol other than ASCII, like an en dash or a curly quote.
|
|
589
|
+
*/
|
|
590
|
+
const MARKER_UNICODE_PUNCTUATION_REGEX =
|
|
591
|
+
/(?<=[*_~])(?=(?![\0-\x7F])[\p{P}\p{S}])|(?<=(?![\0-\x7F])[\p{P}\p{S}])(?=[*_~])/gu;
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Put a period, which Lexical takes as punctuation, between a formatting marker and a punctuation
|
|
595
|
+
* character or a symbol other than ASCII. Unlike CommonMark, Lexical only takes ASCII characters as
|
|
596
|
+
* punctuation, so it reads the marker as being next to a letter: it doesn’t read `**[a](u)**` in
|
|
597
|
+
* `–**[a](u)**` or `“**[a](u)**”` as bold, as the marker can’t open or close the formatting there.
|
|
598
|
+
* The period gives the marker the same neighbor as in CommonMark. A placeholder on the other side
|
|
599
|
+
* lets it be removed.
|
|
600
|
+
* @param {string} line Line.
|
|
601
|
+
* @returns {string} Processed line.
|
|
602
|
+
* @see https://spec.commonmark.org/0.31.2/#unicode-punctuation-character
|
|
603
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1107
|
|
604
|
+
*/
|
|
605
|
+
const markMarkersNextToUnicodePunctuation = (line) =>
|
|
606
|
+
/[^\0-\x7F]/.test(line)
|
|
607
|
+
? line.replace(MARKER_UNICODE_PUNCTUATION_REGEX, (_match, index) =>
|
|
608
|
+
// The period goes next to the marker
|
|
609
|
+
'*_~'.includes(line[index - 1])
|
|
610
|
+
? `.${PUNCTUATION_PLACEHOLDER}`
|
|
611
|
+
: `${PUNCTUATION_PLACEHOLDER}.`,
|
|
612
|
+
)
|
|
613
|
+
: line;
|
|
614
|
+
|
|
539
615
|
/**
|
|
540
616
|
* Replace the tabs in the code spans of a line, and the spaces in a code span made only of spaces,
|
|
541
617
|
* with placeholders. Lexical turns a tab into a tab node, which has no format, so a tab in a code
|
|
@@ -569,17 +645,81 @@ const markCodeSpanWhitespace = (line) => {
|
|
|
569
645
|
return result + line.slice(index);
|
|
570
646
|
};
|
|
571
647
|
|
|
648
|
+
/**
|
|
649
|
+
* Mark the lines of the HTML blocks in a part of a Markdown string with a placeholder at the start,
|
|
650
|
+
* so they’re imported as raw HTML, without the Markdown syntax being read; see `RAW_HTML`. Their
|
|
651
|
+
* tabs, and those in the inline HTML of the other lines, are replaced with a placeholder, as
|
|
652
|
+
* Lexical would otherwise split the raw HTML into separate nodes around a tab node.
|
|
653
|
+
* @param {string} part Part of a Markdown string outside fenced code blocks.
|
|
654
|
+
* @returns {string} Processed part.
|
|
655
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
656
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
657
|
+
*/
|
|
658
|
+
const markHtml = (part) => {
|
|
659
|
+
/**
|
|
660
|
+
* End condition of the current HTML block: a regular expression, `null` for a blank line, or
|
|
661
|
+
* `undefined` if the line is not in an HTML block.
|
|
662
|
+
* @type {RegExp | null | undefined}
|
|
663
|
+
*/
|
|
664
|
+
let end;
|
|
665
|
+
let isInParagraph = false;
|
|
666
|
+
|
|
667
|
+
return part
|
|
668
|
+
.split('\n')
|
|
669
|
+
.map((line) => {
|
|
670
|
+
const isBlankLine = !line.trim();
|
|
671
|
+
|
|
672
|
+
if (end === null && isBlankLine) {
|
|
673
|
+
end = undefined;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (end === undefined) {
|
|
677
|
+
const condition = HTML_BLOCK_CONDITIONS.find(([start]) => start.test(line));
|
|
678
|
+
|
|
679
|
+
if (condition) {
|
|
680
|
+
[, end] = condition;
|
|
681
|
+
} else if (!isInParagraph && STANDALONE_HTML_TAG_REGEX.test(line)) {
|
|
682
|
+
end = null;
|
|
683
|
+
} else {
|
|
684
|
+
isInParagraph = !isBlankLine;
|
|
685
|
+
|
|
686
|
+
return line.includes('\t')
|
|
687
|
+
? line.replace(GLOBAL_INLINE_HTML_REGEX, (tag) =>
|
|
688
|
+
tag.replaceAll('\t', CODE_TAB_PLACEHOLDER),
|
|
689
|
+
)
|
|
690
|
+
: line;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (end?.test(line)) {
|
|
695
|
+
end = undefined;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
isInParagraph = false;
|
|
699
|
+
|
|
700
|
+
return `${HTML_BLOCK_PLACEHOLDER}${line.replaceAll('\t', CODE_TAB_PLACEHOLDER)}`;
|
|
701
|
+
})
|
|
702
|
+
.join('\n');
|
|
703
|
+
};
|
|
704
|
+
|
|
572
705
|
/**
|
|
573
706
|
* Add placeholders to a Markdown string, so Lexical reads it as CommonMark does; see
|
|
574
|
-
* `markLineEndBackslashes()`, `markMarkersNextToBackslashes()
|
|
707
|
+
* `markLineEndBackslashes()`, `markMarkersNextToBackslashes()`,
|
|
708
|
+
* `markMarkersNextToUnicodePunctuation()` and `markCodeSpanWhitespace()`.
|
|
575
709
|
* Lines in fenced code blocks are left untouched. The placeholders are removed from the imported
|
|
576
710
|
* text with `removeImportPlaceholders()`.
|
|
577
711
|
* @param {string} value Markdown string to process.
|
|
578
712
|
* @returns {string} Processed Markdown string.
|
|
579
713
|
*/
|
|
580
714
|
export const addImportPlaceholders = (value) =>
|
|
581
|
-
mapLinesOutsideCodeBlocks(
|
|
582
|
-
|
|
715
|
+
mapLinesOutsideCodeBlocks(
|
|
716
|
+
value.includes('<') ? mapOutsideCodeBlocks(value, markHtml) : value,
|
|
717
|
+
(line) =>
|
|
718
|
+
markCodeSpanWhitespace(
|
|
719
|
+
markMarkersNextToUnicodePunctuation(
|
|
720
|
+
markMarkersNextToBackslashes(markLineEndBackslashes(line)),
|
|
721
|
+
),
|
|
722
|
+
),
|
|
583
723
|
);
|
|
584
724
|
|
|
585
725
|
/**
|
|
@@ -591,6 +731,7 @@ export const removeImportPlaceholders = (text) =>
|
|
|
591
731
|
text
|
|
592
732
|
.replace(new RegExp(`\\.${PUNCTUATION_PLACEHOLDER}|${PUNCTUATION_PLACEHOLDER}\\.`, 'g'), '')
|
|
593
733
|
.replaceAll(ESCAPED_BACKSLASH_PLACEHOLDER, '')
|
|
734
|
+
.replaceAll(HTML_BLOCK_PLACEHOLDER, '')
|
|
594
735
|
.replaceAll(CODE_TAB_PLACEHOLDER, '\t')
|
|
595
736
|
.replaceAll(CODE_SPACE_PLACEHOLDER, ' ');
|
|
596
737
|
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { TextMatchTransformer } from '@lexical/markdown';
|
|
3
|
+
* @import { TextNode } from 'lexical';
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Placeholder put at the start of a line of an HTML block; see `addImportPlaceholders()`.
|
|
7
|
+
*/
|
|
8
|
+
export const HTML_BLOCK_PLACEHOLDER: "\uE007";
|
|
9
|
+
/**
|
|
10
|
+
* Regular expression source to match an HTML open tag or closing tag.
|
|
11
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
12
|
+
*/
|
|
13
|
+
export const HTML_TAG_SOURCE: string;
|
|
14
|
+
/**
|
|
15
|
+
* Regular expression to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
|
|
16
|
+
* processing instruction, a declaration or a CDATA section, which isn’t escaped.
|
|
17
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
18
|
+
*/
|
|
19
|
+
export const INLINE_HTML_REGEX: RegExp;
|
|
20
|
+
export function getImportedRawSyntax(node: TextNode): string | undefined;
|
|
1
21
|
export function getRawSyntax(node: TextNode): string | undefined;
|
|
2
22
|
/**
|
|
3
23
|
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
@@ -8,5 +28,18 @@ export function getRawSyntax(node: TextNode): string | undefined;
|
|
|
8
28
|
* @type {TextMatchTransformer}
|
|
9
29
|
*/
|
|
10
30
|
export const RAW_SYNTAX: TextMatchTransformer;
|
|
31
|
+
/**
|
|
32
|
+
* Import-only transformer that marks raw HTML, so it’s exported as is. Neither the Markdown syntax
|
|
33
|
+
* nor the escapes are read in raw HTML, so an escape added to it on export, like `\_` in an
|
|
34
|
+
* attribute value or `\*` in the text of an HTML block, would show up as a backslash. Lexical
|
|
35
|
+
* removes the escapes from the text it has imported, so the text is restored with
|
|
36
|
+
* `getImportedRawSyntax()` once the import is done.
|
|
37
|
+
* @type {TextMatchTransformer}
|
|
38
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
39
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
40
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1105
|
|
41
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
42
|
+
*/
|
|
43
|
+
export const RAW_HTML: TextMatchTransformer;
|
|
11
44
|
import type { TextNode } from 'lexical';
|
|
12
45
|
import type { TextMatchTransformer } from '@lexical/markdown';
|
|
@@ -5,6 +5,49 @@ import { $getState as getState, $setState as setState, createState } from 'lexic
|
|
|
5
5
|
* @import { TextNode } from 'lexical';
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Placeholder put at the start of a line of an HTML block; see `addImportPlaceholders()`.
|
|
10
|
+
*/
|
|
11
|
+
export const HTML_BLOCK_PLACEHOLDER = '\uE007';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Whitespace within an HTML tag, including the placeholder of a tab; see `addImportPlaceholders()`.
|
|
15
|
+
*/
|
|
16
|
+
const TAG_SPACE = '[ \\t\\uE005]';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Regular expression source to match an HTML open tag or closing tag.
|
|
20
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
21
|
+
*/
|
|
22
|
+
export const HTML_TAG_SOURCE =
|
|
23
|
+
`<[A-Za-z][A-Za-z0-9-]*(?:${TAG_SPACE}+[A-Za-z_:][\\w.:-]*` +
|
|
24
|
+
`(?:${TAG_SPACE}*=${TAG_SPACE}*(?:[^\\s"'=<>\`\\uE005]+|'[^'\\n]*'|"[^"\\n]*"))?)*` +
|
|
25
|
+
`${TAG_SPACE}*/?>|</[A-Za-z][A-Za-z0-9-]*${TAG_SPACE}*>`;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Regular expression to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
|
|
29
|
+
* processing instruction, a declaration or a CDATA section, which isn’t escaped.
|
|
30
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
31
|
+
*/
|
|
32
|
+
export const INLINE_HTML_REGEX = new RegExp(
|
|
33
|
+
[
|
|
34
|
+
// Not escaped. The lookahead comes first, so the lookbehind is only tried before a `<`
|
|
35
|
+
String.raw`(?=<)(?<!(?:^|[^\\])(?:\\\\)*\\)(?:`,
|
|
36
|
+
[
|
|
37
|
+
HTML_TAG_SOURCE,
|
|
38
|
+
// Comment
|
|
39
|
+
String.raw`<!-->|<!--->|<!--.*?-->`,
|
|
40
|
+
// Processing instruction
|
|
41
|
+
String.raw`<\?.*?\?>`,
|
|
42
|
+
// Declaration
|
|
43
|
+
String.raw`<![A-Za-z][^>]*>`,
|
|
44
|
+
// CDATA section
|
|
45
|
+
String.raw`<!\[CDATA\[.*?\]\]>`,
|
|
46
|
+
].join('|'),
|
|
47
|
+
')',
|
|
48
|
+
].join(''),
|
|
49
|
+
);
|
|
50
|
+
|
|
8
51
|
/**
|
|
9
52
|
* Block syntax at the start of a line, after up to 3 spaces.
|
|
10
53
|
*/
|
|
@@ -41,6 +84,13 @@ const rawSyntaxState = createState('suiRawSyntax', {
|
|
|
41
84
|
parse: (value) => (typeof value === 'string' ? value : ''),
|
|
42
85
|
});
|
|
43
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Get the Markdown a text node was imported from, if it’s raw syntax.
|
|
89
|
+
* @param {TextNode} node Text node.
|
|
90
|
+
* @returns {string | undefined} Markdown, or `undefined` if the node is not raw syntax.
|
|
91
|
+
*/
|
|
92
|
+
export const getImportedRawSyntax = (node) => getState(node, rawSyntaxState) || undefined;
|
|
93
|
+
|
|
44
94
|
/**
|
|
45
95
|
* Get the Markdown of a text node imported from raw syntax.
|
|
46
96
|
* @param {TextNode} node Text node.
|
|
@@ -54,7 +104,7 @@ export const getRawSyntax = (node) => {
|
|
|
54
104
|
return undefined;
|
|
55
105
|
}
|
|
56
106
|
|
|
57
|
-
//
|
|
107
|
+
// Raw syntax has no escapes, so the text is the Markdown, even if it has been edited since
|
|
58
108
|
return node.getTextContent();
|
|
59
109
|
};
|
|
60
110
|
|
|
@@ -86,3 +136,37 @@ export const RAW_SYNTAX = {
|
|
|
86
136
|
},
|
|
87
137
|
type: 'text-match',
|
|
88
138
|
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Regular expression to match raw HTML: a line of an HTML block after the placeholder that marks
|
|
142
|
+
* it, or inline HTML, like a tag. The placeholder is left out of the match, so an editor component
|
|
143
|
+
* that matches the line after it, which comes first, still takes precedence; see
|
|
144
|
+
* `removePlaceholdersFromMatch()` in `core.js`.
|
|
145
|
+
*/
|
|
146
|
+
const RAW_HTML_REGEX = new RegExp(`(?<=^${HTML_BLOCK_PLACEHOLDER}).+|${INLINE_HTML_REGEX.source}`);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Import-only transformer that marks raw HTML, so it’s exported as is. Neither the Markdown syntax
|
|
150
|
+
* nor the escapes are read in raw HTML, so an escape added to it on export, like `\_` in an
|
|
151
|
+
* attribute value or `\*` in the text of an HTML block, would show up as a backslash. Lexical
|
|
152
|
+
* removes the escapes from the text it has imported, so the text is restored with
|
|
153
|
+
* `getImportedRawSyntax()` once the import is done.
|
|
154
|
+
* @type {TextMatchTransformer}
|
|
155
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
156
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
157
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1105
|
|
158
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
159
|
+
*/
|
|
160
|
+
export const RAW_HTML = {
|
|
161
|
+
dependencies: [],
|
|
162
|
+
/**
|
|
163
|
+
* Leave the export to `BLOCK_SYNTAX_ESCAPE`.
|
|
164
|
+
* @returns {null} Nothing.
|
|
165
|
+
*/
|
|
166
|
+
export: () => null,
|
|
167
|
+
importRegExp: RAW_HTML_REGEX,
|
|
168
|
+
// Never used as a shortcut
|
|
169
|
+
regExp: /(?!)/,
|
|
170
|
+
replace: RAW_SYNTAX.replace,
|
|
171
|
+
type: 'text-match',
|
|
172
|
+
};
|
|
@@ -27,14 +27,19 @@ import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from '
|
|
|
27
27
|
import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
|
|
28
28
|
import { LEADING_INLINE_CODE } from './inline-code.js';
|
|
29
29
|
import { LINK } from './link.js';
|
|
30
|
-
import { RAW_SYNTAX } from './raw-syntax.js';
|
|
30
|
+
import { RAW_HTML, RAW_SYNTAX } from './raw-syntax.js';
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* @import { ElementTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Regular expression to match a delimiter row, like `| --- | :-: |`. Each cell needs a hyphen, so a
|
|
39
|
+
* row of empty cells, like `| | |`, is an empty header row, not a delimiter row.
|
|
40
|
+
* @see https://github.github.com/gfm/#delimiter-row
|
|
41
|
+
*/
|
|
42
|
+
const TABLE_ROW_DIVIDER_REG_EXP = /^(?:\|[ \t]*:?-+:?[ \t]*)+\|\s?$/;
|
|
38
43
|
|
|
39
44
|
/**
|
|
40
45
|
* Returns the number of columns in the table.
|
|
@@ -98,6 +103,7 @@ export const createTableTransformer = (cellTransformers = []) => {
|
|
|
98
103
|
...cellTransformers,
|
|
99
104
|
...TRANSFORMERS.map((transformer) => (transformer === LEXICAL_LINK ? LINK : transformer)),
|
|
100
105
|
LEADING_INLINE_CODE,
|
|
106
|
+
RAW_HTML,
|
|
101
107
|
RAW_SYNTAX,
|
|
102
108
|
];
|
|
103
109
|
|