Skip to content

Commit 18e4781

Browse files
committed
feat: 순수 markdown editor 추가 및 MDXRenderer가 JSX를 못 사용하는 md 모드 추가
1 parent e2e7830 commit 18e4781

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

apps/pyconkr-admin/src/components/pages/page/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const SectionTextEditor: React.FC<SectionTextEditorPropType> = ({ disabled, defa
5252
</Stack>
5353
<Box sx={{ flexGrow: 1, width: "50%", backgroundColor: "#fff" }}>
5454
<ThemeProvider theme={muiTheme}>
55-
<Common.Components.MDXRenderer text={defaultValue || ""} />
55+
<Common.Components.MDXRenderer text={defaultValue || ""} format="mdx" />
5656
</ThemeProvider>
5757
</Box>
5858
</Stack>

apps/pyconkr/src/components/pages/dynamic_route.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const InnerPageRenderer: React.FC<{ id: string }> = Suspense.with({ fallback: <C
117117
<Stack sx={initialPageStyle(Common.Utils.parseCss(data.css))}>
118118
{data.sections.map((s) => (
119119
<Stack sx={initialSectionStyle(Common.Utils.parseCss(s.css))} key={s.id}>
120-
<Common.Components.MDXRenderer text={s.body} />
120+
<Common.Components.MDXRenderer text={s.body} format="mdx" />
121121
</Stack>
122122
))}
123123
</Stack>

apps/pyconkr/src/debug/page/mdi_test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const MdiTestPage: React.FC = () => {
2828
<Common.Components.MDXEditor defaultValue={state.text} onChange={setMDXInput} />
2929
</Box>
3030
<Box sx={HalfWidthStyle}>
31-
<Common.Components.MDXRenderer {...state} />
31+
<Common.Components.MDXRenderer {...state} format="mdx" />
3232
</Box>
3333
</Stack>
3434
);

packages/common/src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
LottiePlayer as LottiePlayerComponent,
99
NetworkLottiePlayer as NetworkLottiePlayerComponent,
1010
} from "./lottie";
11+
import { MarkdownEditor as MarkdownEditorComponent } from "./md_editor";
1112
import { MDXRenderer as MDXRendererComponent } from "./mdx";
1213
import { Confetti as ConfettiComponent } from "./mdx_components/confetti";
1314
import {
@@ -29,6 +30,7 @@ import { PythonKorea as PythonKoreaComponent } from "./pythonkorea";
2930
namespace Components {
3031
export const CenteredPage = CenteredPageComponent;
3132
export const CommonContextProvider = CommonContextProviderComponent;
33+
export const MarkdownEditor = MarkdownEditorComponent;
3234
export const MDXEditor = MDXEditorComponent;
3335
export const MDXRenderer = MDXRendererComponent;
3436
export const PythonKorea = PythonKoreaComponent;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Stack } from "@mui/material";
2+
import MDEditor, { ICommand, commands } from "@uiw/react-md-editor";
3+
import * as React from "react";
4+
5+
type MDEditorProps = {
6+
disabled?: boolean;
7+
defaultValue?: string;
8+
onChange?: (value?: string) => void;
9+
extraCommands?: ICommand[];
10+
};
11+
12+
const TextEditorStyle: React.CSSProperties = {
13+
flexGrow: 1,
14+
width: "100%",
15+
maxWidth: "100%",
16+
17+
wordBreak: "break-word",
18+
whiteSpace: "pre-wrap",
19+
overflowWrap: "break-word",
20+
21+
fieldSizing: "content",
22+
} as React.CSSProperties;
23+
24+
export const MarkdownEditor: React.FC<MDEditorProps> = ({ disabled, defaultValue, onChange, extraCommands }) => (
25+
<Stack direction="column" spacing={2} sx={{ width: "100%", height: "100%", maxWidth: "100%" }}>
26+
<MDEditor
27+
data-color-mode="light"
28+
textareaProps={{ disabled }}
29+
preview="edit"
30+
highlightEnable={true}
31+
height="none"
32+
minHeight={100}
33+
value={defaultValue}
34+
onChange={onChange}
35+
commands={[
36+
commands.group([commands.title1, commands.title2, commands.title3, commands.title4, commands.title5, commands.title6], {
37+
name: "title",
38+
groupName: "title",
39+
buttonProps: { "aria-label": "Insert title" },
40+
}),
41+
commands.bold,
42+
commands.italic,
43+
commands.strikethrough,
44+
commands.code,
45+
commands.link,
46+
commands.divider,
47+
commands.quote,
48+
commands.codeBlock,
49+
commands.table,
50+
commands.hr,
51+
commands.image,
52+
commands.divider,
53+
commands.unorderedListCommand,
54+
commands.orderedListCommand,
55+
commands.checkedListCommand,
56+
commands.divider,
57+
commands.help,
58+
]}
59+
extraCommands={extraCommands}
60+
style={TextEditorStyle}
61+
/>
62+
</Stack>
63+
);

packages/common/src/components/mdx.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ const lineFormatterForMDX = (line: string) => {
8484
return `${trimmedLine} \n`;
8585
};
8686

87-
export const MDXRenderer: React.FC<{ text: string; resetKey?: number }> = ({ text, resetKey }) => {
87+
type MDXRendererPropType = {
88+
text: string;
89+
resetKey?: number;
90+
format?: "mdx" | "md";
91+
};
92+
93+
export const MDXRenderer: React.FC<MDXRendererPropType> = ({ text, resetKey, format }) => {
8894
const { baseUrl, mdxComponents } = Hooks.Common.useCommonContext();
8995
const [state, setState] = React.useState<{
9096
component: React.ReactNode;
@@ -106,6 +112,7 @@ export const MDXRenderer: React.FC<{ text: string; resetKey?: number }> = ({ tex
106112
const { default: RenderResult } = await evaluate(processedText, {
107113
...runtime,
108114
...provider,
115+
format: format || "md",
109116
baseUrl,
110117
remarkPlugins: [remarkGfm],
111118
});
@@ -120,7 +127,7 @@ export const MDXRenderer: React.FC<{ text: string; resetKey?: number }> = ({ tex
120127
setRenderResult(<ErrorFallback error={error as Error} reset={setRandomResetKey} />);
121128
}
122129
})();
123-
}, [text, resetKey, state.resetKey, baseUrl, mdxComponents]);
130+
}, [text, resetKey, format, state.resetKey, baseUrl, mdxComponents]);
124131

125132
return (
126133
<ErrorBoundary fallback={ErrorFallback} resetKeys={[text, resetKey, state.resetKey]}>

packages/common/src/components/mdx_editor.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,18 @@ export const MDXEditor: React.FC<MDXEditorProps> = ({ disabled, defaultValue, on
253253
}),
254254
commands.bold,
255255
commands.italic,
256+
commands.strikethrough,
256257
commands.code,
257258
commands.link,
258259
commands.divider,
259260
commands.quote,
260261
commands.codeBlock,
262+
commands.table,
261263
commands.hr,
262264
commands.divider,
263265
commands.unorderedListCommand,
264266
commands.orderedListCommand,
267+
commands.checkedListCommand,
265268
commands.divider,
266269
commands.group([], {
267270
name: "custom components",
@@ -277,6 +280,8 @@ export const MDXEditor: React.FC<MDXEditorProps> = ({ disabled, defaultValue, on
277280
children: (props) => <ImageSelector {...props} />,
278281
buttonProps: { "aria-label": "Insert image" },
279282
}),
283+
commands.divider,
284+
commands.help,
280285
]}
281286
extraCommands={extraCommands}
282287
style={TextEditorStyle}

packages/shop/src/components/features/product.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const ProductItem: React.FC<ProductItemPropType> = ({ disabled: rootDisabled, la
248248

249249
return (
250250
<>
251-
<Common.Components.MDXRenderer text={product.description || ""} />
251+
<Common.Components.MDXRenderer text={product.description || ""} format="mdx" />
252252
<br />
253253
<Divider />
254254
{R.isNullish(notPurchasableReason) ? (

0 commit comments

Comments
 (0)