From 2ef1489a4bd2727e399f324df6eed11d29f0d3cb Mon Sep 17 00:00:00 2001 From: footmanff Date: Fri, 28 Nov 2025 16:29:09 +0800 Subject: [PATCH] fix: support copying images with size parameters (e.g. ![[image.png|100]]) --- main.ts | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/main.ts b/main.ts index bb8c6e9..547da6d 100644 --- a/main.ts +++ b/main.ts @@ -124,8 +124,16 @@ export default class CopyImageTextPlugin extends Plugin { for (const match of content.matchAll(imageRegex)) { const imagePath = match[1]; + + let actualImagePath = imagePath; + if (imagePath.includes('|')) { + actualImagePath = imagePath.split('|')[0]; + } + + const fileName = actualImagePath.split('/').pop() || actualImagePath; + const imageFile = this.app.vault.getFiles().find(f => - f.name.toLowerCase().includes(imagePath.split('/').pop()?.toLowerCase() || '') + f.name.toLowerCase().includes(fileName.toLowerCase()) ); if (imageFile) { @@ -249,7 +257,26 @@ export default class CopyImageTextPlugin extends Plugin { async replaceImageWithBase64(imagePath: string, file: TFile): Promise<{ original: string, replacement: string }> { try { - const fileName = imagePath.split('/').pop() || imagePath; + let actualImagePath = imagePath; + let width = ''; + let height = ''; + + // 处理图片尺寸参数 |width 或 |widthxheight + if (imagePath.includes('|')) { + const parts = imagePath.split('|'); + actualImagePath = parts[0]; + const param = parts[parts.length - 1]; // 取最后一个部分作为参数 + + if (/^\d+$/.test(param)) { + width = param; + } else if (/^\d+x\d+$/.test(param)) { + const [w, h] = param.split('x'); + width = w; + height = h; + } + } + + const fileName = actualImagePath.split('/').pop() || actualImagePath; const imageFile = this.app.vault.getFiles().find(f => f.name.toLowerCase().includes(fileName.toLowerCase()) ); @@ -265,11 +292,15 @@ export default class CopyImageTextPlugin extends Plugin { const imageArrayBuffer = await this.app.vault.readBinary(imageFile); const base64 = arrayBufferToBase64(imageArrayBuffer); - const mimeType = this.getMimeType(imagePath); + const mimeType = this.getMimeType(actualImagePath); + + let style = 'max-width: 100%;'; + if (width) style += ` width: ${width}px;`; + if (height) style += ` height: ${height}px;`; return { original: `![[${imagePath}]]`, - replacement: `${imagePath}` + replacement: `${actualImagePath}` }; } catch (error) { return { original: `![[${imagePath}]]`, replacement: `[图片处理错误: ${imagePath}]` };