-
Notifications
You must be signed in to change notification settings - Fork 60.4k
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
support \(...\) and \[...\] style math formula #4186
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,9 +116,27 @@ function escapeDollarNumber(text: string) { | |
return escapedText; | ||
} | ||
|
||
function escapeBrackets(text: string) { | ||
const pattern = | ||
/(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g; | ||
return text.replace( | ||
pattern, | ||
(match, codeBlock, squareBracket, roundBracket) => { | ||
if (codeBlock) { | ||
return codeBlock; | ||
} else if (squareBracket) { | ||
return `$$${squareBracket}$$`; | ||
} else if (roundBracket) { | ||
return `$${roundBracket}$`; | ||
} | ||
return match; | ||
}, | ||
); | ||
} | ||
|
||
function _MarkDownContent(props: { content: string }) { | ||
const escapedContent = useMemo( | ||
() => escapeDollarNumber(props.content), | ||
() => escapeBrackets(escapeDollarNumber(props.content)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original intention of the escapeDollarNumber function was to resolve latex syntax conflicts, but unfortunately, it failed to resolve the problem, so the function can be directly modified, rather than wrapped on top of it |
||
[props.content], | ||
); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the execution performance of the the block of code is a little bad
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.
It's no wonder that regular expressions (regex) often have poor performance, especially for interpreting languages, unlike regex in compiled languages (e.g., regex in Golang, which performs better as it is compiled).
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.
At first I was also worried about the performance, but later I found that this worry was unnecessary.
I used the following code to log the time consumed for each call, and the results showed that the function is fast enough.
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.
Modern js engines compile and cache regexp at load time, so this function will not recompile it every time
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.
it because got handle by react
useMemo
that's why it looks fasterThere 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.
more likely depends of framework web front-end (e.g, react)