Skip to content

Commit 2967116

Browse files
committed
Add support for latex polls
1 parent f1442d0 commit 2967116

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"js-file-download": "^0.4.12",
2121
"jszip": "^3.10.1",
2222
"jwt-decode": "^3.1.2",
23+
"katex": "^0.16.21",
2324
"moment": "^2.29.4",
2425
"pdf-lib": "^1.17.0",
2526
"pica": "^7.1.0",

src/app.jsx

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import Dexie from 'dexie'
5757
import JSZip from 'jszip'
5858
import QrScanner from 'qr-scanner'
5959
import { JupyterEdit } from '@fails-components/jupyter-react-edit'
60+
import katex from 'katex'
61+
import 'katex/dist/katex.min.css'
6062

6163
QrScanner.WORKER_PATH = new URL(
6264
'../node_modules/qr-scanner/qr-scanner-worker.min.js',
@@ -1324,7 +1326,7 @@ class App extends Component {
13241326
<span className='p-buttonset'>
13251327
{!this.state.ispolledit[node.id] ? (
13261328
<Button
1327-
label={node.name}
1329+
label={this.maybeUseLatex(node.name)}
13281330
className='p-button-text p-button-secondary fails-tree'
13291331
tooltip={'Question text'}
13301332
tooltipOptions={ttopts}
@@ -1437,7 +1439,7 @@ class App extends Component {
14371439
<span className='p-buttonset'>
14381440
{!this.state.ispolledit[node.id] ? (
14391441
<Button
1440-
label={node.name}
1442+
label={this.maybeUseLatex(node.name)}
14411443
className='p-button-text p-button-secondary fails-tree'
14421444
tooltip={'Answer text'}
14431445
tooltipOptions={ttopts}
@@ -1995,6 +1997,56 @@ class App extends Component {
19951997
}
19961998
}
19971999

2000+
// latex duplicate to lecture app, maybe move to other package
2001+
maybeUseLatex(item) {
2002+
return this.detectLatex(item) ? this.convertToLatex(item) : item
2003+
}
2004+
2005+
detectLatex(string) {
2006+
return string.indexOf('$') !== -1
2007+
}
2008+
2009+
convertToLatex(string) {
2010+
const retarray = []
2011+
let secstart = 0
2012+
let seclatex = false
2013+
for (let curpos = 0; curpos < string.length; curpos++) {
2014+
const curchar = string.charAt(curpos)
2015+
if (curchar === '$') {
2016+
if (seclatex) {
2017+
const html = katex.renderToString(
2018+
string.substring(secstart, curpos),
2019+
{
2020+
throwOnError: false,
2021+
displayMode: false
2022+
}
2023+
)
2024+
retarray.push(
2025+
<span dangerouslySetInnerHTML={{ __html: html }}></span>
2026+
)
2027+
secstart = curpos + 1
2028+
seclatex = false
2029+
} else {
2030+
retarray.push(
2031+
<React.Fragment>
2032+
{string.substring(secstart, curpos - 1)}{' '}
2033+
</React.Fragment>
2034+
)
2035+
secstart = curpos + 1
2036+
seclatex = true
2037+
}
2038+
}
2039+
}
2040+
2041+
retarray.push(
2042+
<React.Fragment>
2043+
{string.substring(secstart, string.length)}{' '}
2044+
</React.Fragment>
2045+
)
2046+
2047+
return retarray
2048+
}
2049+
19982050
render() {
19992051
let polldata = []
20002052
let jupyterdata = [

0 commit comments

Comments
 (0)