Skip to content

Commit

Permalink
feat: filter html at competition intro page
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamerblue committed Nov 26, 2024
1 parent 490de15 commit 498469d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
19 changes: 5 additions & 14 deletions src/components/ProblemContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { loadCustomFont, getCustomFontStyleForReact } from '@/utils/customFont';
import ReactPlayer from 'react-player/file';
import { userActiveEmitter, UserActiveEvents } from '@/events/userActive';
import { pickGenshinAudioUrlFromConf } from '@/utils/spGenshin';
import { replaceString } from '@/utils/misc';
import { simpleFilterHTML } from '@/utils/filter';

export interface Props {
loading: boolean;
Expand Down Expand Up @@ -93,15 +93,6 @@ class ProblemContent extends React.Component<Props, State> {
});
};

simpleFilterHTML = (html: string) => {
let res = (html || '').replace(/^&nbsp;/, '').trim();
if (res === '<p></p>') {
res = '';
}
process.env.CDN_PROXY && (res = replaceString(res, [process.env.CDN_RAW_URL_BEFORE_PROXY], process.env.CDN_PROXY));
return res;
};

renderContent = (html: string) => {
return (
<div style={this.getFontStyle()}>
Expand Down Expand Up @@ -129,10 +120,10 @@ class ProblemContent extends React.Component<Props, State> {
}

const spConfig = (data.spConfig || {}) as IProblemSpConfig;
const description = this.simpleFilterHTML(data.description);
const input = this.simpleFilterHTML(data.input || '');
const output = this.simpleFilterHTML(data.output || '');
const hint = this.simpleFilterHTML(data.hint || '');
const description = simpleFilterHTML(data.description);
const input = simpleFilterHTML(data.input || '');
const output = simpleFilterHTML(data.output || '');
const hint = simpleFilterHTML(data.hint || '');
const titlePrefix =
typeof problemAlias === 'string'
? `${problemAlias} - `
Expand Down
3 changes: 2 additions & 1 deletion src/pages/competitions-public/$id/intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import getSetTimeStatus from '@/utils/getSetTimeStatus';
import TimeStatusBadge from '@/components/TimeStatusBadge';
import PageLoading from '@/components/PageLoading';
import { genshinCharacters } from '@/configs/genshin';
import { simpleFilterHTML } from '@/utils/filter';

const CLOTHING_SIZES = ['S', 'M', 'L', 'XL', 'XXL', 'XXXL'];

Expand Down Expand Up @@ -502,7 +503,7 @@ class CompetitionIntro extends React.Component<Props, State> {
<div className="competition-intro-content content-view">
{/* <h2 className="competition-intro-header">{detail.title}</h2> */}
<div
dangerouslySetInnerHTML={{ __html: xss(detail.introduction) }}
dangerouslySetInnerHTML={{ __html: xss(simpleFilterHTML(detail.introduction)) }}
className="content-area"
style={{ marginTop: '48px' }}
/>
Expand Down
15 changes: 3 additions & 12 deletions src/pages/competitions/$id/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import ProblemTitle from '@/components/ProblemTitle';
import { Howl } from 'howler';
import GeneralForm from '@/components/GeneralForm';
import constants from '@/configs/constants';
import { sleep, replaceString } from '@/utils/misc';
import { sleep } from '@/utils/misc';
import GenshinTable from '@/components/GenshinTable';
import { simpleFilterHTML } from '@/utils/filter';

export interface Props extends ReduxProps, FormProps {
id: number;
Expand Down Expand Up @@ -505,16 +506,6 @@ class CompetitionOverview extends React.Component<Props, State> {
});
};

simpleFilterHTML = (html: string) => {
let res = (html || '').replace(/^&nbsp;/, '').trim();
if (res === '<p></p>') {
res = '';
}
process.env.CDN_PROXY &&
(res = replaceString(res, [process.env.CDN_RAW_URL_BEFORE_PROXY], process.env.CDN_PROXY));
return res;
};

renderNormalBody() {
const {
id,
Expand Down Expand Up @@ -546,7 +537,7 @@ class CompetitionOverview extends React.Component<Props, State> {
timeStatus === "Ended" &&
detail.ended &&
[ECompetitionUserRole.admin, ECompetitionUserRole.principal].includes(selfUserDetail?.role);
const announcement = this.simpleFilterHTML(detail.announcement);
const announcement = simpleFilterHTML(detail.announcement);

return (
<Row gutter={16} className="content-view" style={{ marginTop: '84px', marginBottom: '32px' }}>
Expand Down
11 changes: 11 additions & 0 deletions src/utils/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { replaceString } from '@/utils/misc';

export function simpleFilterHTML(html: string) {
let res = (html || '').replace(/^&nbsp;/, '').trim();
if (res === '<p></p>') {
res = '';
}
process.env.CDN_PROXY &&
(res = replaceString(res, [process.env.CDN_RAW_URL_BEFORE_PROXY], process.env.CDN_PROXY));
return res;
}

0 comments on commit 498469d

Please sign in to comment.