Skip to content

Commit 4e19097

Browse files
committed
Refactor Question component
1 parent 36455ad commit 4e19097

File tree

13 files changed

+968
-689
lines changed

13 files changed

+968
-689
lines changed

.eslintrc.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
'plugin:react/recommended',
99
'plugin:@typescript-eslint/recommended',
1010
'plugin:eslint-comments/recommended',
11+
'prettier',
1112
],
1213
globals: {
1314
Atomics: 'readonly',
@@ -21,7 +22,13 @@ module.exports = {
2122
ecmaVersion: 2018,
2223
sourceType: 'module',
2324
},
24-
plugins: ['react', 'react-hooks', '@typescript-eslint', 'eslint-comments'],
25+
plugins: [
26+
'react',
27+
'react-hooks',
28+
'@typescript-eslint',
29+
'eslint-comments',
30+
'prettier',
31+
],
2532
rules: {
2633
// Allow JSX appearance in files with these extensions
2734
'react/jsx-filename-extension': [
@@ -35,7 +42,7 @@ module.exports = {
3542
'error',
3643
{
3744
// built-in imports in Docusaurus v2
38-
ignore: ['^@theme', '^@docusaurus'],
45+
ignore: ['^@theme', '^@docusaurus', '^@site'],
3946
},
4047
],
4148
'import/no-duplicates': 'off',
@@ -47,8 +54,8 @@ module.exports = {
4754
{ nonEmpty: 'after-props', selfClosing: 'line-aligned' },
4855
],
4956
'react/jsx-props-no-spreading': 'off',
50-
// Conflicts with Prettier
51-
'object-curly-newline': 'off',
57+
'@typescript-eslint/no-non-null-assertion': 'off',
58+
'@typescript-eslint/no-unused-vars': ['warn', { ignoreRestSiblings: true }],
5259
},
5360
settings: {
5461
'import/resolver': {

.yarn/releases/yarn-3.1.0.cjs

Lines changed: 768 additions & 0 deletions
Large diffs are not rendered by default.

.yarn/releases/yarn-berry.cjs

Lines changed: 0 additions & 631 deletions
This file was deleted.

.yarnrc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
yarnPath: ".yarn/releases/yarn-berry.cjs"
21
nodeLinker: node-modules
2+
3+
yarnPath: .yarn/releases/yarn-3.1.0.cjs

blog/2020-10-10-algorithm1.mdx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,11 @@ tags: [algorithm]
1111

1212
<!--truncate-->
1313

14-
export const Question = ({ children, color }) => (
15-
<div
16-
style={{
17-
backgroundColor: color,
18-
borderRadius: '10px',
19-
color: '#fff',
20-
padding: '1em',
21-
marginBottom: '1em',
22-
marginTop: '1em',
23-
}}
24-
>
25-
{children}
26-
</div>
27-
);
28-
2914
## 题目
3015

31-
<Question color="#1d85c2">
32-
斐波那契数列:
16+
<question>
17+
18+
斐波那契数列:
3319

3420
$$
3521
F_{n}=
@@ -46,7 +32,7 @@ $$
4632
| ------------------- | -------- | -------- |
4733
| $0\le n\le 10^{19}$ | 64MB | 1.0s |
4834

49-
</Question>
35+
</question>
5036

5137
## 题解
5238

blog/2020-11-04-algorithm3.mdx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,11 @@ tags: [algorithm]
1111

1212
<!--truncate-->
1313

14-
export const Question = ({ children, color }) => (
15-
<div
16-
style={{
17-
backgroundColor: color,
18-
borderRadius: '10px',
19-
color: '#fff',
20-
padding: '1em',
21-
marginBottom: '1em',
22-
marginTop: '1em',
23-
}}
24-
>
25-
{children}
26-
</div>
27-
);
28-
2914
## 题目
3015

31-
<Question color="#1d85c2">
32-
<a href="https://www.luogu.com.cn/problem/P3989">原题链接</a>
16+
<question>
17+
18+
**题目来源:[洛谷 P3989](https://www.luogu.com.cn/problem/P3989)**
3319

3420
给定一个由前$n$个小写字母组成的串$S$。串$S$是阶乘字符串当且仅当前$n$个小写字母的全排列(共$n!$种)都作为的子序列(可以不连续)出现。
3521

@@ -39,7 +25,7 @@ export const Question = ({ children, color }) => (
3925
| ------------------------- | -------- | -------- |
4026
| $\|S\|\le 450$, $n\le 26$ | 125MB | 1.0s |
4127

42-
</Question>
28+
</question>
4329

4430
## 题解
4531

blog/2020-12-02-algorithm5.mdx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ export const Question = ({ children, color }) => (
2020
padding: '1em',
2121
marginBottom: '1em',
2222
marginTop: '1em',
23-
}}
24-
>
23+
}}>
2524
{children}
2625
</div>
2726
);
2827

2928
## 题目
3029

31-
<Question color="#1d85c2">
30+
<question>
3231

33-
**题目来源:**<a href="http://www.usaco.org/index.php?page=viewproblem2&cpid=965">**USACO 2019 December Bronze 3**</a>
32+
**题目来源:[USACO 2019 December Bronze 3](http://www.usaco.org/index.php?page=viewproblem2&cpid=965)**
3433

3534
Every day, Farmer John milks his 8 dairy cows, named Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, and Sue.
3635

@@ -42,7 +41,7 @@ Please help Farmer John determine an ordering of his cows that satisfies all of
4241
| ------------- | -------- | -------- |
4342
| $1\le N\le 7$ | 256MB | 2.0s |
4443

45-
</Question>
44+
</question>
4645

4746
## 题解
4847

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
"@typescript-eslint/parser": "5.0.0",
4949
"eslint": "8.1.0",
5050
"eslint-config-airbnb": "18.2.1",
51+
"eslint-config-prettier": "^8.3.0",
5152
"eslint-plugin-eslint-comments": "3.2.0",
5253
"eslint-plugin-import": "2.25.3",
5354
"eslint-plugin-jsx-a11y": "6.5.1",
55+
"eslint-plugin-prettier": "^4.0.0",
5456
"eslint-plugin-react": "7.27.0",
5557
"eslint-plugin-react-hooks": "4.2.0",
5658
"husky": "7.0.2",
@@ -66,5 +68,6 @@
6668
"*.{md,mdx,html,css}": [
6769
"prettier --write"
6870
]
69-
}
71+
},
72+
"packageManager": "yarn@3.1.0"
7073
}

