You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implemented functions: create markdown file in local system using template file.
unimplemented functions: link markdown file with zotero item.
Event
None
Operation
Script
Data
/** * Creates Markdown file by template. * @author cs123951 * @usage * @link https://github.com/windingwind/zotero-actions-tags/discussions/ * @see https://github.com/windingwind/zotero-actions-tags/discussions/ */var{ FilePicker }=ChromeUtils.importESModule('chrome://zotero/content/modules/filePicker.mjs');constwindow=require("window");consttypemap={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",};functiongetCreatorArray(item,creatorType){varcreators=item.getCreators();varcreatorTypeID=Zotero.CreatorTypes.getID(creatorType);varcreatorArray=[];if(creators){for(letcreatorofcreators){if(creator.creatorTypeID===creatorTypeID){letfirstName=creator.firstName.replace(/[^a-zA-Z]/g,"");letlastName=creator.lastName.replace(/[^a-zA-Z]/g,"");// Set author as tags so that we can conclude.letcreatorName=` #${firstName}_${lastName} `;creatorArray.push(creatorName);}}}returncreatorArray;}functiongetPublication(item){letpublication=item.getField("publicationTitle");if(publication){letpubTag=publication.replace(/[^a-zA-Z]/g,"_")return` #${pubTag} `;}else{returnpublication;}}functiongetDOI(item){letdoi=item.getField("DOI");if(doi){return`https://doi.org/${doi}`;}else{returndoi;}}functiongetTitle(item){lettitle=item.getField("title");returntitle;}functiongetYear(item){letdate=item.getField("date");letyear=""if(date){if(date.includes('\/')){// 10/2012year=date.slice(3,7);}elseif(date.includes('-')){// 2012-10-09year=date.slice(0,4);}}returnyear;}functiongetCiteKey(item){if(typeofZotero.BetterBibTeX==="object"&&Zotero.BetterBibTeX!==null){varbbtItem=Zotero.BetterBibTeX.KeyManager.get(item.getField("id"));returnbbtItem.citationKey;// Modified!}return"undefined";}functiongetCollectionNames(item){constcollectionArray=[];varcollections=item.getCollections();for(letcollectionIDofcollections){varcollection=Zotero.Collections.get(collectionID);collectionArray.push(collection.name);}returncollectionArray;}functiongetLocalZoteroLink(item){letlinksString="zotero://select/items/";linksString+=`${item.key}`;returnlinksString;}functiongetItemMetadata(item){letmetadata={};letfields=Zotero.ItemFields.getItemTypeFields(item.getField("itemTypeID"));varzoteroType=Zotero.ItemTypes.getName(item.getField("itemTypeID"));letcreatorTypes=Zotero.Utilities.getCreatorsForType(zoteroType);for(letcreatorTypeofcreatorTypes){letcreatorArray=getCreatorArray(item,creatorType);metadata[creatorType]=creatorArray;}for(letxoffields){letfield=Zotero.ItemFields.getName(x);letcontent=item.getField(field,false,true);if(field==="DOI"){content=getDOI(item);}elseif(field=="title"){content=getTitle(item);}elseif(field=="publicationTitle"){content=getPublication(item);}metadata[field]=content;}metadata.year=getYear(item);metadata.localLink=getLocalZoteroLink(item);metadata.itemType=typemap[zoteroType];metadata.citekey=getCiteKey(item);returnmetadata;}functionreplace_placeholders(str,args){returnstr.replace(/{{(\w+)}}/g,(match,name)=>args[name] ? args[name] : "");}asyncfunctiongetMDNoteFileContents(item,templatesPath){lettemplate;template=awaitZotero.File.getContentsAsync(templatesPath);letitemMetadata=getItemMetadata(item);// Fill template.letformattedTemplate=replace_placeholders(template,itemMetadata);returnformattedTemplate;}asyncfunctioncreateNoteFileMenu(templatesPath,outputFileFolder){// Get selected items.varZoteroPane=Zotero.getActiveZoteroPane();varitems=ZoteroPane.getSelectedItems().filter((item)=>Zotero.ItemTypes.getName(item.itemTypeID)!=="attachment"&&Zotero.ItemTypes.getName(item.itemTypeID)!=="note");awaitZotero.Schema.schemaUpdatePromise;constfp=newFilePicker();fp.displayDirectory=outputFileFolder;// items and item exist.for(constitemofitems){// var itemID = item.id;// ZoteroPane.addAttachmentFromDialog(false, itemID);fp.init(window,"Save markdown note...",fp.modeSave);fp.appendFilter("Markdown","*.md");letcitekey=getCiteKey(item);fp.defaultString=`${citekey}.md`;constrv=awaitfp.show();if(rv==fp.returnOK||rv==fp.returnReplace){letoutputFile=fp.file;if(outputFile.split(".").pop().toLowerCase()!="md"){outputFile+=".md";}letcontents=awaitgetMDNoteFileContents(item,templatesPath);if(rv==fp.returnReplace){constoldContents=awaitZotero.File.getContentsAsync(outputFile);contents=`${contents}${oldContents}`;}Zotero.File.putContentsAsync(outputFile,contents);}}return`Markdown notes have been created for the selected item.`;}// =====Start======// Read template.varoutputFileFolder='/path_to_save_file/';vartemplatesPath='/path_to_read_template/template.md';// When select a field, items and item are created. // This is to avoid duplicate operations// When selecting multiple items in the library, the action will be triggered once for all items // (items=[...], item=undefined) and then one by one for each item (items=[], item=...). // You can use the items variable to get the selected items array and avoid duplicate executions.// See https://github.com/windingwind/zotero-actions-tags/discussions/292// we choose to use the first case: (items=[...], item=undefined).if(items?.length>0){returncreateNoteFileMenu(templatesPath,outputFileFolder);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Description
Since mdnotes is unavailable in zotero 7, I write a simple script to create a markdown file using template.
template.md can be like:
Event
None
Operation
Script
Data
Anything else
Reference:
[1] https://github.com/argenos/zotero-mdnotes/blob/master/content/mdnotes.js
Beta Was this translation helpful? Give feedback.
All reactions