Skip to content

Commit

Permalink
HighlightText now retains spacing and linebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kreneskyp committed May 29, 2023
1 parent dac47c9 commit d357bc7
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions frontend/components/HighlightText.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ const HighlightText = ({ content }) => {
const { mention, artifact } = useChatColorMode();

const formattedContent = React.useMemo(() => {
// Match @mentions and {artifacts} and return an array of Chakra Text components
return content.split(" ").map((word, idx) => {
if (word.startsWith("@")) {
// Word is a mention
return content.split(/(\s|\n)/).map((segment, idx) => {
if (segment.startsWith("@")) {
return (
<Text as="span" color={mention} key={idx}>
{word + " "}
{segment}
</Text>
);
} else if (word.startsWith("{") && word.endsWith("}")) {
// Word is an artifact
} else if (segment.startsWith("{") && segment.endsWith("}")) {
return (
<Text as="span" color={artifact} key={idx}>
{word + " "}
{segment}
</Text>
);
} else if (segment === "\n") {
return <br key={idx} />;
} else {
return word + " ";
return segment;
}
});
}, [content]);
Expand Down

0 comments on commit d357bc7

Please sign in to comment.