UNPKG

@replyke/ui-core-react-native

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

40 lines 1.78 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { Text } from "react-native"; export const parseContentWithMentions = (content, mentions, currentUserId, currentUserClickCallback, otherUserClickCallback) => { if (!mentions.length) return [content]; // Create a regex pattern to match all mentions in the array, escaping special characters const mentionPattern = new RegExp(mentions .map((mention) => `@${mention.username.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")}`) .join("|"), "g"); // Replace mentions with a placeholder and split content based on the regex const parts = content.split(mentionPattern); // Find all matched mentions in the content const matches = Array.from(content.matchAll(mentionPattern)); // Construct the parsed output const parsedContent = []; let lastIndex = 0; parts.forEach((part, index) => { if (part) { parsedContent.push(part); lastIndex += part.length; } const match = matches[index]; if (match) { const matchedMention = mentions.find((mention) => `@${mention.username}` === match[0]); if (matchedMention) { parsedContent.push(_jsx(Text, { style: { color: "#1e40af" }, onPress: () => { if (matchedMention.id === currentUserId) { currentUserClickCallback?.(); } else { otherUserClickCallback?.(matchedMention.id); } }, children: match[0] }, lastIndex)); lastIndex += match[0].length; } } }); return parsedContent; }; //# sourceMappingURL=parseContentWithMentions.js.map