97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
// src/core/parser.ts
|
||
|
|
var mentionLinkRegexp = /@\[[^\]]+]\([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\)/g;
|
||
|
|
var parseMention = (mention) => {
|
||
|
|
const regex = /@\[([^\]]+)\]\(([\w-]{36})\)/;
|
||
|
|
const match = mention.match(regex);
|
||
|
|
if (!match) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const mentionText = match[1];
|
||
|
|
const mentionId = match[2];
|
||
|
|
if (!mentionText || !mentionId) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
mention: `@${mentionText}`,
|
||
|
|
id: mentionId
|
||
|
|
};
|
||
|
|
};
|
||
|
|
var findMentions = (text) => {
|
||
|
|
let match;
|
||
|
|
const matches = [];
|
||
|
|
while ((match = mentionLinkRegexp.exec(text)) !== null) {
|
||
|
|
const parsed = parseMention(match[0]);
|
||
|
|
const baseMention = {
|
||
|
|
start: match.index,
|
||
|
|
end: mentionLinkRegexp.lastIndex,
|
||
|
|
text: match[0],
|
||
|
|
type: "mention",
|
||
|
|
displayText: parsed?.mention ?? match[0]
|
||
|
|
};
|
||
|
|
matches.push(parsed?.id ? { ...baseMention, userId: parsed.id } : baseMention);
|
||
|
|
}
|
||
|
|
return matches;
|
||
|
|
};
|
||
|
|
var findTags = (content) => {
|
||
|
|
const regex = /#[^\s]{1,201}/g;
|
||
|
|
const results = [];
|
||
|
|
let match;
|
||
|
|
while ((match = regex.exec(content)) !== null) {
|
||
|
|
const value = match[0];
|
||
|
|
results.push({
|
||
|
|
start: match.index,
|
||
|
|
end: match.index + value.length,
|
||
|
|
text: value,
|
||
|
|
type: "tag",
|
||
|
|
tag: value.replace("#", "")
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return results;
|
||
|
|
};
|
||
|
|
var findLinks = (content) => {
|
||
|
|
const regex = /\b((https?:\/\/)?(?:[\w-]+\.)+[a-z]{2,}(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?)/gi;
|
||
|
|
const results = [];
|
||
|
|
let match;
|
||
|
|
while ((match = regex.exec(content)) !== null) {
|
||
|
|
const rawUrl = match[0];
|
||
|
|
const hasProtocol = /^https?:\/\//i.test(rawUrl);
|
||
|
|
const fullUrl = hasProtocol ? rawUrl : `https://${rawUrl}`;
|
||
|
|
results.push({
|
||
|
|
start: match.index,
|
||
|
|
end: match.index + rawUrl.length,
|
||
|
|
text: rawUrl,
|
||
|
|
url: fullUrl,
|
||
|
|
type: "link"
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return results;
|
||
|
|
};
|
||
|
|
var findAllEntities = (content) => {
|
||
|
|
const mentions = findMentions(content);
|
||
|
|
const tags = findTags(content);
|
||
|
|
const links = findLinks(content);
|
||
|
|
return [...mentions, ...tags, ...links].sort((a, b) => a.start - b.start);
|
||
|
|
};
|
||
|
|
var processContent = (content) => {
|
||
|
|
const processedText = content.replace(mentionLinkRegexp, (match) => {
|
||
|
|
const parsed = parseMention(match);
|
||
|
|
return parsed ? parsed.mention : match;
|
||
|
|
});
|
||
|
|
const tags = findTags(content).map((tag) => tag.tag);
|
||
|
|
return {
|
||
|
|
processedText,
|
||
|
|
tags
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.findAllEntities = findAllEntities;
|
||
|
|
exports.findLinks = findLinks;
|
||
|
|
exports.findMentions = findMentions;
|
||
|
|
exports.findTags = findTags;
|
||
|
|
exports.mentionLinkRegexp = mentionLinkRegexp;
|
||
|
|
exports.parseMention = parseMention;
|
||
|
|
exports.processContent = processContent;
|
||
|
|
//# sourceMappingURL=index.cjs.map
|
||
|
|
//# sourceMappingURL=index.cjs.map
|