Skip to content

Scripting

flarom edited this page Dec 3, 2025 · 15 revisions

Cohesion allows the use of scripts to create automations. With scripts, you can insert text, create files, manipulate blocks, persist data in memory, fetch API data, and more.

Macros are created via the Macro manager (... > Macros), and can be activated via slash-commands in the editor, by typing /, a selection of macros will be displayed.

Warning

NEVER import a script made by someone you don't trust. Scripts have total access of the application front-end, and can be used maliciously.



Built in macros

Command Description
/abbr Insert an abbreviation
/admonition Note, tip, warning, or important section
/code Create a codeblock
/deflist Insert a definition list
/details Hidable content block
/embed Embeded web content
/graph Pie, bar or line chart
/link Website URL or internal link
/list Ordered, Unordered or Task list
/meta Create a meta block
/resources Pick items from your resources folder
/table Insert a table
/toc Table of contents
/today Today's date and time

UI Tools

ui.notify

Shows a toast notification at the top of the screen.

Syntax

ui.notify("Hello World!");

ui.modal.text

Shows a text input modal and returns the string the user entered.

Syntax

async function promptName() {
    const name = await ui.modal.text("Enter your name:", "Default value");
    ui.notify("You wrote: " + name);
}

promptName();

Document Functions

insert

Inserts text in the document at a given position.

Syntax

insert(Text, Position);

Positions available:

  • documentPosition.cursor → at cursor position
  • documentPosition.end → at end of document (default)
  • documentPosition.start → at start of document
  • documentPosition.start_safe → at start, after the meta block

Example

insert("Hello World!", documentPosition.start);
// Inserts text at the top

insert("Goodbye World!");
// Inserts text at the bottom (default)

insertSnippet

Inserts a snippet that supports placeholder syntax like ${1:placeholder}.

Example

insertSnippet("My name is ${1:Name}");

file.new

Creates a new document (with confirmation from the user).

Syntax

file.new("Document content here");

Example

file.new(`
# Title
This is a paragraph
`);

Memory Functions

The memory object allows you to save values that persist until cleared.

  • memory.set(key, value) → stores a value
  • memory.get(key, fallback) → retrieves a value (or fallback if missing)
  • memory.remove(key) → deletes one value
  • memory.clear() → deletes all memory values
  • memory.list() → returns an object with all stored values

Example

memory.set("username", "Alice");
ui.notify(memory.get("username")); // Alice

memory.remove("username");

Block Functions

Blocks are Markdown structures like:

> [!Note](block-id)
> Example content

The block object lets you create and manipulate them.

block.make

Creates a new block with a type, id, and optional content.

block.make("Hello", "intro-block", "Hello World!");

block.set

Replaces all content of a block.

block.set("intro-block", "New content");

block.add

Appends new lines at the end of a block.

block.add("intro-block", "Added at the bottom");

block.addStart

Adds new lines at the beginning of a block.

block.addStart("intro-block", "Added at the top");

block.getContent

Retrieves the block’s inner content. Modes: "plain", "plain-with-markers", "raw"

const text = block.getContent("intro-block");
ui.notify(text);

block.get

Gets the entire block including header.

const blockData = block.get("intro-block");

block.rename

Renames the block’s type.

block.rename("intro-block", "Warning");

block.getName

Gets the block’s current type.

const type = block.getName("intro-block");

block.remove

Deletes the block.

block.remove("intro-block");

Field Functions

Fields are HTML inputs inside the document.

Manage Field Values

  • field.getValue(id) → get the current value(s)
  • field.setValue(id, value) → update value(s)
  • field.keepValue(id) → keeps field values synced inside the document

Example

const volume = field.getValue("volume");
ui.notify("Volume is " + volume);

field.setValue("volume", 75);

Network Functions

network.fetch

Fetches data from an API and runs a callback with the JSON response.

Syntax

network.fetch(URL, Callback);

Example

network.fetch("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(", ")}
    `;
    block.add("pokemon-info", info);
});

Date/Time Parsers

strftime

Formats the current date/time with strftime syntax.

const now = strftime("The time is %H:%M");
ui.notify(now);

Learn the basic syntax of markdown and the Cohesion flavored markdown

Learn more advanced markdown applications

Learn how to use metadata on your documents to better organize your files

Learn how to create scripts and automation inside of Cohesion documents

Learn how to use commands to speed up your writing

Clone this wiki locally