11var mainWindow ;
22var origin ;
33var editorJS = null ;
4-
4+ var markdownContent = "" ;
55window . MathJax = {
66 tex : {
77 inlineMath : [ [ '$' , '$' ] , [ "\\(" , "\\)" ] ] ,
@@ -25,7 +25,19 @@ window.MathJax = {
2525 load : [ '[tex]/noerrors' ]
2626 }
2727} ;
28-
28+ function updateImage ( id , data , retryAttempt ) {
29+ let image = document . getElementById ( id ) ;
30+ if ( image == null ) {
31+ if ( retryAttempt < 20 ) {
32+ setTimeout ( ( ) => { updateImage ( id , data , retryAttempt + 1 ) } , 50 ) ;
33+ } else {
34+ console . log ( 'unable to set data for image with id:' + id ) ;
35+ }
36+ } else {
37+ let blob = new Blob ( [ data ] ) ;
38+ image . src = URL . createObjectURL ( blob ) ;
39+ }
40+ }
2941window . addEventListener ( 'message' , function ( e ) {
3042 let parentDomain = window . location . host . substring ( window . location . host . indexOf ( "." ) + 1 )
3143 if ( e . origin !== ( window . location . protocol + "//" + parentDomain ) )
@@ -38,9 +50,7 @@ window.addEventListener('message', function (e) {
3850 if ( e . data . action == "ping" ) {
3951 mainWindow . postMessage ( { action :'pong' } , e . origin ) ;
4052 } else if ( e . data . action == "respondToLoadImage" ) {
41- let image = document . getElementById ( e . data . id ) ;
42- let blob = new Blob ( [ e . data . data ] ) ;
43- image . src = URL . createObjectURL ( blob ) ;
53+ updateImage ( e . data . id , e . data . data , 0 ) ;
4454 } else if ( e . data . action == "respondToNavigateTo" ) {
4555 let markdownThemeToUse = e . data . theme != null && e . data . theme == 'dark-mode' ? 'dark' : 'light' ;
4656 if ( e . data . extension == 'note' ) {
@@ -79,8 +89,7 @@ function initialiseMarkdownEditor(theme, subPathInput, text) {
7989 theme : theme ,
8090 subPath : subPath
8191 } ) ;
82- let output = viewer . getHTML ( ) ;
83- let xss = DOMPurify . sanitize ( output ) ;
92+ let xss = rewriteImages ( DOMPurify . sanitize ( markdownContent ) ) ;
8493 let element = document . getElementById ( 'sanitized' ) ;
8594 let body = document . getElementById ( 'body-element' ) ;
8695 let mdElement = document . getElementById ( 'md-element' ) ;
@@ -182,22 +191,46 @@ function updateResourcesInDoc() {
182191 }
183192 } ) ;
184193 }
185- let images = document . getElementsByTagName ( "img" ) ;
186- for ( var i = 0 ; i < images . length ; i ++ ) {
187- let image = images [ i ] ;
188- let isDataImage = image . src . startsWith ( "data:" ) ;
189- if ( ! isDataImage ) {
190- const requestedResource = new URL ( image . src ) ;
191- if ( window . location . host == requestedResource . host ) {
192- image . src = '#' ;
193- let generatedId = 'image-' + uuid ( ) ;
194- image . id = generatedId ;
195- mainWindow . postMessage ( { action :'loadImage' , src : requestedResource . pathname , id : generatedId } , origin ) ;
194+
195+ }
196+ function setMarkdownContent ( content ) {
197+ markdownContent = content ;
198+ }
199+ function rewriteImages ( html ) {
200+ var finished = false ;
201+ var startIndex = 0 ;
202+ while ( ! finished ) {
203+ let imgStartIndex = html . indexOf ( '<img ' , startIndex ) ;
204+ if ( imgStartIndex == - 1 ) {
205+ finished = true ;
206+ } else {
207+ let imgEndIndex = html . indexOf ( '">' , imgStartIndex + 5 ) ;
208+ if ( imgEndIndex == - 1 ) {
209+ finished = true ;
196210 } else {
197- console . log ( 'invalid link: ' + image . src ) ;
211+ startIndex = imgStartIndex + 5 ;
212+ let imgTag = html . substring ( imgStartIndex , imgEndIndex ) ;
213+ startIndex = imgEndIndex ;
214+ let sourceAttrIndex = imgTag . indexOf ( 'src="' ) ;
215+ if ( sourceAttrIndex > 0 ) {
216+ let src = imgTag . substring ( sourceAttrIndex + 5 ) ;
217+ //console.log('image src=' + src);
218+ let generatedId = 'image-' + uuid ( ) ;
219+ html = html . substring ( 0 , imgStartIndex + sourceAttrIndex ) + ' id="' + generatedId + '" src="#' + html . substring ( imgEndIndex ) ;
220+ let tmpImg = new Image ( ) ;
221+ tmpImg . src = src ;
222+ tmpImg . loading = "lazy" ;
223+ const requestedResource = new URL ( tmpImg . src ) ;
224+ if ( window . location . host == requestedResource . host ) {
225+ mainWindow . postMessage ( { action :'loadImage' , src : requestedResource . pathname , id : generatedId } , origin ) ;
226+ } else {
227+ console . log ( 'invalid link: ' + tmpImg . src ) ;
228+ }
229+ }
198230 }
199231 }
200232 }
233+ return html ;
201234}
202235function initialiseEditorJS ( theme , jsonData ) {
203236 let setToDarkMode = theme != null && theme == 'dark-mode' ;
0 commit comments