Skip to content

Commit 3bb174d

Browse files
authored
add one-lang-only components (#3174)
1 parent 3771b45 commit 3bb174d

File tree

4 files changed

+76
-18
lines changed

4 files changed

+76
-18
lines changed

CONTRIBUTING.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# Contributing
22

3-
Check the [Contributing module](https://usaco.guide/general/contributing)
4-
module.
3+
Check the [Contributing](https://usaco.guide/general/contributing) module.

content/1_General/Working_MDX.mdx

+26
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ imported):
118118
- `<CPPSection>`
119119
- `<JavaSection>`
120120
- `<PySection>`
121+
- `<CPPOnly>`
122+
- `<JavaOnly>`
123+
- `<PyOnly>`
121124
- `<IncompleteSection>`
122125
- `<Asterisk>`
123126
- `<Quiz>`
@@ -557,6 +560,29 @@ Java code here
557560
<PySection />
558561
</LanguageSection>
559562

563+
If you want to render content for only a single language, it is more convenient
564+
to use `CPPOnly`, `JavaOnly`, or `PyOnly`:
565+
566+
````
567+
<CPPOnly>
568+
#### A heading that only appears in C++
569+
570+
```cpp
571+
C++ code here
572+
```
573+
574+
</CPPOnly>
575+
````
576+
577+
<CPPOnly>
578+
#### A heading that only appears in C++
579+
580+
```cpp
581+
C++ code here
582+
```
583+
584+
</CPPOnly>
585+
560586
### Quizzes
561587

562588
````

src/components/markdown/LanguageSection.tsx

+43-16
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,8 @@ import { LANGUAGE_LABELS } from '../../context/UserDataContext/properties/userLa
44
import UserDataContext from '../../context/UserDataContext/UserDataContext';
55
import Danger from './Danger';
66

7-
export const LanguageSection = (props: {
8-
children?: React.ReactNode;
9-
}): JSX.Element => {
7+
const sectionFromLang = sections => {
108
const { lang: userLang } = useContext(UserDataContext);
11-
12-
const sections = {};
13-
React.Children.map(props.children, child => {
14-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15-
const type = (child as any).type.name;
16-
const typeToLang = {
17-
CPPSection: 'cpp',
18-
JavaSection: 'java',
19-
PySection: 'py',
20-
};
21-
sections[typeToLang[type]] = child;
22-
});
23-
249
if (userLang === 'showAll') {
2510
return (
2611
<>
@@ -77,6 +62,48 @@ export const LanguageSection = (props: {
7762
return sections[userLang];
7863
};
7964

65+
export const LanguageSection = (props: {
66+
children?: React.ReactNode;
67+
}): JSX.Element => {
68+
const sections = {};
69+
React.Children.map(props.children, child => {
70+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
71+
const type = (child as any).type.name;
72+
const typeToLang = {
73+
CPPSection: 'cpp',
74+
JavaSection: 'java',
75+
PySection: 'py',
76+
};
77+
sections[typeToLang[type]] = child;
78+
});
79+
return sectionFromLang(sections);
80+
};
81+
82+
export const CPPOnly = (props: { children?: React.ReactNode }): JSX.Element => {
83+
return sectionFromLang({
84+
cpp: props.children,
85+
java: <></>,
86+
py: <></>,
87+
});
88+
};
89+
export const JavaOnly = (props: {
90+
children?: React.ReactNode;
91+
}): JSX.Element => {
92+
return sectionFromLang({
93+
cpp: <></>,
94+
java: props.children,
95+
py: <></>,
96+
});
97+
};
98+
99+
export const PyOnly = (props: { children?: React.ReactNode }): JSX.Element => {
100+
return sectionFromLang({
101+
cpp: <></>,
102+
java: <></>,
103+
py: props.children,
104+
});
105+
};
106+
80107
export const CPPSection = (props: {
81108
children?: React.ReactNode;
82109
}): JSX.Element => {

src/components/markdown/MDXComponents.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import HTMLComponents from './HTMLComponents';
66
import { IncompleteSection } from './IncompleteSection';
77
import Info from './Info';
88
import {
9+
CPPOnly,
910
CPPSection,
11+
JavaOnly,
1012
JavaSection,
1113
LanguageSection,
14+
PyOnly,
1215
PySection,
1316
} from './LanguageSection';
1417
import Optional from './Optional';
@@ -51,6 +54,9 @@ export const components = {
5154
DivisionList,
5255
Resource,
5356
TextTooltip,
57+
CPPOnly,
58+
JavaOnly,
59+
PyOnly,
5460
LanguageSection,
5561
CPPSection,
5662
JavaSection,

0 commit comments

Comments
 (0)