@sveltia/ui 0.87.2 → 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/constants.js +3 -2
- package/dist/components/text-editor/core.d.ts +1 -0
- package/dist/components/text-editor/core.js +224 -7
- package/dist/components/text-editor/markdown.d.ts +32 -0
- package/dist/components/text-editor/markdown.js +576 -64
- 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 +45 -0
- package/dist/components/text-editor/transformers/raw-syntax.js +172 -0
- package/dist/components/text-editor/transformers/table.js +17 -2
- package/dist/shiki-engine.js +5 -5
- package/package.json +7 -7
|
@@ -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
|
|
|
@@ -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>;
|
|
@@ -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,16 @@ 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 {
|
|
118
|
+
getImportedRawSyntax,
|
|
119
|
+
HTML_BLOCK_PLACEHOLDER,
|
|
120
|
+
INLINE_HTML_REGEX,
|
|
121
|
+
RAW_HTML,
|
|
122
|
+
RAW_SYNTAX,
|
|
123
|
+
} from './transformers/raw-syntax.js';
|
|
114
124
|
import { $getSelectedAlignment } from './alignment.js';
|
|
115
125
|
import {
|
|
116
126
|
$ensureParagraphAfterTable,
|
|
@@ -900,6 +910,160 @@ const registerTabIndentation = (editor) =>
|
|
|
900
910
|
};
|
|
901
911
|
});
|
|
902
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
|
+
|
|
903
1067
|
/**
|
|
904
1068
|
* Initialize the Lexical editor.
|
|
905
1069
|
* @param {TextEditorConfig} config Editor configuration.
|
|
@@ -976,11 +1140,16 @@ export const initEditor = ({
|
|
|
976
1140
|
},
|
|
977
1141
|
});
|
|
978
1142
|
|
|
979
|
-
const componentTransformers = components.map(({ transformer }) =>
|
|
1143
|
+
const componentTransformers = components.map(({ transformer }) =>
|
|
1144
|
+
removePlaceholdersFromMatch(transformer),
|
|
1145
|
+
);
|
|
980
1146
|
|
|
981
1147
|
/** @type {Transformer[]} */
|
|
982
1148
|
const enabledTransformers = [
|
|
983
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]),
|
|
984
1153
|
...new Set(
|
|
985
1154
|
Object.entries(TRANSFORMER_MAP)
|
|
986
1155
|
.filter(([button]) => enabledButtons.includes(/** @type {TextEditorNodeType} */ (button)))
|
|
@@ -998,6 +1167,8 @@ export const initEditor = ({
|
|
|
998
1167
|
componentTransformers.filter(({ type }) => type === 'text-match')
|
|
999
1168
|
),
|
|
1000
1169
|
),
|
|
1170
|
+
// Last, so the syntax of the editor’s own comes first
|
|
1171
|
+
RAW_SYNTAX,
|
|
1001
1172
|
]),
|
|
1002
1173
|
];
|
|
1003
1174
|
|
|
@@ -1154,6 +1325,39 @@ const importValue = async (editor, { languages, $import, $export, formatName })
|
|
|
1154
1325
|
return editor.read($export);
|
|
1155
1326
|
};
|
|
1156
1327
|
|
|
1328
|
+
/**
|
|
1329
|
+
* Remove the placeholders added by `addImportPlaceholders()` from the imported nodes: the text, the
|
|
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`.
|
|
1333
|
+
*/
|
|
1334
|
+
const removePlaceholders = () => {
|
|
1335
|
+
getRoot()
|
|
1336
|
+
.getAllTextNodes()
|
|
1337
|
+
.forEach((node) => {
|
|
1338
|
+
const text = node.getTextContent();
|
|
1339
|
+
const newText = removeImportPlaceholders(getImportedRawSyntax(node) ?? text);
|
|
1340
|
+
const link = node.getParent();
|
|
1341
|
+
|
|
1342
|
+
if (newText !== text) {
|
|
1343
|
+
node.setTextContent(newText);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
if (isLinkNode(link)) {
|
|
1347
|
+
const url = link.getURL();
|
|
1348
|
+
const title = link.getTitle();
|
|
1349
|
+
|
|
1350
|
+
if (removeImportPlaceholders(url) !== url) {
|
|
1351
|
+
link.setURL(removeImportPlaceholders(url));
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
if (title && removeImportPlaceholders(title) !== title) {
|
|
1355
|
+
link.setTitle(removeImportPlaceholders(title));
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
});
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1157
1361
|
/**
|
|
1158
1362
|
* Convert Markdown to Lexical nodes.
|
|
1159
1363
|
* @param {LexicalEditor} editor Editor instance.
|
|
@@ -1182,19 +1386,32 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1182
1386
|
const normalizedValue = hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
|
|
1183
1387
|
|
|
1184
1388
|
return importValue(editor, {
|
|
1185
|
-
|
|
1389
|
+
// The language is the leading word of the info string, like `js` in ```` ``` js title=x ````;
|
|
1390
|
+
// see `CODE`
|
|
1391
|
+
languages: [...normalizedValue.matchAll(/^`{3,}[ \t]*(?<lang>[\w-]+)/gm)].map(
|
|
1186
1392
|
({ groups: { lang = 'plain' } = {} }) => lang,
|
|
1187
1393
|
),
|
|
1188
1394
|
/**
|
|
1189
1395
|
* Import the Markdown.
|
|
1190
1396
|
*/
|
|
1191
1397
|
$import: () => {
|
|
1398
|
+
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
1399
|
+
const splitValue = splitMultilineFormatting(normalizedValue);
|
|
1400
|
+
// Add placeholders so Lexical reads the backslashes, the tabs in code spans and raw HTML
|
|
1401
|
+
// right
|
|
1402
|
+
const markedValue = addImportPlaceholders(splitValue);
|
|
1403
|
+
|
|
1192
1404
|
convertFromMarkdownString(
|
|
1193
|
-
//
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
enabledTransformers,
|
|
1405
|
+
// Pad blank blockquote lines so they are not imported as literal `>` text
|
|
1406
|
+
padBlankBlockquoteLines(markedValue),
|
|
1407
|
+
// Keep the formatting around a code span after other formatting
|
|
1408
|
+
addCodeSpanSplit(enabledTransformers),
|
|
1197
1409
|
);
|
|
1410
|
+
|
|
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)) {
|
|
1413
|
+
removePlaceholders();
|
|
1414
|
+
}
|
|
1198
1415
|
},
|
|
1199
1416
|
/**
|
|
1200
1417
|
* 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
|
+
};
|