Skip to content

Latest commit

 

History

History
165 lines (132 loc) · 6.42 KB

File metadata and controls

165 lines (132 loc) · 6.42 KB
title File Utility Blocks for Obsidian Tools
description
tags
obsistants
Tools

File Utility Blocks for Obsidian Tools

Introduction

File operations are a fundamental aspect of many Obsistant tools, enabling developers to interact with your vault in meaningful ways. The file utility blocks presented in this document serve as standardized, reusable components for common file operations within the Obsidian environment. These utilities abstract away the complexities of working directly with Obsidian's API, providing a simpler and more consistent interface for file manipulation.

Purpose of File Utilities

The primary purposes of these file utility blocks are:

  1. Simplification: To reduce the complexity of file operations in Obsidian tools.
  2. Consistency: To ensure that file operations are handled uniformly across different tools.
  3. Error Handling: To provide robust error handling for common file-related issues.
  4. Reusability: To promote code reuse and reduce duplication in tool development.
  5. Maintainability: To make tools easier to maintain by using standardized components.

Role of This Documentation

This documentation serves several key purposes:

  1. Standardization: By providing a set of standard utility blocks, we encourage consistent implementation of file operations across different Obsidian tools.
  2. Education: It educates developers on best practices for file operations within the Obsidian environment.
  3. Integration Guide: It offers clear instructions on how to incorporate these utility blocks into new or existing tools.
  4. Design Principles: It explains the reasoning behind the design choices, helping developers understand and adapt the utilities as needed.

1. List Files Utility Block

Design Principle

The List Files utility should provide a simple, error-handled way to retrieve all files within a specified folder in an Obsidian vault.

Reasoning

Listing files is a common operation when working with Obsidian vaults. By encapsulating this functionality in a reusable block, we reduce code duplication and ensure consistent error handling across different tools.

Code Snippet

async function listFiles(folderPath, app) {
  try {
    const folder = app.vault.getAbstractFileByPath(folderPath);
    if (!(folder instanceof Obsidian.TFolder)) {
      throw new Error(`Not a folder: ${folderPath}`);
    }
    return folder.children
      .filter(file => file instanceof Obsidian.TFile)
      .map(file => file.path);
  } catch (error) {
    console.error(`Error listing files in ${folderPath}:`, error);
    throw new Error(`Failed to list files in ${folderPath}: ${error.message}`);
  }
}

Integration

To use this utility in your Obsidian tool:

  1. Copy the listFiles function into your tool's code.
  2. Call the function with the folder path and the app object:
try {
  const files = await listFiles("YourFolderPath", app);
  console.log("Files in folder:", files);
} catch (error) {
  console.error("Error listing files:", error);
}

2. Read File Utility Block

Design Principle

The Read File utility should provide a straightforward way to read the contents of a file in an Obsidian vault, with proper error handling.

Reasoning

Reading file contents is a fundamental operation for many Obsidian tools. This utility ensures that file reading is done consistently and safely across different tools.

Code Snippet

async function readFile(filePath, app) {
  try {
    const file = app.vault.getAbstractFileByPath(filePath);
    if (!(file instanceof Obsidian.TFile)) {
      throw new Error(`Not a file: ${filePath}`);
    }
    return await app.vault.read(file);
  } catch (error) {
    console.error(`Error reading file ${filePath}:`, error);
    throw new Error(`Failed to read file ${filePath}: ${error.message}`);
  }
}

Integration

To use this utility in your Obsidian tool:

  1. Copy the readFile function into your tool's code.
  2. Call the function with the file path and the app object:
try {
  const content = await readFile("YourFilePath.md", app);
  console.log("File content:", content);
} catch (error) {
  console.error("Error reading file:", error);
}

3. Write File Utility Block

Design Principle

The Write File utility should provide a reliable way to write or update file contents in an Obsidian vault, handling both new file creation and existing file modification.

Reasoning

Writing to files is crucial for tools that generate or modify content. This utility ensures that file writing is done safely and consistently, whether creating new files or updating existing ones.

Code Snippet

async function writeFile(filePath, content, app) {
  try {
    const file = app.vault.getAbstractFileByPath(filePath);
    if (file instanceof Obsidian.TFile) {
      await app.vault.modify(file, content);
    } else {
      await app.vault.create(filePath, content);
    }
    return true;
  } catch (error) {
    console.error(`Error writing file ${filePath}:`, error);
    throw new Error(`Failed to write file ${filePath}: ${error.message}`);
  }
}

Integration

To use this utility in your Obsidian tool:

  1. Copy the writeFile function into your tool's code.
  2. Call the function with the file path, content, and the app object:
try {
  const success = await writeFile("YourNewFile.md", "File content here", app);
  if (success) {
    console.log("File written successfully");
  }
} catch (error) {
  console.error("Error writing file:", error);
}

General Design Principles

  1. Error Handling: All utilities include try-catch blocks to handle potential errors gracefully.
  2. Async/Await: These utilities use async/await for better readability and error handling with asynchronous operations.
  3. Type Checking: The utilities check if the provided paths correspond to the expected Obsidian types (TFolder for folders, TFile for files).
  4. Consistent Interface: All utilities follow a similar pattern, taking filePath and app as parameters.
  5. Meaningful Error Messages: Error messages are descriptive and include the operation being performed and the path involved.

By using these file utility blocks, developers can ensure consistent and reliable file operations across different Obsidian tools. These utilities abstract away the complexities of working directly with Obsidian's API, providing a simpler interface for common file operations.