import { Vault } from "obsidian" ; import * as path from "path" ; import { base32 , base64url } from "rfc4648" ; import XRegExp from "xregexp" ; import * as origLog from "loglevel" ; const log = origLog . getLogger ( "rs-default" ) ; param item param loose returns export const isHiddenPath = ( item , loose = true ) => { const k = path . posix . normalize ( item ) ; const k2 = k . split ( "/" ) ; for ( const singlePart of k2 ) { if ( singlePart === "." || singlePart === ".." || singlePart === "" ) { continue ; } if ( singlePart [ 0 ] === "." ) { return true ; } if ( loose && singlePart [ 0 ] === "_" ) { return true ; } } return false ; } ; param x returns export const getFolderLevels = ( x ) => { const res = [ ] ; if ( x === "" || x === "/" ) { return res ; } const y1 = x . split ( "/" ) ; let i = 0 ; for ( let index = 0 ; index + 1 < y1 . length ; index ++ ) { const k = y1 . slice ( 0 , index + 1 ) . join ( "/" ) ; if ( k !== "" && k !== "/" ) { res . push ( k ) ; } } return res ; } ; export const mkdirpInVault = async ( thePath , vault ) => { const foldersToBuild = getFolderLevels ( thePath ) ; for ( const folder of foldersToBuild ) { const r = await vault . adapter . exists ( folder ) ; if ( ! r ) { log . info ( `mkdir ${ folder }` ) ; await vault . adapter . mkdir ( folder ) ; } } } ; param b returns export const bufferToArrayBuffer = ( b ) => { return b . buffer . slice ( b . byteOffset , b . byteOffset + b . byteLength ) ; } ; param b returns export const arrayBufferToBuffer = ( b ) => { return Buffer . from ( b ) ; } ; export const arrayBufferToBase64 = ( b ) => { return arrayBufferToBuffer ( b ) . toString ( "base64" ) ; } ; export const arrayBufferToHex = ( b ) => { return arrayBufferToBuffer ( b ) . toString ( "hex" ) ; } ; export const base64ToArrayBuffer = ( b64text ) => { return bufferToArrayBuffer ( Buffer . from ( b64text , "base64" ) ) ; } ; param hex returns export const hexStringToTypedArray = ( hex ) => { return new Uint8Array ( hex . match ( /[\da-f]{2}/gi ) . map ( function ( h ) { return parseInt ( h , 16 ) ; } ) ) ; } ; export const base64ToBase32 = ( a ) => { return base32 . stringify ( Buffer . from ( a , "base64" ) ) ; } ; export const base64ToBase64url = ( a , pad = false ) => { let b = a . replace ( /\+/g , "-" ) . replace ( /\//g , "_" ) ; if ( ! pad ) { b = b . replace ( /=/g , "" ) ; } return b ; } ; param a export const isVaildText = ( a ) => { return ! XRegExp ( "\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cn}|\\p{Zl}|\\p{Zp}" , "A" ) . test ( a ) ; } ; param a returns export const getPathFolder = ( a ) => { if ( a . endsWith ( "/" ) ) { return a ; } const b = path . posix . dirname ( a ) ; return b . endsWith ( "/" ) ? b : `${ b }/` ; } ; param a param delimiter returns export const setToString = ( a , delimiter = "," ) => { return [ ... a ] . join ( delimiter ) ; } ; export const extractSvgSub = ( x , subEl = "rect" ) => { const parser = new window . DOMParser ( ) ; const dom = parser . parseFromString ( x , "image/svg+xml" ) ; const svg = dom . querySelector ( "svg" ) ; svg . setAttribute ( "viewbox" , "0 0 10 10" ) ; return svg . innerHTML ; } ; param min param max returns export const getRandomIntInclusive = ( min , max ) => { const randomBuffer = new Uint32Array ( 1 ) ; window . crypto . getRandomValues ( randomBuffer ) ; let randomNumber = randomBuffer [ 0 ] / ( 0xffffffff + 1 ) ; min = Math . ceil ( min ) ; max = Math . floor ( max ) ; return Math . floor ( randomNumber * ( max - min + 1 ) ) + min ; } ; param byteLength returns export const getRandomArrayBuffer = ( byteLength ) => { const k = window . crypto . getRandomValues ( new Uint8Array ( byteLength ) ) ; return bufferToArrayBuffer ( k ) ; } ; param x returns export const reverseString = ( x ) => { return [ ... x ] . reverse ( ) . join ( "" ) ; } ;