From a6161e046732b77a7a0b2f58d8ce75b0418a4dcb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 11 Dec 2022 21:33:56 +0800 Subject: [PATCH 1/2] Use multi reader instead to concat strings --- modules/markup/html.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 80b19ba35f213..2a67a7bf734ab 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -305,18 +305,12 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output return err } - res := bytes.NewBuffer(make([]byte, 0, len(rawHTML)+50)) - // prepend "" - _, _ = res.WriteString("") - - // Strip out nuls - they're always invalid - _, _ = res.Write(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("<$1"))) - - // close the tags - _, _ = res.WriteString("") - // parse the HTML - node, err := html.Parse(res) + node, err := html.Parse(io.MultiReader( + strings.NewReader(""), + bytes.NewReader(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("<$1"))), + strings.NewReader(""), + )) if err != nil { return &postProcessError{"invalid HTML", err} } From 632679b2669edcb866f917c670b1c5c5413f7e97 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 12 Dec 2022 10:14:00 +0800 Subject: [PATCH 2/2] Add comment back --- modules/markup/html.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/markup/html.go b/modules/markup/html.go index 2a67a7bf734ab..6b5a8e32d4ef4 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -307,8 +307,11 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output // parse the HTML node, err := html.Parse(io.MultiReader( + // prepend "" strings.NewReader(""), + // Strip out nuls - they're always invalid bytes.NewReader(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("<$1"))), + // close the tags strings.NewReader(""), )) if err != nil {