This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

FrankWikstrom
FrankWikstrom

The OctoberCMS Markdown parser (Parsedown as far as I can tell) has a problem with embedded MathJax. For example

$x_1+x_2$

is parsed to $x<em>1+x</em>2$before it hits MathJax, which of course messes with the formatting of the formula. One way to overcome this problem is to wrap all problematic formulas inside <div>:s or some other element that the Markdown parser ignores, but this quickly gets both annoying and tiresome if you post lots of mathematical formulas.

I thought I'd write a small plugin to take care of this for me. It seems that the easiest way would be to somehow override the beforeSave method in Rainlab.Blog.Models.Post. Is there a reasonable way to do this? I tried hooking into Post::savingbut my code there is called before the original beforeSavemethod. I suppose I could try hooking into Post::savedinstead, but if I do that and re-save the model, what's the best way to avoid an infinite loop?

Is there perhaps a better way to do this? I'm still very new to October, and I may very well be missing something obvious.

Updated I realized that I can hook into Post::saved, change the generated content_htmland save the edited model via DB::update, but it seems like a very inelegant solution. I'd be happy for something a little cleaner.

Last updated

alxy
alxy

Maybe you could use the TagProcessor: https://github.com/rainlab/blog-plugin/blob/master/models/Post.php#L154

You can add custom processors to validate the MathJax codes. Example for images: https://github.com/rainlab/blog-plugin/blob/master/Plugin.php#L88

FrankWikstrom
FrankWikstrom

Thank you the suggestion, alxy. The downside is that TagProcessor is called after Parsedown has mangled the markup, and it's a lot easier to fix this before calling Markdown::parse. For now, I'll go with the solution outlined in my original post, which seems to be working reasonably well. (I just need to figure out a way to hook into the preview generation as well.)

Ideally, it would be a lot easier if the Markdownclass was implemented as a Laravel ServiceProvider allowing you to override the default parser.

Updated Actually, it was not very difficult to change the Markdown helper into a Facade for a MarkdownServiceProvider, thus allowing to change the default interpreter if needed. I have made a pull request for the change.

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.