Skip to content

Commit

Permalink
Merge pull request #60 from kreneskyp/formatted_highlight
Browse files Browse the repository at this point in the history
HighlightText now retains spacing and linebreaks
  • Loading branch information
kreneskyp authored May 29, 2023
2 parents dac47c9 + d357bc7 commit 395bbd6
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 395bbd6

Please sign in to comment.