From b0bda605772e044bd3b799fe674140cdce598c1b Mon Sep 17 00:00:00 2001
From: fiatjaf
Date: Thu, 14 Sep 2023 10:31:38 -0300
Subject: [PATCH] improve and fix telegram instant preview for markdown
articles.
---
main.go | 2 --
nostr.go | 2 +-
render.go | 3 +-
templates/telegram_instant_view.html | 15 ++++++--
utils.go | 54 +++++++++++++++++++++-------
5 files changed, 57 insertions(+), 19 deletions(-)
diff --git a/main.go b/main.go
index 81a5dd6..5ca5d59 100644
--- a/main.go
+++ b/main.go
@@ -3,7 +3,6 @@ package main
import (
"context"
"embed"
- "fmt"
"html"
"net/http"
"os"
@@ -39,7 +38,6 @@ func updateArchives(ctx context.Context) {
for {
select {
case <-ctx.Done():
- fmt.Println("exit updateArchives gracefully...")
return
default:
loadNpubsArchive(ctx)
diff --git a/nostr.go b/nostr.go
index ace0d29..f7f5842 100644
--- a/nostr.go
+++ b/nostr.go
@@ -213,7 +213,7 @@ func contactsForPubkey(ctx context.Context, pubkey string, extraRelays ...string
pubkeyContacts := make([]string, 0, 100)
relays := make([]string, 0, 12)
if ok := cache.GetJSON("cc:"+pubkey, &pubkeyContacts); !ok {
- fmt.Printf("Searching contacts for %s\n", pubkey)
+ log.Debug().Msgf("searching contacts for %s", pubkey)
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
pubkeyRelays := relaysForPubkey(ctx, pubkey, relays...)
diff --git a/render.go b/render.go
index bec2fa2..3a0f184 100644
--- a/render.go
+++ b/render.go
@@ -304,7 +304,7 @@ func render(w http.ResponseWriter, r *http.Request) {
content = strings.ReplaceAll(content, placeholderTag, "nostr:"+nreplace)
}
if event.Kind == 30023 || event.Kind == 30024 {
- content = mdToHTML(content)
+ content = mdToHTML(content, typ == "telegram_instant_view")
} else {
content = renderInlineMentions(basicFormatting(html.EscapeString(content)))
}
@@ -327,6 +327,7 @@ func render(w http.ResponseWriter, r *http.Request) {
"authorLong": authorLong,
"subject": subject,
"description": description,
+ "summary": summary,
"event": event,
"eventJSON": string(eventJSON),
"content": content,
diff --git a/templates/telegram_instant_view.html b/templates/telegram_instant_view.html
index d80f27b..70782e0 100644
--- a/templates/telegram_instant_view.html
+++ b/templates/telegram_instant_view.html
@@ -6,7 +6,10 @@
-
+
{{ if .description }}
{{ end }}
@@ -28,8 +31,14 @@
- {{ if (not (eq .subject ""))}} {{.subject | escapeString}} {{ else }}
- {{.metadata.Name | escapeString}} wrote: {{ end }}
+ {{ if .subject }} {{.subject | escapeString}} {{ else }} {{.metadata.Name |
+ escapeString}} wrote: {{ end }}
+
+ {{ if .summary }}
+ {{ .summary }}
+ {{ end }}
+
+
{{.content}}
diff --git a/utils.go b/utils.go
index cf28bf8..a3a80b0 100644
--- a/utils.go
+++ b/utils.go
@@ -4,12 +4,15 @@ import (
"context"
"encoding/json"
"fmt"
+ "io"
"net/http"
"regexp"
"strings"
"time"
"github.com/gomarkdown/markdown"
+ "github.com/gomarkdown/markdown/ast"
+ "github.com/gomarkdown/markdown/html"
mdhtml "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
@@ -201,7 +204,7 @@ func getParentNevent(event *nostr.Event) string {
// Rendering functions
// ### ### ### ### ### ### ### ### ### ### ###
-func replateImageURLsWithTags(input string, replacement string) string {
+func replaceImageURLsWithTags(input string, replacement string) string {
// Match and replace image URLs with a custom replacement
// Usually is html
=> `
`
// or markdown !()[...] tags for further processing => ``
@@ -221,7 +224,7 @@ func replateImageURLsWithTags(input string, replacement string) string {
return input
}
-func replateVideoURLsWithTags(input string, replacement string) string {
+func replaceVideoURLsWithTags(input string, replacement string) string {
// Match and replace video URLs with a custom replacement
// Usually is html
![`))
+ html.EscapeHTML(w, img.Title)
+ }
+ w.Write([]byte(` `))
+ } else {
+ if img.Title != nil {
+ w.Write([]byte(`](`))
+ html.EscLink(w, src)
+ w.Write([]byte(`)
`))
+ }
+ return ast.GoToNext, true
+ }
+ return ast.GoToNext, false
+ }
+ }
+
// create HTML renderer with extensions
- htmlFlags := mdhtml.CommonFlags | mdhtml.HrefTargetBlank
- opts := mdhtml.RendererOptions{Flags: htmlFlags}
+ opts := mdhtml.RendererOptions{
+ Flags: mdhtml.CommonFlags | mdhtml.HrefTargetBlank,
+ RenderNodeHook: customNodeHook,
+ }
renderer := mdhtml.NewRenderer(opts)
output := string(markdown.Render(doc, renderer))
- // Sanitize content
+ // sanitize content
output = sanitizeXSS(output)
return output