diff --git a/main.go b/main.go index 5ca5d59..adf4aaa 100644 --- a/main.go +++ b/main.go @@ -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( diff --git a/templates/profile.html b/templates/profile.html index d4c83c3..83d59e7 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -124,7 +124,7 @@ {{if not (eq .ParentNevent "")}}
- reply
{{end}} -
{{.Content | escapeString}}
+
{{.Content | escapeString | basicNostrFormatting}}
{{end}} diff --git a/templates/relay.html b/templates/relay.html index c8e3036..49662e2 100644 --- a/templates/relay.html +++ b/templates/relay.html @@ -77,7 +77,7 @@ {{if not (eq .ParentNevent "")}}
- reply
{{end}} -
{{.Content | escapeString}}
+
{{.Content | escapeString | basicNostrFormatting}}
{{end}} diff --git a/utils.go b/utils.go index 9f9df6a..5deaf1c 100644 --- a/utils.go +++ b/utils.go @@ -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(`%s (%s)`, 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(`%s (%s)`, nip19, name, first_chars+"…"+last_chars) + } else if style == "short" { + replacement = "@" + first_chars + "…" + last_chars + } else { + replacement = nip19 + } return replacement } else { - replacement := fmt.Sprintf(`%s`, nip19, first_chars+"…"+last_chars) + if style == "tags" { + replacement = fmt.Sprintf(`%s`, 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, "
") } +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, "
") +} + func mdToHTML(md string, usingTelegramInstantView bool) string { md = strings.ReplaceAll(md, "\u00A0", " ") md = replaceImageURLsWithTags(md, `![](%s)`)