Skip to content

Commit 7d4a904

Browse files
committed
chore: fix tests, add new tests
1 parent 24cc82a commit 7d4a904

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

packages/lexical-markdown/src/MarkdownImport.ts

+34-11
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,41 @@ function $importMultiline(
128128
while (endLineIndex < linesLength) {
129129
const closeMatch = lines[endLineIndex].match(regExpEnd);
130130

131-
// all lines between the open and close match
132-
const linesInBetween =
133-
startLineIndex !== endLineIndex
134-
? lines.slice(startLineIndex + 1, endLineIndex)
135-
: [
136-
lines[startLineIndex].slice(
137-
openMatch[0].length - 1,
138-
-closeMatch[0].length - 1,
139-
),
140-
];
141-
142131
if (closeMatch) {
132+
// everything between the open and close match, not including the matches, split up by lines
133+
const linesInBetween = [];
134+
135+
if (startLineIndex === endLineIndex) {
136+
// In case the end regex matches the same thing as the start regex, we need to continue searching - this is not the actual end
137+
if (closeMatch.index === endLineIndex) {
138+
endLineIndex++;
139+
continue;
140+
}
141+
142+
linesInBetween.push(
143+
lines[startLineIndex].slice(
144+
openMatch[0].length,
145+
-closeMatch[0].length,
146+
),
147+
);
148+
} else {
149+
for (let i = startLineIndex; i <= endLineIndex; i++) {
150+
if (i === startLineIndex) {
151+
const text = lines[i].slice(openMatch[0].length);
152+
if (text.length) {
153+
linesInBetween.push(text);
154+
}
155+
} else if (i === endLineIndex) {
156+
const text = lines[i].slice(0, -closeMatch[0].length);
157+
if (text.length) {
158+
linesInBetween.push(text);
159+
}
160+
} else {
161+
linesInBetween.push(lines[i]);
162+
}
163+
}
164+
}
165+
143166
if (
144167
replace(rootNode, openMatch, closeMatch, linesInBetween) !== false
145168
) {

packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const MDX_HTML_TRANSFORMER: MultilineElementTransformer = {
3535
}
3636
return null; // Run next transformer
3737
},
38-
regExpEnd: /<(\w+)[^>]*>/,
39-
regExpStart: /<\/(\w+)>/,
38+
regExpEnd: /<\/(\w+)\s*>/,
39+
regExpStart: /<(\w+)[^>]*>/,
4040
replace: (rootNode, openMatch, closeMatch, linesInBetween) => {
4141
if (openMatch[1] === 'MyComponent') {
4242
const codeBlockNode = $createCodeNode(openMatch[1]);
@@ -99,7 +99,7 @@ describe('Markdown', () => {
9999
md: '> Hello\n> world!',
100100
},
101101
{
102-
// Miltiline list items
102+
// Multiline list items
103103
html: '<ul><li value="1"><span style="white-space: pre-wrap;">Hello</span></li><li value="2"><span style="white-space: pre-wrap;">world</span><br><span style="white-space: pre-wrap;">!</span><br><span style="white-space: pre-wrap;">!</span></li></ul>',
104104
md: '- Hello\n- world\n!\n!',
105105
},
@@ -213,6 +213,11 @@ describe('Markdown', () => {
213213
md: 'Hello ~~__*world*__~~!',
214214
skipExport: true,
215215
},
216+
/*{
217+
html: '<pre spellcheck="false"><span style="white-space: pre-wrap;">Inline Code</span></pre>',
218+
md: '```Inline Code```',
219+
},*/
220+
// TODO: This test currently fails. Fix it
216221
{
217222
html: '<pre spellcheck="false"><span style="white-space: pre-wrap;">Code</span></pre>',
218223
md: '```\nCode\n```',
@@ -267,6 +272,11 @@ describe('Markdown', () => {
267272
html: '<p><span style="white-space: pre-wrap;">Some HTML in mdx:</span></p><pre spellcheck="false" data-language="MyComponent"><span style="white-space: pre-wrap;">From HTML: Some Text</span></pre>',
268273
md: 'Some HTML in mdx:\n\n<MyComponent>Some Text</MyComponent>',
269274
},
275+
{
276+
customTransformers: [MDX_HTML_TRANSFORMER],
277+
html: '<p><span style="white-space: pre-wrap;">Some HTML in mdx:</span></p><pre spellcheck="false" data-language="MyComponent"><span style="white-space: pre-wrap;">From HTML: Line 1\nSome Text</span></pre>',
278+
md: 'Some HTML in mdx:\n\n<MyComponent>Line 1\nSome Text</MyComponent>',
279+
},
270280
];
271281

272282
const HIGHLIGHT_TEXT_MATCH_IMPORT: TextMatchTransformer = {

0 commit comments

Comments
 (0)