Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import {$findMatchingParent} from '@lexical/utils';
import {
$createLineBreakNode,
$createParagraphNode,
$createTabNode,
$createTextNode,
$getRoot,
$getSelection,
$isElementNode,
$isParagraphNode,
$isTextNode,
ElementNode,
TextNode,
} from 'lexical';

import {importTextTransformers} from './importTextTransformers';
Expand Down Expand Up @@ -84,17 +87,24 @@ export function createMarkdownImport(
);
}

// By default, removing empty paragraphs as md does not really
// allow empty lines and uses them as delimiter.
// If you need empty lines set shouldPreserveNewLines = true.
const children = root.getChildren();
for (const child of children) {
// By default, removing empty paragraphs as md does not really
// allow empty lines and uses them as delimiter.
// If you need empty lines set shouldPreserveNewLines = true.
if (
!shouldPreserveNewLines &&
isEmptyParagraph(child) &&
root.getChildrenSize() > 1
) {
child.remove();
continue;
}
// Convert all '\t' into TabNode.
if ($isElementNode(child)) {
for (const textNode of child.getAllTextNodes()) {
$normalizeMarkdownTextNode(textNode);
}
}
}

Expand Down Expand Up @@ -291,6 +301,28 @@ function $importBlocks(
}
}

// Look in node for '\t' and create a TabNode for each occurrence.
function $normalizeMarkdownTextNode(textNode: TextNode): void {
const tabOffsets: Set<number> = new Set();
const text = textNode.getTextContent();
let index = text.indexOf('\t');

// Find all tab occurrences
while (index !== -1) {
tabOffsets.add(index);
tabOffsets.add(index + 1);
index = text.indexOf('\t', index + 1);
}

// Split node to isolate each tab then replace '\t' into TabNode
const splitNodes = textNode.splitText(...tabOffsets);
splitNodes.forEach((node) => {
if (node.getTextContent() === '\t') {
node.replace($createTabNode());
}
});
}

function createTextFormatTransformersIndex(
textTransformers: Array<TextFormatTransformer>,
): TextFormatTransformersIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,21 @@ describe('Markdown', () => {
md: 'foo\\\nbar',
mdAfterExport: 'foo\nbar',
},
{
html: '<p><span style="white-space: pre-wrap;">foo </span><a href="https://lexical.dev"><span style="white-space: pre-wrap;">bar</span></a><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;">baz</span></p>',
md: 'foo [bar](https://lexical.dev)\tbaz',
skipExport: true,
},
{
html: '<p><span style="white-space: pre-wrap;">foo</span><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;">bar</span><br><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;">baz</span></p>',
md: 'foo\t \tbar\n\tbaz',
skipExport: true,
},
{
html: '<p><span style="white-space: pre-wrap;">Hello</span><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;">\t</span><span style="white-space: pre-wrap;">World</span></p>',
md: 'Hello\t\tWorld',
skipExport: true,
},
];

for (const {
Expand Down
Loading