diff --git a/manifest.json b/manifest.json index 3974546..03a3ce8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "mediaviewer", "name": "Media Viewer", - "version": "1.9.1", + "version": "1.9.2", "minAppVersion": "1.1.0", "description": "View and manage media files within your notes.", "author": "Devon22", diff --git a/package-lock.json b/package-lock.json index 1d7ba2c..f06b9be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mediaviewer", - "version": "1.9.1", + "version": "1.9.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mediaviewer", - "version": "1.9.1", + "version": "1.9.2", "license": "MIT", "devDependencies": { "@types/node": "^16.11.6", diff --git a/package.json b/package.json index 609ce39..6cf5bd2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mediaviewer", - "version": "1.9.1", + "version": "1.9.2", "description": "This is a sample plugin for Obsidian (https://obsidian.md)", "main": "main.js", "scripts": { diff --git a/src/galleryBlock.ts b/src/galleryBlock.ts index c2156c6..c9318b9 100644 --- a/src/galleryBlock.ts +++ b/src/galleryBlock.ts @@ -109,7 +109,7 @@ export class GalleryBlock { } } - // 處理直接的 URL 或路徑 + // 處理直接的 URL 或路徑 (僅img參數) if (linkText.startsWith('http')) { return linkText; } else { @@ -126,8 +126,11 @@ export class GalleryBlock { const findFirstImageInNote = async (file: TFile) => { try { const content = await this.app.vault.cachedRead(file); - const internalMatch = content.match(/(?:!\[\[(.*?\.(?:jpg|jpeg|png|gif|webp))(?:\|.*?)?\]\]|!\[(.*?)\]\(\s*(\S+?(?:\.(?:jpg|jpeg|png|gif|webp)|format=(?:jpg|jpeg|png|gif|webp))[^\s)]*)\s*(?:\s+["'][^"']*["'])?\s*\))/i); + const internalMatch = content.match(/(?:!?\[\[(.*?\.(?:jpg|jpeg|png|gif|webp))(?:\|.*?)?\]\]|!\[(.*?)\]\(\s*(\S+?(?:\.(?:jpg|jpeg|png|gif|webp)|format=(?:jpg|jpeg|png|gif|webp))[^\s)]*)\s*(?:\s+["'][^"']*["'])?\s*\))/i); if (internalMatch) { + if (internalMatch[1]) { + return `![[${internalMatch[1]}]]`; + } return internalMatch[0]; } else { return null; @@ -680,6 +683,59 @@ export class GalleryBlock { }); }); }); + menu.addItem((item) => { + item + .setTitle(t('ungenerate_gallery')) + .setIcon("eraser") + .onClick(() => { + // 獲取 gallery 區塊的 ID + const galleryId = galleryDiv.getAttribute('data-gallery-id'); + if (!galleryId) { + return; + } + + // 獲取當前筆記的文件 + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + new Notice(t('please_open_note')); + return; + } + + // 讀取文件內容 + this.app.vault.read(activeFile).then((content) => { + // 尋找包含當前 gallery ID 的 gallery 區塊 + const galleryBlockRegex = /```gallery\n([\s\S]*?)```/g; + let match; + let matchPosition = { start: 0, end: 0 }; + + while ((match = galleryBlockRegex.exec(content)) !== null) { + const blockContent = match[1]; + const blockId = 'gallery-' + this.hashString(blockContent.trim()); + if (blockId === galleryId) { + matchPosition.start = match.index; + matchPosition.end = match.index + match[0].length; + break; + } + } + + if (!matchPosition.start) { + new Notice(t('gallery_not_found')); + return; + } + + // 移除 gallery 標記,保留內容 + const newContent = content.substring(matchPosition.start + '```gallery\n'.length, matchPosition.end - '```'.length); + + // 使用 vault.process 修改文件 + this.app.vault.process(activeFile, (fileContent) => { + return fileContent.substring(0, matchPosition.start) + + newContent + + fileContent.substring(matchPosition.end); + }); + }); + }); + }); + menu.showAtMouseEvent(event); }); diff --git a/src/translations.ts b/src/translations.ts index 791cdf7..e855159 100644 --- a/src/translations.ts +++ b/src/translations.ts @@ -78,6 +78,7 @@ const TRANSLATIONS: Translations = { 'open_media_viewer': '打開媒體瀏覽器', 'generate_gallery': '生成 Gallery 區塊', 'setting_gallery': '設定 Gallery 區塊', + 'ungenerate_gallery': '取消 Gallery 區塊', 'gallery_title': 'Gallery 標題', 'gallery_title_desc': '設定 Gallery 的標題(選填)', 'gallery_size': 'Gallery 尺寸', @@ -143,6 +144,7 @@ const TRANSLATIONS: Translations = { 'open_media_viewer': 'Open media viewer', 'generate_gallery': 'Generate gallery block', 'setting_gallery': 'Setting gallery block', + 'ungenerate_gallery': 'Cancel gallery block', 'gallery_title': 'Gallery title', 'gallery_title_desc': 'Set the title of the gallery (optional)', 'gallery_size': 'Gallery size', @@ -208,6 +210,7 @@ const TRANSLATIONS: Translations = { 'open_media_viewer': '打开媒体浏览器', 'generate_gallery': '生成 Gallery 区块', 'setting_gallery': '设置 Gallery 区块', + 'ungenerate_gallery': '取消 Gallery 区块', 'gallery_title': '相册标题', 'gallery_title_desc': '设置相册的标题(选填)', 'gallery_size': '相册尺寸', @@ -273,6 +276,7 @@ const TRANSLATIONS: Translations = { 'open_media_viewer': 'メディアビューワーを開く', 'generate_gallery': 'ギャラリーブロックを生成', 'setting_gallery': 'ギャラリーブロックを設定', + 'ungenerate_gallery': 'ギャラリーブロックを削除', 'gallery_title': 'ギャラリータイトル', 'gallery_title_desc': 'ギャラリーのタイトルを設定(オプション)', 'gallery_size': 'ギャラリーサイズ', diff --git a/versions.json b/versions.json index 86c0243..67c1c4b 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.9.1": "1.1.0" + "1.9.2": "1.1.0" } \ No newline at end of file