Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
);
Expand All @@ -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: `<img src="data:${mimeType};base64,${base64}" alt="${imagePath}" style="max-width: 100%;">`
replacement: `<img src="data:${mimeType};base64,${base64}" alt="${actualImagePath}" style="${style}">`
};
} catch (error) {
return { original: `![[${imagePath}]]`, replacement: `[图片处理错误: ${imagePath}]` };
Expand Down