Scripting - flarom/cohesion GitHub Wiki

Cohesion allows the use of scripts to create automations inside of documents. Scripts can insert text into documents, create new documents, fetch data from APIs, edit the content of text blocks, and more.

Script blocks are created simply by titling a codeblock as "JavaScript" or "JS", the rendered codeblock will have a little rocket icon that executes the script when clicked.

Note

The HTML <script> tag is disabled to prevent scripts from auto-running, the only way to execute a script is via a JavaScript codeblock.


Functions

Notify

The notify function show a toast notification at the top of the screen.

Syntax

notify(Message = "Any text string")

Example

notify("Hello World!");

Insert Text

The inserttext function inserts text in the document.

Syntax

inserttext(Text = "Any text string", Position = Insert_position)

  • Insert_position.CURSOR: Will insert the text at the cursor position;
  • Insert_position.DOCUMENT_END: Will insert the text at the end of the document;
  • Insert_position.DOCUMENT_START: Will insert the text at the start of the document;
  • Insert_position.DOCUMENT_START_SAFE: Will insert the text at the start of the document, after the meta block;

Example

inserttext("Hello World!", Insert_position.DOCUMENT_START);
// Will insert text at the first line of the document

inserttext("Goodbye World!");
// Will insert text at the last line of the document, since Insert_position.DOCUMENT_End is the default value.

Create Document

The createdocument function creates a new document.

Syntax

createdocument(Message = "Any text string")

Example

createdocument("# This is a title\n\nthis is a paragraph");
// using \n to create new lines

createdocument(
`
# This is a title

this is a paragraph
`);
// using `` allows creating a string with multiple lines

Note

The Cohesion editor asks the user for permittion to create a document, and this permission can be denied.

Set Block Content

The setblockcontent function sets the text content of a given block as a new string.

Syntax

setblockcontent(ID = "Any text string", Value = "Any text string")

Example

We'll first need to create a Markdown block to set its text:

> [!Block title](block-id)
> 
> 

Now we have a empty block called block-id, we can use the setblockcontent to modify this blocks content.

setblockcontent("block-id", "Hello World!");

Add Block Content

The addblockcontent function adds a string at the end of a given block.

Syntax

addblockcontent(ID = "Any text string", Value = "Any text string")

Example

addblockcontent("block-id", "Hello World!")

Shift Block Content

The shiftblockcontent function acts basically identical as the addblockcontent function, but the string content will be inserted at the start of the block.

Rename Block

The renameblock function updates the name of a given block.

Syntax

renameblock(ID = "Any text string", Name = "Any text string")

Example

renameblock("block-id", "New name")

Fetch Data

The fetchdata function allows you to fetch data from a given URL and execute a callback with the parsed JSON data.

Syntax

fetchdata(URL = "Any text string", Callback = Function)

  • URL: The address of the API or resource you want to access.
  • Callback: A function that will be executed when the data is received, with the JSON data as a parameter.

Example

Let’s fetch data from the PokeAPI about a Pokémon and use addblockcontent to insert that information into a block called pokemon-info.

> [!Pokémon Info](pokemon-info)
> 
> 

Then use fetchdata to retrieve the data and addblockcontent to insert it into the block:

fetchdata("https://pokeapi.co/api/v2/pokemon/pikachu", function(data) {
    const info = `
Name: ${data.name}
Height: ${data.height}
Weight: ${data.weight}
Type: ${data.types.map(t => t.type.name).join(", ")}
    `;
    
    addblockcontent("pokemon-info", info);
});

Get Field Value

The getfieldvalue function allows you to get the text from a <input> field.

Syntax

getfieldvalue(ID = "Any text string");

Example

Let's first create a HTML field:

<input id="my-field">

You can also declare that this field is a text field by using the type="text" attribute, but it's not necessary.

Now that we have a text field, we can take this fields value by using the getfieldvalue function:

const fieldText = getfieldvalue("my-field");
notify(fieldText);

Parsers

StrfTime

The strftime parser converts an explicit date and time format string into the current date and time.

Syntax

strftime(format = "Any strftime string");

Examples

const hours = strftime('%H');
const minutes = strftime('%M');

notify(`The time is ${hours} hours and ${minutes} minutes`);

Shortened:

notify(strftime('The time is %H hours and %M minutes'));

Read Time Formatting to learn more about the strftime syntax.

⚠️ **GitHub.com Fallback** ⚠️