import { App , MarkdownView , TFile , WorkspaceLeaf } from "obsidian" ; export async function findLeafByFile ( app , file , open = false ) { let found = null ; app . workspace . iterateAllLeaves ( leaf => { if ( leaf . view instanceof MarkdownView ) { if ( leaf . view . file . path === file . path ) { found = leaf ; } } } ) ; if ( found === null && open ) { found = app . workspace . getUnpinnedLeaf ( ) ; await found . openFile ( file ) ; return found ; } return found ; } export async function getMarkdownViewFor ( app , file ) { const leaf = await findLeafByFile ( app , file , true ) ; if ( leaf == null ) { return null ; } if ( ! ( leaf . view instanceof MarkdownView ) ) { throw "unexpected view" ; } return leaf . view as MarkdownView ; }