feat: photonify user and comment links
This commit is contained in:
1 parent
5d68d02d3e
commit
22d0f63295
2 files changed
+31
-9
No files matched your search
@@ -1,21 +1,15 @@
|
|||||||
<script lang="ts">
|
<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 source: string = ''
|
||||||
export let inline: boolean = false
|
export let inline: boolean = false
|
||||||
|
|
||||||
function replaceURLs(node: HTMLElement) {
|
function replaceURLs(node: HTMLElement) {
|
||||||
const postRegex = /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/
|
|
||||||
|
|
||||||
const links = node.querySelectorAll('a')
|
const links = node.querySelectorAll('a')
|
||||||
|
|
||||||
links.forEach((l) => {
|
links.forEach((l) => {
|
||||||
if (!postRegex.test(l.href)) return
|
const photonified = photonify(l.href)
|
||||||
|
if (photonified) l.href = photonified
|
||||||
const match = l.href.match(postRegex)
|
|
||||||
if (!match) return
|
|
||||||
|
|
||||||
l.href = `/post/${match?.[1]}/${match?.[2]}`
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,35 @@ md.linkify.add('@', {
|
|||||||
validate: userLinks.validate,
|
validate: userLinks.validate,
|
||||||
normalize: userLinks.normalize,
|
normalize: userLinks.normalize,
|
||||||
})
|
})
|
||||||
|
|
||||||
md.linkify.add('u/', {
|
md.linkify.add('u/', {
|
||||||
validate: userLinks.validate,
|
validate: userLinks.validate,
|
||||||
normalize: userLinks.normalize,
|
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]}`
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user