import { TemplaterError } from "./Error" ; import { App , normalizePath , TAbstractFile , TFile , TFolder , Vault , } from "obsidian" ; export function delay ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } export function escape_RegExp ( str ) { return str . replace ( /[.*+?^${}()|[\]\\]/g , "\\$&" ) ; } export function generate_command_regex ( ) { return /<%(?:-|_)?\s*[*~]{0,1}((?:.|\s)*?)(?:-|_)?%>/g ; } export function generate_dynamic_command_regex ( ) { return /(<%(?:-|_)?\s*[*~]{0,1})\+((?:.|\s)*?%>)/g ; } export function resolve_tfolder ( app , folder_str ) { folder_str = normalizePath ( folder_str ) ; const folder = app . vault . getAbstractFileByPath ( folder_str ) ; if ( ! folder ) { throw new TemplaterError ( `Folder "${ folder_str }" doesn't exist` ) ; } if ( ! ( folder instanceof TFolder ) ) { throw new TemplaterError ( `${ folder_str } is a file, not a folder` ) ; } return folder ; } export function resolve_tfile ( app , file_str ) { file_str = normalizePath ( file_str ) ; const file = app . vault . getAbstractFileByPath ( file_str ) ; if ( ! file ) { throw new TemplaterError ( `File "${ file_str }" doesn't exist` ) ; } if ( ! ( file instanceof TFile ) ) { throw new TemplaterError ( `${ file_str } is a folder, not a file` ) ; } return file ; } export function get_tfiles_from_folder ( app , folder_str ) { const folder = resolve_tfolder ( app , folder_str ) ; const files = [ ] ; Vault . recurseChildren ( folder , ( file ) => { if ( file instanceof TFile ) { files . push ( file ) ; } } ) ; files . sort ( ( a , b ) => { return a . basename . localeCompare ( b . basename ) ; } ) ; return files ; } export function arraymove < T > ( arr , fromIndex , toIndex ) { if ( toIndex < 0 || toIndex === arr . length ) { return ; } const element = arr [ fromIndex ] ; arr [ fromIndex ] = arr [ toIndex ] ; arr [ toIndex ] = element ; }