Skip to content

Commit 2c68871

Browse files
authored
Merge pull request #24 from altano/main
fix: Don't aggressively trim code whitespace
2 parents 4044b8e + ffc5f47 commit 2c68871

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.changeset/twelve-doors-rush.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"bright": patch
3+
"bright-web": patch
4+
---
5+
6+
Don't trim all whitespace

lib/src/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,25 @@ function runExtensionsBeforeHighlight(props: CodeProps): CodeProps {
181181
return newProps
182182
}
183183

184+
function trimTrailingNewline(code: string | null): string | undefined {
185+
return code?.replace(/\n$/, "") ?? undefined
186+
}
187+
184188
function parseChildren(
185189
children: CodeText,
186190
lang?: LanguageAlias,
187191
code?: string
188192
): Partial<BrightProps> {
189193
if (typeof children === "object" && children?.type === "code") {
190194
return {
191-
code: children.props?.children?.trim(),
195+
code: trimTrailingNewline(children.props?.children),
192196
...getLanguageAndTitle(children.props?.className),
193197
}
194198
} else if (typeof children === "object") {
195199
const subProps = React.Children.toArray(children as any).map((c: any) => {
196200
const codeProps = c.props?.children?.props
197201
return {
198-
code: codeProps.children?.trim(),
202+
code: trimTrailingNewline(codeProps.children),
199203
...getLanguageAndTitle(codeProps.className),
200204
}
201205
})

web/app/test-mdx/mdx-demo.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,25 @@ console.log(1) // mark[3:10] red
7171
// mark
7272
const x = 20
7373
```
74+
75+
## Mark code with space
76+
77+
````md
78+
```
79+
80+
81+
here is some text with leading and trailing spaces
82+
that is working if the two lines are left-aligned.
83+
84+
85+
```
86+
````
87+
88+
```
89+
90+
91+
here is some text with leading and trailing spaces
92+
that is working if the two lines are left-aligned.
93+
94+
95+
```

0 commit comments

Comments
 (0)