-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathlink-processor.ts
More file actions
379 lines (328 loc) · 13 KB
/
Copy pathlink-processor.ts
File metadata and controls
379 lines (328 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { App, TFile, Vault } from 'obsidian';
import { FeishuApiClient } from './feishu-api';
/**
* 双链信息接口
*/
export interface WikiLinkInfo {
/** 原始双链文本 */
originalText: string;
/** 链接的文档标题 */
title: string;
/** 在文档中的位置 */
position: number;
/** 对应的文件对象 */
file?: TFile;
/** 上传后的飞书文档链接 */
feishuUrl?: string;
/** 上传后的飞书文档token */
feishuToken?: string;
}
/**
* 上传结果接口
*/
export interface UploadResult {
/** 文档token */
token: string;
/** 文档URL */
url: string;
/** 文档标题 */
title: string;
}
type LinkProcessorPluginContext = {
settings: {
folderToken: string;
};
updateHistoryPermissions?: (docToken: string, permissions: {
isPublic: boolean;
allowCopy: boolean;
allowCreateCopy: boolean;
}) => Promise<void> | void;
};
/**
* 双链处理器类
* 负责处理Obsidian双链格式的笔记上传
*/
export class LinkProcessor {
private app: App;
private vault: Vault;
private feishuClient: FeishuApiClient;
private plugin: LinkProcessorPluginContext;
private uploadedDocuments: Map<string, UploadResult> = new Map(); // 缓存已上传的文档
private processingDocuments: Set<string> = new Set(); // 正在处理的文档,防止循环引用
private static debugEnabled = false;
static setDebugEnabled(enabled: boolean): void {
this.debugEnabled = enabled;
}
private debug(...args: unknown[]): void {
if (LinkProcessor.debugEnabled) {
console.debug(...args);
}
}
private logError(summary: string, error: unknown, details?: Record<string, unknown>): void {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(summary, errorMessage);
this.debug(`${summary} 详情:`, {
...details,
error,
errorMessage,
errorStack: error instanceof Error ? error.stack : undefined
});
}
constructor(app: App, feishuClient: FeishuApiClient, plugin: LinkProcessorPluginContext) {
this.app = app;
this.vault = app.vault;
this.feishuClient = feishuClient;
this.plugin = plugin;
}
/**
* 从Markdown内容中提取所有双链引用
* @param content Markdown内容
* @returns 双链信息数组
*/
extractWikiLinks(content: string): WikiLinkInfo[] {
const wikiLinks: WikiLinkInfo[] = [];
// 匹配Obsidian双链格式: [[文档标题]] 或 [[文档标题|显示文本]]
const wikiLinkRegex = /\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g;
let match;
while ((match = wikiLinkRegex.exec(content)) !== null) {
if (!match[1]) continue; // 跳过无效匹配
const title = match[1].trim(); // 文档标题
const originalText = match[0]; // 完整的双链语法
const position = match.index; // 在内容中的位置
// 查找对应的文件
const file = this.findFileByTitle(title);
wikiLinks.push({
originalText,
title,
position,
...(file && { file }) // 只有当file存在时才添加file属性
});
}
return wikiLinks;
}
/**
* 根据标题查找文件
* @param title 文档标题
* @returns 对应的文件对象或null
*/
private findFileByTitle(title: string): TFile | null {
// 获取所有markdown文件
const files = this.vault.getMarkdownFiles();
// 1. 精确匹配:文件名(不含扩展名)等于标题
let matchedFile = files.find(file => file.basename === title);
if (matchedFile) return matchedFile;
// 2. 路径匹配:支持带路径的引用,如 "folder/filename"
matchedFile = files.find(file => {
const pathWithoutExt = file.path.replace(/\.md$/, '');
return pathWithoutExt === title || pathWithoutExt.endsWith('/' + title);
});
if (matchedFile) return matchedFile;
// 3. 模糊匹配:标题包含在文件名中或文件名包含在标题中
matchedFile = files.find(file => {
const baseName = file.basename.toLowerCase();
const titleLower = title.toLowerCase();
return baseName.includes(titleLower) || titleLower.includes(baseName);
});
if (matchedFile) return matchedFile;
return null;
}
/**
* 递归上传引用的文档
* @param wikiLinks 双链信息数组
* @param onProgress 进度回调
* @param applyPermissions 是否应用权限设置(与主文档保持一致)
* @param permissions 权限设置对象
* @param userId 用户ID(用于转移所有权)
* @returns 上传结果映射
*/
async uploadReferencedDocuments(
wikiLinks: WikiLinkInfo[],
onProgress?: (status: string) => void,
applyPermissions: boolean = false,
permissions?: {
isPublic: boolean;
allowCopy: boolean;
allowCreateCopy: boolean;
allowPrintDownload: boolean;
copyEntity?: string;
securityEntity?: string;
},
userId?: string
): Promise<Map<string, UploadResult>> {
const results = new Map<string, UploadResult>();
for (let i = 0; i < wikiLinks.length; i++) {
const wikiLink = wikiLinks[i];
if (!wikiLink) continue; // 跳过undefined项
const progress = `正在处理引用文档 ${i + 1}/${wikiLinks.length}: ${wikiLink.title}`;
onProgress?.(progress);
try {
const result = await this.uploadSingleDocument(wikiLink);
if (result) {
results.set(wikiLink.title, result);
wikiLink.feishuUrl = result.url;
wikiLink.feishuToken = result.token;
// 如果需要应用权限设置,则为引用文档设置与主文档相同的权限
if (applyPermissions && permissions && result.token) {
try {
onProgress?.(`正在为引用文档设置权限: ${wikiLink.title}`);
// 使用与主文档完全相同的权限设置方法
await this.feishuClient.setDocumentPermissions(result.token, permissions, userId);
// 更新历史记录中的权限设置
if (this.plugin.updateHistoryPermissions) {
const permissionsToSave = {
isPublic: permissions.isPublic,
allowCopy: permissions.allowCopy,
allowCreateCopy: permissions.allowCreateCopy
};
await this.plugin.updateHistoryPermissions(result.token, permissionsToSave);
}
} catch (permissionError) {
this.logError(`[双链处理] 引用文档权限设置失败: ${wikiLink.title}`, permissionError, {
title: wikiLink.title
});
// 权限设置失败不影响文档上传结果,继续处理
}
}
}
} catch (error) {
this.logError(`[双链处理] 文档上传失败: ${wikiLink.title}`, error, {
title: wikiLink.title
});
// 继续处理其他文档,不中断整个流程
}
}
return results;
}
/**
* 上传单个文档到飞书
* @param wikiLink 双链信息
* @returns 上传结果
*/
private async uploadSingleDocument(wikiLink: WikiLinkInfo): Promise<UploadResult | null> {
// 如果没有找到文件,尝试重新查找
if (!wikiLink.file) {
const foundFile = this.findFileByTitle(wikiLink.title);
if (!foundFile) return null;
wikiLink.file = foundFile;
}
// 检查是否已经上传过
if (this.uploadedDocuments.has(wikiLink.title)) {
return this.uploadedDocuments.get(wikiLink.title)!;
}
// 检查是否正在处理中(防止循环引用)
if (this.processingDocuments.has(wikiLink.title)) {
return null;
}
// 标记为正在处理
this.processingDocuments.add(wikiLink.title);
try {
// 读取文件内容
const content = await this.app.vault.read(wikiLink.file);
// 直接使用飞书客户端上传文档,传递正确的folderToken
const uploadResult = await this.feishuClient.uploadDocument(
wikiLink.file.name,
content,
this.plugin.settings.folderToken
);
const result: UploadResult = {
token: uploadResult.token,
url: uploadResult.url,
title: wikiLink.title
};
// 缓存结果
this.uploadedDocuments.set(wikiLink.title, result);
return result;
} catch (error) {
this.logError(`[双链处理] 文档上传失败: ${wikiLink.title}`, error, {
title: wikiLink.title
});
return null;
} finally {
// 移除处理标记
this.processingDocuments.delete(wikiLink.title);
}
}
/**
* 将双链替换为飞书文档链接
* @param content 原始内容
* @param wikiLinks 双链信息数组
* @returns 替换后的内容
*/
replaceWikiLinksWithFeishuLinks(content: string, wikiLinks: WikiLinkInfo[]): string {
let processedContent = content;
// 按位置倒序排列,避免替换时位置偏移
const sortedWikiLinks = [...wikiLinks].sort((a, b) => b.position - a.position);
for (const wikiLink of sortedWikiLinks) {
if (wikiLink.feishuUrl) {
// 替换为Markdown链接格式: [显示文本](飞书链接)
const markdownLink = `[${wikiLink.title}](${wikiLink.feishuUrl})`;
processedContent = processedContent.replace(wikiLink.originalText, markdownLink);
}
}
return processedContent;
}
/**
* 处理文档中的所有双链引用
* @param content 文档内容
* @param onProgress 进度回调
* @param applyPermissions 是否应用权限设置(与主文档保持一致)
* @param permissions 权限设置对象
* @param userId 用户ID(用于转移所有权)
* @returns 处理后的内容和上传结果
*/
async processWikiLinks(
content: string,
onProgress?: (status: string) => void,
applyPermissions: boolean = false,
permissions?: {
isPublic: boolean;
allowCopy: boolean;
allowCreateCopy: boolean;
allowPrintDownload: boolean;
copyEntity?: string;
securityEntity?: string;
},
userId?: string
): Promise<{ processedContent: string; uploadResults: Map<string, UploadResult> }> {
// 提取双链引用
const wikiLinks = this.extractWikiLinks(content);
if (wikiLinks.length === 0) {
return {
processedContent: content,
uploadResults: new Map()
};
}
// 上传引用的文档,传递权限设置参数
onProgress?.('正在上传引用的文档...');
const uploadResults = await this.uploadReferencedDocuments(
wikiLinks,
onProgress,
applyPermissions,
permissions,
userId
);
// 替换双链为飞书文档链接
onProgress?.('正在替换双链为飞书链接...');
const processedContent = this.replaceWikiLinksWithFeishuLinks(content, wikiLinks);
return {
processedContent,
uploadResults
};
}
/**
* 清理缓存
*/
clearCache(): void {
this.uploadedDocuments.clear();
this.processingDocuments.clear();
}
/**
* 获取已上传文档的统计信息
*/
getUploadStats(): { totalUploaded: number; uploadedTitles: string[] } {
return {
totalUploaded: this.uploadedDocuments.size,
uploadedTitles: Array.from(this.uploadedDocuments.keys())
};
}
}