0-1开发自己的obsidian plugin DAY 6

今天也没少写bug,遇到了离奇的问题,很难说已经了解具体原因。阶段性总结分享一下目前比较确定的内容。

我想要实现的功能是从settingtab获取键入的内容并且另外保存到一个.json文件中。

一种可行的方式是先用:

复制代码
const absPath = app.vault.adapter.basePath;
const PATH_TO_JSON = `${absPath}/.obsidian/plugins/your-plugin-name/src/data.json`;

提供当前打开的 Obsidian 笔记库的绝对路径(注意替换这里的your-plugin-name和最后的data.json)

再用fs写入你的data_to_save:

复制代码
import * as fs from 'fs';

const jsonString = JSON.stringify(data_to_save, null, 2);
//new Notice(`Saving settings to ${PATH_TO_JSON}`);
await fs.promises.writeFile(PATH_TO_JSON, jsonString, 'utf8');

中间copilot/chatgpt给我提供了两种比较迷的解法,

一是`${this.manifest.dir},它只能获取从.obsidian之后的(半截子)路径,所以总是写不成功(害我一直去检查拼写 明明都是对的)

二是__dirname,这个的路径是:Applications/obsidian.app/contents/resources/electron.asar/renderer

这两个都是不可以的。

相关推荐
ynchyong4 小时前
TS 基础
typescript·ts
西瓜太郎4995 小时前
别再手算 Token 成本:用 React 状态机做一个可追溯的模型费用计算器
react.js·typescript
右耳朵猫AI9 小时前
Web前端周刊2026W37 | Shopify 转原生、React 编译 Rust 化、Vitest 5.0、Rslib 1.0
前端·javascript·react.js·typescript·node.js
晚安日记wanna1 天前
TS const 类型参数一道面试题的四层追问
前端·面试·typescript
掰头战士1 天前
你说你折这玩意干什么-agent工作的核心,还真得好好考虑怎么折叠上下文
typescript·llm·agent
ynchyong1 天前
VUE 中 不能将类型“R[]”分配给类型“UnwrapRefSimple<R>[]”
typescript·vue·ts·unwraprefsimple
掰头战士1 天前
让散乱的工具调用成为正规军,今天咱们聊聊 Tool System管线的读写锁
typescript·llm·agent
半糖程序员1 天前
从零构建 Agent(5):让事件流同时提供过程和结果
typescript·agent