106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
|
|
import { TFile, Vault, CachedMetadata, App } from 'obsidian';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 安全地创建文件夹(递归)
|
|||
|
|
*/
|
|||
|
|
export async function ensureFolder(vault: Vault, path: string): Promise<void> {
|
|||
|
|
const parts = path.split('/').filter(p => p.length > 0);
|
|||
|
|
let currentPath = '';
|
|||
|
|
|
|||
|
|
for (const part of parts) {
|
|||
|
|
currentPath = currentPath ? `${currentPath}/${part}` : part;
|
|||
|
|
const folder = vault.getAbstractFileByPath(currentPath);
|
|||
|
|
if (!folder) {
|
|||
|
|
await vault.createFolder(currentPath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 从单个文件的 frontmatter 中读取 omnibox_id(旧接口,保留兼容)。
|
|||
|
|
* 新代码请使用 OmniboxIdIndex(基于 metadataCache,零文件 IO)。
|
|||
|
|
*/
|
|||
|
|
export function getOmniboxIdFromFile(file: TFile, vault: Vault): string | null {
|
|||
|
|
// 注意:cachedRead 是异步的,与同步签名冲突,历史遗留桩;
|
|||
|
|
// 实际无人调用,保留仅为兼容。新代码一律走 OmniboxIdIndex。
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 云端 ID -> 本地文件 索引。
|
|||
|
|
*
|
|||
|
|
* 基于 Obsidian 的 metadataCache 构建 frontmatter omnibox_id 映射,
|
|||
|
|
* metadataCache 由 Obsidian 在后台维护,读取零文件 IO、构建 O(文件数),
|
|||
|
|
* 比逐个 vault.cachedRead 快几个数量级。
|
|||
|
|
*
|
|||
|
|
* 背景:旧实现靠「文件名」匹配云端与本地,云端一旦改名就匹配失败,
|
|||
|
|
* 触发重复下载,白烧免费版每天仅有的 10 次配额。ID 匹配为主、文件名为回退。
|
|||
|
|
*/
|
|||
|
|
export class OmniboxIdIndex {
|
|||
|
|
private idToPath = new Map<string, string>();
|
|||
|
|
private syncFolder: string;
|
|||
|
|
|
|||
|
|
constructor(syncFolder: string) {
|
|||
|
|
this.syncFolder = syncFolder || 'OmniBox';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 全量重建索引(打开侧栏或同步文件夹设置变更时调用) */
|
|||
|
|
rebuild(app: App): void {
|
|||
|
|
this.idToPath.clear();
|
|||
|
|
const prefix = `${this.syncFolder}/`;
|
|||
|
|
for (const file of app.vault.getMarkdownFiles()) {
|
|||
|
|
if (!file.path.startsWith(prefix)) continue;
|
|||
|
|
const cache: CachedMetadata | null = app.metadataCache.getFileCache(file);
|
|||
|
|
const id = cache?.frontmatter?.['omnibox_id'];
|
|||
|
|
if (typeof id === 'string' && id) {
|
|||
|
|
this.idToPath.set(id, file.path);
|
|||
|
|
} else if (typeof id === 'number') {
|
|||
|
|
this.idToPath.set(String(id), file.path);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 按 ID 查本地文件;找不到返回 null */
|
|||
|
|
findByCloudId(app: App, cloudId: string): TFile | null {
|
|||
|
|
const path = this.idToPath.get(cloudId);
|
|||
|
|
if (!path) return null;
|
|||
|
|
const f = app.vault.getAbstractFileByPath(path);
|
|||
|
|
return f instanceof TFile ? f : null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** ID 优先、文件名回退的统一查找入口 */
|
|||
|
|
resolveLocalFile(app: App, cloudId: string, cloudName: string, sanitize: (n: string) => string): TFile | null {
|
|||
|
|
const byId = this.findByCloudId(app, cloudId);
|
|||
|
|
if (byId) return byId;
|
|||
|
|
// 回退:老笔记可能没有 omnibox_id(或被手动清掉),按文件名兜底
|
|||
|
|
const filePath = `${this.syncFolder}/${sanitize(cloudName)}.md`;
|
|||
|
|
const f = app.vault.getAbstractFileByPath(filePath);
|
|||
|
|
return f instanceof TFile ? f : null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
get size(): number {
|
|||
|
|
return this.idToPath.size;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 检查文件是否在同步文件夹内
|
|||
|
|
*/
|
|||
|
|
export function isInSyncFolder(filePath: string, syncFolder: string): boolean {
|
|||
|
|
return filePath.startsWith(`${syncFolder}/`) || filePath === syncFolder;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 格式化时间
|
|||
|
|
*/
|
|||
|
|
export function formatTime(date: Date): string {
|
|||
|
|
return date.toLocaleString('zh-CN', {
|
|||
|
|
year: 'numeric',
|
|||
|
|
month: '2-digit',
|
|||
|
|
day: '2-digit',
|
|||
|
|
hour: '2-digit',
|
|||
|
|
minute: '2-digit',
|
|||
|
|
second: '2-digit'
|
|||
|
|
});
|
|||
|
|
}
|