Render mentions as nip19, fix href
This commit is contained in:
parent
3092030dde
commit
605948267d
|
@ -1,7 +1,7 @@
|
||||||
import 'linkify-plugin-hashtag';
|
import 'linkify-plugin-hashtag';
|
||||||
import linkifyStr from 'linkify-string';
|
import linkifyStr from 'linkify-string';
|
||||||
import linkify from 'linkifyjs';
|
import linkify from 'linkifyjs';
|
||||||
import { nip19, nip21 } from 'nostr-tools';
|
import { nip21 } from 'nostr-tools';
|
||||||
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { getUrlMediaType, isPermittedMediaType } from '@/utils/media.ts';
|
import { getUrlMediaType, isPermittedMediaType } from '@/utils/media.ts';
|
||||||
|
@ -18,15 +18,10 @@ const linkifyOpts: linkify.Opts = {
|
||||||
},
|
},
|
||||||
url: ({ content }) => {
|
url: ({ content }) => {
|
||||||
try {
|
try {
|
||||||
const { decoded } = nip21.parse(content);
|
const { value } = nip21.parse(content);
|
||||||
const pubkey = getDecodedPubkey(decoded);
|
const name = value.substring(0, 8);
|
||||||
if (pubkey) {
|
const href = Conf.local(`/@${value}`);
|
||||||
const name = pubkey.substring(0, 8);
|
|
||||||
const href = Conf.local(`/users/${pubkey}`);
|
|
||||||
return `<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>`;
|
return `<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>`;
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
return `<a href="${content}">${content}</a>`;
|
return `<a href="${content}">${content}</a>`;
|
||||||
}
|
}
|
||||||
|
@ -108,14 +103,4 @@ function isLinkURL(link: Link): boolean {
|
||||||
return link.type === 'url';
|
return link.type === 'url';
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get pubkey from decoded bech32 entity, or undefined if not applicable. */
|
|
||||||
function getDecodedPubkey(decoded: nip19.DecodeResult): string | undefined {
|
|
||||||
switch (decoded.type) {
|
|
||||||
case 'npub':
|
|
||||||
return decoded.data;
|
|
||||||
case 'nprofile':
|
|
||||||
return decoded.data.pubkey;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export { getMediaLinks, parseNoteContent, stripimeta };
|
export { getMediaLinks, parseNoteContent, stripimeta };
|
||||||
|
|
Loading…
Reference in New Issue