Skip to content

Commit

Permalink
feat: image path check image dimensions (#56)
Browse files Browse the repository at this point in the history
* feat: image path check image dimensions
  • Loading branch information
bingryan authored Dec 28, 2023
1 parent 7f51926 commit 7d121cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const ATTACHMENT_URL_REGEXP = /!\[\[((.*?)\.(\w+))\]\]/g;
export const ATTACHMENT_URL_REGEXP =
/!\[\[((.*?)\.(\w+))(?:\s*\|\s*(\d+)\s*(?:\*\s*(\d+))?)?\]\]/g;

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

export const EMBED_URL_REGEXP = /!\[\[(.*?)\]\]/g;

export const GFM_IMAGE_FORMAT = "![]({0})";
Expand Down
28 changes: 22 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export async function getImageLinks(markdown: string) {
const markdownImageLinks = markdown.matchAll(
MARKDOWN_ATTACHMENT_URL_REGEXP
);
// console.log("getImageLinks: ", Array.from(imageLinks).concat(Array.from(markdownImageLinks)));
return Array.from(imageLinks).concat(Array.from(markdownImageLinks));
}

Expand Down Expand Up @@ -181,8 +180,16 @@ export async function tryCopyImage(
const imageLinks = await getImageLinks(content);
for (const index in imageLinks) {
const urlEncodedImageLink =
imageLinks[index][imageLinks[index].length - 3];
const imageLink = decodeURI(urlEncodedImageLink);
imageLinks[index][7 - imageLinks[index].length];

// decode and replace the relative path
let imageLink = decodeURI(urlEncodedImageLink).replace(
/\.\.\//g,
""
);
if (imageLink.contains("|")) {
imageLink = imageLink.split("|")[0];
}

const imageLinkMd5 = md5(imageLink);
const imageExt = path.extname(imageLink);
Expand Down Expand Up @@ -293,12 +300,21 @@ export async function tryCopyMarkdownByRead(
try {
await plugin.app.vault.adapter.read(file.path).then(async (content) => {
const imageLinks = await getImageLinks(content);

for (const index in imageLinks) {
const rawImageLink = imageLinks[index][0];
const urlEncodedImageLink =
imageLinks[index][imageLinks[index].length - 3];
const imageLink = decodeURI(urlEncodedImageLink);
imageLinks[index][7 - imageLinks[index].length];

// decode and replace the relative path
let imageLink = decodeURI(urlEncodedImageLink).replace(
/\.\.\//g,
""
);
// link: https://help.obsidian.md/Linking+notes+and+files/Embedding+files#Embed+an+image+in+a+note
// issue: #44 -> figure checkout: ![[name|figure]]
if (imageLink.contains("|")) {
imageLink = imageLink.split("|")[0];
}
const imageLinkMd5 = md5(imageLink);
const imageExt = path.extname(imageLink);
// Unify the link separator in obsidian as a forward slash instead of the default back slash in windows, so that the referenced images can be displayed properly
Expand Down

0 comments on commit 7d121cb

Please sign in to comment.