Shorten nip-119 entities in profiles/relays' last notes

This commit is contained in:
Daniele Tonon
2023-09-19 15:37:41 +02:00
parent 89ba317ede
commit 616242ac3b
4 changed files with 46 additions and 12 deletions

View File

@@ -76,10 +76,11 @@ func main() {
}
funcMap := template.FuncMap{
"basicFormatting": basicFormatting,
"escapeString": html.EscapeString,
"sanitizeXSS": sanitizeXSS,
"trimProtocol": trimProtocol,
"basicFormatting": basicFormatting,
"basicNostrFormatting": basicNostrFormatting,
"escapeString": html.EscapeString,
"sanitizeXSS": sanitizeXSS,
"trimProtocol": trimProtocol,
}
tmpl = template.Must(

View File

@@ -124,7 +124,7 @@
{{if not (eq .ParentNevent "")}}
<div class="is_reply">- reply</div>
{{end}}
<div class="content">{{.Content | escapeString}}</div>
<div class="content">{{.Content | escapeString | basicNostrFormatting}}</div>
</a>
{{end}}
</nav>

View File

@@ -77,7 +77,7 @@
{{if not (eq .ParentNevent "")}}
<div class="is_reply">- reply</div>
{{end}}
<div class="content">{{.Content | escapeString}}</div>
<div class="content">{{.Content | escapeString | basicNostrFormatting}}</div>
</a>
{{end}}
</div>

View File

@@ -245,7 +245,7 @@ func replaceVideoURLsWithTags(input string, replacement string) string {
return input
}
func replaceNostrURLsWithTags(input string) string {
func replaceNostrURLs(input string, style string) string {
// Match and replace npup1, nprofile1, note1, nevent1, etc
input = nostrEveryMatcher.ReplaceAllStringFunc(input, func(match string) string {
submatch := nostrEveryMatcher.FindStringSubmatch(match)
@@ -255,14 +255,27 @@ func replaceNostrURLsWithTags(input string) string {
nip19 := submatch[2]
first_chars := nip19[:8]
last_chars := nip19[len(nip19)-4:]
replacement := ""
if strings.HasPrefix(nip19, "npub1") || strings.HasPrefix(nip19, "nprofile1") {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()
name := getNameFromNip19(ctx, nip19)
replacement := fmt.Sprintf(`<a href="/%s" class="nostr" ><strong>%s</strong> (<i>%s</i>)</a>`, nip19, name, first_chars+"…"+last_chars)
if style == "tags" {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()
name := getNameFromNip19(ctx, nip19)
replacement = fmt.Sprintf(`<a href="/%s" class="nostr" ><strong>%s</strong> (<i>%s</i>)</a>`, nip19, name, first_chars+"…"+last_chars)
} else if style == "short" {
replacement = "@" + first_chars + "…" + last_chars
} else {
replacement = nip19
}
return replacement
} else {
replacement := fmt.Sprintf(`<a href="/%s" class="nostr">%s</a>`, nip19, first_chars+"…"+last_chars)
if style == "tags" {
replacement = fmt.Sprintf(`<a href="/%s" class="nostr">%s</a>`, nip19, first_chars+"…"+last_chars)
} else if style == "short" {
replacement = "#" + first_chars + "…" + last_chars
} else {
replacement = nip19
}
return replacement
}
@@ -270,6 +283,14 @@ func replaceNostrURLsWithTags(input string) string {
return input
}
func replaceNostrURLsWithTags(input string) string {
return replaceNostrURLs(input, "tags")
}
func shortenNostrURLs(input string) string {
return replaceNostrURLs(input, "short")
}
func getNameFromNip19(ctx context.Context, nip19 string) string {
author, err := getEvent(ctx, nip19)
if err != nil {
@@ -422,6 +443,18 @@ func basicFormatting(input string) string {
return strings.Join(processedLines, "<br/>")
}
func basicNostrFormatting(input string) string {
lines := strings.Split(input, "\n")
var processedLines []string
for _, line := range lines {
processedLine := shortenNostrURLs(line)
processedLines = append(processedLines, processedLine)
}
return strings.Join(processedLines, "<br/>")
}
func mdToHTML(md string, usingTelegramInstantView bool) string {
md = strings.ReplaceAll(md, "\u00A0", " ")
md = replaceImageURLsWithTags(md, `![](%s)`)