package main import ( "io" "regexp" "strings" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/ast" "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" ) var mdrenderer = html.NewRenderer(html.RendererOptions{ Flags: html.CommonFlags | html.HrefTargetBlank, }) func stripLinksFromMarkdown(md string) string { // Regular expression to match Markdown links and HTML links linkRegex := regexp.MustCompile(`\[([^\]]*)\]\([^)]*\)|]*>(.*?)`) // 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{ Flags: html.CommonFlags | html.HrefTargetBlank, RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) { // telegram instant view really doesn't like when there is an image inside a paragraph (like
`)) } return ast.GoToNext, true } return ast.GoToNext, false }, }) func mdToHTML(md string, usingTelegramInstantView bool, skipLinks bool) string { md = strings.ReplaceAll(md, "\u00A0", " ") md = replaceNostrURLsWithTags(nostrEveryMatcher, md) // create markdown parser with extensions // this parser is stateful so it must be reinitialized every time doc := parser.NewWithExtensions( parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock | parser.Footnotes, ).Parse([]byte(md)) renderer := mdrenderer if usingTelegramInstantView { renderer = tgivmdrenderer } // create HTML renderer with extensions output := string(markdown.Render(doc, renderer)) if skipLinks { output = stripLinksFromMarkdown(output) } // sanitize content output = sanitizeXSS(output) return output }