diff --git a/packages/lexical-markdown/src/MarkdownTransformers.ts b/packages/lexical-markdown/src/MarkdownTransformers.ts index 0270e4e2bba..0847de2047b 100644 --- a/packages/lexical-markdown/src/MarkdownTransformers.ts +++ b/packages/lexical-markdown/src/MarkdownTransformers.ts @@ -98,6 +98,23 @@ const createBlockNode = ( // TODO: should be an option const LIST_INDENT_SIZE = 4; +function getIndent(whitespaces: string): number { + const tabs = whitespaces.match(/\t/g); + const spaces = whitespaces.match(/ /g); + + let indent = 0; + + if (tabs) { + indent += tabs.length; + } + + if (spaces) { + indent += Math.floor(spaces.length / LIST_INDENT_SIZE); + } + + return indent; +} + const listReplace = (listType: ListType): ElementTransformer['replace'] => { return (parentNode, children, match) => { const previousNode = parentNode.getPreviousSibling(); @@ -130,7 +147,7 @@ const listReplace = (listType: ListType): ElementTransformer['replace'] => { } listItem.append(...children); listItem.select(0, 0); - const indent = Math.floor(match[1].length / LIST_INDENT_SIZE); + const indent = getIndent(match[1]); if (indent) { listItem.setIndent(indent); } diff --git a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts index cc83046e335..dfc9ed3994e 100644 --- a/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts +++ b/packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts @@ -79,6 +79,12 @@ describe('Markdown', () => { html: '

Hello world

', md: '- Level 1\n - Level 2\n - Level 3\n\nHello world', }, + // List indentation with tabs, Import only: export will use " " only for one level of indentation + { + html: '

Hello world

', + md: '- Level 1\n\t- Level 2\n \t - Level 3\n\nHello world', + skipExport: true, + }, { // Import only: export will use "-" instead of "*" html: '

Hello world

',