优化图片引用处理和 activeFile 管理逻辑

- 改进图片引用正则表达式,支持带属性的图片链接
- 更新图片替换方法,确保正确处理带属性的图片引用
- 重构 openCategoryModal 方法,每次都获取最新的文件内容
- 在文件修改后更新 activeFile 对象,确保状态同步
This commit is contained in:
wood chen 2025-03-07 15:49:43 +08:00
parent 9152aa5d60
commit 094784f28e

View File

@ -58,7 +58,7 @@ export default class DiscourseSyncPlugin extends Plugin {
} }
extractImageReferences(content: string): string[] { extractImageReferences(content: string): string[] {
const regex = /!\[\[(.*?)\]\]/g; const regex = /!\[\[(.*?)(?:\|.*?)?\]\]/g;
const matches = []; const matches = [];
let match; let match;
while ((match = regex.exec(content)) !== null) { while ((match = regex.exec(content)) !== null) {
@ -150,10 +150,12 @@ export default class DiscourseSyncPlugin extends Plugin {
const imageReferences = this.extractImageReferences(content); const imageReferences = this.extractImageReferences(content);
const imageUrls = await this.uploadImages(imageReferences); const imageUrls = await this.uploadImages(imageReferences);
// 替换所有图片引用,包括带有属性的图片
imageReferences.forEach((ref, index) => { imageReferences.forEach((ref, index) => {
const obsRef = `![[${ref}]]`; // 使用正则表达式匹配包含属性的图片引用
const regex = new RegExp(`!\\[\\[${ref.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\|.*?)?\\]\\]`, 'g');
const discoRef = `![${ref}](${imageUrls[index]})`; const discoRef = `![${ref}](${imageUrls[index]})`;
content = content.replace(obsRef, discoRef); content = content.replace(regex, discoRef);
}); });
const isUpdate = postId !== undefined; const isUpdate = postId !== undefined;
@ -342,6 +344,13 @@ export default class DiscourseSyncPlugin extends Plugin {
} }
await this.app.vault.modify(activeFile, newContent); await this.app.vault.modify(activeFile, newContent);
// 更新 activeFile 对象,确保它反映最新状态
this.activeFile = {
name: activeFile.basename,
content: newContent,
postId: postId
};
} catch (error) { } catch (error) {
return { return {
message: "Error", message: "Error",
@ -517,23 +526,21 @@ export default class DiscourseSyncPlugin extends Plugin {
} }
private async openCategoryModal() { private async openCategoryModal() {
// 确保activeFile已设置 // 每次都重新获取 activeFile 的最新内容,不使用缓存
if (!this.activeFile) { const activeFile = this.app.workspace.getActiveFile();
const activeFile = this.app.workspace.getActiveFile(); if (!activeFile) {
if (!activeFile) { new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open();
new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open(); return;
return;
}
const content = await this.app.vault.read(activeFile);
const fm = this.getFrontMatter(content);
this.activeFile = {
name: activeFile.basename,
content: content,
postId: fm?.discourse_post_id
};
} }
const content = await this.app.vault.read(activeFile);
const fm = this.getFrontMatter(content);
this.activeFile = {
name: activeFile.basename,
content: content,
postId: fm?.discourse_post_id
};
const [categories, tags] = await Promise.all([ const [categories, tags] = await Promise.all([
this.fetchCategories(), this.fetchCategories(),
this.fetchTags() this.fetchTags()