Skip to content
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

Add support for LaTeX formula rendering when rendering markdown #158

Merged
merged 4 commits into from
Aug 3, 2024

Conversation

yym68686
Copy link
Contributor

@yym68686 yym68686 commented Aug 2, 2024

I added a few lines of code to support KaTeX formula rendering. Here is a test case:

from fasthtml.common import *

hdrs = (
    MarkdownJS(), HighlightJS(langs=['python', 'javascript', 'html', 'css']),
    Link(rel='stylesheet', href='https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css')
)

app, rt = fast_app(hdrs=hdrs)

content = """
Here are some _markdown_ elements.

- This is a list item
- This is another list item
- And this is a third list item

```python
print("Hello, World!")

$$ 1 \times 2 = 2 $$

$$1 \times 2 = 2$$

$1 \times 2 = 2$

Fenced code blocks work here.
"""

@rt('/')
def get(req):
return Titled("Markdown rendering example", Div(content,cls="marked"))

if name == "main":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=5001,
reload=True,
)

@yym68686
Copy link
Contributor Author

yym68686 commented Aug 2, 2024

It seems GitHub automatically renders formulas. I'll paste a screenshot of the test code, which will make it clearer to see.

image

@johnowhitaker
Copy link
Contributor

Neat! @jph00 any objections to adding this to MarkdownJS?

@jph00
Copy link
Contributor

jph00 commented Aug 2, 2024

Let's add a bool param to enable/disable it, and default to false. Thanks!

@jph00
Copy link
Contributor

jph00 commented Aug 2, 2024

Oh also would be nice if start/end tags were configurable. E.g OpenAI models use different ones.

@dzenny
Copy link

dzenny commented Aug 3, 2024

Could it be technically feasible and more useful to add direct mathjax support?
That way it would be possible to use tex-formulas not only in markdown, but in other contexts too, like buttons or other control elements.
All my attempts to add standard mathjax initiation script to hdrs, ftrs or Main have failed so far.

@jph00
Copy link
Contributor

jph00 commented Aug 3, 2024

Could it be technically feasible and more useful to add direct mathjax support?

I think that would be an "addition to", not an "instead of"! :D Feel free to create a new issue showing what obstacles you're facing with mathjax, or drop by the discord (https://discord.gg/qcXvcxMhdP).

@yym68686
Copy link
Contributor Author

yym68686 commented Aug 3, 2024

Let's add a bool param to enable/disable it, and default to false. Thanks!

Hello, I have added a parameter to control whether to enable KaTeX rendering.

@yym68686
Copy link
Contributor Author

yym68686 commented Aug 3, 2024

Oh also would be nice if start/end tags were configurable. E.g OpenAI models use different ones.

I have already added custom start/end tags to the MarkdownJS function.

@yym68686
Copy link
Contributor Author

yym68686 commented Aug 3, 2024

This is the new test script: https://pb.yym68686.top/ywjD?lang=python

@jph00 jph00 merged commit e0d5664 into AnswerDotAI:main Aug 3, 2024
@jph00
Copy link
Contributor

jph00 commented Aug 3, 2024

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants