Skip to content

Commit cbd2760

Browse files
author
xiangyu
committed
feat: export origin note
1 parent e1ef674 commit cbd2760

File tree

6 files changed

+82
-63
lines changed

6 files changed

+82
-63
lines changed

addon/chrome/content/export.xul

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<checkbox id="__addonRef__-export-enablefile" checked="true" />
2020
<label value="&zotero.__addonRef__.export.file.enable.label;" />
2121
</row>
22+
<row>
23+
<checkbox id="__addonRef__-export-embedLink" checked="true" />
24+
<label value="&zotero.__addonRef__.export.link.enable.label;" />
25+
</row>
2226
<row>
2327
<checkbox id="__addonRef__-export-embedImage" checked="true" />
2428
<label value="&zotero.__addonRef__.export.image.enable.label;" />

addon/chrome/locale/en-US/overlay.dtd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<!ENTITY zotero.__addonRef__.export.title "Export">
1717
<!ENTITY zotero.__addonRef__.export.file.enable.label "Export to file(MarkDown/HTML/RDF)">
18+
<!ENTITY zotero.__addonRef__.export.link.enable.label "Embed Linked Notes">
1819
<!ENTITY zotero.__addonRef__.export.image.enable.label "Embed Images(MarkDown file only)">
1920
<!ENTITY zotero.__addonRef__.export.note.enable.label "Export to new note">
2021
<!ENTITY zotero.__addonRef__.export.copy.enable.label "Export to clipboard">

addon/chrome/locale/zh-CN/overlay.dtd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<!ENTITY zotero.__addonRef__.export.title "导出笔记">
1717
<!ENTITY zotero.__addonRef__.export.file.enable.label "导出为文件(MarkDown/HTML/RDF)">
18+
<!ENTITY zotero.__addonRef__.export.link.enable.label "嵌入链接的子笔记">
1819
<!ENTITY zotero.__addonRef__.export.image.enable.label "嵌入图片(只在MarkDown文件导出时勾选)">
1920
<!ENTITY zotero.__addonRef__.export.note.enable.label "导出到新笔记">
2021
<!ENTITY zotero.__addonRef__.export.copy.enable.label "导出到剪贴板">

