Skip to content

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 ​

FieldRequiredDescription
idyesLocal id. Register the handler with the same id: typora.commands.register(id, fn).
titleyesShown in the command palette and menus.
categoryPrefix 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 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.

PlaceWhere it appears
editor/contextThe editor's right-click menu, in a section after the built-in items.
inlinePaletteThe / menu, in the Extensions group.
extensionsReserved 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 &&:

TermTrue when
editorHasSelectionSome text is selected.
!editorHasSelectionNothing 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.

Theme adapted from Nimbus (MIT).