Repository navigation
[lexical-markdown] Bug Fix: Keep tabs and whitespace-only inline code as code on import #9433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
x-labb
wants to merge
2
commits into
meta:main
Choose a base branch
from
x-labb:fix-9409-tab-in-inline-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/lexical-markdown/src/__tests__/unit/CodeSpanWhitespaceImport.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| */ | ||
|
|
||
| // @vitest-environment node | ||
|
|
||
| import {buildEditorFromExtensions} from '@lexical/extension'; | ||
| import { | ||
| $convertFromMarkdownString, | ||
| $convertToMarkdownString, | ||
| TRANSFORMERS, | ||
| } from '@lexical/markdown'; | ||
| import {$getRoot, $isElementNode, $isTabNode, $isTextNode} from 'lexical'; | ||
| import {describe, expect, it} from 'vitest'; | ||
|
|
||
| import {MarkdownTestExtension} from '../utils'; | ||
|
|
||
| type Inline = {code: boolean; tab: boolean; text: string}; | ||
|
|
||
| function importParagraphs(markdown: string): Inline[][] { | ||
| using editor = buildEditorFromExtensions([MarkdownTestExtension]); | ||
| let paragraphs: Inline[][] = []; | ||
| editor.update( | ||
| () => { | ||
| $convertFromMarkdownString(markdown, TRANSFORMERS); | ||
| paragraphs = $getRoot() | ||
| .getChildren() | ||
| .map(block => | ||
| $isElementNode(block) | ||
| ? block.getChildren().map(child => ({ | ||
| code: $isTextNode(child) && child.hasFormat('code'), | ||
| tab: $isTabNode(child), | ||
| text: child.getTextContent(), | ||
| })) | ||
| : [], | ||
| ); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return paragraphs; | ||
| } | ||
|
|
||
| function roundTrip(markdown: string): string { | ||
| using editor = buildEditorFromExtensions([MarkdownTestExtension]); | ||
| editor.update( | ||
| () => { | ||
| $convertFromMarkdownString(markdown, TRANSFORMERS); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return editor.read(() => $convertToMarkdownString(TRANSFORMERS)); | ||
| } | ||
|
|
||
| const code = (text: string): Inline => ({code: true, tab: false, text}); | ||
| const text = (value: string): Inline => ({ | ||
| code: false, | ||
| tab: false, | ||
| text: value, | ||
| }); | ||
| const tab: Inline = {code: false, tab: true, text: '\t'}; | ||
|
|
||
| describe('whitespace inside inline code (#9409)', () => { | ||
| it('keeps a tab inside a code span as part of the code', () => { | ||
| expect(importParagraphs('`a\tb`')).toEqual([[code('a\tb')]]); | ||
| expect(importParagraphs('a `\t` b')).toEqual([ | ||
| [text('a '), code('\t'), text(' b')], | ||
| ]); | ||
| expect(importParagraphs('`\t`')).toEqual([[code('\t')]]); | ||
| }); | ||
|
|
||
| it('round trips a code span that holds a tab', () => { | ||
| expect(roundTrip('`a\tb`')).toBe('`a\tb`'); | ||
| // A span that starts and ends with whitespace is padded, and a reader | ||
| // strips that padding back to the tab. | ||
| expect(roundTrip('`\t`')).toBe('` \t `'); | ||
| expect(roundTrip('` \t `')).toBe('` \t `'); | ||
| expect(roundTrip('a `\t` b')).toBe('a ` \t ` b'); | ||
| expect(roundTrip('a ` \t ` b')).toBe('a ` \t ` b'); | ||
| }); | ||
|
|
||
| it('keeps a paragraph whose only content is whitespace code', () => { | ||
| expect(importParagraphs('x\n\n` `\n\ny')).toEqual([ | ||
| [text('x')], | ||
| [code(' ')], | ||
| [text('y')], | ||
| ]); | ||
| expect(importParagraphs('x\n\n`\t`\n\ny')).toEqual([ | ||
| [text('x')], | ||
| [code('\t')], | ||
| [text('y')], | ||
| ]); | ||
| }); | ||
|
|
||
| it('still turns tabs outside code into tab nodes', () => { | ||
| expect(importParagraphs('a\tb')).toEqual([[text('a'), tab, text('b')]]); | ||
| expect(importParagraphs('a\t`b`')).toEqual([[text('a'), tab, code('b')]]); | ||
| }); | ||
|
|
||
| it('still drops empty paragraphs', () => { | ||
| expect(importParagraphs('x\n\n\n\ny')).toEqual([[text('x')], [text('y')]]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: this check also decides list joining during import (
MarkdownTransformers.ts:630) and block separators on export (MarkdownExport.ts:83-84). Both new behaviors are correct:- a\n\n` `\n\n- bnow imports as list, paragraph, list, and a whitespace-code paragraph exports with blank lines around it. Neither is tested yet, and two extra cases inCodeSpanWhitespaceImport.test.tswould cover them.