# How to Write Your Own Template This documentation is like a dictionary. For beginners, see [How to Use Templates](./TemplateUsage.md). Use `Ctrl+F` to look up what you need and make your own template! ## Stages Some type of templates(Item) support stages. Code wrapped inside the stage will be called at a specific time. For example, the `beforeloop` stage code: ```js // @beforeloop-begin code; // @beforeloop-end ``` | stage | calling time | | ---------- | --------------------------------- | | beforeloop | before entering the loop of items | | default | loop of items | | afterloop | after leaving the loop of items | In other type of templates, the default stage is called. ## Variables ### QuickInsert > variables: link: string, subNoteItem, noteItem ### QuickBackLink > variables: subNoteItem, noteItem ### QuickImport > variables: subNoteLines: string[], subNoteItem, noteItem ### QuickNoteV3 > variables: annotationItem, topItem ### ExportMDFileName > variables: noteItem ### ExportMDFileHeader > variables: noteItem The return value must be a JSON string. ### Text > variables: - ### Item > beforeloop stage: items, copyNoteImage, editor, sharedObj(for temporary variables, shared by all stages) > default stage: topItem, itemNotes, copyNoteImage, editor, sharedObj > afterloop stage: items, copyNoteImage, editor, sharedObj ### Note > Removed after v0.7.15+ ## Formats ### Line Description: Normal line. Template Type: None. Required Variable: None. ```html
Your Line Here
``` ### Heading Description: From h1 to h6. Template Type: None. Required Variable: None. ```htmlText
``` ### Strong Description: **Strong text**. Put it in a `p`. Template Type: None. Required Variable: None. ```html Your Text ``` ### Underline Description: Underline text. Put it in a `p`. Template Type: None. Required Variable: None. ```html Your Text ``` ### Deleteline Description: Deleteline text. Put it in a `p`. Template Type: None. Required Variable: None. ```html Your Text ``` ### Link Description: Link. Put it in a `p`. Template Type: None. Required Variable: None. ```html Link Text ``` ### Number List Description: Number List. Template Type: None. Required Variable: None. ```htmlThesupand thesub text
``` ### Block Quote Description: > Block Quote Template Type: None. Required Variable: None. ```html``` ### Monospaced Description: `Monospaced` Template Type: None. Required Variable: None. ```htmlText
Text``` ### Table Description: | Table | Column1 | Column2 | | ---- | ---- | ---- | | 00 | 01 | 02 | | 10 | 11 | 12 | Template Type: None. Required Variable: None. ```html
|
Table |
Column1 | Column2 |
|---|---|---|
| 00 | 01 | 02 |
| 10 | 11 | 12 |
${new Date().toLocaleDateString()}
``` ### Tags Description: `item.getTags()` returns tags in list. Usually use `tags.includes("YourTag") ? do sth : do sth else`. Required Variable: any item/note/annotation. ```js${topItem.getTags()}
``` ## Note Fields ### Note Title Description: Note Title. First line of note. Template Type: Note. Required Variable: noteItem/subNoteItem. ```js${noteItem.getNoteTitle()}
``` ### Note Content Description: Note Content. Template Type: Note. Required Variable: noteItem/subNoteItem. ```js${noteItem.getNote()}
``` ### Note Link Description: Note Link. Template Type: Note. Required Variable: noteItem/subNoteItem. ```js ``` ## Item Fields ### Title Description: Item title. Template Type: Item. Required Variable: topItem. ```js${topItem.getField("title")}
``` ### Publisher/Journal Description: Publisher/Journal. Template Type: Item. Required Variable: topItem. ```js$ {(() => { if (topItem.itemType === "conferencePaper") { return; topItem.getField("conferenceName") || topItem.getField("proceedingsTitle"); } if (topItem.itemType === "journalArticle") return topItem.getField("publicationTitle"); if (topItem.itemType === "report") return topItem.getField("institution"); return topItem.getField("publicationTitle"); })()}
``` ### Authors Description: Authors. Template Type: Item. Required Variable: topItem. ```js$ {topItem .getCreators() .map((v) => v.firstName + " " + v.lastName) .join("; ")}
``` ### Pub. date Description: Pub. date. Template Type: Item. Required Variable: topItem. ```js${topItem.getField("date")}
``` ### Pub. date Description: Publication date/time. Template Type: Item. Required Variable: topItem. ```js${topItem.getField("date")}
``` ### DOI Description: DOI. Template Type: Item. Required Variable: topItem. ```js ``` ### URL Description: URL. Template Type: Item. Required Variable: topItem. ```js ``` ### CitationKey Description: CitationKey. Template Type: Item. Required Variable: topItem. ```js${topItem.citationKey ? topItem.citationKey : ""}
``` ### PDF Link Description: URL. Template Type: Item. Required Variable: topItem. ```js$ {((topItem) => { const getPDFLink = (_item) => { let libraryID = _item.libraryID; let library = Zotero.Libraries.get(libraryID); let itemKey = _item.key; let itemLink = ""; if (library.libraryType === "user") { itemLink = `zotero://open-pdf/library/items/${itemKey}`; } else if (library.libraryType === "group") { itemLink = `zotero://open-pdf/groups/${library.id}/items/${itemKey}`; } return `${_item.getFilename()}`; }; return Zotero.Items.get(topItem.getAttachments()) .filter((i) => i.isPDFAttachment()) .map((i) => getPDFLink(i)) .join("\n"); })(topItem)}
``` ### Item Notes Description: Walk all sub notes under an item. You can add your code inside the loop. Template Type: Item. Required Variable: topItem. ```js ${itemNotes.map((noteItem)=>{ // process each item note const noteLine = `${noteItem.getNote()}`; copyNoteImage(noteItem); return noteLine; }).join("\n")} ``` ### About Item Fields The `noteItem` and `topItem` is a Zotero Item object. The general data can be accessed using the `getField()` method. For example: `topItem.getField('title')` will return the title of the `topItem`. ```ts // Get Item Fields getField: (void)=>string; // Get Authors getCreators: (void)=>{ fieldMode: number, firstName: string, // may be empty lastName: string, creatorTypeID: number, }[]; ``` Find available fields of the selected item with the code below: ```js const item = ZoteroPane.getSelectedItems().shift(); const usedFields = item.getUsedFields(); Zotero.ItemFields.getAll() .filter((e) => usedFields.indexOf(e.id) >= 0) .map((e) => e.name); ``` The result is like this (depending on the item you select): ```JSON [ "0": "title" "1": "date" "2": "language" "3": "shortTitle" "4": "libraryCatalog" "5": "url" "6": "accessDate" "7": "pages" "8": "conferenceName" ] ``` or see https://aurimasv.github.io/z2csl/typeMap.xml for the detailed Zotero fields documentation.