Skip to content
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

style: add Math Preprocessing for Markdown Component #335

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

Hobo12
Copy link
Contributor

@Hobo12 Hobo12 commented Feb 18, 2025

Add Math Preprocessing for Markdown Component

Summary

This PR introduces preprocessing for math expressions within the Markdown component. The component now transforms inline math (e.g., \(...\)) to use the $ ... $ format and block math (e.g., \[...\]) to use the $$ ... $$ format. This enhancement helps in standardizing the math syntax before it’s processed by the Markdown renderer.

Changes

  • Enhanced Markdown Component:
    • Added a useMemo hook in the Markdown function to preprocess the children content.
    • Inline Math: Replaces \(...\) with $ ... $.
    • Block Math: Replaces \[...\] with \n$$\n ... \n$$\n, ensuring that block math expressions are placed on separate lines.
  • Refactoring:
    • The processed content is then passed to the existing MarkdownContent component, preserving all existing functionalities like code highlighting, link rendering, and file block handling.

Rationale

Previously, the Markdown component did not handle the conversion of LaTeX-style math delimiters wrapped in \(...\) and \[...\], which could lead to inconsistent rendering of math expressions. By preprocessing the content, we ensure a more robust and flexible handling of both inline and block math expressions.

Possible Drawbacks

  • Non-Mathematical Content Interference: The preprocessing approach targets \(...\) and \[...\] delimiters universally. This means that any non-mathematical content using these delimiters for other purposes might be unintentionally transformed, potentially leading to unexpected formatting issues.

How to Test

  1. Inline Math:
    • Input: This is inline math: \(a^2 + b^2 = c^2\)
    • Expected Transformation: This is inline math: $ a^2 + b^2 = c^2 $
  2. Block Math:
    • Input: Here is block math: \[ \int_0^\infty e^{-x} dx \]
    • Expected Transformation: Here is block math: $$ \int_0^\infty e^{-x} dx $$
  3. Non-Mathematical Content:
    • Ensure that content not intended as math that might incidentally use these delimiters is reviewed to avoid unexpected changes.
  4. Overall Rendering:
    • Verify that the Markdown renders as expected with both math expressions and other markdown elements (e.g., code blocks, links).

Additional Notes

  • The changes maintain backwards compatibility. If there are no math delimiters present, the content remains unaffected.
  • The updated component still supports HTML rendering based on the acceptHtml prop.
  • No changes were made to the auxiliary components (Label, Link, Code), ensuring that the overall markdown styling and functionalities remain consistent.

###  Add Math Preprocessing for Markdown Component

#### Summary
This PR introduces preprocessing for math expressions within the Markdown component. The component now transforms inline math (e.g., `\(...\)`) to use the `$ ... $` format and block math (e.g., `\[...\]`) to use the `$$ ... $$` format. This enhancement helps in standardizing the math syntax before it’s processed by the Markdown renderer.

#### Changes
- **Enhanced Markdown Component:**
  - Added a `useMemo` hook in the `Markdown` function to preprocess the `children` content.
  - **Inline Math:** Replaces `\(...\)` with `$ ... $`.
  - **Block Math:** Replaces `\[...\]` with `\n$$\n ... \n$$\n`, ensuring that block math expressions are placed on separate lines.
- **Refactoring:**
  - The processed content is then passed to the existing `MarkdownContent` component, preserving all existing functionalities like code highlighting, link rendering, and file block handling.

#### Rationale
Previously, the Markdown component did not handle the conversion of LaTeX-style math delimiters, which could lead to inconsistent rendering of math expressions. By preprocessing the content, we ensure a more robust and flexible handling of both inline and block math expressions.

#### Possible Drawbacks
- **Non-Mathematical Content Interference:**  
  The preprocessing approach targets `\(...\)` and `\[...\]` delimiters universally. This means that any non-mathematical content using these delimiters for other purposes might be unintentionally transformed, potentially leading to unexpected formatting issues.

#### How to Test
1. **Inline Math:**  
   - Input: `This is inline math: \(a^2 + b^2 = c^2\)`
   - Expected Transformation: `This is inline math: $ a^2 + b^2 = c^2 $`
2. **Block Math:**  
   - Input: `Here is block math: \[ \int_0^\infty e^{-x} dx \]`
   - Expected Transformation:  
     ```
     Here is block math: 
     $$
     \int_0^\infty e^{-x} dx
     $$
     ```
3. **Non-Mathematical Content:**  
   - Ensure that content not intended as math that might incidentally use these delimiters is reviewed to avoid unexpected changes.
4. **Overall Rendering:**  
   - Verify that the Markdown renders as expected with both math expressions and other markdown elements (e.g., code blocks, links).

#### Additional Notes
- The changes maintain backwards compatibility. If there are no math delimiters present, the content remains unaffected.
- The updated component still supports HTML rendering based on the `acceptHtml` prop.
- No changes were made to the auxiliary components (`Label`, `Link`, `Code`), ensuring that the overall markdown styling and functionalities remain consistent.
Copy link

vercel bot commented Feb 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
chatnio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 18, 2025 2:26am

@Sh1n3zZ Sh1n3zZ changed the title Update Markdown.tsx style: add Math Preprocessing for Markdown Component Feb 18, 2025
@Sh1n3zZ Sh1n3zZ merged commit 7a57d86 into coaidev:main Feb 18, 2025
1 check passed
@Sh1n3zZ
Copy link
Collaborator

Sh1n3zZ commented Feb 18, 2025

Hello,

We sincerely appreciate your contribution to the project and look forward to your next contribution!

BEST Regards,
Junhai Deng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants