replyke-rn
Version:
Replyke React Native components: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
47 lines • 2.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseContentWithMentions = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_native_1 = require("react-native");
var parseContentWithMentions = function (content, mentions, currentUserId, callbacks) {
if (!mentions.length)
return [content];
// Create a regex pattern to match all mentions in the array, escaping special characters
var mentionPattern = new RegExp(mentions
.map(function (mention) {
return "@".concat(mention.username.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"));
})
.join("|"), "g");
// Replace mentions with a placeholder and split content based on the regex
var parts = content.split(mentionPattern);
// Find all matched mentions in the content
var matches = Array.from(content.matchAll(mentionPattern));
// Construct the parsed output
var parsedContent = [];
var lastIndex = 0;
parts.forEach(function (part, index) {
if (part) {
parsedContent.push(part);
lastIndex += part.length;
}
var match = matches[index];
if (match) {
var matchedMention_1 = mentions.find(function (mention) { return "@".concat(mention.username) === match[0]; });
if (matchedMention_1) {
parsedContent.push((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: { color: "#1e40af" }, onPress: function () {
var _a, _b;
if (matchedMention_1.id === currentUserId) {
(_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks.currentUserClickCallback) === null || _a === void 0 ? void 0 : _a.call(callbacks);
}
else {
(_b = callbacks === null || callbacks === void 0 ? void 0 : callbacks.otherUserClickCallback) === null || _b === void 0 ? void 0 : _b.call(callbacks, matchedMention_1.id);
}
}, children: match[0] }, lastIndex));
lastIndex += match[0].length;
}
}
});
return parsedContent;
};
exports.parseContentWithMentions = parseContentWithMentions;
//# sourceMappingURL=parseContentWithMentions.js.map
;