Skip to content

Commit 6b49db1

Browse files
committed
Fix polynomial regular expression issue
1 parent 6d4100a commit 6b49db1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/streamdown/lib/code-block/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { CodeBlockContainer } from "./container";
66
import { CodeBlockContext } from "./context";
77
import { CodeBlockHeader } from "./header";
88

9-
const TRAILING_NEWLINES_REGEX = /\n+$/;
9+
const trimTrailingNewlines = (str: string): string => {
10+
let end = str.length;
11+
while (end > 0 && str[end - 1] === "\n") {
12+
end--;
13+
}
14+
return str.slice(0, end);
15+
};
1016

1117
type CodeBlockProps = HTMLAttributes<HTMLDivElement> & {
1218
code: string;
@@ -35,7 +41,7 @@ export const CodeBlock = ({
3541
const cn = useCn();
3642
// Remove trailing newlines to prevent empty line at end of code blocks
3743
const trimmedCode = useMemo(
38-
() => code.replace(TRAILING_NEWLINES_REGEX, ""),
44+
() => trimTrailingNewlines(code),
3945
[code]
4046
);
4147

0 commit comments

Comments
 (0)