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

Improve dollar sign escape for LaTeX compatibility #4354

Closed

Conversation

Algorithm5838
Copy link
Contributor

Improve the implementation of the dollar sign escape to address the issues with LaTeX.

Copy link

vercel bot commented Mar 19, 2024

@Algorithm5838 is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

Your build has completed!

Preview deployment

@Algorithm5838
Copy link
Contributor Author

Algorithm5838 commented Mar 20, 2024

I just made another change. Now, it should not escape the dollar sign if it is inside a code block or inline code.
Update 1: I fixed an issue rendering $1 $2.
Update 2: I optimized the regex, removing duplication and better escaping.

    // Regex explanation:
    // (?<!`)                 # Negative lookbehind to ensure the '$' is not preceded by a backtick (`)
    // \$                     # Match a literal '$' character
    // (\d+(?:[.,]\d+)*)      # Capture group 1: Match one or more digits, optionally followed by a decimal part (e.g., 123.45)
    // (?=                    # Positive lookahead to ensure the following conditions are met:
    //   \s*[.,;!?]\s*\B      #   The number is followed by a punctuation mark (.,;!?) and a non-word boundary
    //   |                    #   OR
    //   \s+[a-zA-Z]          #   The number is followed by one or more whitespace characters and a letter
    //   |                    #   OR
    //   \s+\$                #   The number is followed by one or more whitespace characters and a '$' sign
    // )
    // (?!`)                  # Negative lookahead to ensure the '$' is not followed by a backtick (`)
    // /g                     # Global flag to replace all occurrences

Update 3: I fixed 2 issues, dollar signs written in two lines, and not escaping if already escaped.
Update 4: I optimized it for the use of ! in Math, and possible other scenarios.
Update 5: I optimized it again for rare use cases, hopefuly this is the last one.

@Algorithm5838 Algorithm5838 deleted the dollar-sign-latex-fix branch March 21, 2024 06:07
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.

1 participant