Appearance
Commands and Menus
Commands are declared in the manifest, so Typora can list them before any code runs, and handled in your main script. Running a command starts main if it isn't running yet.
json
{
"contributes": {
"commands": [
{ "id": "spacing", "title": "Add Spaces Between CJK and Latin", "category": "CJK" }
],
"keyBindings": [{ "command": "spacing", "key": "Mod+Alt+Shift+S" }],
"menus": {
"editor/context": [{ "command": "spacing", "when": "editorHasSelection" }],
"inlinePalette": [{ "command": "spacing" }]
}
},
"main": "main.js"
}js
// main.js
export function activate(ctx) {
ctx.subscriptions.push(
typora.commands.register("spacing", async () => {
const before = await typora.editor.getMarkdown();
const after = addSpacing(before);
if (after !== before) await typora.editor.replaceDocument(after);
else await typora.ui.toast("Nothing to change.");
}),
);
}commands
| Field | Required | Description |
|---|---|---|
id | yes | Local id. Register the handler with the same id: typora.commands.register(id, fn). |
title | yes | Shown in the command palette and menus. |
category | Prefix in the command palette. Defaults to the extension name. |
Every command appears in the command palette. A command with no registered handler shows an error in the extension's log when run.
menus
menus places commands in additional places. Each item is { "command": "<id>", "when"?: "…", "group"?: "…" }, where command is one of your command ids or a Typora command id.
| Place | Where it appears |
|---|---|
editor/context | The editor's right-click menu, in a section after the built-in items. |
inlinePalette | The / menu, in the Extensions group. |
extensions | Reserved for an Extensions menu in the menu bar. Until it ships, these commands are reachable from the command palette. |
when clauses
when limits an editor/context item to a context. Terms can be joined with &&:
| Term | True when |
|---|---|
editorHasSelection | Some text is selected. |
!editorHasSelection | Nothing is selected. |
blockType == 'table' | The caret is inside a block of that type (any block type). |
An unknown term is false, so the item is hidden.
Formatters
A formatter is a command-like contribution registered entirely from main: it receives the document's Markdown and returns new Markdown, applied as one undo step.
js
typora.formatters.register("spacing", addSpacing, { title: "CJK Spacing" });It appears in the command palette as Format Document: CJK Spacing. See Worker API › formatters.
Running Typora commands
typora.commands.execute(id) runs a Typora command by id from main — for example paragraph.insertTable or format.strong.