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

38 wiersze
1.1 KiB
JavaScript
Czysty Zwykły widok Historia

2023-11-25 13:26:27 +00: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 15:35:17 +00:00
/^\/p\/[^\/]+\/([^\/]+)/i, // Pixelfed
2023-11-25 13:26:27 +00:00
];
export function getInstanceStatusObject(url) {
2023-04-17 10:56:09 +00:00
// Regex /:username/:id, where username = @username or @username@domain, id = anything
const { hostname, pathname } = new URL(url);
2023-11-25 13:26:27 +00: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 10:56:09 +00:00
}
2024-01-02 23:27:39 +00:00
return {};
2023-11-25 13:26:27 +00:00
}
2023-04-17 10:56:09 +00:00
2023-11-25 13:26:27 +00:00
function getInstanceStatusURL(url) {
const { instance, id } = getInstanceStatusObject(url);
if (instance && id) {
return `/${instance}/s/${id}`;
2023-04-17 10:56:09 +00:00
}
2023-11-25 13:26:27 +00:00
return null;
2023-04-17 10:56:09 +00:00
}
export default getInstanceStatusURL;