Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Nov 5, 2024
1 parent cf83a4c commit 9ae0ef9
Showing 1 changed file with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {$createCodeNode, CodeNode} from '@lexical/code';
import {createHeadlessEditor} from '@lexical/headless';
import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html';
import {LinkNode} from '@lexical/link';
import {$createLinkNode, LinkNode} from '@lexical/link';
import {ListItemNode, ListNode} from '@lexical/list';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {$createTextNode, $getRoot, $insertNodes} from 'lexical';
Expand All @@ -28,6 +28,51 @@ import {
normalizeMarkdown,
} from '../../MarkdownTransformers';

const SIMPLE_INLINE_JSX_MATCHER: TextMatchTransformer = {
dependencies: [LinkNode],
getEndIndex(node, match) {
// Find the closing tag. Count the number of opening and closing tags to find the correct closing tag.
// For simplicity, this will only count the opening and closing tags without checking for "MyTag" specifically.
let openedSubStartMatches = 0;
const start = (match.index ?? 0) + match[0].length;
let endIndex = start;
const line = node.getTextContent();

for (let i = start; i < line.length; i++) {
const char = line[i];
if (char === '<') {
const nextChar = line[i + 1];
if (nextChar === '/') {
if (openedSubStartMatches === 0) {
endIndex = i + '</MyTag>'.length;
break;
}
openedSubStartMatches--;
} else {
openedSubStartMatches++;
}
}
}
return endIndex;
},
importRegExp: /<(MyTag)\s*>/,
regExp: /__ignore__/,
replace: (textNode, match) => {
const linkNode = $createLinkNode('simple-jsx');

const textStart = match[0].length + (match.index ?? 0);
const textEnd =
(match.index ?? 0) + textNode.getTextContent().length - '</MyTag>'.length;
const text = match.input?.slice(textStart, textEnd);

const linkTextNode = $createTextNode(text);
linkTextNode.setFormat(textNode.getFormat());
linkNode.append(linkTextNode);
textNode.replace(linkNode);
},
type: 'text-match',
};

// Matches html within a mdx file
const MDX_HTML_TRANSFORMER: MultilineElementTransformer = {
dependencies: [CodeNode],
Expand Down Expand Up @@ -461,6 +506,12 @@ describe('Markdown', () => {
md: '```ts\nCode\n```ts\nSub Code\n```\n```',
skipExport: true,
},
{
customTransformers: [SIMPLE_INLINE_JSX_MATCHER],
html: '<p><span style="white-space: pre-wrap;">Hello </span><a href="simple-jsx"><span style="white-space: pre-wrap;">One &lt;MyTag&gt;Two&lt;/MyTag&gt;</span></a><span style="white-space: pre-wrap;"> there</span></p>',
md: 'Hello <MyTag>One <MyTag>Two</MyTag></MyTag> there',
skipExport: true,
},
];

const HIGHLIGHT_TEXT_MATCH_IMPORT: TextMatchTransformer = {
Expand Down

0 comments on commit 9ae0ef9

Please sign in to comment.