-
Notifications
You must be signed in to change notification settings - Fork 834
LaTeX Block: Switch to MathML and add styling support #44928
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
base: trunk
Are you sure you want to change the base?
Conversation
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Code Coverage Summary7 files are newly checked for coverage. Only the first 5 are listed here.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
Are the |
It seems to come from the user agent. So I guess MDN is incorrect or something. Addressed all the rest. Looks great now. |
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text> | ||
{ source.trim() && ( | ||
<div | ||
className="jetpack-math-rendered" | ||
style={ style } | ||
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) } | ||
/> | ||
) } |
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.
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text> | |
{ source.trim() && ( | |
<div | |
className="jetpack-math-rendered" | |
style={ style } | |
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) } | |
/> | |
) } | |
{ source.trim() && ( | |
<> | |
<Text upperCase>{ __( 'Preview', 'jetpack' ) }</Text> | |
<div | |
className="jetpack-math-rendered" | |
style={ style } | |
ref={ r => r !== renderAreaRef && setRenderAreaRef( r ) } | |
/> | |
</> | |
) } |
I think it looks better hiding the "Preview" until there's some content:


@dmsnell curious for your take in this PR considering your comment from core issue:
|
"supports": { | ||
"html": true, | ||
"color": { | ||
"gradients": true | ||
}, | ||
"spacing": { | ||
"margin": true, | ||
"padding": true | ||
}, | ||
"typography": { | ||
"fontSize": true | ||
}, | ||
"align": [ "wide", "full" ], | ||
"__experimentalBorder": { | ||
"color": true, | ||
"radius": true, | ||
"style": true, | ||
"width": true, | ||
"__experimentalDefaultControls": { | ||
"color": true, | ||
"radius": true, | ||
"style": true, | ||
"width": true | ||
} | ||
} | ||
}, |
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.
@alshakero it might be easier to split this PR into two: MathML and styles separately, as both have separate concerns to work through, so it would be faster keep momentum going in smaller PRs.
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.
I'm really slammed right now. Thank you so much for pushing. Why don't we just merge?
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.
Feel free to merge and we can iterate behind beta.
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.
thanks for the ping.
first of all, it’s fine to experiment with this in beta and work to understand the domain of what kses
is going to do and not do.
I’m curious about the purpose of stripTags()
in here.
With a modernized code block, @sirreal went through a lot of similar challenges in representing the original source while providing a rendered output. We explored a number of issues there.
The MathML spec highlights a use of <annotation>
to represent the equation in its LaTeX form. While hidden from the browser’s rendering, that could be a potentially nice feature here where if someone wanted to inspect the page and extract the source formula they could.
Potentially that could also provide a mechanism to recover from corruption if we needed to. The HTML API does properly parse MathML embedded within HTML so it shouldn’t be tricked, though kses
might still apply its own creative choices.
Storing a base64
encoding of the generated MathML is an option for last-resort reliability. The server can dynamically replace the block contents on render from it, and not have to worry about pipelines of corruption that may or may not occur.
That being said, I’m not up to date on what Core does and doesn’t allow. It would be good to run a fuzzing test against this on a vanilla WP host with Jetpack, editing the post after disabling Jetpack, hosted on WordPress.com (in its different environments), and with things like &
in the formula, things like x<sum_0
in the formula. We would want to know what kinds of corruption might sneak in so that we can fix Core to prevent it.
Well done y’all. This is a nice enhancement; I’m really happy to see that the block no longer hides its content in the static fallback render, but instead preserves the rendered formula through things like site exports and the removal of the Jetpack plugin.
} | ||
|
||
export function renderMath( source ) { | ||
if ( source.toLowerCase().includes( '<math' ) ) { |
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.
a small change covers all potential cases of MATH
tags and avoids an allocation
if ( source.match( /<math/i ) ) {
Proposed changes:
This switches the LaTeX block to render MathML instead of HTML and renames it to Math. This has many benefits:
1. Works natively in the Jetpack app!
2. No need to render on the client side thus load a 3rd party library. Faster. Safer.
3. No need for client side CSS since
<math>
comes styled natively.4. Supported by all browsers.
It also extends the block to support font size, colors, and spacing.
Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
Tex format
\int_{x=a}^{y} \frac{df(x)}{dx} \, dx = f(y) - f(a)
LaTeX.MathML
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mo>=</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">f=x^2</annotation></semantics></math>