import { getMarkdownParser , MarkdownLatexQuill } from "./MarkdownLatexQuill" ; export const getHTML = ( quillEditor , document ) : string => { const node = quillEditor . container . firstChild . cloneNode ( true ) ; const allNodes = [ ... node . getElementsByTagName ( "*" ) ] ; let prevElement = { isMarkdownBlock : false , } ; const finalizeMarkdownBlock = ( doc ) => { if ( prevElement . isMarkdownBlock ) { const currString = prevElement . currString || "" ; prevElement . currString = currString . split ( "<" ) . join ( "&lt;" ) . split ( ">" ) . join ( "&gt;" ) ; const newNode = doc . createElement ( "div" ) ; newNode . style . whiteSpace = "normal" ; newNode . classList . add ( "markdown-body" ) ; newNode . innerHTML = getMarkdownParser ( ) . render ( prevElement . currString ) . split ( "&amp;lt;" ) . join ( "&lt;" ) . split ( "&amp;gt;" ) . join ( "&gt;" ) ; if ( prevElement . containerNode ) { prevElement . containerNode . before ( newNode ) ; } prevElement = { isMarkdownBlock : false , } ; } } ; const toBeDeletedNodes = [ ] ; for ( const domNode of allNodes ) { const isMarkdownNode = ( domNode . tagName === "PRE" && domNode . classList . contains ( MarkdownLatexQuill . blotName ) ) || ( prevElement . isMarkdownBlock && domNode . tagName === "BR" ) ; if ( isMarkdownNode ) { toBeDeletedNodes . push ( domNode ) ; if ( prevElement . isMarkdownBlock ) { if ( domNode . textContent !== "\n" ) { prevElement . currString += domNode . textContent ; } prevElement . currString += "\n" ; } else { prevElement = { isMarkdownBlock : true , containerNode : domNode , currString : `${ domNode . textContent }\n` , } ; } } else if ( domNode . tagName === "P" && domNode . innerHTML === "" ) { const br = document . createElement ( "br" ) ; domNode . parentNode . replaceChild ( br , domNode ) ; if ( ! domNode . classList . contains ( MarkdownLatexQuill . blotName ) ) { finalizeMarkdownBlock ( document ) ; } } else { finalizeMarkdownBlock ( document ) ; } } finalizeMarkdownBlock ( document ) ; toBeDeletedNodes . forEach ( ( domNode ) => { domNode . remove ( ) ; } ) ; const newAllNodes = [ ... node . getElementsByTagName ( "*" ) ] ; for ( const domNode of newAllNodes ) { const tagName = domNode . tagName ; if ( tagName === "H1" || tagName === "H2" || tagName === "H3" ) { const innerText = ( domNode . innerText || domNode . textContent ) as string ; domNode . id = innerText . split ( " " ) . join ( "-" ) . split ( "\n" ) . join ( "-" ) . toLowerCase ( ) ; } } return node . innerHTML . split ( "\u0338" ) . join ( "\uE020" ) ; } ;