Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): support source code syntax highlighting in statement text #561

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentCard } from '../ContentCard/ContentCard';
import { KatexText } from '../KatexText/KatexText';
import RichStatementText from '../RichStatementText/RichStatementText';

import './LessonStatementCard.scss';

Expand All @@ -10,7 +10,7 @@ export function LessonStatementCard({ alias, statement }) {
{alias}. {statement.title}
</h2>
<div className="lesson-statement__text">
<KatexText key={alias}>{statement.text}</KatexText>
<RichStatementText key={alias}>{statement.text}</RichStatementText>
</div>
</ContentCard>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fragment } from 'react';

import { UserRef } from '../UserRef/UserRef';
import { KatexText } from '../KatexText/KatexText';
import RichStatementText from '../RichStatementText/RichStatementText';

export function ProblemEditorial({ title, settersMap, profilesMap, children }) {
const renderWriters = () => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export function ProblemEditorial({ title, settersMap, profilesMap, children }) {
{renderEditorialists()}
</ul>
<hr />
<KatexText key={title}>{children}</KatexText>
<RichStatementText key={title}>{children}</RichStatementText>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentCard } from '../../../ContentCard/ContentCard';
import { KatexText } from '../../../KatexText/KatexText';
import RichStatementText from '../../../RichStatementText/RichStatementText';

export function ProblemEditorialCard({ alias, statement: { title }, editorial: { text }, titleSuffix }) {
return (
Expand All @@ -10,7 +10,7 @@ export function ProblemEditorialCard({ alias, statement: { title }, editorial: {
{titleSuffix}
</h2>
<div className="programming-problem-statement__text">
<KatexText key={alias}>{text}</KatexText>
<RichStatementText key={alias}>{text}</RichStatementText>
</div>
</ContentCard>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HTMLTable } from '@blueprintjs/core';

import { ContentCard } from '../../../ContentCard/ContentCard';
import { KatexText } from '../../../KatexText/KatexText';
import RichStatementText from '../../../RichStatementText/RichStatementText';

import './ProblemStatementCard.scss';

Expand Down Expand Up @@ -45,7 +45,7 @@ export function ProblemStatementCard({ alias, statement: { title, text }, limits
</tbody>
</HTMLTable>
<div className="programming-problem-statement__text">
<KatexText key={alias}>{text}</KatexText>
<RichStatementText key={alias}>{text}</RichStatementText>
</div>
</ContentCard>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createRef, PureComponent } from 'react';
import HTMLReactParser from 'html-react-parser';
import render from 'preact-render-to-string';
import { createRef, Component } from 'react';
import { connect } from 'react-redux';

import { HtmlText } from '../HtmlText/HtmlText';
import { SourceCode } from '../SourceCode/SourceCode';
import { selectIsDarkMode } from '../../modules/webPrefs/webPrefsSelectors';

import './KatexText.scss';
import './RichStatementText.scss';

export class KatexText extends PureComponent {
export class RichStatementText extends Component {
ref;

constructor(props) {
Expand Down Expand Up @@ -54,10 +59,27 @@ export class KatexText extends PureComponent {
}

render() {
const { isDarkMode, children } = this.props;

let str = children;

str = str.replace(/<pre data-lang="(.+?)">(.*?)<\/pre>/gs, (match, lang, code) => {
return render(
<SourceCode isDarkMode={isDarkMode} language={lang} showLineNumbers={false}>
{HTMLReactParser(code.trim())}
</SourceCode>
);
});

return (
<div className="katex-wrapper" ref={this.ref}>
<HtmlText>{this.props.children}</HtmlText>
<div className="rich-statement-text" ref={this.ref}>
<HtmlText>{str}</HtmlText>
</div>
);
}
}
const mapStateToProps = state => ({
isDarkMode: selectIsDarkMode(state),
});

export default connect(mapStateToProps)(RichStatementText);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import '../../styles/base';

.katex-wrapper {
.rich-statement-text {
.katex {
font-size: 16px !important;
}
Expand Down
4 changes: 2 additions & 2 deletions judgels-client/src/components/SourceCode/SourceCode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ registerLanguage('pascal', pascal);
registerLanguage('python', python);
registerLanguage('rust', rust);

function SourceCode({ isDarkMode, language, children }) {
export function SourceCode({ isDarkMode, language, showLineNumbers = true, children }) {
return (
<SyntaxHighlighter
className="source-code"
style={isDarkMode ? tomorrow : coy}
language={language}
wrapLines={true}
showLineNumbers={true}
showLineNumbers={showLineNumbers}
lineNumberContainerStyle={{
backgroundColor: isDarkMode ? '#394B59' : '#f0f0f0',
float: 'left',
Expand Down
8 changes: 7 additions & 1 deletion judgels-client/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ pre {
}

pre.source-code {
background-color: inherit !important;
border-color: #cccccc;
background-color: #f5f5f5 !important;
}

.bp4-dark pre.source-code {
border-color: $dark-gray2;
background-color: $dark-gray3 !important;
}

em {
Expand Down