At Testive we are building a student native mobile app. Up until now we have relied on having a mobile friendly web-app, but since it’s been a hotly requested featuring we are currently working to build a version of our app using React Native.
The core features of the app will be the ability to answer practice questions/complete assignments, review incorrect answers, and schedule meetings with your coach. This means that a crucial feature will be the ability to display our question and answer texts.
Displaying questions requires that we not only parse our own markup language and inline HTML, but also KaTeX as that is how we display most math data in app. Parsing and displaying HTML/other Markup language, was not a particularly easy problem to solve, but after a lot of RegEx hacking I was able to parse the tags and create native components for them (a story for another blog post). Display KaTeX natively though, was not a wheel I wanted to reinvent.
My search for libraries and other solutions returned very few results, which is what I expected as parsing & displaying KaTeX in React Native is a pretty niche topic. Especially if you want a solution that works well with Expo. The only thing close to a solution I found was a Github thread linking to this library by 3axap4eHko which pretty much exactly what I wanted except that it had build issues that the maintainer wasn’t responding to and that I couldn’t figure out how to fix.
So instead of rewriting something from scratch, or trying to write my own KaTeX parsing library, I decided to rebuild the library for my own purposes.
Note: My solution here is not pretty, elegant, “correct”, or really all that good, but as there is not any other readily available solution to this problem, and I have already spent way too long trying to solve it, this is what I’m sticking with. I’m only sharing, so that some other poor soul doens’t have to spend hours digging around only to come to this same conclusion.
(my full solution will be posted at the end of this)
Start by creating a file that I named katex.js, that is going to house our KaTeX webview component. To start we’re going to layout a basic Component, with a webview that we’re dynamically gonna set the content of. It’s also going to include some other props that will make life easier down the road.
As you can see, a lot of things will be defined later, but the basic idea is a webview, that we are creating content for on the fly, that accepts things like styles, and other KaTeX arguements that we might need to pass.
The crux of this is going to be defined in our getContent method, that looks like this:
The last thing to do, is just to define our default styles, and wrap it all together which I’ll do by posting my complete definition and outlining some of the problems I’m still facing with it. Enjoy.
The biggest issue you’ll find, is formatting the webviews. I have hacked in a width value there, that I’m constantly tweaking and haven’t found a good solution for yet. If you find a way to have our View container’s width work well with a dynamically sized webview/katex expression please let me know in the comments. The error handling on this is also not amazing.
Anyway, I had a ton of trouble with this so I wanted to make this post so that it might be easier for someone else to land at this same solution, or figure out that there isn’t a really elegant solution to this problem right now. Please let me know any suggestions you have for this code.
Thanks for reading.