-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Added LaTeX Paste Support to Editor #8303
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
Added LaTeX Paste Support to Editor #8303
Conversation
Enables users to paste LaTeX-formatted content (e.g. =mc^2$, \[\int x dx\]) directly into the editor
Detects inline ($...$) and display (\[\]) LaTeX syntax on paste
Enhances usability for users working with mathematical or scientific content
Integrated into the existing paste handler logic
Feature Preview
Paste ^2 + y^2 = z^2$ handled as LaTeX inline math
Paste \[\frac{a}{b}\] handled as LaTeX display math
Plain text pastes remain unaffected
PR Checklist
My code adheres to AppFlowys conventions
I've listed at least one issue that this PR fixes in the description above
I've added a test(s) to validate changes in this PR, or this PR only contains semantic changes
All existing tests are passing
Reviewer's GuideImplements LaTeX paste support by adding preprocessing for bracketed LaTeX blocks, integrating a custom MarkdownMathEquationParser into the markdown-to-document flow, and updating HTML paste logic to detect math and list structures and route rich content through markdown conversion for proper rendering. Sequence diagram for LaTeX paste processing in the editorsequenceDiagram
actor User
participant EditorState
participant MarkdownMathEquationParser
participant Document
User->>EditorState: Paste LaTeX content
EditorState->>EditorState: Detect math/list structures in HTML
EditorState->>MarkdownMathEquationParser: Parse Markdown for LaTeX blocks
MarkdownMathEquationParser->>Document: Convert LaTeX to mathEquationNode
Document->>EditorState: Render equation in document
Class diagram for MarkdownMathEquationParser and related changesclassDiagram
class MarkdownMathEquationParser {
+transform(element, parsers, listType, startNumber)
-_looksLikeLaTeX(content)
-_fixLatexSpacing(latex)
-_fixLatexLineBreaks(latex)
}
class CustomMarkdownParser {
<<abstract>>
+transform(...)
}
MarkdownMathEquationParser --|> CustomMarkdownParser
class Document {
+root
+customMarkdownToDocument(markdown, tableWidth)
}
class EditorState {
+convertHtmlToNodes(html)
}
EditorState --> Document
Document --> MarkdownMathEquationParser
Flow diagram for HTML paste handling with LaTeX detectionflowchart TD
A["User pastes HTML content"] --> B["EditorState.convertHtmlToNodes"]
B --> C{"Contains math/list structures?"}
C -- Yes --> D["Convert HTML to Markdown"]
D --> E["customMarkdownToDocument (with MarkdownMathEquationParser)"]
E --> F["Document nodes with mathEquationNode"]
C -- No --> G["htmlToDocument"]
G --> H["Remove empty lines, convert tables"]
H --> I{"Nodes empty or contains table?"}
I -- Yes --> D
I -- No --> F
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey there - I've reviewed your changes - here's some feedback:
- There’s a lot of duplicated LaTeX logic (_fixLatexSpacing and _fixLatexLineBreaks) in both the preprocessor and the parser—consider moving those into a shared helper to keep the behavior in sync and reduce code duplication.
- Many RegExp objects (e.g. the LaTeX detection and parsing patterns) are recreated on each call; extracting them as static final constants would improve performance and readability.
- The fallback decision tree in paste_from_html is fairly complex—consider encapsulating that logic into a well-named helper or adding clarifying comments so future maintainers can follow when you switch between htmlToDocument vs. markdown conversion.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- There’s a lot of duplicated LaTeX logic (_fixLatexSpacing and _fixLatexLineBreaks) in both the preprocessor and the parser—consider moving those into a shared helper to keep the behavior in sync and reduce code duplication.
- Many RegExp objects (e.g. the LaTeX detection and parsing patterns) are recreated on each call; extracting them as static final constants would improve performance and readability.
- The fallback decision tree in paste_from_html is fairly complex—consider encapsulating that logic into a well-named helper or adding clarifying comments so future maintainers can follow when you switch between htmlToDocument vs. markdown conversion.
## Individual Comments
### Comment 1
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/parsers/markdown_math_equation_parser.dart:96-105` </location>
<code_context>
+ /// Ensure common spacing in LaTeX (e.g. `\, dx`).
+ String _fixLatexSpacing(String latex) {
+ String result = latex;
+ result = result.replaceAll(', dx', r'\, dx');
+ result = result.replaceAll(', dy', r'\, dy');
+ result = result.replaceAll(', dz', r'\, dz');
+ result = result.replaceAll(', dt', r'\, dt');
+ result = result.replaceAll(', dV', r'\, dV');
+ result = result.replaceAll(', dA', r'\, dA');
+ result = result.replaceAll(', ds', r'\, ds');
+ result = result.replaceAll(', du', r'\, du');
+ result = result.replaceAll(', dv', r'\, dv');
+ result = result.replaceAll(', dw', r'\, dw');
+ return result;
+ }
</code_context>
<issue_to_address>
**suggestion:** The _fixLatexSpacing function duplicates logic from markdown_to_document.dart.
Refactor the spacing logic into a shared utility to reduce duplication and maintain consistency.
Suggested implementation:
```
import 'package:appflowy_flutter/plugins/document/presentation/editor_plugins/parsers/markdown_latex_utils.dart';
/// Ensure common spacing in LaTeX (e.g. `\, dx`).
String _fixLatexSpacing(String latex) {
return fixLatexSpacing(latex);
}
```
You must also:
1. Create a new file `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/parsers/markdown_latex_utils.dart` (if it doesn't exist).
2. Move the LaTeX spacing logic into a public function, e.g.:
```dart
String fixLatexSpacing(String latex) {
String result = latex;
result = result.replaceAll(', dx', r'\, dx');
result = result.replaceAll(', dy', r'\, dy');
result = result.replaceAll(', dz', r'\, dz');
result = result.replaceAll(', dt', r'\, dt');
result = result.replaceAll(', dV', r'\, dV');
result = result.replaceAll(', dA', r'\, dA');
result = result.replaceAll(', ds', r'\, ds');
result = result.replaceAll(', du', r'\, du');
result = result.replaceAll(', dv', r'\, dv');
result = result.replaceAll(', dw', r'\, dw');
return result;
}
```
3. Update any other files (e.g., `markdown_to_document.dart`) to use this shared utility instead of duplicating the logic.
</issue_to_address>
### Comment 2
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/parsers/markdown_math_equation_parser.dart:119-120` </location>
<code_context>
+ return latex;
+ }
+
+ // Avoid sequences like " \-" being treated poorly.
+ String result = latex.replaceAll(RegExp(r'(?<!\\)\\-'), r'\\ -');
+
+ // Replace single backslash followed by spaces and a letter/digit with a double
</code_context>
<issue_to_address>
**suggestion:** The line break fixer may introduce unintended formatting changes.
The current replacement logic could unintentionally alter valid LaTeX. Please clarify the intent with comments or refine the checks to avoid modifying correct syntax.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Feature Preview
This PR adds LaTeX paste support to AppFlowy, allowing users to paste mathematical equations from ChatGPT and other sources directly into documents with proper rendering.
Fixes #8177
Summary
This PR implements comprehensive LaTeX equation parsing when pasting mathematical content into AppFlowy documents. Users can now copy LaTeX equations from ChatGPT, Gemini, or any other source and paste them directly into AppFlowy with correct rendering.
What this PR adds:
\begin{cases}environments\begin{aligned}environments\begin{array}\to double\\) for multi-line equations, dx→\, dx)\text{...}blocks to preserve inline text formattingBefore:
beforef.mp4
After:
after.mp4
PR Checklist
Summary by Sourcery
Enable users to paste LaTeX equations directly into the editor by parsing and rendering mathematical content in both markdown and HTML paste flows.
New Features: