javascript - Parsing plain text Markdown from a ContentEditable div -


i know there other questions on editable divs, couldn't find 1 specific markdown-related issue have.

user typing inside contenteditable div. , may choose number of markdown-related things code blocks, headers, , whatever.

i having issues extracting source , storing database displayed again later standard markdown parser. have tried 2 ways:

  1. $('.content').text()

in method, problem line breaks stripped out , of course not okay.

  1. $('.content').html()

in method, can line breaks working fine using regex replace <br\> \n before inserting database. browser wraps things ## heading here divs, this: <div>## heading here</div>. problematic me because when go display afterwards, don't proper markdown formatting.

what's best (most simple , reliable) way solve problem of 2015?

edit: found potential solution here: http://www.davidtong.me/innerhtml-innertext-textcontent-html-and-text/

if check documentation of jquery's .text() method,

the result of .text() method string containing combined text of matched elements. (due variations in html parsers in different browsers, text returned may vary in newlines , other white space.)

so getting whitespaces not guaranteed in browsers.

try using innertext property of element.

document.getelementsbyclassname('content')[0].innertext 

this returns text white spacing intact. not cross browser compatible. works in ie , chrome, not in firefox.

the innertext equivalent firefox textcontent (link), strips out whitespaces.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -