@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
|
@@ -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,45 @@
|
|
|
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;
|
|
21
|
+
export function getRawSyntax(node: TextNode): string | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
24
|
+
* support, like a list in a blockquote, so it’s exported as is, while the same text typed by the
|
|
25
|
+
* user, pasted as text, or imported from escaped Markdown, like `\- a`, is escaped; see
|
|
26
|
+
* `BLOCK_SYNTAX_ESCAPE`. Without it, the syntax from the Markdown would be escaped, changing the
|
|
27
|
+
* meaning of the Markdown.
|
|
28
|
+
* @type {TextMatchTransformer}
|
|
29
|
+
*/
|
|
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;
|
|
44
|
+
import type { TextNode } from 'lexical';
|
|
45
|
+
import type { TextMatchTransformer } from '@lexical/markdown';
|
|
@@ -0,0 +1,172 @@
|
|
|
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
|
+
* 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
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Block syntax at the start of a line, after up to 3 spaces.
|
|
53
|
+
*/
|
|
54
|
+
const BLOCK_SYNTAX = [
|
|
55
|
+
// Thematic break, which comes first as it can start like a bullet list item
|
|
56
|
+
/([-*_])(?:[ \t]*\1){2,}[ \t]*$/,
|
|
57
|
+
// Setext heading underline
|
|
58
|
+
/(?:=+|-{2,})[ \t]*$/,
|
|
59
|
+
// ATX heading, bullet list item or ordered list item
|
|
60
|
+
/(?:#{1,6}|[-+*]|\d{1,9}[.)])(?=[ \t]|$)/,
|
|
61
|
+
// Blockquote or code fence
|
|
62
|
+
/>|`{3,}|~{3,}/,
|
|
63
|
+
]
|
|
64
|
+
.map(({ source }) => source)
|
|
65
|
+
.join('|');
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Regular expression to match the Markdown syntax that the editor imports as text, but that has to
|
|
69
|
+
* be exported as is: the block syntax at the start of a line, which can’t be nested in a blockquote
|
|
70
|
+
* or a list item, or isn’t supported, like the `)` ordered list delimiter.
|
|
71
|
+
*/
|
|
72
|
+
const RAW_SYNTAX_REGEX = new RegExp(`^ {0,3}(?:${BLOCK_SYNTAX})`);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* State of a text node imported from the {@link RAW_SYNTAX_REGEX raw syntax}, holding the Markdown
|
|
76
|
+
* it was imported from.
|
|
77
|
+
*/
|
|
78
|
+
const rawSyntaxState = createState('suiRawSyntax', {
|
|
79
|
+
/**
|
|
80
|
+
* Parse the state value.
|
|
81
|
+
* @param {unknown} value Value.
|
|
82
|
+
* @returns {string} Markdown, or an empty string if the node is not raw syntax.
|
|
83
|
+
*/
|
|
84
|
+
parse: (value) => (typeof value === 'string' ? value : ''),
|
|
85
|
+
});
|
|
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
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get the Markdown of a text node imported from raw syntax.
|
|
96
|
+
* @param {TextNode} node Text node.
|
|
97
|
+
* @returns {string | undefined} The Markdown the node was imported from, or its current text if it
|
|
98
|
+
* has been edited since. `undefined` if the node is not raw syntax.
|
|
99
|
+
*/
|
|
100
|
+
export const getRawSyntax = (node) => {
|
|
101
|
+
const markdown = getState(node, rawSyntaxState);
|
|
102
|
+
|
|
103
|
+
if (!markdown) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Raw syntax has no escapes, so the text is the Markdown, even if it has been edited since
|
|
108
|
+
return node.getTextContent();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
113
|
+
* support, like a list in a blockquote, so it’s exported as is, while the same text typed by the
|
|
114
|
+
* user, pasted as text, or imported from escaped Markdown, like `\- a`, is escaped; see
|
|
115
|
+
* `BLOCK_SYNTAX_ESCAPE`. Without it, the syntax from the Markdown would be escaped, changing the
|
|
116
|
+
* meaning of the Markdown.
|
|
117
|
+
* @type {TextMatchTransformer}
|
|
118
|
+
*/
|
|
119
|
+
export const RAW_SYNTAX = {
|
|
120
|
+
dependencies: [],
|
|
121
|
+
/**
|
|
122
|
+
* Leave the export to `BLOCK_SYNTAX_ESCAPE`.
|
|
123
|
+
* @returns {null} Nothing.
|
|
124
|
+
*/
|
|
125
|
+
export: () => null,
|
|
126
|
+
importRegExp: RAW_SYNTAX_REGEX,
|
|
127
|
+
// Never used as a shortcut
|
|
128
|
+
regExp: /(?!)/,
|
|
129
|
+
/**
|
|
130
|
+
* Mark the text node as raw syntax. The node is left out of the other transformers, as nothing
|
|
131
|
+
* in the syntax is converted.
|
|
132
|
+
* @param {TextNode} node Text node with the matched text.
|
|
133
|
+
*/
|
|
134
|
+
replace: (node) => {
|
|
135
|
+
setState(node, rawSyntaxState, node.getTextContent());
|
|
136
|
+
},
|
|
137
|
+
type: 'text-match',
|
|
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
|
+
};
|
|
@@ -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,13 +26,20 @@ 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_HTML, RAW_SYNTAX } from './raw-syntax.js';
|
|
28
31
|
|
|
29
32
|
/**
|
|
30
33
|
* @import { ElementTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
|
|
31
34
|
*/
|
|
32
35
|
|
|
33
36
|
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
|
|
34
|
-
|
|
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?$/;
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
45
|
* Returns the number of columns in the table.
|
|
@@ -91,7 +99,14 @@ export const mapToTableCells = (textContent, transformers) => {
|
|
|
91
99
|
*/
|
|
92
100
|
export const createTableTransformer = (cellTransformers = []) => {
|
|
93
101
|
/** @type {Transformer[]} */
|
|
94
|
-
const transformers = [
|
|
102
|
+
const transformers = [
|
|
103
|
+
...cellTransformers,
|
|
104
|
+
...TRANSFORMERS.map((transformer) => (transformer === LEXICAL_LINK ? LINK : transformer)),
|
|
105
|
+
LEADING_INLINE_CODE,
|
|
106
|
+
RAW_HTML,
|
|
107
|
+
RAW_SYNTAX,
|
|
108
|
+
];
|
|
109
|
+
|
|
95
110
|
/** @type {Transformer[]} */
|
|
96
111
|
const exportTransformers = [...transformers, BLOCK_SYNTAX_ESCAPE];
|
|
97
112
|
|