Skip to content

[lexical-markdown] Bug Fix: strip the fence indentation from an indented code block - #9004

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-fence-indent
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-code-fence-indent

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

An opening code fence may be indented by up to three spaces. CommonMark then
requires that indentation to be removed from the block's content:

If the leading code fence is indented N spaces, then up to N spaces of
indentation are removed from each line of the content (if present).
https://spec.commonmark.org/0.31.2/#fenced-code-blocks

CODE_START_REGEX already accepts a leading-whitespace run and captures it as
part of startMatch[1], and CODE_END_REGEX accepts an indented closing
fence, so an indented block is imported as a code block. But CODE.replace
only ever used startMatch[1].trim() to derive the fence string and never
looked at the indentation, so every content line kept it:

  ```js
  code

imported as a code block whose text is `"  code"` rather than `"code"`. The
indentation is dropped from the fence lines on export but kept in the content,
so the block also comes back wider on every markdown round trip.

This computes the fence indent from the whitespace `startMatch[1]` already
carries, and removes up to that many leading space/tab characters from each
content line. "Up to" matters: content indented less than the fence is left
alone rather than having non-whitespace removed, and indentation *relative* to
the fence is preserved. A fence with no indentation is unaffected, so code
blocks that intentionally start with indented lines still keep it.

## Test plan

New unit test `packages/lexical-markdown/src/__tests__/unit/CodeBlockFenceIndent.test.ts`.

### Before

$ npx vitest run packages/lexical-markdown/src/tests/unit/CodeBlockFenceIndent.test.ts

 × removes the fence indentation from the content 7ms
 × preserves indentation relative to the fence 1ms
 × removes at most the fence indentation 1ms
 × handles a tab-indented fence 1ms

AssertionError: expected ' code' to be 'code' // Object.is equality
AssertionError: expected ' a\n b' to be 'a\n b' // Object.is equality
AssertionError: expected 'a\n b' to be 'a\nb' // Object.is equality
AssertionError: expected '\tcode' to be 'code' // Object.is equality

  Tests  4 failed | 1 passed (5)

### After

$ npx vitest run packages/lexical-markdown/src/tests/unit/CodeBlockFenceIndent.test.ts

Test Files 1 passed (1)
Tests 5 passed (5)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core packages/lexical-code-prism packages/lexical-code-shiki

Test Files 13 passed (13)
Tests 643 passed (643)

…ted code block

## Description

An opening code fence may be indented by up to three spaces. CommonMark then
requires that indentation to be removed from the block's content:

> If the leading code fence is indented N spaces, then up to N spaces of
> indentation are removed from each line of the content (if present).
> https://spec.commonmark.org/0.31.2/#fenced-code-blocks

`CODE_START_REGEX` already accepts a leading-whitespace run and captures it as
part of `startMatch[1]`, and `CODE_END_REGEX` accepts an indented closing
fence, so an indented block is imported as a code block. But `CODE.replace`
only ever used `startMatch[1].trim()` to derive the fence string and never
looked at the indentation, so every content line kept it:

```
  ```js
  code
  ```
```

imported as a code block whose text is `"  code"` rather than `"code"`. The
indentation is dropped from the fence lines on export but kept in the content,
so the block also comes back wider on every markdown round trip.

This computes the fence indent from the whitespace `startMatch[1]` already
carries, and removes up to that many leading space/tab characters from each
content line. "Up to" matters: content indented less than the fence is left
alone rather than having non-whitespace removed, and indentation *relative* to
the fence is preserved. A fence with no indentation is unaffected, so code
blocks that intentionally start with indented lines still keep it.

## Test plan

New unit test `packages/lexical-markdown/src/__tests__/unit/CodeBlockFenceIndent.test.ts`.

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockFenceIndent.test.ts

     × removes the fence indentation from the content 7ms
     × preserves indentation relative to the fence 1ms
     × removes at most the fence indentation 1ms
     × handles a tab-indented fence 1ms

AssertionError: expected '  code' to be 'code' // Object.is equality
AssertionError: expected '  a\n     b' to be 'a\n  b' // Object.is equality
AssertionError: expected 'a\n  b' to be 'a\nb' // Object.is equality
AssertionError: expected '\tcode' to be 'code' // Object.is equality

      Tests  4 failed | 1 passed (5)
```

### After

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/CodeBlockFenceIndent.test.ts

 Test Files  1 passed (1)
      Tests  5 passed (5)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core packages/lexical-code-prism packages/lexical-code-shiki

 Test Files  13 passed (13)
      Tests  643 passed (643)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9055 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant