File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed
packages/streamdown/lib/code-block Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,13 @@ import { CodeBlockContainer } from "./container";
66import { CodeBlockContext } from "./context" ;
77import { 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
1117type 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
You can’t perform that action at this time.
0 commit comments