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

这两个都是不可以的。

相关推荐
沐爸muba11 小时前
TS系列(7):知识点汇总
typescript
战族狼魂12 小时前
基于TypeScript+React+AntDesign 的车辆车型管理页面
javascript·react.js·typescript
终末圆21 小时前
TypeScript 基本使用指南【前端 26】
前端·javascript·typescript
MavenTalk1 天前
TypeScript是基于LLM上层研发的受益者
前端·javascript·typescript
无名小小卒2 天前
三小时快速上手TypeScript,TS速通教程(上篇、中篇、下篇、附加篇)
开发语言·前端·typescript
人工智能的苟富贵2 天前
如何封装微信小程序中的图片上传功能
微信小程序·小程序·typescript
马可家的菠萝2 天前
apply、call和bind的作用和区别
前端·javascript·typescript
那些免费的砖3 天前
Vue-Bag-Admin 采用漂亮的 Naive UI 构建的开源中后台系统,基于 Vue3 / Vite / TypeScript 等最新的前端技术栈
前端·vue.js·typescript
bobostudio19953 天前
TypeScript 设计模式之【状态模式】
前端·javascript·设计模式·typescript·状态模式