Skip to content

Commit

Permalink
fix(renderText): special case @ symbol at the end of the message (Get…
Browse files Browse the repository at this point in the history
…Stream#1873)

* fix: handle special case of @ symbol at the end of the message

* test: add test for special case with @ symbol
  • Loading branch information
arnautov-anton authored Dec 15, 2022
1 parent 6ec31af commit 7dced57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renderText handles the special case where there's at least one mention and @ symbol at the end 1`] = `
<p>
<span
className="str-chat__message-mention"
data-user-id="[email protected]"
>
@[email protected]
</span>
@
</p>
`;

exports[`renderText handles the special case where user name matches to an e-mail pattern - 1 1`] = `
<p>
Hello
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ describe(`renderText`, () => {
const tree = renderer.create(Markdown).toJSON();
expect(tree).toMatchSnapshot();
});

it("handles the special case where there's at least one mention and @ symbol at the end", () => {
const Markdown = renderText('@[email protected] @', [
{ id: '[email protected]', name: '[email protected]' },
]);
const tree = renderer.create(Markdown).toJSON();
expect(tree).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const mentionsMarkdownPlugin = <
if (!parent) return;

const nextChild = parent.children.at(index + 1) as Element;
const nextChildHref = nextChild?.properties?.href as string;
const nextChildHref = nextChild?.properties?.href as string | undefined;

if (
node.type === 'text' &&
Expand All @@ -197,7 +197,7 @@ export const mentionsMarkdownPlugin = <
// valid cases: "text @", "@", " @"
// invalid cases: "text@", "@text",
/.?\s?@$|^@$/.test(node.value) &&
nextChildHref.startsWith('mailto:')
nextChildHref?.startsWith('mailto:')
) {
const newTextValue = node.value.replace(/@$/, '');
const username = nextChildHref.replace('mailto:', '');
Expand Down

0 comments on commit 7dced57

Please sign in to comment.