import { readFileSync } from "fs-extra" ; import { App , FileSystemAdapter } from "obsidian" ; import path from "path" ; import { ImageCollector } from "./imageCollector" ; import { AdvancedSlidesSettings } from "./main" ; export class ObsidianUtils { private app : App ; private fileSystem : FileSystemAdapter ; private settings : AdvancedSlidesSettings ; private yamlRegex = /^---[^-]*---/ ; constructor ( app , settings ) { this . app = app ; this . fileSystem = this . app . vault . adapter as FileSystemAdapter ; this . settings = settings ; } getVaultName ( ) { return this . app . vault . getName ( ) ; } getVaultDirectory ( ) { return this . fileSystem . getBasePath ( ) ; } getPluginDirectory ( ) { return path . join ( this . getVaultDirectory ( ) , this . app . vault . configDir , 'plugins/obsidian-advanced-slides/' ) ; } getDistDirectory ( ) { return path . join ( this . getPluginDirectory ( ) , '/dist/' ) ; } getExportDirectory ( ) { return path . join ( this . getVaultDirectory ( ) , this . settings . exportDirectory ) ; } getSettings ( ) { return this . settings ; } /** TODO: Refactoring ************************** */ getAbsolutePath ( filename ) { const markdownFile = this . app . vault . getMarkdownFiles ( ) . filter ( ( item ) => { return item . path . contains ( filename ) ; } ) . first ( ) ; if ( markdownFile ) return this . fileSystem . getFullPath ( markdownFile . path ) ; else { return null ; } } findFile ( imagePath ) { const expDir = this . settings . exportDirectory . startsWith ( "/" ) ? this . settings . exportDirectory . substring ( 1 ) : this . settings . exportDirectory ; const imgFile = this . app . vault . getFiles ( ) . filter ( item => item . path . contains ( imagePath ) && ! item . path . contains ( expDir ) ) . first ( ) ; let base = '' ; if ( ! ImageCollector . getInstance ( ) . shouldCollect ( ) ) { base = '/' ; } if ( imgFile ) { return base + imgFile . path ; } else { return imagePath ; } } findImageEx ( filePath ) { const expDir = this . settings . exportDirectory . startsWith ( "/" ) ? this . settings . exportDirectory . substring ( 1 ) : this . settings . exportDirectory ; let imagePath = filePath + '.svg' ; let imgFile = this . app . vault . getFiles ( ) . filter ( item => item . path . contains ( imagePath ) && ! item . path . contains ( expDir ) ) . first ( ) ; let base = '' ; if ( ! ImageCollector . getInstance ( ) . shouldCollect ( ) ) { base = '/' ; } if ( imgFile ) { return base + imagePath ; } imagePath = filePath + '.png' ; imgFile = this . app . vault . getFiles ( ) . filter ( item => item . path . contains ( imagePath ) && ! item . path . contains ( expDir ) ) . first ( ) ; if ( imgFile ) { return base + imagePath ; } return null ; } parseFile ( file , header ) { const fileContent = readFileSync ( file , { encoding : 'utf-8' } ) ; if ( header === null ) { return fileContent . replace ( this . yamlRegex , '' ) ; } else { const lines = fileContent . split ( '\n' ) ; let startIdx = null ; let endIdx = null ; for ( let i = 0 ; i < lines . length ; i ++ ) { if ( startIdx != null && lines [ i ] . startsWith ( '#' ) ) { endIdx = i ; break ; } if ( lines [ i ] . includes ( header ) ) { startIdx = i ; } } if ( startIdx === null ) { return "![[" + file + "#" + header + "]]" ; } if ( endIdx === null ) { return lines . slice ( startIdx ) . join ( '\n' ) ; } else { return lines . slice ( startIdx , endIdx ) . join ( '\n' ) ; } } } }