diff options
author | 2021-08-16 19:17:56 +0200 | |
---|---|---|
committer | 2021-08-16 19:17:56 +0200 | |
commit | ce190d867ca126001a1c0417b00810fc03c0b3ba (patch) | |
tree | 364b00118a405239bc6bcac0bfb7891c83655c23 /internal/text/common.go | |
parent | Timeline loop fix (#140) (diff) | |
download | gotosocial-ce190d867ca126001a1c0417b00810fc03c0b3ba.tar.xz |
Text/status parsing fixes (#141)
* aaaaaa
* vendor minify
* update + test markdown parsing
Diffstat (limited to 'internal/text/common.go')
-rw-r--r-- | internal/text/common.go | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/internal/text/common.go b/internal/text/common.go index 4f0bad9dc..f6a5ca5f5 100644 --- a/internal/text/common.go +++ b/internal/text/common.go @@ -20,6 +20,7 @@ package text import ( "fmt" + "html" "strings" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -29,23 +30,33 @@ import ( // preformat contains some common logic for making a string ready for formatting, which should be used for all user-input text. func preformat(in string) string { // do some preformatting of the text - // 1. Trim all the whitespace - s := strings.TrimSpace(in) + + // 1. unescape everything that might be html escaped + s := html.UnescapeString(in) + + // 2. trim leading or trailing whitespace + s = strings.TrimSpace(s) return s } // postformat contains some common logic for html sanitization of text, wrapping elements, and trimming newlines and whitespace func postformat(in string) string { // do some postformatting of the text - // 1. sanitize html to remove any dodgy scripts or other disallowed elements - s := SanitizeOutgoing(in) - // 2. wrap the whole thing in a paragraph - s = fmt.Sprintf(`<p>%s</p>`, s) - // 3. remove any cheeky newlines - s = strings.ReplaceAll(s, "\n", "") - // 4. remove any whitespace added as a result of the formatting - s = strings.TrimSpace(s) - return s + + // 1. sanitize html to remove potentially dangerous elements + s := SanitizeHTML(in) + + // 2. the sanitize step tends to escape characters inside codeblocks, which is behavior we don't want, so unescape everything again + s = html.UnescapeString(s) + + // 3. minify html to remove any trailing newlines, spaces, unnecessary elements, etc etc + mini, err := minifyHTML(s) + if err != nil { + // if the minify failed, just return what we have + return s + } + // return minified version of the html + return mini } func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string { |