Skip to content

Commit 47cce3f

Browse files
authored
Merge pull request #42 from malikpiara/latex
Implemented Latex rendering with Katex
2 parents c912b8d + 9c57d87 commit 47cce3f

File tree

6 files changed

+371
-314
lines changed

6 files changed

+371
-314
lines changed

components/katexSpan.jsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client';
2+
import renderMathInElement from 'katex/dist/contrib/auto-render';
3+
import 'katex/dist/katex.min.css';
4+
import { useEffect, useRef } from 'react';
5+
6+
export default function KatexSpan({ text, ...delegated }) {
7+
const katexTextRef = useRef();
8+
useEffect(() => {
9+
if (katexTextRef.current) {
10+
renderMathInElement(katexTextRef.current, {
11+
delimiters: [
12+
{ left: '$$', right: '$$', display: true },
13+
{ left: '$', right: '$', display: false },
14+
],
15+
});
16+
}
17+
}, [text]);
18+
19+
return (
20+
<div ref={katexTextRef} {...delegated}>
21+
{text}
22+
</div>
23+
);
24+
}

components/option.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import classNames from 'classnames';
22
import React from 'react';
3+
import KatexSpan from './katexSpan';
34

45
export interface OptionProps {
56
ref?: React.Ref<HTMLButtonElement>;
67
index?: number;
78
showIndex?: boolean;
8-
label: string;
9+
label: string | JSX.Element; // Allow label to be string or JSX
910
isSelected: boolean;
1011
isCorrect: boolean;
1112
showSolution: boolean;
@@ -18,15 +19,15 @@ const Option = React.forwardRef<HTMLButtonElement, OptionProps>(
1819
ref
1920
) => {
2021
const optionClasses = classNames(
21-
'w-full ps-4 text-gray-900 flex items-center border rounded-lg focus:outline-primaryColor transition-colors duration-300',
22+
'w-full ps-4 text-gray-900 flex items-center border rounded-lg focus:outline-primary transition-colors duration-300',
2223
{
2324
'border-gray-200': !isSelected && !showSolution,
2425
'cursor-not-allowed': showSolution,
2526
'bg-[#1ad85f]': showSolution && isCorrect,
2627
'border-rose-200 text-red-500': showSolution && !isCorrect,
27-
'border-primaryColor outline-double outline-primaryColor outline-offset-0 ring-2 ring-offset-0 ring-primary':
28+
'border-primary outline-double outline-primary outline-offset-0 ring-2 ring-offset-0 ring-primary':
2829
!showSolution && isSelected,
29-
'hover:border-primaryColor focus:border-primaryColor': !showSolution,
30+
'hover:border-primary focus:border-primary': !showSolution,
3031
}
3132
);
3233

@@ -51,7 +52,9 @@ const Option = React.forwardRef<HTMLButtonElement, OptionProps>(
5152
</div>
5253
</div>
5354
)}
54-
<div className='py-4 ms-2 font-medium'>{label}</div>
55+
<div className='py-4 ms-2 font-medium'>
56+
<KatexSpan text={label} />
57+
</div>
5558
</div>
5659
</button>
5760
);

0 commit comments

Comments
 (0)