Math not rendering in embedded posts

I have some posts embedded within my website. The mathematics in them isn’t rendering within the embeds.

Using the old plugin, we could patch the embed.js.erb file to get the math to render. Is there something we can do with discourse-math to make it work in embeds?

Here’s a sample of what I’m talking about (scroll to the bottom).

Thanks!

It looks like the solution is in the last comment on the Github thread that you referenced:

https://github.com/kasperpeulen/discourse-mathjax/issues/21#issuecomment-471256399

Have you tried it?

Yes, and that works with the old third-party extension discourse-mathjax . However, discourse has implemented their own discourse-math extension and this solution does not work.

I don’t think the issue is with the plugin. The new plugin still uses the math spans (as you can see if you inspect the source of your page) so the solution should still work. What may have changes since then is how Discourse code embeds the posts?

To be clear, what I’m suggesting that this patch should still work?

@@ -70,5 +70,11 @@
   </head>
   <body>
     <%= yield %>
+    <script type="text/x-mathjax-config">
+       MathJax.Hub.Config({
+        extensions: ["jsMath2jax.js"]
+      });
+    </script>
+    <script src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
   </body>
 </html>

Currently, I don’t see code in your embedded comment section?
Can you verify you’re actually applying the patch?

./launcher enter app
cat app/views/layouts/embed.html.erb

The problem with that patch might be that it tries to load Javascript from mathjax.org, which I think the current Discourse version blocks by default? You might need to add https://cdn.mathjax.org to the cors origins Site Setting.

2 Likes

That might be it! Now, the cors origins site setting will be on my discourse server or the web server in which I’m embedding?

It’s one of the Discourse settings.

2 Likes

This solved it for me! Thank you so much for that help; that site settings for security had never crossed my mind.

Thank you so much!

2 Likes

I wonder whether we could do something better here. @sam is there a way for a plugin to somehow modify app/views/layouts/embed.html.erb? So that the Discourse could load MathJax/KaTeX JS inside the embedded iframe?

I certainly support making such a change. We may need to add some sort of hook in core though like: (perhaps an embed_javascripts in https://github.com/discourse/discourse/blob/7829558c6d42b5f0dfbb0c2351fb332a940d651e/lib/discourse_plugin_registry.rb#L50-L50 .

4 Likes

This topic was automatically closed after 1236 days. New replies are no longer allowed.