Skip to content

Commit 3fa6d9e

Browse files
committed
fix: 텍스트 변환 후 import문이 제대로 동작하지 않을 수 있던 문제 수정
1 parent 60a883a commit 3fa6d9e

File tree

1 file changed

+17
-1
lines changed
  • packages/common/src/components

1 file changed

+17
-1
lines changed

packages/common/src/components/mdx.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,29 @@ const InnerMDXRenderer: React.FC<{ text: string, baseUrl: string }> = ({ text, b
7777
return <>{data}</>;
7878
}
7979

80+
const lineFormatterForMDX = (line: string) => {
81+
const trimmedLine = line.trim();
82+
83+
if (R.isEmpty(trimmedLine)) return "\n";
84+
85+
// import문을 위한 꼼수 - import문 다음 줄은 반드시 빈 줄이어야 합니다.
86+
// 그러나 \n\n으로 변환할 경우, 다음 단계에서 <br />로 변환되므로, import문 다음에 공백이 있는 줄을 넣어서 <br />로 변환되지 않도록 합니다.
87+
if (trimmedLine.startsWith("import")) return `${trimmedLine}\n \n`;
88+
89+
return `${trimmedLine} \n`;
90+
}
91+
8092
export const MDXRenderer: React.FC<{ text: string }> = ({ text }) => {
8193
// 원래 MDX는 각 줄의 마지막에 공백 2개가 있어야 줄바꿈이 되고, 또 연속 줄바꿈은 무시되지만,
8294
// 편의성을 위해 렌더러 단에서 공백 2개를 추가하고 연속 줄바꿈을 <br />로 변환합니다.
8395
const { baseUrl, debug } = useCommonContext();
8496

8597
const ErrorHandler = debug ? DetailedMDXErrorFallback : SimplifiedMDXErrorFallback;
86-
const processedText = text.split("\n").map((line) => R.isEmpty(line.trim()) ? "" : `${line.trim()} `).join("\n").replaceAll("\n\n", "\n<br />\n");
98+
const processedText = text
99+
.split("\n")
100+
.map(lineFormatterForMDX)
101+
.join("")
102+
.replaceAll("\n\n", "\n<br />\n");
87103

88104
return <ErrorBoundary fallback={ErrorHandler}>
89105
<Suspense fallback={<CircularProgress />}>

0 commit comments

Comments
 (0)