Skip to content

Commit

Permalink
Merge pull request #42 from malikpiara/latex
Browse files Browse the repository at this point in the history
Implemented Latex rendering with Katex
  • Loading branch information
malikpiara authored May 7, 2024
2 parents c912b8d + 9c57d87 commit 47cce3f
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 314 deletions.
24 changes: 24 additions & 0 deletions components/katexSpan.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';
import renderMathInElement from 'katex/dist/contrib/auto-render';
import 'katex/dist/katex.min.css';
import { useEffect, useRef } from 'react';

export default function KatexSpan({ text, ...delegated }) {
const katexTextRef = useRef();
useEffect(() => {
if (katexTextRef.current) {
renderMathInElement(katexTextRef.current, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
],
});
}
}, [text]);

return (
<div ref={katexTextRef} {...delegated}>
{text}
</div>
);
}
13 changes: 8 additions & 5 deletions components/option.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import classNames from 'classnames';
import React from 'react';
import KatexSpan from './katexSpan';

export interface OptionProps {
ref?: React.Ref<HTMLButtonElement>;
index?: number;
showIndex?: boolean;
label: string;
label: string | JSX.Element; // Allow label to be string or JSX
isSelected: boolean;
isCorrect: boolean;
showSolution: boolean;
Expand All @@ -18,15 +19,15 @@ const Option = React.forwardRef<HTMLButtonElement, OptionProps>(
ref
) => {
const optionClasses = classNames(
'w-full ps-4 text-gray-900 flex items-center border rounded-lg focus:outline-primaryColor transition-colors duration-300',
'w-full ps-4 text-gray-900 flex items-center border rounded-lg focus:outline-primary transition-colors duration-300',
{
'border-gray-200': !isSelected && !showSolution,
'cursor-not-allowed': showSolution,
'bg-[#1ad85f]': showSolution && isCorrect,
'border-rose-200 text-red-500': showSolution && !isCorrect,
'border-primaryColor outline-double outline-primaryColor outline-offset-0 ring-2 ring-offset-0 ring-primary':
'border-primary outline-double outline-primary outline-offset-0 ring-2 ring-offset-0 ring-primary':
!showSolution && isSelected,
'hover:border-primaryColor focus:border-primaryColor': !showSolution,
'hover:border-primary focus:border-primary': !showSolution,
}
);

Expand All @@ -51,7 +52,9 @@ const Option = React.forwardRef<HTMLButtonElement, OptionProps>(
</div>
</div>
)}
<div className='py-4 ms-2 font-medium'>{label}</div>
<div className='py-4 ms-2 font-medium'>
<KatexSpan text={label} />
</div>
</div>
</button>
);
Expand Down
Loading

1 comment on commit 47cce3f

@vercel
Copy link

@vercel vercel bot commented on 47cce3f May 7, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.