feat: add u/ syntax for users

This commit is contained in:
Xyphyn committed 2023-08-25 14:45:41 -07:00
1 parent b041727ae1
commit 0fbe243f7b
2 files changed
+39 -21

No files matched your search

+7 -1
View File
@@ -18,10 +18,16 @@
l.href = `/post/${match?.[1]}/${match?.[2]}` l.href = `/post/${match?.[1]}/${match?.[2]}`
}) })
} }
let div: HTMLElement
$: if (source && div) {
replaceURLs(div)
}
</script> </script>
<div <div
use:replaceURLs bind:this={div}
class="break-words flex flex-col markdown gap-2 leading-[1.5]" class="break-words flex flex-col markdown gap-2 leading-[1.5]"
> >
{#if inline} {#if inline}
+32 -20
View File
@@ -71,13 +71,38 @@ const communityLinks = {
return 0 return 0
}, },
normalize: function (match: any) { normalize: function (match: any) {
let prefix = `${match.url}` let prefix = match.url
prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1) prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1)
match.url = `/c/${prefix}` match.url = `/c/${prefix}`
}, },
} }
const userLinks = {
validate: function (text: any, pos: any, self: any) {
var tail = text.slice(pos)
if (!self.re.user) {
self.re.user = new RegExp(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,6})/)
}
if (self.re.user.test(tail)) {
// Linkifier allows punctuation chars before prefix,
// but we additionally disable `@` ("@@mention" is invalid)
if (pos >= 2 && tail[pos - 2] === '!') {
return false
}
return tail.match(self.re.user)![0].length
}
return 0
},
normalize: function (match: any) {
let prefix = match.url
prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1)
match.url = `/u/${prefix}`
},
}
md.linkify.add('!', { md.linkify.add('!', {
validate: communityLinks.validate, validate: communityLinks.validate,
normalize: communityLinks.normalize, normalize: communityLinks.normalize,
@@ -89,23 +114,10 @@ md.linkify.add('c/', {
}) })
md.linkify.add('@', { md.linkify.add('@', {
validate: function (text, pos, self) { validate: userLinks.validate,
var tail = text.slice(pos) normalize: userLinks.normalize,
})
if (!self.re.user) { md.linkify.add('u/', {
self.re.user = new RegExp(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,6})/) validate: userLinks.validate,
} normalize: userLinks.normalize,
if (self.re.user.test(tail)) {
// Linkifier allows punctuation chars before prefix,
// but we additionally disable `@` ("@@mention" is invalid)
if (pos >= 2 && tail[pos - 2] === '@') {
return false
}
return tail.match(self.re.user)![0].length
}
return 0
},
normalize: function (match) {
match.url = `/u/${match.url.slice(1)}`
},
}) })