Skip to content

Commit 14fd0e0

Browse files
committed
Add parsing of html question descriptions
1 parent 201c213 commit 14fd0e0

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

frontend/package-lock.json

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

frontend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"axios": "^1.5.1",
1717
"dotenv": "^16.3.1",
1818
"firebase": "^10.4.0",
19+
"html-entities": "^2.4.0",
20+
"htmr": "^1.0.2",
1921
"https-browserify": "^1.0.0",
2022
"js-sha256": "^0.10.1",
2123
"monaco-editor": "^0.44.0",

frontend/src/components/CollabProblemSolverLeft.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useEffect } from "react";
22
import { Paper, Typography, Divider, CardMedia } from "@mui/material";
33
import { useData } from "../data/data.context";
4+
import htmr from "htmr";
5+
import {decode} from "html-entities";
6+
import {parseHtmlDescription} from "../utils/utils";
47

58
function CollabProblemSolverLeft({
69
questionNumber,
@@ -43,10 +46,8 @@ function CollabProblemSolverLeft({
4346
{question.title}
4447
</Typography>
4548
<Divider sx={{ marginBottom: 2, marginTop: 5 }} />
46-
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: "18px" }}>
47-
{question.description.split("\\n").map((s, key) => {
48-
return <p key={key}>{s}</p>;
49-
})}
49+
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: "18px", overflowX: 'auto' }}>
50+
{parseHtmlDescription(question.description)}
5051
</Typography>
5152
<Divider sx={{ marginBottom: 10 }} />
5253
<Typography variant="h6" gutterBottom sx={{ fontSize: "18px" }}>

frontend/src/components/ProblemSolverLeft.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
CardMedia,
99
} from '@mui/material';
1010
import { useData } from '../data/data.context';
11+
import parse from 'html-react-parser';
12+
import {decode} from "html-entities";
13+
import {parseHtmlDescription} from "../utils/utils";
1114
// import { ClosedCaptionDisabledSharp } from '@mui/icons-material';
1215

1316
const ProblemSolverLeft = () => {
@@ -45,10 +48,8 @@ const ProblemSolverLeft = () => {
4548
{question.title}
4649
</Typography>
4750
<Divider sx={{ marginBottom: 2, marginTop: 5 }} />
48-
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: '18px' }}>
49-
{question.description.split("\\n").map((s, key) => {
50-
return <p key={key}>{s}</p>;
51-
})}
51+
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: '18px', overflowX: 'auto' }}>
52+
{parseHtmlDescription(question.description)}
5253
</Typography>
5354
<Divider sx={{ marginBottom: 10 }} />
5455
<Typography variant="h6" gutterBottom sx={{ fontSize: '18px' }}>

frontend/src/components/Questions/EditQuestionTab.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Grid from '@mui/material/Grid';
55
import Button from '@mui/material/Button';
66
import {Box} from "@mui/material";
77
import QuestionForm from "./QuestionForm";
8+
import {parseHtmlDescription} from "../../utils/utils";
89

910
interface EditQuestionPreviewProps {
1011
question: Question;
@@ -47,10 +48,8 @@ const EditQuestionTab: React.FC<EditQuestionPreviewProps> = ({question, onEdit,
4748
<Typography variant="h5" gutterBottom component="div">
4849
{question.title}
4950
</Typography>
50-
<Typography variant="body2" gutterBottom component="div" sx={{ whiteSpace: 'pre-line'}}>
51-
{question.description.split("\\n").map((s, key) => {
52-
return <p key={key}>{s}</p>;
53-
})}
51+
<Typography variant="body2" gutterBottom component="div">
52+
{parseHtmlDescription(question.description)}
5453
</Typography>
5554
<br />
5655
{question.constraints.length > 0 &&

frontend/src/components/Questions/QuestionsTable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Typography,
2121
} from "@mui/material";
2222
import { useData } from "../../data/data.context";
23+
import {parseHtmlDescription} from "../../utils/utils";
2324

2425
interface Question {
2526
id: string;
@@ -202,7 +203,7 @@ const InterviewQuestionsTable: React.FC = () => {
202203
<b>Difficulty:</b> {selectedQuestion.difficulty}
203204
</Typography>
204205
<Typography variant="body2" style={{ padding: "5px" }}>
205-
<b>Description</b>: {selectedQuestion.description}
206+
<b>Description</b>: {parseHtmlDescription(selectedQuestion.description)}
206207
</Typography>
207208
</DialogContent>
208209
<DialogActions>

frontend/src/utils/utils.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {decode} from "html-entities";
2+
import htmr from "htmr";
3+
import {ReactNode} from "react";
4+
5+
export function parseHtmlDescription(description: string): ReactNode {
6+
// Decode escaped HTML characters and add text wrap to pre tags in the question description
7+
let decodedDescription = decode(description)
8+
.replace(/<pre>/g, "<pre style=\"white-space: pre-wrap;\">");
9+
return htmr(decodedDescription);
10+
}

0 commit comments

Comments
 (0)