mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 14:24:27 +01:00
Add an option to mdToHTML to strip links
This commit is contained in:
17
markdown.go
17
markdown.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
@@ -21,6 +22,16 @@ var mdrenderer = html.NewRenderer(html.RendererOptions{
|
|||||||
Flags: html.CommonFlags | html.HrefTargetBlank,
|
Flags: html.CommonFlags | html.HrefTargetBlank,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
func stripLinksFromMarkdown(md string) string {
|
||||||
|
// Regular expression to match Markdown links and HTML links
|
||||||
|
linkRegex := regexp.MustCompile(`\[([^\]]*)\]\([^)]*\)|<a[^>]*>(.*?)</a>`)
|
||||||
|
|
||||||
|
// Replace both Markdown and HTML links with just the link text
|
||||||
|
strippedMD := linkRegex.ReplaceAllString(md, "$1$2")
|
||||||
|
|
||||||
|
return strippedMD
|
||||||
|
}
|
||||||
|
|
||||||
var tgivmdrenderer = html.NewRenderer(html.RendererOptions{
|
var tgivmdrenderer = html.NewRenderer(html.RendererOptions{
|
||||||
Flags: html.CommonFlags | html.HrefTargetBlank,
|
Flags: html.CommonFlags | html.HrefTargetBlank,
|
||||||
RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
@@ -46,7 +57,7 @@ var tgivmdrenderer = html.NewRenderer(html.RendererOptions{
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
func mdToHTML(md string, usingTelegramInstantView bool) string {
|
func mdToHTML(md string, usingTelegramInstantView bool, skipLinks bool) string {
|
||||||
md = strings.ReplaceAll(md, "\u00A0", " ")
|
md = strings.ReplaceAll(md, "\u00A0", " ")
|
||||||
md = replaceNostrURLsWithTags(nostrEveryMatcher, md)
|
md = replaceNostrURLsWithTags(nostrEveryMatcher, md)
|
||||||
|
|
||||||
@@ -61,6 +72,10 @@ func mdToHTML(md string, usingTelegramInstantView bool) string {
|
|||||||
// create HTML renderer with extensions
|
// create HTML renderer with extensions
|
||||||
output := string(markdown.Render(doc, renderer))
|
output := string(markdown.Render(doc, renderer))
|
||||||
|
|
||||||
|
if skipLinks {
|
||||||
|
output = stripLinksFromMarkdown(output)
|
||||||
|
}
|
||||||
|
|
||||||
// sanitize content
|
// sanitize content
|
||||||
output = sanitizeXSS(output)
|
output = sanitizeXSS(output)
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func renderEmbedded(w http.ResponseWriter, r *http.Request, code string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if data.event.Kind == 30023 || data.event.Kind == 30024 {
|
if data.event.Kind == 30023 || data.event.Kind == 30024 {
|
||||||
data.content = mdToHTML(data.content, data.templateId == TelegramInstantView)
|
data.content = mdToHTML(data.content, data.templateId == TelegramInstantView, true)
|
||||||
} else {
|
} else {
|
||||||
// first we run basicFormatting, which turns URLs into their appropriate HTML tags
|
// first we run basicFormatting, which turns URLs into their appropriate HTML tags
|
||||||
data.content = basicFormatting(html.EscapeString(data.content), true, false, false)
|
data.content = basicFormatting(html.EscapeString(data.content), true, false, false)
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
data.content = strings.ReplaceAll(data.content, placeholderTag, "nostr:"+nreplace)
|
data.content = strings.ReplaceAll(data.content, placeholderTag, "nostr:"+nreplace)
|
||||||
}
|
}
|
||||||
if data.event.Kind == 30023 || data.event.Kind == 30024 {
|
if data.event.Kind == 30023 || data.event.Kind == 30024 {
|
||||||
data.content = mdToHTML(data.content, data.templateId == TelegramInstantView)
|
data.content = mdToHTML(data.content, data.templateId == TelegramInstantView, false)
|
||||||
} else {
|
} else {
|
||||||
// first we run basicFormatting, which turns URLs into their appropriate HTML tags
|
// first we run basicFormatting, which turns URLs into their appropriate HTML tags
|
||||||
data.content = basicFormatting(html.EscapeString(data.content), true, false, false)
|
data.content = basicFormatting(html.EscapeString(data.content), true, false, false)
|
||||||
|
|||||||
Reference in New Issue
Block a user