src/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ class AddonEvents extends AddonBase {
690690
const options = io.dataOut;
691691
await this._Addon.knowledge.exportNoteToFile(
692692
message.content.editorInstance._item,
693-
true,
693+
options.embedLink,
694694
options.embedImage,
695695
options.exportFile,
696696
options.exportNote,

src/export.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class AddonExport extends AddonBase {
2323
) as XUL.Checkbox
2424
).checked = exportFile;
2525
}
26+
let embedLink = Zotero.Prefs.get("Knowledge4Zotero.embedLink");
27+
if (typeof embedLink !== "undefined") {
28+
(
29+
this._window.document.getElementById(
30+
"Knowledge4Zotero-export-embedLink"
31+
) as XUL.Checkbox
32+
).checked = embedLink;
33+
}
2634
let embedImage = Zotero.Prefs.get("Knowledge4Zotero.embedImage");
2735
if (typeof embedImage !== "undefined") {
2836
(
@@ -58,6 +66,11 @@ class AddonExport extends AddonBase {
5866
"Knowledge4Zotero-export-enablefile"
5967
) as XUL.Checkbox
6068
).checked;
69+
let embedLink = (
70+
this._window.document.getElementById(
71+
"Knowledge4Zotero-export-embedLink"
72+
) as XUL.Checkbox
73+
).checked;
6174
let embedImage = (
6275
this._window.document.getElementById(
6376
"Knowledge4Zotero-export-embedImage"
@@ -74,13 +87,15 @@ class AddonExport extends AddonBase {
7487
) as XUL.Checkbox
7588
).checked;
7689
Zotero.Prefs.set("Knowledge4Zotero.exportFile", exportFile);
90+
Zotero.Prefs.set("Knowledge4Zotero.embedLink", embedLink);
7791
Zotero.Prefs.set("Knowledge4Zotero.embedImage", embedImage);
7892
Zotero.Prefs.set("Knowledge4Zotero.exportNote", exportNote);
7993
Zotero.Prefs.set("Knowledge4Zotero.exportCopy", exportCopy);
8094
Zotero.debug(this.io);
8195
Zotero.debug(this.io.dataOut);
8296
this.io.dataOut = {
8397
exportFile: exportFile,
98+
embedLink: embedLink,
8499
embedImage: embedImage,
85100
exportNote: exportNote,
86101
exportCopy: exportCopy,

src/knowledge.ts

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -620,45 +620,40 @@ class Knowledge extends AddonBase {
620620
saveNote: boolean = false,
621621
saveCopy: boolean = false
622622
) {
623+
if (!saveFile && !saveNote && !saveCopy) {
624+
return;
625+
}
623626
note = note || this.getWorkspaceNote();
624-
if (convertNoteLinks) {
625-
const noteID = await ZoteroPane_Local.newNote();
626-
const item = Zotero.Items.get(noteID);
627-
const rootNoteIds = [note.id];
628-
629-
const newLines = await this.convertNoteLines(
630-
note,
631-
rootNoteIds,
632-
convertNoteLinks,
633-
convertNoteImages
634-
);
627+
const noteID = await ZoteroPane_Local.newNote();
628+
const item = Zotero.Items.get(noteID);
629+
const rootNoteIds = [note.id];
635630

636-
this.setLinesToNote(item, newLines);
637-
if (saveFile) {
638-
const exporter = new Zotero_File_Exporter();
639-
exporter.items = [item];
640-
await exporter.save();
641-
}
631+
const newLines = await this.convertNoteLines(
632+
note,
633+
rootNoteIds,
634+
convertNoteLinks,
635+
convertNoteImages
636+
);
637+
638+
this.setLinesToNote(item, newLines);
639+
if (saveFile) {
640+
const exporter = new Zotero_File_Exporter();
641+
exporter.items = [item];
642+
await exporter.save();
643+
}
644+
if (saveCopy) {
645+
Zotero_File_Interface.exportItemsToClipboard(
646+
[item],
647+
Zotero.Translators.TRANSLATOR_ID_MARKDOWN_AND_RICH_TEXT
648+
);
649+
this._Addon.views.showProgressWindow("Better Notes", "Note Copied");
650+
}
651+
if (!saveNote) {
642652
if (saveCopy) {
643-
Zotero_File_Interface.exportItemsToClipboard(
644-
[item],
645-
Zotero.Translators.TRANSLATOR_ID_MARKDOWN_AND_RICH_TEXT
646-
);
647-
this._Addon.views.showProgressWindow("Better Notes", "Note Copied");
648-
}
649-
if (!saveNote) {
650-
if (saveCopy) {
651-
// Wait copy finish
652-
await Zotero.Promise.delay(500);
653-
}
654-
await Zotero.Items.erase(item.id);
655-
}
656-
} else {
657-
if (saveFile) {
658-
const exporter = new Zotero_File_Exporter();
659-
exporter.items = [note];
660-
exporter.save();
653+
// Wait copy finish
654+
await Zotero.Promise.delay(500);
661655
}
656+
await Zotero.Items.erase(item.id);
662657
}
663658
}
664659

@@ -670,9 +665,7 @@ class Knowledge extends AddonBase {
670665
if (imageIndex !== -1) {
671666
const lineStart = line.slice(0, imageIndex);
672667
const imageLine = line.slice(imageIndex);
673-
const lineEnd = line.slice(
674-
imageLine.search(imageBrReg) + imageBrReg.source.length + 3
675-
);
668+
const lineEnd = imageLine.slice(imageLine.search(imageBrReg));
676669
const attachmentKeyIndex = imageLine.search(imageKeyReg);
677670

678671
if (attachmentKeyIndex !== -1) {
@@ -690,6 +683,9 @@ class Knowledge extends AddonBase {
690683
// const imageData = await editorInstance._getDataURL(
691684
// attachmentItem
692685
// );
686+
Zotero.debug(line);
687+
Zotero.debug(lineStart);
688+
Zotero.debug(lineEnd);
693689
newLines.push(`<p>!<a href="${attachmentURL}">image</a></p>`);
694690
newLines.push(`${lineStart}${lineEnd}`);
695691
return true;
@@ -725,32 +721,34 @@ class Knowledge extends AddonBase {
725721
}
726722
newLines.push(noteLines[i]);
727723
// Convert Link
728-
let link = this.getLinkFromText(noteLines[i]);
729-
while (link) {
730-
Zotero.debug("convert link");
731-
let res = await this.getNoteFromLink(link);
732-
const subNote = res.item;
733-
if (subNote && _rootNoteIds.indexOf(subNote.id) === -1) {
734-
Zotero.debug(`Knowledge4Zotero: Exporting sub-note ${link}`);
735-
newLines.push("<blockquote>");
736-
newLines.push(`<p><strong>Linked Note:</strong></p>`);
737-
newLines = newLines.concat(
738-
await this.convertNoteLines(
739-
subNote,
740-
_rootNoteIds,
741-
convertNoteLinks,
742-
convertNoteImages
743-
)
724+
if (convertNoteLinks) {
725+
let link = this.getLinkFromText(noteLines[i]);
726+
while (link) {
727+
Zotero.debug("convert link");
728+
let res = await this.getNoteFromLink(link);
729+
const subNote = res.item;
730+
if (subNote && _rootNoteIds.indexOf(subNote.id) === -1) {
731+
Zotero.debug(`Knowledge4Zotero: Exporting sub-note ${link}`);
732+
newLines.push("<blockquote>");
733+
newLines.push(`<p><strong>Linked Note:</strong></p>`);
734+
newLines = newLines.concat(
735+
await this.convertNoteLines(
736+
subNote,
737+
_rootNoteIds,
738+
convertNoteLinks,
739+
convertNoteImages
740+
)
741+
);
742+
newLines.push("</blockquote>");
743+
}
744+
noteLines[i] = noteLines[i].substring(
745+
noteLines[i].search(/zotero:\/\/note\//g)
746+
);
747+
noteLines[i] = noteLines[i].substring(
748+
noteLines[i].search(/<\/a>/g) + "</a>".length
744749
);
745-
newLines.push("</blockquote>");
750+
link = this.getLinkFromText(noteLines[i]);
746751
}
747-
noteLines[i] = noteLines[i].substring(
748-
noteLines[i].search(/zotero:\/\/note\//g)
749-
);
750-
noteLines[i] = noteLines[i].substring(
751-
noteLines[i].search(/<\/a>/g) + "</a>".length
752-
);
753-
link = this.getLinkFromText(noteLines[i]);
754752
}
755753
}
756754
return newLines;

0 commit comments

Comments
 (0)