@@ -18,8 +18,10 @@ marked.use({ renderer });
18
18
19
19
document . addEventListener ( "DOMContentLoaded" , async function ( ) {
20
20
21
- const pageUrl = document . getElementById ( 'markdown_content' ) . getAttribute ( 'data-page-url' ) ;
21
+ const contentDiv = document . getElementById ( "markdown_content" ) ;
22
+ const pageUrl = contentDiv . getAttribute ( 'data-page-url' ) ;
22
23
24
+
23
25
try {
24
26
const response = await fetch ( pageUrl ) ; // Path to raw Markdown file
25
27
const blob = await response . blob ( ) ; // Unwrap to a blob...
@@ -52,12 +54,49 @@ document.addEventListener("DOMContentLoaded", async function () {
52
54
parsedHTML = marked . parse ( markdown . replace ( / ^ [ \u200B \u200C \u200D \u200E \u200F \uFEFF ] / , "" ) ) ;
53
55
parsedHTML = DOMPurify . sanitize ( parsedHTML ) ;
54
56
}
55
-
57
+
56
58
// Inject sanitized HTML into the page
57
- const contentDiv = document . getElementById ( "markdown_content" ) ;
58
59
contentDiv . innerHTML = parsedHTML ;
59
60
60
-
61
+ // After content is injected, get all images
62
+ const images = contentDiv . getElementsByTagName ( 'img' ) ;
63
+ let loadedImagesCount = 0 ;
64
+
65
+ // Function to scroll to the highlighted element
66
+ const scrollToHighlighted = ( ) => {
67
+ const highlightedElement = document . getElementById ( 'highlighted' ) ;
68
+ if ( highlightedElement ) {
69
+ highlightedElement . scrollIntoView ( {
70
+ behavior : "smooth" ,
71
+ block : "start" ,
72
+ inline : "nearest"
73
+ } ) ;
74
+ }
75
+ } ;
76
+
77
+ // If there are images, wait for all of them to load
78
+ if ( images . length > 0 ) {
79
+ for ( let img of images ) {
80
+ img . addEventListener ( 'load' , ( ) => {
81
+ loadedImagesCount ++ ;
82
+ // If all images are loaded, scroll
83
+ if ( loadedImagesCount === images . length ) {
84
+ scrollToHighlighted ( ) ;
85
+ }
86
+ } ) ;
87
+
88
+ img . addEventListener ( 'error' , ( ) => {
89
+ // Handle image loading errors if necessary
90
+ loadedImagesCount ++ ;
91
+ if ( loadedImagesCount === images . length ) {
92
+ scrollToHighlighted ( ) ;
93
+ }
94
+ } ) ;
95
+ }
96
+ } else {
97
+ // If no images, scroll immediately
98
+ scrollToHighlighted ( ) ;
99
+ }
61
100
// Render math in the body
62
101
renderMathInElement ( document . body , {
63
102
delimiters : [
@@ -68,20 +107,7 @@ document.addEventListener("DOMContentLoaded", async function () {
68
107
] ,
69
108
throwOnError : false
70
109
} ) ;
71
- // Use requestAnimationFrame to ensure the scroll happens after rendering
72
- requestAnimationFrame ( ( ) => {
73
- // Delay scrolling slightly to allow other JS to affect rendering
74
- setTimeout ( ( ) => {
75
- const highlightedElement = document . getElementById ( 'highlighted' ) ;
76
- if ( highlightedElement ) {
77
- highlightedElement . scrollIntoView ( {
78
- behavior : "smooth" ,
79
- block : "start" ,
80
- inline : "nearest"
81
- } ) ;
82
- }
83
- } , 500 ) ; // Adjust the delay as necessary
84
- } ) ;
110
+
85
111
} catch ( error ) {
86
112
console . error ( "Failed to load Markdown content:" , error ) ;
87
113
}
0 commit comments