From 3e8285b824d77bafa3335765d68a6b499d9faad4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 12 Dec 2022 11:03:54 +0800 Subject: [PATCH] Use multi reader instead to concat strings (#22099) extract from #20326 --- modules/markup/html.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 80b19ba35..6b5a8e32d 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -305,18 +305,15 @@ 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( + // 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 { return &postProcessError{"invalid HTML", err} }