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 LaTeX support with MathJax #201

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">

<script type="text/javascript" src="https://unpkg.com/zip-js@0.0.2/WebContent/zip.js"></script>
<script type="text/javascript" src="https://unpkg.com/sanitize-html@1.18.2/dist/sanitize-html.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/sanitize-html@1.18.2/dist/sanitize-html.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/mathjax@3.2.2/es5/tex-mml-chtml.js" async></script>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of using external CDNs, but since it is not something that this PR introduces, that's okay for now.

We'll need to upgrade a lot of tickets shortly if we want to maintain/keep this project in good shape, so at that point, we can reconsider how we want to include these dependencies.


<title>common-cartridge-viewer</title>
</head>
Expand Down
Binary file added public/test-cartridges/latex.imscc
Binary file not shown.
3 changes: 2 additions & 1 deletion src/RichContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
CANVAS_OBJECT_REFERENCE,
resourceTypeToHref
} from "./constants";
import { getFileResourcePath } from "./utils";
import { getFileResourcePath, mathJaxTypeset } from "./utils";
woodie marked this conversation as resolved.
Show resolved Hide resolved
import Text from "@instructure/ui-elements/lib/components/Text";
import _ from "lodash";

Expand Down Expand Up @@ -169,6 +169,7 @@ export default class RichContent extends Component {
if (this.contentNode) {
this.contentNode.appendChild(fragment);
}
setTimeout(mathJaxTypeset, 0);
}

setContentRef = node => {
Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const zip = window.zip;

export const pipe = (g, f) => x => f(g(x));

export function mathJaxTypeset() {
if (typeof MathJax !== "undefined") {
// eslint-disable-next-line no-undef
woodie marked this conversation as resolved.
Show resolved Hide resolved
MathJax.typesetPromise();
}
}

export function getReaderFromXHR(url) {
const request = new XMLHttpRequest();
const promise = new Promise((resolve, reject) => {
Expand Down
22 changes: 22 additions & 0 deletions tests/test.latex-equations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Selector } from "testcafe";

fixture`LaTeX Equations`
.page`http://localhost:5000/?cartridge=${encodeURIComponent(
"/test-cartridges/latex.imscc"
)}#/`;

test("Page 1 displays MathJax", async t => {
const modulePage1 = Selector("a").withText("Page 1");
await t
.click(modulePage1)
.expect(Selector("mjx-container").withAttribute("class", "MathJax"))
.ok();
});

test("Page 2 displays MathJax", async t => {
const modulePage2 = Selector("a").withText("Page 2");
await t
.click(modulePage2)
.expect(Selector("mjx-container").withAttribute("class", "MathJax"))
.ok();
});