@replyke/ui-core-react-native
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
44 lines • 2.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseContentWithMentions = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_native_1 = require("react-native");
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((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: { color: "#1e40af" }, onPress: () => {
if (matchedMention.id === currentUserId) {
currentUserClickCallback?.();
}
else {
otherUserClickCallback?.(matchedMention.id);
}
}, children: match[0] }, lastIndex));
lastIndex += match[0].length;
}
}
});
return parsedContent;
};
exports.parseContentWithMentions = parseContentWithMentions;
//# sourceMappingURL=parseContentWithMentions.js.map
;