This commit is contained in:
Devon22 2025-04-07 00:25:34 +08:00
parent d0df4fe2d6
commit d5ff6ed55b
6 changed files with 67 additions and 7 deletions

View file

@ -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",

4
package-lock.json generated
View file

@ -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",

View file

@ -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": {

View file

@ -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);
});

View file

@ -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': 'ギャラリーサイズ',

View file

@ -1,3 +1,3 @@
{
"1.9.1": "1.1.0"
"1.9.2": "1.1.0"
}