11import type { MarkdownPostProcessorContext , WorkspaceLeaf } from 'obsidian' ;
2- import { Plugin , stringifyYaml } from 'obsidian' ;
2+ import { loadPrism , Plugin , stringifyYaml } from 'obsidian' ;
33import { RenderChildType } from 'packages/core/src/config/APIConfigs' ;
44import { EMBED_MAX_DEPTH } from 'packages/core/src/config/FieldConfigs' ;
55import type { IPlugin } from 'packages/core/src/IPlugin' ;
@@ -115,6 +115,7 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
115115 }
116116
117117 console . timeEnd ( 'meta-bind | Main >> load-time' ) ;
118+
118119 // TODO: not sure if this is still needed, but it adds 100+ms to the load time, so I disabled it for now
119120 // we need to wait for prism to load first, otherwise prism will cause problems by highlighting things that it shouldn't
120121 // await loadPrism();
@@ -168,6 +169,11 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
168169 }
169170
170171 addPostProcessors ( ) : void {
172+ // In every processor we await prism to load, otherwise prism may break our rendering.
173+ // Luckily `await loadPrism()` is a no-op if prism is already loaded.
174+ // We could also load prism once on startup, but that would add 100+ms to the reported plugin load time.
175+ // Prism is always loaded, so the total startup time would be the same, but the higher reported time may scare users.
176+
171177 // inline code blocks
172178 this . registerMarkdownPostProcessor ( ( el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
173179 const codeBlocks = el . querySelectorAll ( 'code' ) ;
@@ -192,7 +198,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
192198 } , 1 ) ;
193199
194200 // "meta-bind" code blocks
195- this . registerMarkdownCodeBlockProcessor ( 'meta-bind' , ( source , el , ctx ) => {
201+ this . registerMarkdownCodeBlockProcessor ( 'meta-bind' , async ( source , el , ctx ) => {
202+ await loadPrism ( ) ;
203+
196204 const codeBlock = el ;
197205 const content = source . trim ( ) ;
198206 const filePath = ctx . sourcePath ;
@@ -214,7 +222,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
214222 } ) ;
215223
216224 // "meta-bind-js-view" code blocks
217- this . registerMarkdownCodeBlockProcessor ( 'meta-bind-js-view' , ( source , el , ctx ) => {
225+ this . registerMarkdownCodeBlockProcessor ( 'meta-bind-js-view' , async ( source , el , ctx ) => {
226+ await loadPrism ( ) ;
227+
218228 const mountable = this . api . createJsViewFieldMountable ( ctx . sourcePath , {
219229 declaration : source ,
220230 } ) ;
@@ -223,7 +233,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
223233 } ) ;
224234
225235 // "meta-bind-embed" code blocks
226- this . registerMarkdownCodeBlockProcessor ( 'meta-bind-embed' , ( source , el , ctx ) => {
236+ this . registerMarkdownCodeBlockProcessor ( 'meta-bind-embed' , async ( source , el , ctx ) => {
237+ await loadPrism ( ) ;
238+
227239 const mountable = this . api . createEmbedMountable ( ctx . sourcePath , {
228240 content : source ,
229241 depth : 0 ,
@@ -233,7 +245,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
233245 } ) ;
234246
235247 for ( let i = 1 ; i <= EMBED_MAX_DEPTH ; i ++ ) {
236- this . registerMarkdownCodeBlockProcessor ( `meta-bind-embed-internal-${ i } ` , ( source , el , ctx ) => {
248+ this . registerMarkdownCodeBlockProcessor ( `meta-bind-embed-internal-${ i } ` , async ( source , el , ctx ) => {
249+ await loadPrism ( ) ;
250+
237251 const mountable = this . api . createEmbedMountable ( ctx . sourcePath , {
238252 content : source ,
239253 depth : i ,
@@ -244,7 +258,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
244258 }
245259
246260 // "meta-bind-button" code blocks
247- this . registerMarkdownCodeBlockProcessor ( 'meta-bind-button' , ( source , el , ctx ) => {
261+ this . registerMarkdownCodeBlockProcessor ( 'meta-bind-button' , async ( source , el , ctx ) => {
262+ await loadPrism ( ) ;
263+
248264 const mountable = this . api . createButtonMountable ( ctx . sourcePath , {
249265 declaration : source ,
250266 isPreview : false ,
0 commit comments