/
mdnotes.js
1135 lines (954 loc) · 30.8 KB
/
mdnotes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*globals Zotero, OS, require, Components, window */
const mdnotesTemplate = `{{title}}
![[%(metadataFileName)#Metadata]]
Other files:
{{mdnotesFileName}}
{{metadataFileName}}
## Zotero links
{{localLibrary}}
{{cloudLibrary}}
## Notes
- `;
const standaloneTemplate = `Related to: [[%(metadataFileName)]]
## Notes
- `;
const zoteroNoteTemplate = `{{tags}}
{{related}}
{{mdnotesFileName}}
{{title}}
{{noteContent}}`;
const zoteroMetadataTemplate = `{{title}}
## Metadata
{{itemType}}
{{author}}
{{proceedingsTitle}}
{{date}}
{{dateAdded}}
{{url}}
{{DOI}}
{{citekey}}
{{collections}}
{{related}}
{{tags}}, #zotero, #literature-notes, #reference
{{pdfAttachments}}
{{abstractNote}}
## Zotero links
{{localLibrary}}
{{cloudLibrary}}
{{notes}}`;
function getPref(pref_name) {
return Zotero.Prefs.get(`extensions.mdnotes.${pref_name}`, true);
}
const typemap = {
artwork: "Illustration",
audioRecording: "Recording",
bill: "Legislation",
blogPost: "Blog post",
book: "Book",
bookSection: "Chapter",
case: "Legal case",
computerProgram: "Data",
conferencePaper: "Conference paper",
email: "Letter",
encyclopediaArticle: "Encyclopaedia article",
film: "Film",
forumPost: "Forum post",
hearing: "Hearing",
instantMessage: "Instant message",
interview: "Interview",
journalArticle: "Article",
letter: "Letter",
magazineArticle: "Magazine article",
manuscript: "Manuscript",
map: "Image",
newspaperArticle: "Newspaper article",
patent: "Patent",
podcast: "Podcast",
presentation: "Presentation",
radioBroadcast: "Radio broadcast",
report: "Report",
statute: "Legislation",
thesis: "Thesis",
tvBroadcast: "TV broadcast",
videoRecording: "Recording",
webpage: "Webpage",
};
function getDateAdded(item) {
const date = new Date(item.getField("dateAdded"));
return simpleISODate(date)
}
function getCiteKey(item) {
if (typeof Zotero.BetterBibTeX === "object" && Zotero.BetterBibTeX !== null) {
var bbtItem = Zotero.BetterBibTeX.KeyManager.get(item.getField("id"));
return bbtItem.citekey;
}
return "undefined";
}
function getLocalZoteroLink(item) {
let linksString = "zotero://select/items/";
const library_id = item.libraryID ? item.libraryID : 0;
linksString += `${library_id}_${item.key}`;
return linksString;
}
function getCloudZoteroLink(item) {
return `${Zotero.URI.getItemURI(item)}`;
}
function getDOI(item) {
let doi = item.getField("DOI");
if (doi) {
return `[${doi}](https://doi.org/${doi})`;
} else {
return doi;
}
}
function getURL(item) {
let url = item.getField("url");
if (url) {
return `[${url}](${url})`;
} else {
return url;
}
}
function getTags(item) {
const tagsArray = [];
var itemTags = item.getTags();
if (itemTags) {
for (const tag of itemTags) {
tagsArray.push(tag.tag);
}
}
return tagsArray;
}
function getCollectionNames(item) {
const collectionArray = [];
var collections = item.getCollections();
for (let collectionID of collections) {
var collection = Zotero.Collections.get(collectionID);
collectionArray.push(collection.name);
}
return collectionArray;
}
function getRelatedItems(item) {
var relatedItemUris = item.getRelations()["dc:relation"],
relatedItemsArray = [];
if (relatedItemUris) {
for (let uri of relatedItemUris) {
var itemID = Zotero.URI.getURIItemID(uri),
relatedItem = Zotero.Items.get(itemID);
// Get the link content based on settings and item type
let linkContent;
if (!relatedItem.isNote()) {
linkContent = getMDNoteFileName(relatedItem);
} else if (relatedItem.isNote() && !relatedItem.isTopLevelItem()) {
linkContent = getZNoteFileName(relatedItem);
} else {
linkContent = relatedItem.getField("title");
}
relatedItemsArray.push(linkContent);
}
}
return relatedItemsArray;
}
function getCreatorArray(item, creatorType) {
var creators = item.getCreators();
var creatorTypeID = Zotero.CreatorTypes.getID(creatorType);
var creatorArray = [];
if (creators) {
for (let creator of creators) {
if (creator.creatorTypeID === creatorTypeID) {
let creatorName = `${creator.firstName} ${creator.lastName}`;
creatorArray.push(creatorName);
}
}
}
return creatorArray;
}
function getItemMetadata(item) {
let metadata = {};
let fields = Zotero.ItemFields.getItemTypeFields(item.getField("itemTypeID"));
var zoteroType = Zotero.ItemTypes.getName(item.getField("itemTypeID"));
let creatorTypes = Zotero.Utilities.getCreatorsForType(zoteroType);
for (let creatorType of creatorTypes) {
let creatorArray = getCreatorArray(item, creatorType);
metadata[creatorType] = creatorArray;
}
for (let x of fields) {
let field = Zotero.ItemFields.getName(x);
let content = item.getField(field, false, true);
if (field === "DOI") {
content = getDOI(item);
} else if (field === "url") {
content = getURL(item);
} else if (field === "dateAdded") {
content = simpleISODate(content);
}
metadata[field] = content;
}
metadata.itemType = typemap[zoteroType];
metadata.citekey = getCiteKey(item);
metadata.collections = getCollectionNames(item);
metadata.related = getRelatedItems(item);
metadata.tags = getTags(item);
metadata.pdfAttachments = getZoteroAttachments(item);
metadata.localLibrary = getLocalZoteroLink(item);
metadata.cloudLibrary = getCloudZoteroLink(item);
metadata.dateAdded = getDateAdded(item);
metadata.notes = getZoteroNoteTitles(item);
metadata.mdnotesFileName = getMDNoteFileName(item);
metadata.metadataFileName = getZMetadataFileName(item);
return metadata;
}
function formatInternalLink(content, linkStyle) {
linkStyle =
typeof linkStyle !== "undefined" ? linkStyle : getPref("link_style");
if (linkStyle === "wiki") {
return `[[${content}]]`;
} else if (linkStyle === "markdown") {
return `[${content}](${lowerCaseDashTitle(content)})`;
} else {
return `${content}`;
}
}
function lowerCaseDashTitle(content) {
return content.replace(/\s+/g, "-").toLowerCase();
}
function getZoteroNotes(item) {
var noteIDs = item.getNotes();
var noteArray = [];
if (noteIDs) {
for (let noteID of noteIDs) {
let note = Zotero.Items.get(noteID);
noteArray.push(note);
}
}
return noteArray;
}
function getZoteroPDFLink(attachment) {
return `zotero://open-pdf/library/items/${attachment.key}`;
}
function getPDFFileLink(attachment) {
let fileLink = Zotero.File.pathToFileURI(attachment.getFilePath());
return fileLink;
}
function getZoteroAttachments(item) {
const linkStylePref = getPref("pdf_link_style");
let attachmentIDs = item.getAttachments();
var linksArray = [];
for (let id of attachmentIDs) {
let attachment = Zotero.Items.get(id);
if (attachment.attachmentContentType == "application/pdf") {
let link;
if (linkStylePref === "zotero") {
link = `[${attachment.getField("title")}](${getZoteroPDFLink(attachment)})`;
} else if (linkStylePref === "wiki") {
link = formatInternalLink(attachment.getField("title"), "wiki");
} else {
link = `[${attachment.getField("title")}](${getPDFFileLink(attachment)})`;
}
linksArray.push(link);
}
}
return linksArray;
}
// Hacky solution from https://stackoverflow.com/a/25047903
var isDate = function (date) {
return new Date(date).toString() !== "Invalid Date" && !isNaN(new Date(date));
};
// From https://stackoverflow.com/a/29774197
// Return the date in yyyy-mm-dd format
function simpleISODate(date) {
const offset = date.getTimezoneOffset();
date = new Date(date.getTime() + offset * 60 * 1000);
return date.toISOString().split("T")[0];
}
function formatNoteTitle(titleString) {
var strInParenthesis = titleString.match(/\(([^\)]+)\)/g);
if (!strInParenthesis) {
// Just replace all slashes and colons with dashes
return titleString.replace(/\/|:/g, "-");
} else {
var dateInParenthesis = strInParenthesis[0].replace(/\(|\)/g, "");
if (isDate(dateInParenthesis)) {
var date = new Date(dateInParenthesis);
return titleString.replace(dateInParenthesis, simpleISODate(date));
} else {
return titleString;
}
}
}
function noteToMarkdown(item) {
let noteContent = item.getNote();
var noteMD = {};
// Use the turndown provider to turn the HTML into Markdown
noteMD.noteContent = Zotero.MarkdownUtils.html2md(noteContent);
// The original implementation took the text contents of the first
// paragraph and formatted it. Let's do the same with the first
// Markdown line (= paragraph).
const extractedTitle = Zotero.MarkdownUtils.extractTitle(noteMD.noteContent);
noteMD.title = formatNoteTitle(extractedTitle);
noteMD.noteTitle = noteMD.title;
noteMD.tags = getTags(item);
noteMD.related = getRelatedItems(item);
let parentItem = Zotero.Items.get(item.parentItemID);
noteMD.mdnotesFileName = getMDNoteFileName(parentItem);
noteMD.metadataFileName = getZMetadataFileName(parentItem);
if (getPref("obsidian.blocks")) {
let citekey = '';
if (citekey !== "undefined" && getPref("obsidian.blocks.use_citekey")) {
citekey = getCiteKey(parentItem);
}
noteMD.noteContent = Zotero.MarkdownUtils.addBlockIds(noteMD.noteContent, citekey);
}
return noteMD;
}
/*
* Get an item's base file name from setting's preferences
*/
function getFileName(item) {
let citekeyTitle = getPref("citekey_title");
if (citekeyTitle) {
return getCiteKey(item);
} else {
if (getPref("link_style") === "wiki") {
return sanitizeFilename(item.getField("title"));
} else {
return sanitizeFilename(lowerCaseDashTitle(item.getField("title")));
}
}
}
/**
* Return file names for an array of notes based on the naming convention
*
* @param {object} item A Zotero item
*/
function getZoteroNoteTitles(item) {
let noteTitleArray = [];
let noteArray = getZoteroNotes(item);
for (let note of noteArray) {
let noteFileName = getZNoteFileName(note);
noteTitleArray.push(noteFileName);
}
return noteTitleArray;
}
/**
* Return the file name according to the naming convention
* @param {Item|NoteExport} item A Zotero item
* @param {string} filePrefs The substring of the preferences to get the prefix and suffix
*/
function getNCFileName(item, filePrefs) {
let fileName;
if (item.isNote()) {
let parentItem = Zotero.Items.get(item.parentItemID);
let parentTitle = getFileName(parentItem);
let noteTitle = item.getField("title");
fileName = `${parentTitle} - ${noteTitle}`;
} else {
fileName = getFileName(item);
}
fileName = Zotero.File.getValidFileName(fileName);
let prefix = getPref("files." + filePrefs + ".prefix");
let suffix = getPref("files." + filePrefs + ".suffix");
return `${prefix}${fileName}${suffix}`;
}
function getMDNoteFileName(item) {
return getNCFileName(item, "mdnotes.hub");
}
function getStandaloneFileName(item) {
return getNCFileName(item, "mdnotes.standalone");
}
/**
* Return the file name for a Zotero note based on the naming convention
* @param {object} item A Zotero item that isNote()
*/
function getZNoteFileName(item) {
return getNCFileName(item, "zotero.note");
}
function getZMetadataFileName(item) {
return getNCFileName(item, "zotero.metadata");
}
/**
* Return the contents of an Mdnotes file based on an item's placeholders and wildcards
* @param {item} item A Zotero item
*/
async function getMDNoteFileContents(item, standalone) {
let metadata = getItemMetadata(item);
let template;
let fileName;
if (standalone) {
template = await readTemplate("Standalone Note Template");
fileName = getStandaloneFileName(item);
} else {
fileName = metadata.mdnotesFileName;
template = await readTemplate("Mdnotes Default Template");
}
// Add custom placeholders
get_placeholder_contents(template, metadata);
let formattedPlaceholders = format_placeholders(metadata);
let content = remove_invalid_placeholders(
replace_placeholders(template, formattedPlaceholders)
);
content = replace_wildcards(content, metadata);
return { content: content, name: fileName };
}
function getDefaultTemplate(fileName) {
switch (fileName) {
case "Mdnotes Default Template":
return mdnotesTemplate;
case "Standalone Note Template":
return standaloneTemplate;
case "Zotero Metadata Template":
return zoteroMetadataTemplate;
case "Zotero Note Template":
return zoteroNoteTemplate;
}
}
async function readTemplate(fileName) {
let templateDir = getPref("templates.directory");
if (templateDir === "") {
return getDefaultTemplate(fileName);
}
let availableTemplates = await getTemplatesAtDirectory();
let template;
if (availableTemplates.includes(`${fileName}.md`)) {
template = await Zotero.File.getContentsAsync(
getFilePath(getPref("templates.directory"), fileName)
);
return template;
} else {
return getDefaultTemplate(fileName);
}
}
async function getTemplatesAtDirectory() {
let fileArray = [];
await Zotero.File.iterateDirectory(
getPref("templates.directory"),
async function (entry) {
if (
entry.isDir ||
entry.name.startsWith(".") ||
!entry.name.endsWith(".md")
) {
return;
}
fileArray.push(entry.name);
}
);
return fileArray;
}
// From https://github.com/jlegewie/zotfile/blob/master/chrome/content/zotfile/utils.js#L104
function replace_wildcards(str, args) {
return str.replace(/%\((\w+)\)/g, (match, name) => args[name]);
}
/**
*
* @param {string} str The string to be replaced
* @param {Object} args An array with the placeholder name as key and the (formatted) contents as values
* @returns {string} A string with the placeholders replaced
*/
function replace_placeholders(str, args) {
return str.replace(/{{(\w+)}}/g, (match, name) =>
args[name] ? args[name] : "{{invalid}}"
);
}
function remove_invalid_placeholders(str) {
return str.replace(/{{invalid}}\n?\r?/g, (match, name) => "");
}
function skipItem(value) {
let skip = false;
// If we have an empty field and we DON'T want to include empty values, continue
// If the value is an empty string, we also skip it
if (value === "" || value === undefined) {
skip = true;
}
// If it's an array and is empty
if (typeof value === "object" && value.length === 0) {
skip = true;
}
return skip && !getPref("templates.include_empty_placeholders");
}
function get_placeholder_contents(template, fields) {
const re = /{{(\w+)}}/g;
let placeholders = template.match(re);
// In case a template doesn't have any placeholders
if (!placeholders) return;
// Loop through all the placeholders
for (const result of placeholders) {
let placeholder = result.substring(2, result.length - 2)
// Check if it's a normal field, and skip it if so
if (fields.hasOwnProperty(placeholder)) {
continue;
}
// Get the settings for that particular placeholder
let settings = getFormattingSettings(placeholder);
if (settings.zotero_field){
if (fields.hasOwnProperty(settings.zotero_field)) {
let content = fields[settings.zotero_field];
fields[placeholder] = content;
}
} else if (settings.custom_content) {
fields[placeholder] = settings.custom_content;
}
}
}
/**
* @param {Object} placeholders An object that contains the item's fields as keys, and its contents as values
* @return {string} The formatted string
*/
function format_placeholders(placeholders) {
let formattedPlaceholders = {};
// Loop through the item metadata and replace any placeholder that matches its field
for (const [key, value] of Object.entries(placeholders)) {
if (skipItem(value)) {
continue;
}
let settings = getFormattingSettings(key);
// Replace a potential undefined with an empty string instead of undefined
let newValue = value ? value : "";
let formatted_label = capitalize_field_label(key);
// Exceptions that already come formatted as external links
if (
[
"pdfAttachments",
"url",
"DOI",
"cloudLibrary",
"localLibrary",
"noteContent",
].includes(key)
) {
settings.link_style = "no-links";
}
let formatted_content = format_field_content(newValue, settings);
let placeholder = settings.content
? settings.content
: `{{bullet}} {{field_name}}: {{field_contents}}`;
let args = {
field_contents: formatted_content,
bullet: `${getPref("html2md.default.bullet")}`,
field_name: formatted_label,
};
formattedPlaceholders[key] = replace_placeholders(placeholder, args);
}
return formattedPlaceholders;
}
function capitalize_field_label(text) {
if (text === "url") return "URL";
if (text === "DOI") return text;
return Zotero.Utilities.capitalize(
text.replace(/([A-Z])/g, " $1"),
true
).replace("Pdf", "PDF");
}
function format_field_content(fieldContent, settings) {
if (typeof fieldContent === "object") {
return format_array(fieldContent, settings);
}
return format_string(fieldContent, settings);
}
function change_case(text, textCase) {
switch (textCase) {
case "title":
return Zotero.Utilities.capitalizeTitle(text);
case "sentence":
return Zotero.Utilities.capitalize(text);
case "lower":
return text.toLowerCase();
case "upper":
return text.toUpperCase();
default:
return text;
}
}
function format_string(str, settings) {
let formattedString;
// First format links
formattedString = `${formatInternalLink(str, settings.link_style)}`;
let format = settings.field_contents
? settings.field_contents
: "{{content}}";
if (settings.remove_spaces) {
formattedString = formattedString.replace(/\s+/g, "-");
}
if (settings.text_case) {
formattedString = change_case(formattedString, settings.text_case);
}
// Format the field
formattedString = replace_placeholders(format, {
content: `${formattedString}`,
});
return formattedString;
}
function camelToTitleCase(str) {
let new_str = str.replace(/_/g, " ");
return Zotero.Utilities.capitalizeTitle(
new_str.replace(/([A-Z])/g, " $1"),
true
);
}
function format_array(array, settings) {
let formattedArray = [];
for (let item of array) {
formattedArray.push(format_string(item, settings));
}
let list_separator = settings.list_separator ? settings.list_separator : ", ";
return formattedArray.join(list_separator);
}
function getFormattingSettings(field) {
let fieldPrefs = getPref("placeholder." + field);
let fieldSettings = fieldPrefs ? JSON.parse(fieldPrefs) : {};
return fieldSettings;
}
async function getZoteroNoteFileContents(item) {
let fileName = getZNoteFileName(item);
let template = await readTemplate("Zotero Note Template");
let note = noteToMarkdown(item);
// Add custom placeholders
get_placeholder_contents(template, note);
let formattedPlaceholders = format_placeholders(note);
let fileContents = remove_invalid_placeholders(
replace_placeholders(template, formattedPlaceholders)
);
fileContents = replace_wildcards(fileContents, note);
return { content: fileContents, name: fileName };
}
function getFilePath(path, filename) {
return OS.Path.join(OS.Path.normalize(path), `${filename}.md`);
}
function getObsidianURI(fileName) {
let uriStart = `obsidian://open?vault=${getPref("obsidian.vault")}&file=`;
let fileWithPath;
if(getPref("obsidian.dir").length > 0) {
fileWithPath = getPref("obsidian.dir") + "/" + fileName;
}
else{
fileWithPath = fileName;
}
let encodedFileName = Zotero.File.encodeFilePath(fileWithPath);
return `${uriStart}${encodedFileName}`;
}
function itemHasAttachment(comparableField, parentItem) {
let existingAttachments = parentItem.getAttachments();
let linkExists = false;
for (let id of existingAttachments) {
let attachment = Zotero.Items.get(id);
if (attachment.attachmentContentType === "text/markdown") {
if (attachment.attachmentPath === comparableField) {
linkExists = true;
break;
}
} else if (
attachment.attachmentContentType === "x-scheme-handler/obsidian"
) {
if (attachment.getField("url") === comparableField) {
linkExists = true;
break;
}
}
}
return linkExists;
}
function getParentItem(item) {
let parentItem;
if (item.isNote()) {
parentItem = Zotero.Items.get(item.parentItemID);
} else {
parentItem = item;
}
return parentItem;
}
async function addObsidianLink(outputFile, item) {
let fileName = OS.Path.basename(outputFile);
fileName = fileName.split(".")[0];
let obsidianURI = getObsidianURI(fileName);
let parentItem = getParentItem(item);
if (
!itemHasAttachment(obsidianURI, parentItem) &&
getPref("obsidian.attach_obsidian_uri")
) {
await Zotero.Attachments.linkFromURL({
url: obsidianURI,
contentType: "x-scheme-handler/obsidian",
title: fileName,
parentItemID: parentItem.id,
});
}
}
/**
* Sanitizes the filename using the given replacement
*
* @param {string} filename The filename to sanitize
* @param {string} replacement The replacement character(s), default none
*
* @return {string} The sanitized filename.
*/
function sanitizeFilename(filename, replacement = '') {
return filename.replace(/[\/\?<>\\:\*\|"]/g, replacement).trim()
}
Zotero.Mdnotes =
Zotero.Mdnotes ||
new (class {
async openPreferenceWindow(paneID, action) {
const io = {
pane: paneID,
action,
};
window.openDialog(
"chrome://mdnotes/content/options.xul",
"mdnotes-options",
"chrome,titlebar,toolbar,centerscreen" +
Zotero.Prefs.get("browser.preferences.instantApply", true)
? "dialog=no"
: "modal",
io
);
}
updateMenus() {
// Follow Zotfile's example:
// https://github.com/jlegewie/zotfile/blob/master/chrome/content/zotfile/ui.js#L190
let win = Services.wm.getMostRecentWindow("navigator:browser");
let menu = win.ZoteroPane.document.getElementById("id-mdnotes-menupopup");
// This is the order in which menuitems are added in overlay.xul
let items = {
mdexport: 0,
separator: 1,
single: 2,
batch: 3,
mdnotes: 4,
standalone: 5,
};
let disableItems = [];
if (getPref("file_conf") === "split") {
disableItems.push(items.single);
} else {
disableItems.push(items.batch);
disableItems.push(items.mdnotes);
}
if (!getPref("standalone_menu")) {
disableItems.push(items.standalone)
}
// Enable all items by default and make them visible
for (let i = 0; i < menu.childNodes.length; i++) {
menu.childNodes[i].setAttribute("disabled", false);
menu.childNodes[i].setAttribute("hidden", false);
}
// Hide and disable menus based on the single vs split files
for (let i in disableItems) {
menu.childNodes[disableItems[i]].setAttribute("disabled", true);
menu.childNodes[disableItems[i]].setAttribute("hidden", true);
}
}
setPref(pref_name, value) {
Zotero.Prefs.set(`extensions.mdnotes.${pref_name}`, value, true);
}
async addLinkToMDNote(outputFile, item) {
let parentItem = getParentItem(item);
if (
!itemHasAttachment(outputFile, parentItem) &&
getPref("attach_to_zotero")
) {
await Zotero.Attachments.linkFromFile({
file: outputFile,
parentItemID: parentItem.id,
});
}
}
async createNoteFileMenu(standalone) {
var items = Zotero.getActiveZoteroPane()
.getSelectedItems()
.filter(
(item) =>
Zotero.ItemTypes.getName(item.itemTypeID) !== "attachment" &&
Zotero.ItemTypes.getName(item.itemTypeID) !== "note"
);
await Zotero.Schema.schemaUpdatePromise;
const FilePicker = require("zotero/filePicker").default;
const fp = new FilePicker();
var oldPath = getPref("directory")
? getPref("directory")
: OS.Constants.Path.homeDir;
if (oldPath) {
fp.displayDirectory = oldPath;
}
for (const item of items) {
Zotero.debug("Creating markdown note...");
Zotero.debug("Standalone: " + standalone);
fp.init(window, "Save markdown note...", fp.modeSave);
fp.appendFilter("Markdown", "*.md");
let fileName;
if (standalone) {
fileName = getStandaloneFileName(item);
} else {
fileName = getMDNoteFileName(item);
}
fp.defaultString = `${fileName}.md`;
const rv = await fp.show();
if (rv == fp.returnOK || rv == fp.returnReplace) {
let outputFile = fp.file;
if (outputFile.split(".").pop().toLowerCase() != "md") {
outputFile += ".md";
}
const file = await getMDNoteFileContents(item, standalone);
Zotero.File.putContentsAsync(outputFile, file.content);
// Attach note
this.addLinkToMDNote(outputFile, item);
addObsidianLink(outputFile, item);
}
}
}
async getRegularItemContents(item) {
let metadata = getItemMetadata(item);
let template = await readTemplate("Zotero Metadata Template");
// Add custom placeholders
get_placeholder_contents(template, metadata);
// Add formatting
let formattedPlaceholders = format_placeholders(metadata);
let newContents = remove_invalid_placeholders(
replace_placeholders(template, formattedPlaceholders)
);
let fileName = getZMetadataFileName(item);
return { content: newContents, name: fileName };
}
/**
* Return an object with all the exportable files from a top-level item.
* Only used for batch export.
* @param {Item} item A Zotero item
*/
async getFiles(item) {
var fileArray = [];
let mdnotesFile = await getMDNoteFileContents(item);
fileArray.push({
name: mdnotesFile.name,
content: mdnotesFile.content,
});
// Only add the metadata file for multi-file exports
if (getPref("file_conf") === "split") {
let itemFile = await this.getRegularItemContents(item);
fileArray.push({ name: itemFile.name, content: itemFile.content });
}
let noteIDs = item.getNotes();
if (noteIDs) {
for (let noteID of noteIDs) {
let note = Zotero.Items.get(noteID);
let zotNoteFile = await getZoteroNoteFileContents(note);
fileArray.push({
name: zotNoteFile.name,
content: zotNoteFile.content,
});
}
}
return fileArray;