diff --git a/expenseManipulation.ts b/expenseManipulation.ts new file mode 100644 index 0000000..1a578c7 --- /dev/null +++ b/expenseManipulation.ts @@ -0,0 +1,31 @@ +import { App, TFile } from "obsidian"; + +export async function playWithTable(app: App) { + const file = await this.app.workspace.getActiveFile(); + // trigger if something changed in the file (ex. new expense added) + app.vault.on("modify", async (changedFile: TFile) => { + if (changedFile.path === file.path) { + if (file) { + const fileContent = await this.app.vault.read(file); + const lines = fileContent.split("\n"); + let inTable = false; + lines.forEach((line) => { + if (line.startsWith("|")) { + if (line.includes("---")) { + // This is the header line, start reading from next line + inTable = true; + } else if (inTable) { + // This is a table line, read the cells + const cells = line + .split("|") + .map((cell) => cell.trim()); + console.log(cells); + } + } else { + inTable = false; + } + }); + } + } + }); +} diff --git a/main.ts b/main.ts index b05da53..bf6738c 100644 --- a/main.ts +++ b/main.ts @@ -1,6 +1,7 @@ import test from "node:test"; import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian"; import { ExpenseModal } from "./expenseModal"; +import { playWithTable } from "./expenseManipulation"; interface BudgetSettings { expenseCategories: object; @@ -56,8 +57,14 @@ export default class budgetPlugin extends Plugin { // need to set active md file to budget console.log("Debug: trigger new expense modal from ribbon"); }); + // Adds a setting tag so the user can configure the aspects of the plugin this.addSettingTab(new ExpenseSettingTab(this.app, this)); + + // Adds a listener to the layout-ready event to trigger the playWithTable function + this.app.workspace.on("layout-ready", async () => { + await playWithTable(this.app); + }); } async loadSettings(): Promise {