@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
|
@@ -1,10 +1,136 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
$createTextNode as createTextNode,
|
|
3
|
+
$isParagraphNode as isParagraphNode,
|
|
4
|
+
$isRootNode as isRootNode,
|
|
5
|
+
$isTextNode as isTextNode,
|
|
6
|
+
} from 'lexical';
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* @import { TextMatchTransformer } from '@lexical/markdown';
|
|
5
|
-
* @import { TextNode } from 'lexical';
|
|
10
|
+
* @import { ElementNode, LexicalNode, TextNode } from 'lexical';
|
|
6
11
|
*/
|
|
7
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Check if the given node is inline code made only of whitespace.
|
|
15
|
+
* @param {LexicalNode | null} node Node.
|
|
16
|
+
* @returns {boolean} Result.
|
|
17
|
+
*/
|
|
18
|
+
const isWhitespaceCode = (node) =>
|
|
19
|
+
isTextNode(node) && node.hasFormat('code') && /^\s+$/.test(node.getTextContent());
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get a view of the given node that Lexical can export with the formats of inline code made only
|
|
23
|
+
* of whitespace, like the bold in `` **` `** ``. Lexical takes a node with only whitespace as
|
|
24
|
+
* having no format, as it can’t put the syntax of a format around whitespace, but code is exported
|
|
25
|
+
* with its backticks, around which the syntax can go. So the view, and the views of its siblings,
|
|
26
|
+
* give a placeholder as the text of such code. Lexical only reads the text to tell if it has any,
|
|
27
|
+
* and exports the text it’s given. Other methods are those of the node.
|
|
28
|
+
* @param {TextNode} node Text node.
|
|
29
|
+
* @returns {TextNode} View of the node, or the node itself if neither it nor its siblings are such
|
|
30
|
+
* code.
|
|
31
|
+
*/
|
|
32
|
+
export const getFormatExportNode = (node) => {
|
|
33
|
+
const previous = node.getPreviousSibling();
|
|
34
|
+
const next = node.getNextSibling();
|
|
35
|
+
|
|
36
|
+
if (![node, previous, next].some(isWhitespaceCode)) {
|
|
37
|
+
return node;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get a view of a node with a placeholder as its text, if it’s inline code made only of
|
|
42
|
+
* whitespace.
|
|
43
|
+
* @template {LexicalNode | null} T
|
|
44
|
+
* @param {T} target Node.
|
|
45
|
+
* @returns {T} View or the node itself.
|
|
46
|
+
*/
|
|
47
|
+
const getView = (target) =>
|
|
48
|
+
isWhitespaceCode(target)
|
|
49
|
+
? Object.create(target, {
|
|
50
|
+
getTextContent: {
|
|
51
|
+
/**
|
|
52
|
+
* Get a placeholder instead of the whitespace.
|
|
53
|
+
* @returns {string} Placeholder.
|
|
54
|
+
*/
|
|
55
|
+
value: () => '\uE004',
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
: target;
|
|
59
|
+
|
|
60
|
+
return Object.create(getView(node), {
|
|
61
|
+
getPreviousSibling: {
|
|
62
|
+
/**
|
|
63
|
+
* Get the view of the previous sibling.
|
|
64
|
+
* @returns {LexicalNode | null} View or the sibling itself.
|
|
65
|
+
*/
|
|
66
|
+
value: () => getView(previous),
|
|
67
|
+
},
|
|
68
|
+
getNextSibling: {
|
|
69
|
+
/**
|
|
70
|
+
* Get the view of the next sibling.
|
|
71
|
+
* @returns {LexicalNode | null} View or the sibling itself.
|
|
72
|
+
*/
|
|
73
|
+
value: () => getView(next),
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Check if the given node is a paragraph Lexical takes as empty when it exports the document: one
|
|
80
|
+
* with nothing but a text node of up to 3 whitespace characters, even if the text is code.
|
|
81
|
+
* @param {LexicalNode | null} node Node.
|
|
82
|
+
* @returns {boolean} Result.
|
|
83
|
+
*/
|
|
84
|
+
const isLexicalEmptyParagraph = (node) => {
|
|
85
|
+
if (!isParagraphNode(node)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const child = node.getFirstChild();
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
!child ||
|
|
93
|
+
(node.getChildrenSize() === 1 && isTextNode(child) && /^\s{0,3}$/.test(child.getTextContent()))
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get the line breaks to add around a code node made only of whitespace that is the only content
|
|
99
|
+
* of a paragraph in the document root. Lexical takes such a paragraph as empty, so it puts a single
|
|
100
|
+
* line break, not a blank line, between it and the blocks next to it, which would make one
|
|
101
|
+
* paragraph of them. The line break is added on the side of the code, unless the block next to it
|
|
102
|
+
* is a paragraph Lexical takes as empty too: the line break is added by the one before, if it has
|
|
103
|
+
* code, and isn’t needed otherwise, as an empty paragraph adds a line break of its own.
|
|
104
|
+
* @param {TextNode} node Code node.
|
|
105
|
+
* @returns {[string, string]} Line breaks to add before and after the code.
|
|
106
|
+
*/
|
|
107
|
+
const getParagraphLineBreaks = (node) => {
|
|
108
|
+
const paragraph = node.getParent();
|
|
109
|
+
|
|
110
|
+
if (!isLexicalEmptyParagraph(paragraph) || !isRootNode(paragraph?.getParent())) {
|
|
111
|
+
return ['', ''];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const previous = paragraph?.getPreviousSibling() ?? null;
|
|
115
|
+
const next = paragraph?.getNextSibling() ?? null;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Check if a paragraph is empty, with no code.
|
|
119
|
+
* @param {LexicalNode | null} sibling Sibling.
|
|
120
|
+
* @returns {boolean} Result.
|
|
121
|
+
*/
|
|
122
|
+
const isEmpty = (sibling) => {
|
|
123
|
+
const child = /** @type {ElementNode} */ (sibling)?.getFirstChild();
|
|
124
|
+
|
|
125
|
+
return isLexicalEmptyParagraph(sibling) && !(isTextNode(child) && child.hasFormat('code'));
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return [
|
|
129
|
+
previous && !isLexicalEmptyParagraph(previous) ? '\n' : '',
|
|
130
|
+
next && !isEmpty(next) ? '\n' : '',
|
|
131
|
+
];
|
|
132
|
+
};
|
|
133
|
+
|
|
8
134
|
/**
|
|
9
135
|
* Import-only transformer for a code span at the start of a node, such as a table cell, a
|
|
10
136
|
* paragraph or bold text starting with inline code. Lexical’s `INLINE_CODE` transformer then reuses
|
|
@@ -12,17 +138,56 @@ import { $createTextNode as createTextNode } from 'lexical';
|
|
|
12
138
|
* been imported, so `` `C:\$MFT` `` would become `C:$MFT`, though backslashes are literal in a code
|
|
13
139
|
* span. Backslashes in a code span split from the text before it are kept. A text match wrapping a
|
|
14
140
|
* code span takes priority over it, and this one replaces the node with a new one, leaving the old
|
|
15
|
-
* node, which is no longer in the document, to be unescaped.
|
|
141
|
+
* node, which is no longer in the document, to be unescaped. It also exports a code span that
|
|
142
|
+
* starts and ends with whitespace, which Lexical pads with a space on both sides, though CommonMark
|
|
143
|
+
* only strips that padding if both ends are spaces and the content isn’t all spaces, so `` ` ` ``
|
|
144
|
+
* would get two spaces longer on every save, and `` `\t` `` would become `` ` \t ` ``.
|
|
145
|
+
* It exports code next to inline code made only of whitespace too; see `getFormatExportNode()`.
|
|
16
146
|
* @type {TextMatchTransformer}
|
|
17
147
|
* @see https://github.com/sveltia/sveltia-cms/issues/1100
|
|
148
|
+
* @see https://spec.commonmark.org/0.31.2/#code-spans
|
|
18
149
|
*/
|
|
19
150
|
export const LEADING_INLINE_CODE = {
|
|
20
151
|
dependencies: [],
|
|
21
152
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
153
|
+
* Export a code span that starts and ends with whitespace without the padding Lexical adds, if
|
|
154
|
+
* CommonMark wouldn’t strip it, and with the line breaks around it if it’s the only content of a
|
|
155
|
+
* paragraph; see `getParagraphLineBreaks()`. Lexical exports the other code spans.
|
|
156
|
+
* @param {LexicalNode} node Node.
|
|
157
|
+
* @param {(node: ElementNode) => string} _exportChildren Function to export the children of an
|
|
158
|
+
* element, unused.
|
|
159
|
+
* @param {(node: TextNode, textContent: string) => string} exportFormat Function to export a text
|
|
160
|
+
* node with its format.
|
|
161
|
+
* @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
|
|
24
162
|
*/
|
|
25
|
-
export: () =>
|
|
163
|
+
export: (node, _exportChildren, exportFormat) => {
|
|
164
|
+
if (!isTextNode(node) || !node.hasFormat('code')) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const content = node.getTextContent();
|
|
169
|
+
const exportNode = getFormatExportNode(node);
|
|
170
|
+
const [before, after] = isWhitespaceCode(node) ? getParagraphLineBreaks(node) : ['', ''];
|
|
171
|
+
|
|
172
|
+
// Lexical pads code that starts and ends with whitespace, though CommonMark only strips one
|
|
173
|
+
// space from both ends, if the code isn’t all spaces
|
|
174
|
+
const hasExtraPadding =
|
|
175
|
+
!content.includes('`') &&
|
|
176
|
+
/^\s/.test(content) &&
|
|
177
|
+
/\s$/.test(content) &&
|
|
178
|
+
!(content.startsWith(' ') && content.endsWith(' ') && /[^ ]/.test(content));
|
|
179
|
+
|
|
180
|
+
if (!hasExtraPadding && exportNode === node && !before && !after) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const markdown = exportFormat(exportNode, content);
|
|
185
|
+
|
|
186
|
+
// The code is wrapped with the syntax of its other formats, if any, which has no whitespace
|
|
187
|
+
return `${before}${
|
|
188
|
+
hasExtraPadding ? markdown.replace(`\` ${content} \``, () => `\`${content}\``) : markdown
|
|
189
|
+
}${after}`;
|
|
190
|
+
},
|
|
26
191
|
// A code span as Lexical’s `INLINE_CODE` transformer reads it: from a backtick run to the next
|
|
27
192
|
// run of the same length
|
|
28
193
|
// @see https://spec.commonmark.org/0.31.2/#code-spans
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Link transformer that exports the formats of the whole link text outside the link, where they’re
|
|
3
|
+
* imported from; see `getOuterFormats()`. The rest is Lexical’s.
|
|
4
|
+
* @type {TextMatchTransformer}
|
|
5
|
+
*/
|
|
6
|
+
export const LINK: TextMatchTransformer;
|
|
7
|
+
import type { TextMatchTransformer } from '@lexical/markdown';
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { $isLinkNode as isLinkNode } from '@lexical/link';
|
|
2
|
+
import { LINK as LEXICAL_LINK } from '@lexical/markdown';
|
|
3
|
+
import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { TextMatchTransformer } from '@lexical/markdown';
|
|
7
|
+
* @import { ElementNode, LexicalNode, TextFormatType, TextNode } from 'lexical';
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Text formats that can be moved out of a link, with the Markdown delimiters Lexical can export
|
|
12
|
+
* for each. Inline code can’t, as it would make the link text code.
|
|
13
|
+
* @type {Partial<Record<TextFormatType, string[]>>}
|
|
14
|
+
*/
|
|
15
|
+
const FORMAT_DELIMITERS = {
|
|
16
|
+
bold: ['**', '__'],
|
|
17
|
+
italic: ['_', '*'],
|
|
18
|
+
strikethrough: ['~~'],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Regular expression to match a format delimiter at the start of a string.
|
|
23
|
+
*/
|
|
24
|
+
const DELIMITER_REGEX = /^(?:\*\*|__|~~|\*|_)/;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get the text of a node as exported: inline code starts and ends with a backtick, even if its text
|
|
28
|
+
* is only whitespace.
|
|
29
|
+
* @param {LexicalNode | null | undefined} node Node.
|
|
30
|
+
* @returns {string} Text.
|
|
31
|
+
*/
|
|
32
|
+
const getExportedText = (node) =>
|
|
33
|
+
isTextNode(node) && node.hasFormat('code') ? '`' : (node?.getTextContent() ?? '');
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get the formats a link can be wrapped with instead of having them in its text, like `**[a](b)**`
|
|
37
|
+
* instead of `[**a**](b)`: the ones every text node in the link has. Lexical puts the delimiters in
|
|
38
|
+
* the link text, so the Markdown would change every time it’s exported. They’re only moved out if
|
|
39
|
+
* the link text is all text with no whitespace at either end, other than in inline code, and the
|
|
40
|
+
* character on either side of the link is whitespace or punctuation, which the delimiters need to
|
|
41
|
+
* open and close emphasis there. If the text next to the link has any of the formats, it must be
|
|
42
|
+
* whitespace, or the delimiters of the link and the text would meet.
|
|
43
|
+
* @param {ElementNode} node Link node.
|
|
44
|
+
* @returns {TextFormatType[]} Formats.
|
|
45
|
+
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
|
|
46
|
+
*/
|
|
47
|
+
const getOuterFormats = (node) => {
|
|
48
|
+
const children = node.getChildren();
|
|
49
|
+
|
|
50
|
+
if (
|
|
51
|
+
!children.length ||
|
|
52
|
+
!children.every((child) => isTextNode(child) && /\S/.test(getExportedText(child))) ||
|
|
53
|
+
/^\s/.test(getExportedText(children[0])) ||
|
|
54
|
+
/\s$/.test(getExportedText(children.at(-1)))
|
|
55
|
+
) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const formats = /** @type {TextFormatType[]} */ (Object.keys(FORMAT_DELIMITERS)).filter(
|
|
60
|
+
(format) => children.every((child) => /** @type {TextNode} */ (child).hasFormat(format)),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Check if the given sibling of the link allows the delimiters next to the link.
|
|
65
|
+
* @param {LexicalNode | null} sibling Previous or next sibling.
|
|
66
|
+
* @param {(text: string) => string | undefined} getChar Function to get the character next to
|
|
67
|
+
* the link from the sibling’s text.
|
|
68
|
+
* @returns {boolean} Result.
|
|
69
|
+
*/
|
|
70
|
+
const isSeparated = (sibling, getChar) => {
|
|
71
|
+
if (!sibling || isLineBreakNode(sibling)) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!isTextNode(sibling)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const char = getChar(getExportedText(sibling)) ?? '';
|
|
80
|
+
|
|
81
|
+
// Lexical puts the delimiters of the text inside its whitespace, which keeps them apart
|
|
82
|
+
return formats.some((format) => sibling.hasFormat(format))
|
|
83
|
+
? /\s/.test(char)
|
|
84
|
+
: /[\s\p{P}\p{S}]/u.test(char);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return formats.length &&
|
|
88
|
+
isSeparated(node.getPreviousSibling(), (text) => text.at(-1)) &&
|
|
89
|
+
isSeparated(node.getNextSibling(), (text) => text[0])
|
|
90
|
+
? formats
|
|
91
|
+
: [];
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Remove the given formats’ delimiters from either end of the exported link text, where Lexical
|
|
96
|
+
* puts them when every text node in the link has the formats.
|
|
97
|
+
* @param {string} markdown Exported link text.
|
|
98
|
+
* @param {TextFormatType[]} formats Formats.
|
|
99
|
+
* @returns {{ text: string, delimiters: string } | undefined} The text without the delimiters, and
|
|
100
|
+
* the opening delimiters, or `undefined` if the text doesn’t start and end with them.
|
|
101
|
+
*/
|
|
102
|
+
const stripDelimiters = (markdown, formats) => {
|
|
103
|
+
const remaining = [...formats];
|
|
104
|
+
let delimiters = '';
|
|
105
|
+
|
|
106
|
+
while (remaining.length) {
|
|
107
|
+
const [delimiter] = markdown.slice(delimiters.length).match(DELIMITER_REGEX) ?? [];
|
|
108
|
+
|
|
109
|
+
const index = remaining.findIndex((format) =>
|
|
110
|
+
FORMAT_DELIMITERS[format]?.includes(delimiter ?? ''),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
if (!delimiter || index === -1) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
remaining.splice(index, 1);
|
|
118
|
+
delimiters += delimiter;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const closing = [...delimiters].reverse().join('');
|
|
122
|
+
|
|
123
|
+
if (markdown.length < delimiters.length * 2 || !markdown.endsWith(closing)) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { text: markdown.slice(delimiters.length, -closing.length), delimiters };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Link transformer that exports the formats of the whole link text outside the link, where they’re
|
|
132
|
+
* imported from; see `getOuterFormats()`. The rest is Lexical’s.
|
|
133
|
+
* @type {TextMatchTransformer}
|
|
134
|
+
*/
|
|
135
|
+
export const LINK = {
|
|
136
|
+
...LEXICAL_LINK,
|
|
137
|
+
/**
|
|
138
|
+
* Export a link node.
|
|
139
|
+
* @param {LexicalNode} node Node.
|
|
140
|
+
* @param {(node: ElementNode) => string} exportChildren Function to export the children of an
|
|
141
|
+
* element.
|
|
142
|
+
* @param {(node: TextNode, textContent: string) => string} exportFormat Function to export a text
|
|
143
|
+
* node with its format.
|
|
144
|
+
* @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
|
|
145
|
+
*/
|
|
146
|
+
export: (node, exportChildren, exportFormat) => {
|
|
147
|
+
const formats = isLinkNode(node) ? getOuterFormats(node) : [];
|
|
148
|
+
/** @type {string | undefined} */
|
|
149
|
+
let delimiters;
|
|
150
|
+
|
|
151
|
+
const markdown = LEXICAL_LINK.export?.(
|
|
152
|
+
node,
|
|
153
|
+
(linkNode) => {
|
|
154
|
+
const text = exportChildren(linkNode);
|
|
155
|
+
const stripped = formats.length ? stripDelimiters(text, formats) : undefined;
|
|
156
|
+
|
|
157
|
+
delimiters = stripped?.delimiters;
|
|
158
|
+
|
|
159
|
+
return stripped?.text ?? text;
|
|
160
|
+
},
|
|
161
|
+
exportFormat,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return markdown && delimiters
|
|
165
|
+
? `${delimiters}${markdown}${[...delimiters].reverse().join('')}`
|
|
166
|
+
: (markdown ?? null);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function getRawSyntax(node: TextNode): string | undefined;
|
|
2
|
+
/**
|
|
3
|
+
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
4
|
+
* support, like a list in a blockquote, so it’s exported as is, while the same text typed by the
|
|
5
|
+
* user, pasted as text, or imported from escaped Markdown, like `\- a`, is escaped; see
|
|
6
|
+
* `BLOCK_SYNTAX_ESCAPE`. Without it, the syntax from the Markdown would be escaped, changing the
|
|
7
|
+
* meaning of the Markdown.
|
|
8
|
+
* @type {TextMatchTransformer}
|
|
9
|
+
*/
|
|
10
|
+
export const RAW_SYNTAX: TextMatchTransformer;
|
|
11
|
+
import type { TextNode } from 'lexical';
|
|
12
|
+
import type { TextMatchTransformer } from '@lexical/markdown';
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { $getState as getState, $setState as setState, createState } from 'lexical';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @import { TextMatchTransformer } from '@lexical/markdown';
|
|
5
|
+
* @import { TextNode } from 'lexical';
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Block syntax at the start of a line, after up to 3 spaces.
|
|
10
|
+
*/
|
|
11
|
+
const BLOCK_SYNTAX = [
|
|
12
|
+
// Thematic break, which comes first as it can start like a bullet list item
|
|
13
|
+
/([-*_])(?:[ \t]*\1){2,}[ \t]*$/,
|
|
14
|
+
// Setext heading underline
|
|
15
|
+
/(?:=+|-{2,})[ \t]*$/,
|
|
16
|
+
// ATX heading, bullet list item or ordered list item
|
|
17
|
+
/(?:#{1,6}|[-+*]|\d{1,9}[.)])(?=[ \t]|$)/,
|
|
18
|
+
// Blockquote or code fence
|
|
19
|
+
/>|`{3,}|~{3,}/,
|
|
20
|
+
]
|
|
21
|
+
.map(({ source }) => source)
|
|
22
|
+
.join('|');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Regular expression to match the Markdown syntax that the editor imports as text, but that has to
|
|
26
|
+
* be exported as is: the block syntax at the start of a line, which can’t be nested in a blockquote
|
|
27
|
+
* or a list item, or isn’t supported, like the `)` ordered list delimiter.
|
|
28
|
+
*/
|
|
29
|
+
const RAW_SYNTAX_REGEX = new RegExp(`^ {0,3}(?:${BLOCK_SYNTAX})`);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* State of a text node imported from the {@link RAW_SYNTAX_REGEX raw syntax}, holding the Markdown
|
|
33
|
+
* it was imported from.
|
|
34
|
+
*/
|
|
35
|
+
const rawSyntaxState = createState('suiRawSyntax', {
|
|
36
|
+
/**
|
|
37
|
+
* Parse the state value.
|
|
38
|
+
* @param {unknown} value Value.
|
|
39
|
+
* @returns {string} Markdown, or an empty string if the node is not raw syntax.
|
|
40
|
+
*/
|
|
41
|
+
parse: (value) => (typeof value === 'string' ? value : ''),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the Markdown of a text node imported from raw syntax.
|
|
46
|
+
* @param {TextNode} node Text node.
|
|
47
|
+
* @returns {string | undefined} The Markdown the node was imported from, or its current text if it
|
|
48
|
+
* has been edited since. `undefined` if the node is not raw syntax.
|
|
49
|
+
*/
|
|
50
|
+
export const getRawSyntax = (node) => {
|
|
51
|
+
const markdown = getState(node, rawSyntaxState);
|
|
52
|
+
|
|
53
|
+
if (!markdown) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The block syntax has no escapes, so the text is the Markdown, even if it has been edited since
|
|
58
|
+
return node.getTextContent();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
63
|
+
* support, like a list in a blockquote, so it’s exported as is, while the same text typed by the
|
|
64
|
+
* user, pasted as text, or imported from escaped Markdown, like `\- a`, is escaped; see
|
|
65
|
+
* `BLOCK_SYNTAX_ESCAPE`. Without it, the syntax from the Markdown would be escaped, changing the
|
|
66
|
+
* meaning of the Markdown.
|
|
67
|
+
* @type {TextMatchTransformer}
|
|
68
|
+
*/
|
|
69
|
+
export const RAW_SYNTAX = {
|
|
70
|
+
dependencies: [],
|
|
71
|
+
/**
|
|
72
|
+
* Leave the export to `BLOCK_SYNTAX_ESCAPE`.
|
|
73
|
+
* @returns {null} Nothing.
|
|
74
|
+
*/
|
|
75
|
+
export: () => null,
|
|
76
|
+
importRegExp: RAW_SYNTAX_REGEX,
|
|
77
|
+
// Never used as a shortcut
|
|
78
|
+
regExp: /(?!)/,
|
|
79
|
+
/**
|
|
80
|
+
* Mark the text node as raw syntax. The node is left out of the other transformers, as nothing
|
|
81
|
+
* in the syntax is converted.
|
|
82
|
+
* @param {TextNode} node Text node with the matched text.
|
|
83
|
+
*/
|
|
84
|
+
replace: (node) => {
|
|
85
|
+
setState(node, rawSyntaxState, node.getTextContent());
|
|
86
|
+
},
|
|
87
|
+
type: 'text-match',
|
|
88
|
+
};
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import {
|
|
8
8
|
$convertFromMarkdownString as convertFromMarkdownString,
|
|
9
9
|
$convertToMarkdownString as convertToMarkdownString,
|
|
10
|
+
LINK as LEXICAL_LINK,
|
|
10
11
|
TRANSFORMERS,
|
|
11
12
|
} from '@lexical/markdown';
|
|
12
13
|
import {
|
|
@@ -25,6 +26,8 @@ import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from '
|
|
|
25
26
|
|
|
26
27
|
import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
|
|
27
28
|
import { LEADING_INLINE_CODE } from './inline-code.js';
|
|
29
|
+
import { LINK } from './link.js';
|
|
30
|
+
import { RAW_SYNTAX } from './raw-syntax.js';
|
|
28
31
|
|
|
29
32
|
/**
|
|
30
33
|
* @import { ElementTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
|
|
@@ -91,7 +94,13 @@ export const mapToTableCells = (textContent, transformers) => {
|
|
|
91
94
|
*/
|
|
92
95
|
export const createTableTransformer = (cellTransformers = []) => {
|
|
93
96
|
/** @type {Transformer[]} */
|
|
94
|
-
const transformers = [
|
|
97
|
+
const transformers = [
|
|
98
|
+
...cellTransformers,
|
|
99
|
+
...TRANSFORMERS.map((transformer) => (transformer === LEXICAL_LINK ? LINK : transformer)),
|
|
100
|
+
LEADING_INLINE_CODE,
|
|
101
|
+
RAW_SYNTAX,
|
|
102
|
+
];
|
|
103
|
+
|
|
95
104
|
/** @type {Transformer[]} */
|
|
96
105
|
const exportTransformers = [...transformers, BLOCK_SYNTAX_ESCAPE];
|
|
97
106
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltia/ui",
|
|
3
|
-
"version": "0.87.
|
|
3
|
+
"version": "0.87.3",
|
|
4
4
|
"description": "A collection of Svelte components and utilities for building user interfaces.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"eslint-plugin-svelte": "^3.23.1",
|
|
87
87
|
"globals": "^17.13.0",
|
|
88
88
|
"happy-dom": "^21.0.3",
|
|
89
|
-
"oxlint": "^1.
|
|
89
|
+
"oxlint": "^1.89.0",
|
|
90
90
|
"playwright": "^1.64.1",
|
|
91
91
|
"postcss": "^8.5.31",
|
|
92
92
|
"postcss-html": "^2.0.0",
|