From 76fc0c3f38e7dd0444fd231c5e5d1277801695d4 Mon Sep 17 00:00:00 2001 From: wnhlee <40269597+2wheeh@users.noreply.github.com> Date: Sun, 17 Mar 2024 07:33:42 +0900 Subject: [PATCH] Fix to count tabs as list indentation on importing markdown (#5706) --- .../src/MarkdownTransformers.ts | 19 ++++++++++++++++++- .../__tests__/unit/LexicalMarkdown.test.ts | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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

',