-
Notifications
You must be signed in to change notification settings - Fork 15
Add KaTeX rendering engine #14
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
Open
ongchi
wants to merge
15
commits into
main
Choose a base branch
from
katex-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f45ff14
Rename project to wagtail_polymath
ongchi 2b3b9b3
Extract js include path config into config.py
ongchi 4496586
Update ruff GitHub Action
ongchi 11dc0c9
Add support for KaTeX engine
ongchi ddb074b
Renaming classes
ongchi 3326314
Add inline mode support for MathJax
ongchi 944fa13
Add polymath_css template tag
ongchi 0b6c133
Update README
ongchi 4be578f
Add basic test case
ongchi 5f5b0e9
Bump version to 2.0.0
ongchi d278530
Remove CONTRIBUTORS.md
ongchi 0b8a57e
Merge WAGTAILPOLYMATH_SETTINGS and WAGTAILPOLYMATH_ENGINE into WAGTAI…
ongchi dbcf5ab
Validate resource paths in settings
ongchi bd4c365
Update tests
ongchi 4240e58
Update README.md
ongchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| include CONTRIBUTORS.md | ||
| include CONTRIBUTING.md | ||
| include CHANGELOG.md | ||
| include LICENSE | ||
| include README.md | ||
| recursive-include wagtail_polymath *.html *.png *.gif *js *.css *jpg *jpeg *svg *py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,37 +13,20 @@ | |
| - [Discussions](https://github.com/wagtail-nest/wagtail-polymath/discussions) | ||
| - [Security](https://github.com/wagtail-nest/wagtail-polymath/security) | ||
|
|
||
| wagtail-polymath allows you to write equations in your | ||
| [Wagtail](https://github.com/wagtail/wagtail) content using markup and | ||
| render them beautifully. | ||
| `wagtail-polymath` provides robust math typesetting capabilities in LaTeX syntax. | ||
| This project supports multiple typesetting engines, including MathJax and KaTeX, allowing users to choose the one that best fits their needs. | ||
|
|
||
| wagtail-polymath provides a `MathBlock` so you can write equations in markup | ||
| (TeX, MathML, ASCIIMath) and render them with MathJax. It features a | ||
| live preview: | ||
| - [LaTeX Syntax Support](https://en.wikibooks.org/wiki/LaTeX/Mathematics): Write complex mathematical expressions using familiar LaTeX syntax. | ||
| - Multiple Typesetting Engines: Currently, [MathJax](https://www.mathjax.org/) (default) and [KaTeX](https://katex.org/) are supported. | ||
| - Live preview. | ||
|
|
||
|  | ||
|
|
||
| `MathBlock` uses MathJax for rendering so there is very little to do on | ||
| the front end. Simply include the MathJax JS and render the raw | ||
| `MathBlock` content as you would for any other streamfield plain text | ||
| block. | ||
|
|
||
| wagtail-polymath even includes a template tag to include the MathJax JS for | ||
| you from a CDN. By default, MathJax is configured to accept all | ||
| recognised markup (TeX, MathML, ASCIIMath) and renders them to HTML. To | ||
| change the configuration, you can pass the desired config command to the | ||
| templatetag. See the [MathJax documentation](https://docs.mathjax.org/en/v2.7-latest/config-files.html#combined-configurations) | ||
| for possible configurations. | ||
|
|
||
| For help on using the markup languages see the relevant MathJax | ||
| documentation (e.g. https://docs.mathjax.org/en/v2.7-latest/tex.html) and | ||
| the markup language-specific documentation (e.g. https://en.wikibooks.org/wiki/LaTeX) | ||
|
|
||
| ## Quickstart | ||
|
|
||
| Install wagtailmath: | ||
| Install wagtail-polymath: | ||
|
|
||
| pip install wagtailmath | ||
| pip install wagtail-polymath | ||
|
|
||
| Add it to your `INSTALLED_APPS`: | ||
|
|
||
|
|
@@ -52,34 +35,93 @@ Add it to your `INSTALLED_APPS`: | |
|
|
||
| INSTALLED_APPS = ( | ||
| # ... | ||
| "wagtailmath", | ||
| "wagtail_polymath", | ||
| # ... | ||
| ) | ||
| ``` | ||
|
|
||
| Use `MathBlock` in your `StreamField` content: | ||
|
|
||
| ```python | ||
| from wagtailmath.blocks import MathBlock | ||
| from wagtail_polymath.blocks import MathBlock | ||
|
|
||
| class MyPage(Page): | ||
| body = StreamField([ | ||
| ('heading', blocks.CharBlock(classname="full title")), | ||
| ('paragraph', blocks.RichTextBlock()), | ||
| ('equation', MathBlock()) | ||
| ]) | ||
| body = StreamField( | ||
| [ | ||
| ('heading', blocks.CharBlock(classname="full title")), | ||
| ('paragraph', blocks.RichTextBlock()), | ||
| ('equation', MathBlock()) | ||
| ], | ||
| use_json_field=True, | ||
| ) | ||
|
|
||
| content_panels = Page.content_panels + [FieldPanel("body")] | ||
| ``` | ||
|
|
||
| Use the `mathjax` template tag in your front end template to load the | ||
| MathJax library: | ||
| Use the `polymath_js` template tag in your front-end template to load the typesetting library: | ||
|
|
||
| ```django+html | ||
| {% load wagtailmath %} | ||
| ... | ||
| {% load wagtail_polymath %} | ||
|
|
||
| {# include this line in css block #} | ||
| {% polymath_css %} | ||
|
|
||
| {# include this line in js block #} | ||
| {% polymath_js %} | ||
| ``` | ||
|
|
||
| <script src="{% mathjax %}"></script> | ||
| Now you can use LaTeX syntax in your StreamField editor to create mathematical expressions. | ||
| By default, `wagtail-polymath` uses MathJax for rendering, but KaTeX is also supported (see below). | ||
|
|
||
| ## Settings | ||
|
|
||
| ### WAGTAILPOLYMATH_ENGINE | ||
|
|
||
| If you wants to use KaTeX instead of MathJax, simply add the following line to your Django settings: | ||
|
|
||
| ```python | ||
| WAGTAILPOLYMATH_ENGINE = "katex" | ||
ongchi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| This value is set to "mathjax" by default. | ||
|
|
||
| ### WAGTAILPOLYMATH_SETTINGS | ||
ongchi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For those who want to specify the CDN provider or for internal site hosting. | ||
| The rendering javascript library path can be customized for different use cases. | ||
|
|
||
| Default settings for `MathJax`: | ||
|
|
||
| ```python | ||
| WAGTAILPOLYMATH_SETTINGS = { | ||
| "js": [ | ||
| "https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-MML-AM_CHTML", | ||
| ], | ||
| } | ||
| ``` | ||
|
|
||
| Default settings for `KaTeX`: | ||
|
|
||
| ```python | ||
| WAGTAILPOLYMATH_SETTINGS = { | ||
| "js": [ | ||
| "https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js", | ||
| "https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js", | ||
| ], | ||
| "css": ["https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"], | ||
| } | ||
| ``` | ||
|
|
||
| ## Migration from `wagtail-math` | ||
|
|
||
| - Django settings: | ||
| - Replace "wagtailmath" with "wagtail_polymath" in `INSTALLED_APPS` | ||
| - Import section: | ||
| - Replace "from wagtailmath.blocks import MathBlock" with "from wagtail_polymath.blocks import MathBlock" | ||
| - HTML template: | ||
| - Replace "wagtailmath" with "wagtail_polymath" in the load section | ||
| - Add "{% polymath_css %}" to the css block | ||
| - Replace "<script src={% mathjax %}></script>" with "{% polymath_js %}" in the js block | ||
|
|
||
| ## Contributing | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ requires = ["flit_core >=3.2,<4"] | |
| build-backend = "flit_core.buildapi" | ||
|
|
||
| [project] | ||
| name = "wagtailmath" | ||
| name = "wagtail-polymath" | ||
| authors = [{name = "James Ramm", email = "[email protected]"}] | ||
| description = "Wagtail StreamField block for rendering mathematical equations" | ||
| readme = "README.md" | ||
|
|
@@ -52,7 +52,7 @@ Changelog = "https://github.com/wagtail-nest/wagtail-polymath/blob/main/CHANGELO | |
| Documentation = "https://github.com/wagtail-nest/wagtail-polymath/blob/main/docs/" | ||
|
|
||
| [tool.flit.module] | ||
| name = "wagtailmath" | ||
| name = "wagtail_polymath" | ||
|
|
||
| [tool.flit.sdist] | ||
| exclude = [ | ||
|
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class WagtailPolymathConfig(AppConfig): | ||
| name = "wagtail_polymath" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from django.conf import settings | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note to self: move settings to a lazy object a la Django settings |
||
| from django.contrib.staticfiles import finders | ||
| from django.core.exceptions import ImproperlyConfigured | ||
| from wagtail import VERSION as WAGTAIL_VERSION | ||
| from wagtail.admin.staticfiles import versioned_static | ||
|
|
||
|
|
||
| TYPESETTING_ENGINE = getattr(settings, "WAGTAILPOLYMATH_ENGINE", "mathjax") | ||
|
|
||
| # The path could be static files or external URLs. | ||
| DEFAULT_MATHJAX_SETTINGS = { | ||
| "js": [ | ||
| "https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-MML-AM_CHTML", | ||
| ], | ||
| "widget": [ | ||
| "wagtail_polymath/js/polymath-widget-mathjax.js", | ||
| ], | ||
| } | ||
|
|
||
| DEFAULT_KATEX_SETTINGS = { | ||
| "js": [ | ||
| "https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js", | ||
| "https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js", | ||
| ], | ||
| "widget": [ | ||
| "wagtail_polymath/js/polymath-widget-katex.js", | ||
| ], | ||
| "css": ["https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"], | ||
| } | ||
|
|
||
| if TYPESETTING_ENGINE == "mathjax": | ||
| DEFAULT_SETTINGS = DEFAULT_MATHJAX_SETTINGS | ||
| elif TYPESETTING_ENGINE == "katex": | ||
| DEFAULT_SETTINGS = DEFAULT_KATEX_SETTINGS | ||
| else: | ||
| raise ImproperlyConfigured( | ||
| f"WAGTAILPOLYMATH_ENGINE: invalid typesetting engine: `{TYPESETTING_ENGINE}` " | ||
| "(valid options: 'mathjax', 'katex')" | ||
| ) | ||
|
|
||
| if WAGTAIL_VERSION >= (6, 0): | ||
| DEFAULT_SETTINGS["widget"].append( | ||
| "wagtail_polymath/js/polymath-textarea-controller.js", | ||
| ) | ||
| else: | ||
| DEFAULT_SETTINGS["widget"].append( | ||
| "wagtail_polymath/js/polymath-textarea-adapter.js", | ||
| ) | ||
|
|
||
| WAGTAILPOLYMATH_SETTINGS = DEFAULT_SETTINGS | ||
| WAGTAILPOLYMATH_SETTINGS.update(getattr(settings, "WAGTAILPOLYMATH_SETTINGS", {})) | ||
|
|
||
|
|
||
| def get_polymath_config(key): | ||
| paths = WAGTAILPOLYMATH_SETTINGS.get(key, []) | ||
|
|
||
| # Return absolute path to the asset if it's a static file path. | ||
| return [versioned_static(path) if finders.find(path) else path for path in paths] | ||
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/wagtail_polymath/static/wagtail_polymath/js/polymath-textarea-controller.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class PolymathTextareaController extends window.StimulusModule.Controller { | ||
| connect() { | ||
| initPolymathTextareaPreview(this.element.id); | ||
| } | ||
| } | ||
|
|
||
| window.wagtail.app.register('polymath-textarea-controller', PolymathTextareaController); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.