简体   繁体   中英

How to render the quill js content using go buffalo framework

The options that i am aware of are,

  1. Get the content of the quilljs from getContents api which gives the JSON structure. I can post this to server and store it in server.

  2. Get the innerHTML of the div which is passed to Quill editor and store it.

Approach 1:

While displaying it back I need to write the content in my buffalo template in a variable like

<script> var contentJSON = "<%= content %>"</script>

Then once the page loaded I need to set the contents like quillInstance.setContents(contentJSON)

Approach 2:

Incase the request is compromised then the html may contain scripts unescaped. So if I try like this

c.Set("getContent", func(content string) template.HTML {
    return template.HTML(html.EscapeString(content))
})

This escapes all the html entities. So all the div, styles introduced by quill js also gone with this. So the whole content looks just like a plain string.

Whats the right approach in storing the content? I am looking for a way to get this rendered in the server.

Finally i end with the following,

Helpers: render.Helpers{
            "quil_for": func(content string) template.HTML {
                content = strings.ReplaceAll(content, "<script>", "&lt;script&gt;")
                content = strings.ReplaceAll(content, "<a>", "&lt;a&gt;")
                content = strings.ReplaceAll(content, "</a>", "&lt;/a&gt;")
                content = strings.ReplaceAll(content, "</script>", "&lt;/script&gt;")
                return template.HTML(content)
            },
        },

instead of this

c.Set("getContent", func(content string) template.HTML {
    return template.HTML(html.EscapeString(content))
})

This escapes only the script and the anchor tag and the res of html as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM