Skip to content

Commit 4bddb0e

Browse files
authored
Merge pull request #155 from chvmvd/add-latex-playground
Add LaTeX playground
2 parents e13ce13 + 66d7825 commit 4bddb0e

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

src/components/InteractiveCodeEditor/index.tsx

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,9 @@ import TabItem from "@theme/TabItem";
44
import Editor from "@monaco-editor/react";
55
import IframeOutput from "@site/src/components/IframeOutput";
66
import mdToHTML from "@site/src/components/mdToHTML";
7+
import latexToHTML from "@site/src/components/latexToHTML";
78
import styles from "./styles.module.css";
89

9-
function latexRenderer(value: string): string {
10-
const header = `\
11-
<!DOCTYPE html>
12-
<html lang="ja">
13-
<head>
14-
<meta charset="UTF-8" />
15-
<script type="module">
16-
import { LaTeXJSComponent } from "https://cdn.jsdelivr.net/npm/latex.js/dist/latex.mjs";
17-
customElements.define("latex-js", LaTeXJSComponent);
18-
</script>
19-
</head>
20-
<body>
21-
<latex-js baseURL="https://cdn.jsdelivr.net/npm/latex.js/dist/">\
22-
`;
23-
const footer = `\
24-
</latex-js>
25-
</body>
26-
</html>\
27-
`;
28-
return header + value + footer;
29-
}
30-
3110
function HtmlCssJavascriptEditor({
3211
defaultHTML,
3312
setHTML,
@@ -175,7 +154,7 @@ export default function InteractiveCodeEditor({
175154
: language === "markdown"
176155
? mdToHTML(code)
177156
: language === "latex"
178-
? latexRenderer(code)
157+
? latexToHTML(code)
179158
: language === "html-css"
180159
? `<style>${css}</style>${code}`
181160
: `<style>${css}</style>${code}<script>${js}</script>`}

src/components/latexToHTML/index.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default function latexToHTML(value: string): string {
2+
const header = `\
3+
<!DOCTYPE html>
4+
<html lang="ja">
5+
<head>
6+
<meta charset="UTF-8" />
7+
<script type="module">
8+
import { LaTeXJSComponent } from "https://cdn.jsdelivr.net/npm/latex.js/dist/latex.mjs";
9+
customElements.define("latex-js", LaTeXJSComponent);
10+
</script>
11+
</head>
12+
<body>
13+
<latex-js baseURL="https://cdn.jsdelivr.net/npm/latex.js/dist/">\
14+
`;
15+
const footer = `\
16+
</latex-js>
17+
</body>
18+
</html>\
19+
`;
20+
return header + value + footer;
21+
}

src/pages/latex-playground/index.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { useState } from "react";
2+
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
3+
import Layout from "@theme/Layout";
4+
import Editor from "@monaco-editor/react";
5+
import IframeOutput from "@site/src/components/IframeOutput";
6+
import { Grid, Box } from "@mui/material";
7+
import latexToHTML from "@site/src/components/latexToHTML";
8+
9+
const defaultLatex = `\
10+
\\documentclass{article}
11+
\\begin{document}
12+
Hello World!
13+
\\[
14+
a^2 + b^2 = c^2
15+
\\]
16+
\\end{document}
17+
\
18+
`;
19+
20+
export default function Home(): JSX.Element {
21+
const { siteConfig } = useDocusaurusContext();
22+
const [latex, setLatex] = useState<string>(defaultLatex);
23+
return (
24+
<Layout
25+
title={`Hello from ${siteConfig.title}`}
26+
description="This is a playground of LaTeX"
27+
>
28+
<Box p={2} mt={2}>
29+
<Grid container direction="row" spacing={2}>
30+
<Grid item xs={6}>
31+
<Editor
32+
height="80vh"
33+
defaultLanguage="html"
34+
defaultValue={defaultLatex}
35+
onChange={(value) => {
36+
setLatex(value);
37+
}}
38+
/>
39+
</Grid>
40+
<Grid item xs={6}>
41+
<IframeOutput height="80vh">{latexToHTML(latex)}</IframeOutput>
42+
</Grid>
43+
</Grid>
44+
</Box>
45+
</Layout>
46+
);
47+
}

0 commit comments

Comments
 (0)