Skip to content

Commit

Permalink
fix: #763
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Nov 5, 2023
1 parent a8aae93 commit ae049aa
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/modules/convert/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async function md2note(
const _note = rehype2note(_rehype as HRoot);
const rehype = note2rehype(_note);

// Check if image already belongs to note
// Check if image citation already belongs to note
processM2NRehypeMetaImageNodes(getM2NRehypeImageNodes(rehype));

processM2NRehypeHighlightNodes(getM2NRehypeHighlightNodes(rehype));
Expand Down Expand Up @@ -1122,7 +1122,6 @@ function processM2NRehypeNoteLinkNodes(nodes: string | any[]) {
}

async function processM2NRehypeImageNodes(
this: any,
nodes: any[],
noteItem: Zotero.Item,
fileDir: string,
Expand All @@ -1132,26 +1131,36 @@ async function processM2NRehypeImageNodes(
return;
}

let attKeys = [] as string[];
if (isImport) {
attKeys = Zotero.Items.get(noteItem.getAttachments()).map(
(item) => item.key,
);
}

for (const node of nodes) {
if (isImport) {
// We encode the src in md2remark and decode it here.
let src = formatPath(decodeURIComponent(node.properties.src));
const srcType = (src as string).startsWith("data:")
? "b64"
: (src as string).startsWith("http")
? "url"
: "file";
if (srcType === "file") {
if (!PathUtils.isAbsolute(src)) {
src = jointPath(fileDir, src);
}
if (!(await fileExists(src))) {
ztoolkit.log("parse image, path invalid", src);
continue;
// If image is already an attachment of note, skip import
if (!attKeys.includes(node.properties.dataAttachmentKey)) {
// We encode the src in md2remark and decode it here.
let src = formatPath(decodeURIComponent(node.properties.src));
const srcType = (src as string).startsWith("data:")
? "b64"
: (src as string).startsWith("http")
? "url"
: "file";
if (srcType === "file") {
if (!PathUtils.isAbsolute(src)) {
src = jointPath(fileDir, src);
}
if (!(await fileExists(src))) {
ztoolkit.log("parse image, path invalid", src);
continue;
}
}
const key = await importImageToNote(noteItem, src, srcType);
node.properties.dataAttachmentKey = key;
}
const key = await importImageToNote(noteItem, src, srcType);
node.properties.dataAttachmentKey = key;
}
delete node.properties.src;
node.properties.ztype && delete node.properties.ztype;
Expand Down

0 comments on commit ae049aa

Please sign in to comment.