/** Converts <>&" to their HTML escape sequences */
function escapeHTMLEntities(str) {
return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
/** Restores the original source string's '<' and '>' as entered in
the document, before the browser processed it as HTML. There is no
way in an HTML document to distinguish an entity that was entered
as an entity.*/
function unescapeHTMLEntities(str) {
// Process & last so that we don't recursively unescape
// escaped escape sequences.
return str.
replace(/</g, '<').
replace(/>/g, '>').
replace(/"/g, '"').
replace(/'/g, "'").
replace(/–/g, '--').
replace(/—/g, '---').
replace(/&/g, '&');
}
/**
\param node A node from an HTML DOM
\return A String that is a very good reconstruction of what the
original source looked like before the browser tried to correct
it to legal HTML.
*/
function nodeToMarkdeepSource(node, leaveEscapes) {
var source = node.innerHTML;
// Markdown uses e-mail syntax, which HTML parsing
// will try to close by inserting the matching close tags at the end of the
// document. Remove anything that looks like that and comes *after*
// the first fallback style.
source = source.replace(/(?: