-
Notifications
You must be signed in to change notification settings - Fork 1
Scripting
Cohesion allows the use of scripts to create automations inside of documents. With scripts, you can insert text, create files, manipulate blocks, handle input fields, persist data in memory, fetch API data, and more.
Script blocks are created by titling a codeblock as JavaScript or JS. The rendered codeblock will have run icon that executes the script when clicked.
Note
The HTML <script> tag is disabled. The only way to run a script is by clicking the run icon of a JavaScript codeblock.
- UI Tools
- Document Functions
- Memory Functions
- Block Functions
- Field Functions
- Network Functions
- Date/Time Parsers
Shows a toast notification at the top of the screen.
Syntax
ui.notify("Hello World!");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();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)Creates a new document (with confirmation from the user).
Syntax
file.new("Document content here");Example
file.new(`
# Title
This is a paragraph
`);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");Blocks are Markdown structures like:
> [!Note](block-id)
> Example contentThe block object lets you create and manipulate them.
Creates a new block with a type, id, and optional content.
block.make("Hello", "intro-block", "Hello World!");Replaces all content of a block.
block.set("intro-block", "New content");Appends new lines at the end of a block.
block.add("intro-block", "Added at the bottom");Adds new lines at the beginning of a block.
block.addStart("intro-block", "Added at the top");Retrieves the block’s inner content.
Modes: "plain", "plain-with-markers", "raw"
const text = block.getContent("intro-block");
ui.notify(text);Gets the entire block including header.
const blockData = block.get("intro-block");Renames the block’s type.
block.rename("intro-block", "Warning");Gets the block’s current type.
const type = block.getName("intro-block");Deletes the block.
block.remove("intro-block");Fields are HTML inputs inside the document.
field.make.textbox("name", "Alice", 50); /*id, value, max lenght*/
field.make.slider("volume", 30, 0, 100); /*id, value, min, max*/
field.make.spinner("count", 1, 0, 10); /*id, value, min, max*/
field.make.date("birthdate"); /*id*/
field.make.time("alarm"); /*id*/
field.make.select("options", ["A", "B", "C"], "B"); /*id, options, default*/-
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);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);
});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