File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -526,7 +526,23 @@ <h1>Markdown Preview <small>with MathJax</small></h1>
526526 // Use a small delay to ensure MathJax has a chance to load, especially on slower connections
527527 // Although the typesetPromise logic should handle readiness checks.
528528 // TODO: 这里不太对,有的时候好像会报错,我们应该找一个可以订阅是否 init 结束的地方
529- setTimeout ( updatePreview , 150 ) ;
529+ function init_update ( ) {
530+ // 因为 plugin 似乎是异步注册的...所以我们需要检测一下,如果渲染之后的 html 中不包含 `.MathJax` 元素,我们就再次 update ,最多试 5 次
531+ // 也就是我们假设必须有 math jax 在 markdown 里
532+ const is_init_ok = ( ) => htmlOutput . querySelector ( '.MathJax' ) ;
533+ let count = 0 ;
534+ updatePreview ( ) ;
535+ const intervalId = setInterval ( ( ) => {
536+ if ( is_init_ok ( ) || count > 5 ) {
537+ clearInterval ( intervalId ) ;
538+ console . log ( "Initial preview finished." ) ;
539+ return ;
540+ }
541+ updatePreview ( ) ;
542+ count ++ ;
543+ } , 100 ) ;
544+ }
545+ setTimeout ( init_update ) ;
530546
531547 /**
532548 * 读取某个 query 参数 用于附加功能
You can’t perform that action at this time.
0 commit comments