From 44843ceeda505b29060bf768f8642c23af9283bb Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Tue, 10 May 2022 11:55:26 +0800 Subject: [PATCH] fix: export md with image bug --- src/knowledge.ts | 175 ++++++++++++++++++++++++++++------------------- 1 file changed, 106 insertions(+), 69 deletions(-) diff --git a/src/knowledge.ts b/src/knowledge.ts index cb9f1c6..fc3d274 100644 --- a/src/knowledge.ts +++ b/src/knowledge.ts @@ -580,76 +580,15 @@ class Knowledge extends AddonBase { if (convertNoteLinks) { const noteID = await ZoteroPane_Local.newNote(); const item = Zotero.Items.get(noteID); - item.setNote(note.getNote()); - item.saveTx(); - let noteLines = this.getLinesInNote(note); - let newLines = []; - const imageReg = new RegExp(""); - const imageKeyReg = new RegExp(`data-attachment-key="`); - const editorInstance = new Zotero.EditorInstance(); - for (let i in noteLines) { - // Embed Image - if (convertNoteImages) { - const imageIndex = noteLines[i].search(imageReg); - if (imageIndex !== -1) { - const lineStart = noteLines[i].slice(0, imageIndex); - const imageLine = noteLines[i].slice(imageIndex); - const lineEnd = noteLines[i].slice( - imageLine.search(imageBrReg) + imageBrReg.source.length + 3 - ); - const attachmentKeyIndex = imageLine.search(imageKeyReg); + const rootNoteIds = [note.id]; + + const newLines = await this.converNoteLines( + note, + rootNoteIds, + convertNoteLinks, + convertNoteImages + ); - if (attachmentKeyIndex !== -1) { - let attachmentKey = imageLine.slice( - attachmentKeyIndex + imageKeyReg.source.length - ); - attachmentKey = attachmentKey.slice( - 0, - attachmentKey.search(/"/g) - ); - const attachmentItem = await Zotero.Items.getByLibraryAndKeyAsync( - note.libraryID, - attachmentKey - ); - const attachmentURL = await attachmentItem.getFilePathAsync(); - if (attachmentURL) { - // const imageData = await editorInstance._getDataURL( - // attachmentItem - // ); - newLines.push(`

!image

`); - newLines.push(`${lineStart}${lineEnd}`); - continue; - } - } - } - } - newLines.push(noteLines[i]); - // Convert Link - let linkIndex = noteLines[i].search(/zotero:\/\/note\//g); - while (linkIndex >= 0) { - let link = noteLines[i].substring(linkIndex); - link = link.substring(0, link.search('"')); - let res = await this.getNoteFromLink(link); - if (res.item && res.item.id !== note.id) { - Zotero.debug(`Knowledge4Zotero: Exporting sub-note ${link}`); - newLines.push("
"); - newLines.push( - `

Linked Note: ${res.item.getNoteTitle()}

` - ); - let linkedLines = this.getLinesInNote(res.item); - newLines = newLines.concat(linkedLines); - newLines.push("
"); - } - noteLines[i] = noteLines[i].substring( - noteLines[i].search(/zotero:\/\/note\//g) - ); - noteLines[i] = noteLines[i].substring( - noteLines[i].search(/<\/a>/g) + "".length - ); - linkIndex = noteLines[i].search(/zotero:\/\/note\//g); - } - } this.setLinesToNote(item, newLines); if (saveFile) { const exporter = new Zotero_File_Exporter(); @@ -679,6 +618,104 @@ class Knowledge extends AddonBase { } } + async convertImage(line: string, newLines: string[], sourceNote: ZoteroItem) { + const imageReg = new RegExp(""); + const imageKeyReg = new RegExp(`data-attachment-key="`); + const imageIndex = line.search(imageReg); + if (imageIndex !== -1) { + const lineStart = line.slice(0, imageIndex); + const imageLine = line.slice(imageIndex); + const lineEnd = line.slice( + imageLine.search(imageBrReg) + imageBrReg.source.length + 3 + ); + const attachmentKeyIndex = imageLine.search(imageKeyReg); + + if (attachmentKeyIndex !== -1) { + let attachmentKey = imageLine.slice( + attachmentKeyIndex + imageKeyReg.source.length + ); + attachmentKey = attachmentKey.slice(0, attachmentKey.search(/"/g)); + const attachmentItem = await Zotero.Items.getByLibraryAndKeyAsync( + sourceNote.libraryID, + attachmentKey + ); + const attachmentURL = await attachmentItem.getFilePathAsync(); + if (attachmentURL) { + Zotero.debug("convert image"); + // const imageData = await editorInstance._getDataURL( + // attachmentItem + // ); + newLines.push(`

!image

`); + newLines.push(`${lineStart}${lineEnd}`); + return true; + } + } + } + return false; + } + + async converNoteLines( + currentNote: ZoteroItem, + rootNoteIds: number[], + convertNoteLinks: boolean = true, + convertNoteImages: boolean = true + ) { + Zotero.debug(`convert note ${currentNote.id}`); + + const [..._rootNoteIds] = rootNoteIds; + _rootNoteIds.push(currentNote.id); + let newLines = []; + const noteLines = this.getLinesInNote(currentNote); + for (let i in noteLines) { + // Embed Image + if (convertNoteImages) { + const hasImage = await this.convertImage( + noteLines[i], + newLines, + currentNote + ); + if (hasImage) { + continue; + } + } + newLines.push(noteLines[i]); + // Convert Link + let linkIndex = noteLines[i].search(/zotero:\/\/note\//g); + while (linkIndex >= 0) { + Zotero.debug("convert link"); + let link = noteLines[i].substring(linkIndex); + link = link.substring(0, link.search('"')); + let res = await this.getNoteFromLink(link); + const subNote = res.item; + if (subNote && _rootNoteIds.indexOf(subNote.id) === -1) { + Zotero.debug(`Knowledge4Zotero: Exporting sub-note ${link}`); + newLines.push("
"); + newLines.push( + `

Linked Note: ${res.item.getNoteTitle()}

` + ); + newLines = newLines.concat( + await this.converNoteLines( + subNote, + _rootNoteIds, + convertNoteLinks, + convertNoteImages + ) + ); + newLines.push("
"); + } + noteLines[i] = noteLines[i].substring( + noteLines[i].search(/zotero:\/\/note\//g) + ); + noteLines[i] = noteLines[i].substring( + noteLines[i].search(/<\/a>/g) + "".length + ); + linkIndex = noteLines[i].search(/zotero:\/\/note\//g); + } + } + return newLines; + } + async getNoteFromLink(uri: string) { let [groupID, noteKey] = uri.substring("zotero://note/".length).split("/");