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(`
"); - 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("Linked Note: ${res.item.getNoteTitle()}
` - ); - let linkedLines = this.getLinesInNote(res.item); - newLines = newLines.concat(linkedLines); - newLines.push("
"); + 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("/");Linked Note: ${res.item.getNoteTitle()}
` + ); + newLines = newLines.concat( + await this.converNoteLines( + subNote, + _rootNoteIds, + convertNoteLinks, + convertNoteImages + ) + ); + newLines.push("