Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle line breaks #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ type Modifier = Exclude<keyof TextInlineNode, 'type' | 'text'>;

type TextInlineProps = Omit<TextInlineNode, 'type'>;

const replaceLineBreaks = (text: string) => {
const split = text.split(/\r?\n|\r/g);
return (
<>
{split.map((part, idx) => (
<React.Fragment key={idx}>
{!!idx && <br />}
r-o-b-o-t-o marked this conversation as resolved.
Show resolved Hide resolved
{part}
</React.Fragment>
))}
</>
);
};

const Text = ({ text, ...modifiers }: TextInlineProps) => {
// Get matching React component from the context
const { modifiers: modifierComponents, missingModifierTypes } = useComponentsContext();
Expand Down Expand Up @@ -48,7 +62,7 @@ const Text = ({ text, ...modifiers }: TextInlineProps) => {
return <ModifierComponent>{children}</ModifierComponent>;
},
// By default, return the text without any wrapper to avoid useless nesting
<>{text}</>
replaceLineBreaks(text)
);
};

Expand Down