import { CompletrSettings } from "../settings" ; import { DictionaryProvider } from "./dictionary_provider" ; import { Notice , Vault } from "obsidian" ; const BASE_FOLDER_PATH = ".obsidian/plugins/obsidian-completr/wordLists" ; class WordListSuggestionProvider extends DictionaryProvider { readonly wordMap : Map < string , string [ ] > = new Map < string , string [ ] > ( ) ; isEnabled ( settings ) { return settings . wordListProviderEnabled ; } async loadFromFiles ( vault , settings ) { this . wordMap . clear ( ) ; const fileNames = await this . getRelativeFilePaths ( vault ) ; for ( let i = fileNames . length - 1 ; i >= 0 ; i -- ) { const fileName = fileNames [ i ] ; let data ; try { data = await vault . adapter . read ( fileName ) ; } catch ( e ) { console . log ( "Completr: Unable to read " + fileName ) ; continue ; } const lines = data . split ( "\n" ) ; for ( let line of lines ) { if ( line === "" || line . length < settings . minWordLength ) continue ; let list = this . wordMap . get ( line . charAt ( 0 ) ) ; if ( ! list ) { list = [ ] ; this . wordMap . set ( line . charAt ( 0 ) , list ) ; } list . push ( line . trim ( ) ) ; } } let count = 0 ; for ( let entry of this . wordMap . entries ( ) ) { entry [ 1 ] = entry [ 1 ] . sort ( ( a , b ) => a . length - b . length ) ; count += entry [ 1 ] . length ; } if ( count > 0 ) new Notice ( "Loaded " + count + " words from " + BASE_FOLDER_PATH ) ; } async deleteWordList ( vault , path ) { await vault . adapter . remove ( path ) ; } async importWordList ( vault , name , text ) { const path = BASE_FOLDER_PATH + "/" + name ; if ( await vault . adapter . exists ( path ) ) return false ; await vault . adapter . write ( path , text ) ; return true ; } /**
     * Returns all files inside of  BASE_FOLDER_PATH . The resulting strings are full paths, relative to the vault
     * root. <br>
     *  example param vault async getRelativeFilePaths ( vault ) { if ( ! ( await vault . adapter . exists ( BASE_FOLDER_PATH ) ) ) await vault . adapter . mkdir ( BASE_FOLDER_PATH ) ; return ( await vault . adapter . list ( BASE_FOLDER_PATH ) ) . files ; } } export const WordList = new WordListSuggestionProvider ( ) ;