mirror of
https://github.com/woodchen-ink/obsidian-publish-to-discourse.git
synced 2025-07-18 05:42:05 +08:00
优化图片引用处理和 activeFile 管理逻辑
- 改进图片引用正则表达式,支持带属性的图片链接 - 更新图片替换方法,确保正确处理带属性的图片引用 - 重构 openCategoryModal 方法,每次都获取最新的文件内容 - 在文件修改后更新 activeFile 对象,确保状态同步
This commit is contained in:
parent
9152aa5d60
commit
094784f28e
19
src/main.ts
19
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 = ``;
|
||||
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,8 +526,7 @@ export default class DiscourseSyncPlugin extends Plugin {
|
||||
}
|
||||
|
||||
private async openCategoryModal() {
|
||||
// 确保activeFile已设置
|
||||
if (!this.activeFile) {
|
||||
// 每次都重新获取 activeFile 的最新内容,不使用缓存
|
||||
const activeFile = this.app.workspace.getActiveFile();
|
||||
if (!activeFile) {
|
||||
new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open();
|
||||
@ -532,7 +540,6 @@ export default class DiscourseSyncPlugin extends Plugin {
|
||||
content: content,
|
||||
postId: fm?.discourse_post_id
|
||||
};
|
||||
}
|
||||
|
||||
const [categories, tags] = await Promise.all([
|
||||
this.fetchCategories(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user