Fix: fix fediverse link

This commit is contained in:
Pieter Vander Vennet 2024-10-29 23:46:52 +01:00
parent 7c8591500d
commit 3683780f9d
3 changed files with 58 additions and 49 deletions

View file

@ -12,27 +12,42 @@ export default class FediverseValidator extends Validator {
)
}
/**
* FediverseValidator.extractServer(undefined) // => undefined
* FediverseValidator.extractServer("@pietervdvn@en.osm.town") // => {server: "en.osm.town", username: "pietervdvn"}
*/
public static extractServer(handle: string): { server: string; username: string } {
const match = handle?.match(this.usernameAtServer)
if(!match){
return undefined
}
const [_, username, server] = match
return {username, server}
}
/**
* Returns an `@username@host`
* @param s
* new FediverseValidator().reformat("https://hsnl.social/@pixelbar") // => "@pixelbar@hsnl.social"
*/
reformat(s: string): string {
s = s.trim()
try {
const url = new URL(s)
const path = url.pathname
if (path.match(/^\/@?\w+$/)) {
return `${path.substring(1)}@${url.hostname}`;
}
} catch (e) {
// Nothing to do here
}
if (!s.startsWith("@")) {
s = "@" + s
}
if (s.match(FediverseValidator.usernameAtServer)) {
return s
}
try {
const url = new URL(s)
const path = url.pathname
if (path.match(/^\/\w+$/)) {
return `@${path.substring(1)}@${url.hostname}`
}
} catch (e) {
// Nothing to do here
}
return undefined
}
getFeedback(s: string): Translation | undefined {