From 094784f28e58beb9bf3c9ddf348997b2efe4c837 Mon Sep 17 00:00:00 2001 From: wood chen Date: Fri, 7 Mar 2025 15:49:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E5=A4=84=E7=90=86=E5=92=8C=20activeFile=20=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改进图片引用正则表达式,支持带属性的图片链接 - 更新图片替换方法,确保正确处理带属性的图片引用 - 重构 openCategoryModal 方法,每次都获取最新的文件内容 - 在文件修改后更新 activeFile 对象,确保状态同步 --- src/main.ts | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main.ts b/src/main.ts index e2924b2..74941d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -58,7 +58,7 @@ export default class DiscourseSyncPlugin extends Plugin { } extractImageReferences(content: string): string[] { - const regex = /!\[\[(.*?)\]\]/g; + const regex = /!\[\[(.*?)(?:\|.*?)?\]\]/g; const matches = []; let match; while ((match = regex.exec(content)) !== null) { @@ -150,10 +150,12 @@ export default class DiscourseSyncPlugin extends Plugin { const imageReferences = this.extractImageReferences(content); const imageUrls = await this.uploadImages(imageReferences); + // 替换所有图片引用,包括带有属性的图片 imageReferences.forEach((ref, index) => { - const obsRef = `![[${ref}]]`; + // 使用正则表达式匹配包含属性的图片引用 + const regex = new RegExp(`!\\[\\[${ref.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\|.*?)?\\]\\]`, 'g'); const discoRef = `![${ref}](${imageUrls[index]})`; - content = content.replace(obsRef, discoRef); + content = content.replace(regex, discoRef); }); const isUpdate = postId !== undefined; @@ -342,6 +344,13 @@ export default class DiscourseSyncPlugin extends Plugin { } await this.app.vault.modify(activeFile, newContent); + + // 更新 activeFile 对象,确保它反映最新状态 + this.activeFile = { + name: activeFile.basename, + content: newContent, + postId: postId + }; } catch (error) { return { message: "Error", @@ -517,23 +526,21 @@ export default class DiscourseSyncPlugin extends Plugin { } private async openCategoryModal() { - // 确保activeFile已设置 - if (!this.activeFile) { - const activeFile = this.app.workspace.getActiveFile(); - if (!activeFile) { - new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open(); - 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 - }; + // 每次都重新获取 activeFile 的最新内容,不使用缓存 + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open(); + 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 [categories, tags] = await Promise.all([ this.fetchCategories(), this.fetchTags()