Skip to content

Commit abfbf26

Browse files
authored
fix: images with Metadata (#130)
* Fix Bug * Fix * Add escaped metadata for use in tables * Metadata is now linked to GFM * Disable Metdata if convertWikiLinksToMarkdown is true.
1 parent 60ed673 commit abfbf26

2 files changed

Lines changed: 50 additions & 15 deletions

File tree

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const ATTACHMENT_URL_REGEXP =
2-
/!\[\[((.*?)\.(\w+))(?:\s*\|\s*(?<width>\d+%?)\s*(?:[*|x]\s*(?<height>\d+%?))?)?\]\]/g;
2+
/!\[\[((.*?)\.(\w+))(?:\s*(?<metadata>(?:\|(?<width>\d+%?)(?:[*|x](?<height>\d+%?))?|\\\|(?<escapedWidth>\d+%?)(?:[*|x](?<escapedHeight>\d+%?))?|\|[^\]]*|\\\|[^\]]*|#[^\]]*)))?\]\]/g;
33

44
export const MARKDOWN_ATTACHMENT_URL_REGEXP = /!\[(.*?)\]\(((.*?)\.(\w+))\)/g;
55

src/utils.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,30 @@ type CopyMarkdownOptions = {
2222
outputSubPath: string;
2323
};
2424

25+
type ImageLink = {
26+
rawImageLink: string;
27+
imageLink: string;
28+
metadata: string;
29+
width?: string;
30+
height?: string;
31+
};
32+
2533
export async function getImageLinks(markdown: string) {
2634
const imageLinks = markdown.matchAll(ATTACHMENT_URL_REGEXP);
2735
const markdownImageLinks = markdown.matchAll(
2836
MARKDOWN_ATTACHMENT_URL_REGEXP
2937
);
30-
return Array.from(imageLinks).concat(Array.from(markdownImageLinks));
38+
return Array.from(imageLinks)
39+
.concat(Array.from(markdownImageLinks))
40+
.map((match): ImageLink => ({
41+
rawImageLink: match[0],
42+
imageLink: match[0].startsWith("![[") ? match[1] : match[2],
43+
metadata: match[0].startsWith("![[")
44+
? match.groups?.metadata || ""
45+
: "",
46+
width: match.groups?.width || match.groups?.escapedWidth,
47+
height: match.groups?.height || match.groups?.escapedHeight,
48+
}));
3149
}
3250

3351
export async function getEmbeds(markdown: string) {
@@ -340,8 +358,7 @@ export async function tryCopyImage(
340358
const fileNameWithoutExt = filename.replace(/\.[^/.]+$/, "");
341359

342360
for (const index in imageLinks) {
343-
const urlEncodedImageLink =
344-
imageLinks[index][7 - imageLinks[index].length];
361+
const urlEncodedImageLink = imageLinks[index].imageLink;
345362

346363
// decode and replace the relative path
347364
let imageLink = "";
@@ -361,10 +378,15 @@ export async function tryCopyImage(
361378
imageLink,
362379
contentPath
363380
);
381+
const imageFile =
382+
ifile ||
383+
plugin.app.vault.getAbstractFileByPath(
384+
path.join(path.dirname(contentPath), imageLink)
385+
);
364386

365387
const filePath =
366-
ifile !== null
367-
? ifile.path
388+
imageFile !== null
389+
? imageFile.path
368390
: path.join(path.dirname(contentPath), imageLink);
369391

370392
// filter markdown link eg: http://xxx.png
@@ -408,7 +430,7 @@ export async function tryCopyImage(
408430
) {
409431
const resourceOsPath = getResourceOsPath(
410432
plugin,
411-
ifile
433+
imageFile instanceof TFile ? imageFile : null
412434
);
413435
fs.copyFileSync(resourceOsPath, targetPath);
414436
} else {
@@ -851,10 +873,11 @@ export async function tryCopyMarkdownByRead(
851873
}
852874

853875
for (const index in imageLinks) {
854-
const rawImageLink = imageLinks[index][0];
855-
856-
const urlEncodedImageLink =
857-
imageLinks[index][7 - imageLinks[index].length];
876+
const rawImageLink = imageLinks[index].rawImageLink;
877+
const urlEncodedImageLink = imageLinks[index].imageLink;
878+
const imageMetadata = plugin.settings.GFM
879+
? ""
880+
: imageLinks[index].metadata;
858881

859882
// decode and replace the relative path
860883
let imageLink = "";
@@ -899,8 +922,7 @@ export async function tryCopyMarkdownByRead(
899922
}
900923

901924
if (plugin.settings.displayImageAsHtml) {
902-
const { width = null, height = null } =
903-
imageLinks[index]?.groups || {};
925+
const { width = null, height = null } = imageLinks[index];
904926
// Helper to format size value - append 'px' only if not a percentage
905927
const formatSize = (value: string | null) => {
906928
if (!value) return "";
@@ -919,13 +941,26 @@ export async function tryCopyMarkdownByRead(
919941
rawImageLink,
920942
`<img src="${hashLink}"${style} />`
921943
);
922-
} else if (plugin.settings.GFM) {
944+
} else if (
945+
plugin.settings.convertWikiLinksToMarkdown &&
946+
rawImageLink.startsWith("![[")
947+
) {
923948
content = content.replace(
924949
rawImageLink,
925950
GFM_IMAGE_FORMAT.format(hashLink)
926951
);
952+
} else if (plugin.settings.GFM) {
953+
content = content.replace(
954+
rawImageLink,
955+
GFM_IMAGE_FORMAT.format(hashLink + imageMetadata)
956+
);
927957
} else {
928-
content = content.replace(urlEncodedImageLink, hashLink);
958+
content = content.replace(
959+
rawImageLink,
960+
rawImageLink.startsWith("![[")
961+
? `![[${hashLink}${imageMetadata}]]`
962+
: rawImageLink.replace(urlEncodedImageLink, hashLink)
963+
);
929964
}
930965
}
931966

0 commit comments

Comments
 (0)