Code snippets

A few snippets that provide building blocks for custom formatting functions

This is a list of JavaScript snippets which can be used as building blocks when writing custom formatting functions. They correspond to common operations, like converting a full date into Roam's Daily Notes format or processing an array of author names into a comma-separated list. Most of them are taken straight from the extension's built-in getItemMetadata function.

Generic snippets

Basic function structure

window.myFuncName = function(item){
    let metadata = [];
    
    // Code snippets should be here
    
    return metadata = [];
}
  • If you're writing a function that creates a flat output, you can simply add blocks by using metadata.push("String of block").

  • If you're writing a function that creates a nested output, you'll have to create intermediate Object blocks during the processing/formatting of the item's data.

Adding an element as block if it exists

if(item.<element_path>{
    metadata.push(item.<element_path>);
}

// Example :

// Item title, located under the item's `data` property
if(item.data.title){
    metadata.push(item.data.title);
}

Attribute-specific snippets

data.creators

Goes through the item's list of creators, makes each creator's name into a page reference with format [[firstName lastName]], and adds the creator's role between parentheses after if the creator is not an author. Returns : Author(s):: [[First Author]], [[Second Author]], [[An Editor]] (editor)

data.dateAdded

Converts the date on which the item was added to Zotero into Roam's Daily Notes format. Returns : Date Added:: [[March 10th, 2021]]

data.itemType

Maps an item's type to its non-machine name, as specified in the user's or extension's typemap. Returns : Type:: [[Book]]

data.tags

Goes through the list of the item's tags, and make every tag into a Roam tag with format #[[tag]] to support multi-word tags. Returns : Tags:: #[[tag1]], #[[tag2]], #[[a multi-word tag]]

Additional data (collections, PDFs, notes...)

Getting an item's collections

Collections data is requested along with items data, and is stored in the zoteroRoam.data.collections object. You can inspect its contents using the console if you'd like.

This allows the extension to identify which collections an item belongs to - and that's what the zoteroRoam.formatting.getItemCollections function does :

Getting an item's children (PDF + notes)

If you want to get data about PDF attachments + notes, it's recommended to include them in your data request. That requires asking for items (all of them) rather than items/top (top-level items) in your data URI, and (for notes) giving your API key access to notes (see Step 1.1).

The utility function zoteroRoam.formatting.getItemChildren can search for an item's children within the loaded dataset. If they're present, it will return them (as raw data or formatted output) ; if there aren't any, it will return 'false'. Lastly, in the case that the item has children but none are found in the dataset, the function will also return a remoteChildren: true property - which you can use to request them if you want to.

Basic syntax

Function arguments

Argument

default

Possible values

What it does

pdf_as

"links"

"links" or "raw"

Determines the output format of PDF items :

  • "links" -> an Array of Markdown-style links to the PDFs

  • "raw" -> an Array containing the raw data for the PDF items, as returned by the Zotero API

notes_as

"formatted"

"formatted" or "raw"

Determines the output format of note items :

  • "formatted" -> an Array of Arrays, where each child Array contains one note item's contents

  • "raw" -> an Array containing the raw data for the note items, as returned by the Zotero API

split_char

"\n"

Anything

Determines how to split notes into blocks :

  • "\n" will split by newline ;

  • "</p>" will split by paragraph ;

  • ...

What the function returns

How to use the function in your code

To make use of this function, there are 2 important things : 1) know what output format you're asking for (cf. function arguments above), 2) begin by checking if there is any data on the children.

This is how the built-in function zoteroRoam.formatting.getItemMetadata handles this :

This is what the code above will output as Roam blocks :

  • PDF links : [First PDF attachment.pdf](link to this PDF), [Second PDF attachment.pdf](link to that PDF)

  • [[Notes]]

    • Block from note 1

    • Another block from note 1

    • Block from note 2

    • Another block from note 2

Last updated

Was this helpful?