Skip to content

Commit

Permalink
[fix, feature] Support other code languages besides HTML (#708)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Seabock (Centific Technologies Inc) <[email protected]>
  • Loading branch information
iseabock and Ian Seabock (Centific Technologies Inc) committed Mar 26, 2024
1 parent 1942217 commit 68c8051
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
18 changes: 18 additions & 0 deletions frontend/src/components/Answer/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { parseAnswer } from "./AnswerParser";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import supersub from 'remark-supersub'
import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter';
import { nord } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { ThumbDislike20Filled, ThumbLike20Filled } from "@fluentui/react-icons";
import { XSSAllowTags } from "../../constants/xssAllowTags";

Expand Down Expand Up @@ -178,6 +180,21 @@ export const Answer = ({
);
}

const components = {
code({node, ...props}: {node: any, [key: string]: any}) {
let language;
if (props.className) {
const match = props.className.match(/language-(\w+)/);
language = match ? match[1] : undefined;
}
const codeString = node.children[0].value ?? '';
return (
<SyntaxHighlighter style={nord} language={language} PreTag="div" {...props}>
{codeString}
</SyntaxHighlighter>
);
},
};
return (
<>
<Stack className={styles.answerContainer} tabIndex={0}>
Expand All @@ -190,6 +207,7 @@ export const Answer = ({
remarkPlugins={[remarkGfm, supersub]}
children={SANITIZE_ANSWER ? DOMPurify.sanitize(parsedAnswer.markdownFormatText, {ALLOWED_TAGS: XSSAllowTags}) : parsedAnswer.markdownFormatText}
className={styles.answerText}
components={components}
/>
</Stack.Item>
<Stack.Item className={styles.answerHeader}>
Expand Down
Loading

4 comments on commit 68c8051

@mmdmirh
Copy link

Choose a reason for hiding this comment

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

Hi,
I have tried in another way but it has some other issues such as not being responsive :(

Screen.Recording.1402-12-27.at.12.47.55.at.night.mov

@hunguyenv
Copy link

Choose a reason for hiding this comment

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

The react-syntax-highlighter is not installed.

@mmdmirh
Copy link

@mmdmirh mmdmirh commented on 68c8051 Apr 2, 2024

Choose a reason for hiding this comment

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

@hunguyenv
hi, Actually I installed that. Thats why the frontend could be created. But i do not know why the answer the width is not being smaller than a size.

@abhahn
Copy link
Member

@abhahn abhahn commented on 68c8051 Apr 4, 2024

Choose a reason for hiding this comment

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

Hey @hunguyenv / @mmdmirh , thanks for your comments. Just FYI, I've just recently merged a PR to fix this issue, and also added a workflow to build the frontend as a part of the PR gating process, so hopefully issues like this will be easier to catch later.

Please sign in to comment.