feat: add link autoconversion

This commit is contained in:
Xyphyn committed 2023-08-25 14:37:21 -07:00
1 parent 892212d599
commit b041727ae1
3 files changed
+37 -7

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "photon-lemmy", "name": "photon-lemmy",
"version": "1.0.1", "version": "1.0.2",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
+19 -1
View File
@@ -3,9 +3,27 @@
export let source: string = '' export let source: string = ''
export let inline: boolean = false 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]}`
})
}
</script> </script>
<div class="break-words flex flex-col markdown gap-2 leading-[1.5]"> <div
use:replaceURLs
class="break-words flex flex-col markdown gap-2 leading-[1.5]"
>
{#if inline} {#if inline}
{@html mdInline.render(source)} {@html mdInline.render(source)}
{:else} {:else}
+17 -5
View File
@@ -51,8 +51,8 @@ export const mdInline: MarkdownIt = new MarkdownIt('zero')
.use(markdown_it_sub) .use(markdown_it_sub)
.use(markdown_it_sup) .use(markdown_it_sup)
md.linkify.add('!', { const communityLinks = {
validate: function (text, pos, self) { validate: function (text: any, pos: any, self: any) {
var tail = text.slice(pos) var tail = text.slice(pos)
if (!self.re.community) { if (!self.re.community) {
@@ -70,9 +70,22 @@ md.linkify.add('!', {
} }
return 0 return 0
}, },
normalize: function (match) { normalize: function (match: any) {
match.url = `/c/${match.url.slice(1)}` let prefix = `${match.url}`
prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1)
match.url = `/c/${prefix}`
}, },
}
md.linkify.add('!', {
validate: communityLinks.validate,
normalize: communityLinks.normalize,
})
md.linkify.add('c/', {
validate: communityLinks.validate,
normalize: communityLinks.normalize,
}) })
md.linkify.add('@', { md.linkify.add('@', {
@@ -96,4 +109,3 @@ md.linkify.add('@', {
match.url = `/u/${match.url.slice(1)}` match.url = `/u/${match.url.slice(1)}`
}, },
}) })