dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit c3717635c49c50a7d6cb036d91db03e86ef752f0
parent 78a13c46236703a8621df92d3fea0355c8370b4a
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 28 Jun 2023 13:12:51 -0700

proper way of handling manual newline feature

Diffstat:
Mpkg/blackfriday/v2/inline.go | 3+++
Mpkg/blackfriday/v2/markdown.go | 1+
Mpkg/database/utils/processMessage.go | 27++++++++++-----------------
3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/pkg/blackfriday/v2/inline.go b/pkg/blackfriday/v2/inline.go @@ -680,6 +680,9 @@ func escape(p *Markdown, data []byte, offset int) (int, *Node) { data = data[offset:] if len(data) > 1 { + if p.extensions&ManualLineBreak != 0 && data[1] == 'n' { + return 2, NewNode(Hardbreak) + } if p.extensions&BackslashLineBreak != 0 && data[1] == '\n' { return 2, NewNode(Hardbreak) } diff --git a/pkg/blackfriday/v2/markdown.go b/pkg/blackfriday/v2/markdown.go @@ -48,6 +48,7 @@ const ( BackslashLineBreak // Translate trailing backslashes into line breaks DefinitionLists // Render definition lists NoLink // No links/images + ManualLineBreak // CommonHTMLFlags HTMLFlags = UseXHTML | Smartypants | SmartypantsFractions | SmartypantsDashes | SmartypantsLatexDashes diff --git a/pkg/database/utils/processMessage.go b/pkg/database/utils/processMessage.go @@ -101,7 +101,7 @@ var msgPolicy = bluemonday.NewPolicy(). func ProcessRawMessage(db *database.DkfDB, in, roomKey string, authUserID database.UserID, roomID database.RoomID, upload *database.Upload, canUseMultiline, manualML bool) (string, map[database.UserID]database.User, error) { html, quoted := convertQuote(db, in, roomKey, roomID) // Get raw quote text which is not safe to render - html = convertNewLines(html, canUseMultiline, manualML) + html = convertNewLines(html, canUseMultiline) html = html2.EscapeString(html) // Makes user input safe to render // All html generated from this point on shall be safe to render. html = convertPGPClearsignToFile(db, html, authUserID) @@ -109,7 +109,7 @@ func ProcessRawMessage(db *database.DkfDB, in, roomKey string, authUserID databa html = convertPGPPublicKeyToFile(db, html, authUserID) html = convertAgeMessageToFile(db, html, authUserID) html = convertLinksWithoutScheme(html) - html = convertMarkdown(html) + html = convertMarkdown(html, manualML) html = convertBangShortcuts(html) html = convertArchiveLinks(db, html, roomID, authUserID) html = convertLinks(html, roomID, db.GetUserByUsername, db.GetLinkByShorthand, db.GetChatMessageByUUID) @@ -235,18 +235,9 @@ func GetQuoteTxt(db *database.DkfDB, roomKey string, quoted database.ChatMessage return `“[` + quoted.CreatedAt.Format("15:04:05") + "]" + remaining + `”` } -var manualMultilineRgx = regexp.MustCompile(`(\\)?(\\n)`) - -func convertNewLines(html string, canUseMultiline, manualML bool) string { +func convertNewLines(html string, canUseMultiline bool) string { if !canUseMultiline { html = strings.ReplaceAll(html, "\n", "") - } else if manualML { - html = manualMultilineRgx.ReplaceAllStringFunc(html, func(s string) string { - if s == `\\n` { - return `\n` // "\" and "n" characters - } - return "\n" // new line character - }) } return html } @@ -849,12 +840,14 @@ func convertBangShortcuts(html string) string { return r.Replace(html) } -func convertMarkdown(in string) string { +func convertMarkdown(in string, manualML bool) string { out := strings.Replace(in, "\r", "", -1) - resBytes := bf.Run([]byte(out), bf.WithRenderer(utils.MyRenderer(false, false)), bf.WithExtensions( - bf.NoIntraEmphasis|bf.Tables|bf.FencedCode| - bf.Strikethrough|bf.SpaceHeadings| - bf.DefinitionLists|bf.HardLineBreak|bf.NoLink)) + flags := bf.NoIntraEmphasis | bf.Tables | bf.FencedCode | bf.Strikethrough | bf.SpaceHeadings | + bf.DefinitionLists | bf.HardLineBreak | bf.NoLink + if manualML { + flags |= bf.ManualLineBreak + } + resBytes := bf.Run([]byte(out), bf.WithRenderer(utils.MyRenderer(false, false)), bf.WithExtensions(flags)) out = string(resBytes) return out }