ace_dev
ace_dev

Reputation: 21

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.

Upvotes: 0

Views: 351

Answers (1)

ace_dev
ace_dev

Reputation: 21

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.

Upvotes: 0

Related Questions