import { MarkdownLatexQuill } from "../../../../cshub-shared/src/utilities/MarkdownLatexQuill" ; import { JSDOM , VirtualConsole } from "jsdom" ; import QuillDefaultOptions from "../../../../cshub-shared/src/utilities/QuillDefaultOptions" ; import logger from "../../utilities/Logger" ; import { getHTML } from "../../../../cshub-shared/src/utilities/EditsHandler" ; process . on ( "message" , ( delta ) => { logger . info ( "Spawned child process" ) ; const virtualConsole = new VirtualConsole ( ) ; virtualConsole . on ( "error" , ( err ) => { logger . error ( `Virtual console: ${ err }` ) ; } ) ; virtualConsole . on ( "warn" , ( warn ) => { logger . warn ( `Virtual console: ${ warn }` ) ; } ) ; const jsdom = new JSDOM ( `
    <!DOCTYPE html>
    <html>
        <head>
            <script src="file://${ __dirname }/assets/quill.min.js"></script>
            <script src="file://${ __dirname }/assets/katex.min.js"></script>
        </head>
        <body><div id="editor-container"></div></body>
    </html>` , { runScripts : "dangerously" , resources : "usable" , virtualConsole , } ) ; const window = jsdom . window ; const document = window . document ; document . execCommand = ( ) => { return false ; } ; window . onerror = ( err ) => { logger . error ( "JSDOM Save errors" ) ; logger . error ( err . toString ( ) ) ; } ; window . onload = ( ) => { const container = document . getElementById ( "editor-container" ) ; document . getSelection = function ( ) { return null ; } ; const quillWindow = ( window as any ) . Quill ; const options = QuillDefaultOptions ; delete options . modules . cursors ; delete options . modules . resize ; const quill = new quillWindow ( container , options ) ; const markdownParser = new MarkdownLatexQuill ( quillWindow ) ; markdownParser . registerQuill ( ) ; quill . setContents ( delta ) ; const html = getHTML ( quill , document ) ; const filteredArr = html . toLowerCase ( ) . replace ( /<(.+?)>/g , " " ) . replace ( /[^a-zA-Z -]/g , " " ) . replace ( /\b[a-zA-Z]{1,3}\b/g , " " ) . replace ( /\s+/g , "\n" ) . split ( "\n" ) ; const unique = [ ... new Set ( filteredArr ) ] ; const htmlFiltered = unique . join ( "" ) ; logger . info ( "Returning from child process" ) ; process . send ! ( { html , indexWords : htmlFiltered } ) ; } ; } ) ;