feat: add link autoconversion
This commit is contained in:
1 parent
892212d599
commit
b041727ae1
3 files changed
+37
-7
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "photon-lemmy",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
|
||||
@@ -3,9 +3,27 @@
|
||||
|
||||
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]}`
|
||||
})
|
||||
}
|
||||
</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}
|
||||
{@html mdInline.render(source)}
|
||||
{:else}
|
||||
|
||||
@@ -51,8 +51,8 @@ export const mdInline: MarkdownIt = new MarkdownIt('zero')
|
||||
.use(markdown_it_sub)
|
||||
.use(markdown_it_sup)
|
||||
|
||||
md.linkify.add('!', {
|
||||
validate: function (text, pos, self) {
|
||||
const communityLinks = {
|
||||
validate: function (text: any, pos: any, self: any) {
|
||||
var tail = text.slice(pos)
|
||||
|
||||
if (!self.re.community) {
|
||||
@@ -70,9 +70,22 @@ md.linkify.add('!', {
|
||||
}
|
||||
return 0
|
||||
},
|
||||
normalize: function (match) {
|
||||
match.url = `/c/${match.url.slice(1)}`
|
||||
normalize: function (match: any) {
|
||||
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('@', {
|
||||
@@ -96,4 +109,3 @@ md.linkify.add('@', {
|
||||
match.url = `/u/${match.url.slice(1)}`
|
||||
},
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user