Skip to content

Commit 482570c

Browse files
authored
Merge pull request #107 from CS3219-AY2324S1/add-html-parsing
Add html parsing
2 parents 525d4ed + 893db09 commit 482570c

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect } from "react";
22
import { Paper, Typography, Divider, CardMedia } from "@mui/material";
33
import { useData } from "../data/data.context";
4+
import {parseHtmlDescription} from "../utils/utils";
45

56
function CollabProblemSolverLeft({
67
questionNumber,
@@ -43,10 +44,8 @@ function CollabProblemSolverLeft({
4344
{question.title}
4445
</Typography>
4546
<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-
})}
47+
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: "18px", overflowX: 'auto' }}>
48+
{parseHtmlDescription(question.description)}
5049
</Typography>
5150
<Divider sx={{ marginBottom: 10 }} />
5251
<Typography variant="h6" gutterBottom sx={{ fontSize: "18px" }}>

frontend/src/components/ProblemSolverLeft.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CardMedia,
99
} from '@mui/material';
1010
import { useData } from '../data/data.context';
11+
import {parseHtmlDescription} from "../utils/utils";
1112
// import { ClosedCaptionDisabledSharp } from '@mui/icons-material';
1213

1314
const ProblemSolverLeft = () => {
@@ -45,10 +46,8 @@ const ProblemSolverLeft = () => {
4546
{question.title}
4647
</Typography>
4748
<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-
})}
49+
<Typography variant="body1" sx={{ marginBottom: 2, fontSize: '18px', overflowX: 'auto' }}>
50+
{parseHtmlDescription(question.description)}
5251
</Typography>
5352
<Divider sx={{ marginBottom: 10 }} />
5453
<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: 4 additions & 2 deletions
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;
@@ -122,7 +123,7 @@ const InterviewQuestionsTable: React.FC = () => {
122123
</MenuItem>
123124
))}
124125
</Select>
125-
<div style={{ maxHeight: "300px", overflowY: "auto", width: "80%" }}>
126+
<div style={{ maxHeight: "800px", overflowY: "auto", width: "80%" , display: "flex"}}>
126127
<TableContainer
127128
component={Paper}
128129
style={{ margin: "10px", padding: "10px" }}
@@ -148,6 +149,7 @@ const InterviewQuestionsTable: React.FC = () => {
148149
fontSize: "16px",
149150
fontWeight: "bold",
150151
textTransform: "initial",
152+
textAlign: "left",
151153
}}
152154
>
153155
{question.title}
@@ -201,7 +203,7 @@ const InterviewQuestionsTable: React.FC = () => {
201203
<b>Difficulty:</b> {selectedQuestion.difficulty}
202204
</Typography>
203205
<Typography variant="body2" style={{ padding: "5px" }}>
204-
<b>Description</b>: {selectedQuestion.description}
206+
<b>Description</b>: {parseHtmlDescription(selectedQuestion.description)}
205207
</Typography>
206208
</DialogContent>
207209
<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)