Compare commits

...

2 Commits
main ... 1.0.13

Author SHA1 Message Date
6b81570366 1.0.13 2025-03-07 15:49:55 +08:00
094784f28e 优化图片引用处理和 activeFile 管理逻辑
- 改进图片引用正则表达式,支持带属性的图片链接
- 更新图片替换方法,确保正确处理带属性的图片引用
- 重构 openCategoryModal 方法,每次都获取最新的文件内容
- 在文件修改后更新 activeFile 对象,确保状态同步
2025-03-07 15:49:43 +08:00
5 changed files with 31 additions and 23 deletions

View File

@ -1,7 +1,7 @@
{
"id": "publish-to-discourse",
"name": "Publish to Discourse",
"version": "1.0.12",
"version": "1.0.13",
"minAppVersion": "0.15.0",
"description": "Publish notes to the Discourse forum.",
"author": "WoodChen",

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "obsidian-publish-to-discourse",
"version": "1.0.12",
"version": "1.0.13",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "obsidian-publish-to-discourse",
"version": "1.0.12",
"version": "1.0.13",
"license": "MIT",
"dependencies": {
"yaml": "^2.7.0"

View File

@ -1,6 +1,6 @@
{
"name": "obsidian-publish-to-discourse",
"version": "1.0.12",
"version": "1.0.13",
"description": "Post notes to the Discourse forum.",
"main": "main.js",
"scripts": {

View File

@ -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()

View File

@ -11,5 +11,6 @@
"1.0.9": "0.15.0",
"1.0.10": "0.15.0",
"1.0.11": "0.15.0",
"1.0.12": "0.15.0"
"1.0.12": "0.15.0",
"1.0.13": "0.15.0"
}