add: export with image

This commit is contained in:
xiangyu 2022-05-09 17:50:14 +08:00
parent ac350862ce
commit 80266bb3fd
6 changed files with 67 additions and 1 deletions

View File

@ -19,6 +19,11 @@
<checkbox id="__addonRef__-export-enablefile" checked="true" />
<label value="&zotero.__addonRef__.export.file.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-embedImage" checked="true" />
<label value="&zotero.__addonRef__.export.image.enable.label;" />
</row>
<separator></separator>
<row>
<checkbox id="__addonRef__-export-enablenote" checked="false" />
<label value="&zotero.__addonRef__.export.note.enable.label;" />

View File

@ -3,6 +3,7 @@
<!ENTITY zotero.__addonRef__.export.title "Export Knowledge">
<!ENTITY zotero.__addonRef__.export.file.enable.label "Export to file(MarkDown/HTML/RDF)">
<!ENTITY zotero.__addonRef__.export.image.enable.label "Embed Images(MarkDown file only)">
<!ENTITY zotero.__addonRef__.export.note.enable.label "Export to new note">
<!ENTITY zotero.__addonRef__.export.copy.enable.label "Export to clipboard">

View File

@ -3,6 +3,7 @@
<!ENTITY zotero.__addonRef__.export.title "导出笔记">
<!ENTITY zotero.__addonRef__.export.file.enable.label "导出为文件(MarkDown/HTML/RDF)">
<!ENTITY zotero.__addonRef__.export.image.enable.label "嵌入图片(只在MarkDown文件导出时勾选)">
<!ENTITY zotero.__addonRef__.export.note.enable.label "导出到新笔记">
<!ENTITY zotero.__addonRef__.export.copy.enable.label "导出到剪贴板">

View File

@ -564,6 +564,7 @@ class AddonEvents extends AddonBase {
await this._Addon.knowledge.exportNoteToFile(
message.content.editorInstance._item,
true,
options.embedImage,
options.exportFile,
options.exportNote,
options.exportCopy

View File

@ -23,6 +23,14 @@ class AddonExport extends AddonBase {
) as XUL.Checkbox
).checked = exportFile;
}
let embedImage = Zotero.Prefs.get("Knowledge4Zotero.embedImage");
if (typeof embedImage !== "undefined") {
(
this._window.document.getElementById(
"Knowledge4Zotero-export-embedImage"
) as XUL.Checkbox
).checked = embedImage;
}
let exportNote = Zotero.Prefs.get("Knowledge4Zotero.exportNote");
if (typeof exportNote !== "undefined") {
(
@ -50,6 +58,11 @@ class AddonExport extends AddonBase {
"Knowledge4Zotero-export-enablefile"
) as XUL.Checkbox
).checked;
let embedImage = (
this._window.document.getElementById(
"Knowledge4Zotero-export-embedImage"
) as XUL.Checkbox
).checked;
let exportNote = (
this._window.document.getElementById(
"Knowledge4Zotero-export-enablenote"
@ -61,12 +74,14 @@ class AddonExport extends AddonBase {
) as XUL.Checkbox
).checked;
Zotero.Prefs.set("Knowledge4Zotero.exportFile", exportFile);
Zotero.Prefs.set("Knowledge4Zotero.embedImage", embedImage);
Zotero.Prefs.set("Knowledge4Zotero.exportNote", exportNote);
Zotero.Prefs.set("Knowledge4Zotero.exportCopy", exportCopy);
Zotero.debug(this.io);
Zotero.debug(this.io.dataOut);
this.io.dataOut = {
exportFile: exportFile,
embedImage: embedImage,
exportNote: exportNote,
exportCopy: exportCopy,
};

View File

@ -572,18 +572,61 @@ class Knowledge extends AddonBase {
async exportNoteToFile(
note: ZoteroItem,
convertNoteLinks: boolean = true,
convertNoteImages: boolean = true,
saveFile: boolean = true,
saveNote: boolean = false,
saveCopy: boolean = false
) {
if (convertNoteLinks) {
let item: ZoteroItem = new Zotero.Item("note");
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("<img");
const imageBrReg = new RegExp("<br>");
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
);
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(
note.libraryID,
attachmentKey
);
const attachmentURL = await attachmentItem.getFilePathAsync();
if (attachmentURL) {
// const imageData = await editorInstance._getDataURL(
// attachmentItem
// );
// TODO: deal with Zotero parse
newLines.push(`<p>![](${attachmentURL})</p>`);
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);