src/components/Question.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
export default function Question({ children }: { children: React.ReactNode }) {
4+
return <div className="alert alert--info">{children}</div>;
5+
}

src/theme/MDXComponents/index.tsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React, { ComponentProps, isValidElement, ReactElement } from 'react';
9+
import Head from '@docusaurus/Head';
10+
import Link from '@docusaurus/Link';
11+
import CodeBlock, { Props } from '@theme/CodeBlock';
12+
import Heading from '@theme/Heading';
13+
import Details from '@theme/Details';
14+
import type { MDXComponentsObject } from '@theme/MDXComponents';
15+
import Question from '@site/src/components/Question';
16+
17+
import './styles.css';
18+
19+
// MDX elements are wrapped through the MDX pragma
20+
// In some cases (notably usage with Head/Helmet) we need to unwrap those elements.
21+
function unwrapMDXElement(element: ReactElement) {
22+
if (element?.props?.mdxType && element?.props?.originalType) {
23+
const { mdxType, originalType, ...newProps } = element.props;
24+
return React.createElement(element.props.originalType, newProps);
25+
}
26+
return element;
27+
}
28+
29+
const MDXComponents: MDXComponentsObject & {
30+
question: (props) => JSX.Element;
31+
} = {
32+
head: (props) => {
33+
const unwrappedChildren = React.Children.map(props.children, (child) =>
34+
unwrapMDXElement(child as ReactElement),
35+
);
36+
return <Head {...props}>{unwrappedChildren}</Head>;
37+
},
38+
code: (props) => {
39+
const { children } = props;
40+
41+
// For retrocompatibility purposes (pretty rare use case)
42+
// See https://github.com/facebook/docusaurus/pull/1584
43+
if (isValidElement(children)) {
44+
return children;
45+
}
46+
47+
return !children.includes('\n') ? (
48+
<code {...props} />
49+
) : (
50+
<CodeBlock {...props} />
51+
);
52+
},
53+
a: (props) => <Link {...props} />,
54+
pre: (props) => {
55+
const { children } = props;
56+
57+
// See comment for `code` above
58+
if (isValidElement(children) && isValidElement(children?.props?.children)) {
59+
return children.props.children;
60+
}
61+
62+
return (
63+
<CodeBlock
64+
{...((isValidElement(children)
65+
? children?.props
66+
: { ...props }) as Props)}
67+
/>
68+
);
69+
},
70+
details: (props): JSX.Element => {
71+
const items = React.Children.toArray(props.children) as ReactElement[];
72+
// Split summary item from the rest to pass it as a separate prop to the Detais theme component
73+
const summary: ReactElement<ComponentProps<'summary'>> = items.find(
74+
(item) => item?.props?.mdxType === 'summary',
75+
)!;
76+
const children = <>{items.filter((item) => item !== summary)}</>;
77+
78+
return (
79+
<Details {...props} summary={summary}>
80+
{children}
81+
</Details>
82+
);
83+
},
84+
question: (props) => <Question {...props} />,
85+
h1: Heading('h1'),
86+
h2: Heading('h2'),
87+
h3: Heading('h3'),
88+
h4: Heading('h4'),
89+
h5: Heading('h5'),
90+
h6: Heading('h6'),
91+
};
92+
93+
export default MDXComponents;

0 commit comments

Comments
 (0)