phanpy/src/utils/get-instance-status-url.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-11-25 14:26:27 +01:00
// export const statusRegex = /\/@([^@\/]+)@?([^\/]+)?\/([^\/]+)\/?$/i;
// export const statusNoteRegex = /\/notes\/([^\/]+)\/?$/i;
const statusPostRegexes = [
/^\/@[^@\/]+\/(?:statuses|posts)\/([^\/]+)/i, // GoToSocial, Takahe
/\/notes\/([^\/]+)/i, // Misskey, Firefish
/^\/(?:notice|objects)\/([a-z0-9-]+)/i, // Pleroma
/\/@[^@\/]+@?[^\/]+?\/([^\/]+)/i, // Mastodon
2024-04-09 17:35:17 +02:00
/^\/p\/[^\/]+\/([^\/]+)/i, // Pixelfed
2023-11-25 14:26:27 +01:00
];
export function getInstanceStatusObject(url) {
2023-04-17 12:56:09 +02:00
// Regex /:username/:id, where username = @username or @username@domain, id = anything
2024-06-14 02:34:50 +02:00
const { hostname, pathname } = URL.parse(url);
2023-11-25 14:26:27 +01:00
// const [, username, domain, id] = pathname.match(statusRegex) || [];
for (const regex of statusPostRegexes) {
const [, id] = pathname.match(regex) || [];
console.log(pathname, regex, id);
if (id) {
return {
instance: hostname,
id,
};
}
2023-04-17 12:56:09 +02:00
}
2024-01-03 00:27:39 +01:00
return {};
2023-11-25 14:26:27 +01:00
}
2023-04-17 12:56:09 +02:00
2023-11-25 14:26:27 +01:00
function getInstanceStatusURL(url) {
const { instance, id } = getInstanceStatusObject(url);
if (instance && id) {
return `/${instance}/s/${id}`;
2023-04-17 12:56:09 +02:00
}
2023-11-25 14:26:27 +01:00
return null;
2023-04-17 12:56:09 +02:00
}
export default getInstanceStatusURL;