update note parser

This commit is contained in:
Ren Amamiya
2023-05-07 12:42:40 +07:00
parent 730f3be016
commit c39987ff1c
4 changed files with 27 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
import { Event } from 'nostr-tools';
import { Event, parseReferences } from 'nostr-tools';
const getURLs = new RegExp(
'(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal|wss|ws):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))',
@@ -6,6 +6,7 @@ const getURLs = new RegExp(
);
export const noteParser = (event: Event) => {
const references = parseReferences(event);
const content: { original: string; parsed: any; notes: string[]; images: string[]; videos: string[] } = {
original: event.content,
parsed: event.content,
@@ -23,30 +24,33 @@ export const noteParser = (event: Event) => {
// image url
content.images.push(url);
// remove url from original content
content.parsed = content.parsed.toString().replace(url, '');
content.parsed = content.parsed.replace(url, '');
} else if (url.match(/\.(mp4|webm|mov)$/i)) {
// video
content.videos.push(url);
// remove url from original content
content.parsed = content.parsed.toString().replace(url, '');
content.parsed = content.parsed.replace(url, '');
}
});
// extract note mention
content.original.match(/^(nostr:)?(note1|nevent1).*$/gm)?.forEach((item) => {
content.notes.push(item);
// remove url from original content
content.parsed = content.parsed.toString().replace(item, '');
});
// map hashtag to em
content.original.match(/#(\w+)(?!:\/\/)/gi)?.forEach((item) => {
content.parsed = content.parsed.replace(item, `*${item}*`);
content.parsed = content.parsed.replace(item, `[${item}](https://snort.social/search/#${item})`);
});
// map profile mention to h6 (markdown)
content.original.match(/^(nostr:)?(nprofile1|npub1).*$/gm)?.forEach((item) => {
content.parsed = content.parsed.replace(item, `###### ${item}`);
// handle nostr mention
references.forEach((item) => {
const profile = item.profile;
const event = item.event;
if (event) {
content.notes.push(event.id);
content.parsed = content.parsed.replace(item.text, '');
}
if (profile) {
content.parsed = content.parsed.replace(item.text, `*${profile.pubkey}*`);
}
});
return content;