Search code examples
javascriptrtf

Seeing copy and pasted formatting


If I have a google doc with styling (bold, underline, etc), is there a way to parse the formatting with Javascript? I am trying to make an extremely simple WYSWIG -> HTML formatter, and easily was able to substitute whitespace with HTML, but am having a harder time with formatting.

TL/DR: If I copy and paste from a Google Doc to a textarea, can I use Javascript to see what the formatting was?


Solution

  • Instead of using a textbox, which will remove all formatting, you can use HTML5's contenteditable property.

    <div id="editable" contenteditable="true">
    edit me
    </div>
    

    If you paste something into that box, it will preserve formatting. Now you can use jQuery's .html() to get the HTML of the container.

    $("#editable").html()
    

    Demo