Skip to content

[lexical-playground] Bug Fix: Preserve block equation markdown#8562

Open
vivekjm wants to merge 1 commit into
facebook:mainfrom
vivekjm:fix-block-equation-markdown
Open

[lexical-playground] Bug Fix: Preserve block equation markdown#8562
vivekjm wants to merge 1 commit into
facebook:mainfrom
vivekjm:fix-block-equation-markdown

Conversation

@vivekjm

@vivekjm vivekjm commented May 25, 2026

Copy link
Copy Markdown

Description

Preserves block equation markdown in the playground transformer by adding a block-level $$...$$ equation transformer and by teaching the existing equation text-match transformer to distinguish $$...$$ from $...$ during import.

This keeps block equations from being serialized as inline equations when converting playground content to Markdown and back.

Closes #6936

Test plan

Before

A block EquationNode exported through the playground markdown transformers produced the raw equation text / inline-style markdown instead of preserving $$...$$ delimiters.

After

  • source ~/.nvm/nvm.sh && nvm use 20.20.0 >/dev/null && pnpm exec vitest --project unit packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts --run
  • source ~/.nvm/nvm.sh && nvm use 20.20.0 >/dev/null && pnpm exec prettier --check packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts
  • source ~/.nvm/nvm.sh && nvm use 20.20.0 >/dev/null && pnpm exec eslint packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts

@vercel

vercel Bot commented May 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment May 26, 2026 5:58am
lexical-playground Ready Ready Preview, Comment May 26, 2026 5:58am

Request Review

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be a fair number of edge cases with this:

  • the $$ syntax isn't handled as a markdown shortcut (e.g. typing $$x$$ results in an inline equation with the contents $x$)
  • if there are literal $ in the document flanking an inline equation, or inside the inline equation, there is no escaping so exporting then importing will incorrectly round-trip to a block equation

@vivekjm

vivekjm commented May 26, 2026

Copy link
Copy Markdown
Author

Thanks for the review. I updated the PR to cover both cases you called out:

  • Typing 12647x12647 now stays a block equation instead of being treated as an inline equation with $ content.
  • Inline equation export now uses \(...\) instead of $...$, which avoids ambiguous round-trips when literal $ characters flank an inline equation or appear inside one.

I added focused regression coverage for block shortcut typing, block import/export, slash-paren inline import/export, legacy single-dollar inline import, literal-dollar flanking, and dollar signs inside inline equation content.

Checks run locally with Node 20.20.0:

  • pnpm exec vitest --project unit packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts --run
  • pnpm exec prettier --check packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts
  • pnpm exec eslint packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts

@etrepum

etrepum commented May 26, 2026

Copy link
Copy Markdown
Collaborator

I think the behavior of the inline export/import can be improved without changing it to an entirely different syntax

@vivekjm vivekjm force-pushed the fix-block-equation-markdown branch from 3761f3e to 74ec4ab Compare May 26, 2026 05:57
@vivekjm

vivekjm commented May 26, 2026

Copy link
Copy Markdown
Author

Good point. I revised this to keep inline equations on the $...$ syntax instead of switching them to \(...\).

The updated approach is:

  • inline equations still export as $...$
  • dollar signs inside inline equation content are escaped, e.g. $price = \$5$
  • block equations export as multiline display math:
$$
x^2 + y^2 = z^2
$$

That keeps exported block equations distinct from a paragraph with literal $ text flanking an inline equation, while preserving the single-dollar inline syntax. I also kept the markdown shortcut fix so typing $$x$$ creates a block equation.

I reran:

  • pnpm exec vitest --project unit packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts --run
  • pnpm exec prettier --check packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts
  • pnpm exec eslint packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts

Also, small correction to my previous comment: I meant typed $$x$$; my shell expanded the $$ in that comment before posting it.

@potatowagon

Copy link
Copy Markdown
Contributor

Review: Preserve Block Equation Markdown + Test Coverage

Reviewed by: Navi (AI review assistant for @potatowagon)

Summary

Adds comprehensive unit tests for the playground's equation markdown transformers (both inline $...$ and block $$...$$) including import/export roundtrips, escaped dollar signs, and markdown shortcut typing. Also adds a new BLOCK_EQUATION transformer.

What I Verified

  • Test coverage: 11 well-structured tests covering:
    • Inline equation export/import roundtrip
    • Block equation export/import (multiline $$\n...\n$$ format)
    • Escaped dollar signs inside equations
    • Ambiguity handling (adjacent $ text nodes + inline equation)
    • Markdown shortcut typing for both inline and block equations
    • Empty equation handling
  • Logic correctness: The BLOCK_EQUATION transformer uses a multiline regex to match $$\n...\n$$ blocks and creates non-inline EquationNodes. Export produces the expected fenced format.
  • No regressions: Tests validate roundtrip consistency — export then import produces the same structure.
  • CI status: Full e2e + integration test suite passes on all platforms (Linux, Mac, Windows × Chromium, Firefox, WebKit).

Verdict

Safe to approve — pure test coverage addition with a clean new transformer. Increases confidence in equation markdown handling without changing existing behavior.

@potatowagon potatowagon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — Preserve Block Equation Markdown

Assessment: Looks good to land

What I verified:

  1. Feature scope: Adds markdown transformer support for block/inline equation nodes in the playground. The EQUATION transformer handles inline ($...$) and BLOCK_EQUATION handles block ($$...$$) math equations.

  2. Implementation quality: Clean TextMatchTransformer implementation. The regex patterns are correct — inline uses /(?<!\$)\$(?!\$)(.+?)(?<!\$)\$(?!\$)/ (excludes $$), block uses /^\$\$\n([\s\S]+?)\n\$\$/ (multiline). Import/export logic properly handles the inline flag on EquationNode.

  3. Test coverage: Comprehensive test suite (261 lines) covering export, import, round-trip, and markdown shortcut typing for both inline and block equations. Tests verify correct node creation, content preservation, and non-interference with other content.

  4. CI status: Full CI suite green (39 checks pass).

  5. Risk assessment: Playground-only change, adds new transformers without modifying existing ones. No regression risk.

— via Navi on behalf of potatowagon

etrepum pushed a commit to etrepum/lexical that referenced this pull request Jun 10, 2026
Follow-ups to the block equation markdown support (facebook#8562):

- Typing $$...$$ only converts to a block equation when it is the sole
  content of a top-level paragraph. Previously the parent element was
  replaced unconditionally, destroying soft-wrapped lines and formatted
  siblings and ejecting list items out of their list.
- Inline equation export now escapes only bare `$` (as the KaTeX-valid
  `\$`) instead of doubling every backslash, and import keeps content
  verbatim instead of unescaping. Exported LaTeX like `$\frac{1}{2}$`
  renders correctly in external markdown renderers while bare `$` is
  still disambiguated from the delimiters.
- An escaped dollar (`\$`) no longer opens an inline equation shortcut
  match, so markdown-style escapes are honored while typing.
- Non-inline equations nested inside another element export in inline
  form again instead of silently dropping their delimiters.
- Use $createEquationNode instead of the raw EquationNode constructor.

Also adds a browser-mode suite that drives the shortcuts through real
key events to cover caret behavior around the conversions, which the
jsdom unit suite cannot observe.

https://claude.ai/code/session_01WpKgF4fzRPkybpU6mwUXFB
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Block math equation is falsely converted to inline equation on MarkdownShortcutPlugin

4 participants