feat: photonify user and comment links

This commit is contained in:
Xyphyn committed 2023-08-27 12:18:18 -07:00
1 parent 5d68d02d3e
commit 22d0f63295
2 files changed
+31 -9

No files matched your search

+28
View File
@@ -117,7 +117,35 @@ md.linkify.add('@', {
validate: userLinks.validate,
normalize: userLinks.normalize,
})
md.linkify.add('u/', {
validate: userLinks.validate,
normalize: userLinks.normalize,
})
const regexes = {
post: /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/,
comment: /^https:\/\/([a-zA-Z0-9.-]+)\/comment\/(\d+)$/,
user: /^https:\/\/([a-zA-Z0-9.-]+)\/user\/(\d+)$/,
}
/**
* Convert links to photon links
*/
export const photonify = (link: string) => {
if (regexes.post.test(link)) {
const match = link.match(regexes.post)
if (!match) return
return `/post/${match?.[1]}/${match?.[2]}`
}
if (regexes.comment.test(link)) {
const match = link.match(regexes.comment)
if (!match) return
return `/comment/${match?.[1]}/${match?.[2]}`
}
if (regexes.user.test(link)) {
const match = link.match(regexes.user)
if (!match) return
return `/u/${match?.[2]}@${match?.[1]}`
}
}