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

+3 -9
View File
@@ -1,21 +1,15 @@
<script lang="ts">
import { md, mdInline } from '$lib/components/markdown/markdown'
import { md, mdInline, photonify } from '$lib/components/markdown/markdown'
export let source: string = ''
export let inline: boolean = false
function replaceURLs(node: HTMLElement) {
const postRegex = /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/
const links = node.querySelectorAll('a')
links.forEach((l) => {
if (!postRegex.test(l.href)) return
const match = l.href.match(postRegex)
if (!match) return
l.href = `/post/${match?.[1]}/${match?.[2]}`
const photonified = photonify(l.href)
if (photonified) l.href = photonified
})
}
+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]}`
}
}