Markdown

Markdown Code

Toggle Markdown code markup around selected text.

OfficialOn-device

Download action

What Markdown Code does

Uses inline backticks for one line and a safe-length fenced code block for multiple standard or rich-text lines. Running it on matching code markup removes the markup.

CategoryMarkdown
Action typeJavaScript
Default resultReplace selected text by default
InternetNot required

Action and outcome

Markdown Code example
ActionOutcome
Markdown CodeSelected text: total += item`total += item`

Use Markdown Code

Follow these steps to toggle Markdown code markup around selected text with ActionClip.

  1. Select the text you want to use.
  2. Choose Markdown Code from ActionClip.
  3. In editable text, ActionClip replaces the selection immediately. In read-only text, it opens the result for review and copying.

Requirements and privacy

  • Before you start: No setup required.
  • Your selected text: Runs locally in ActionClip’s JavaScript sandbox. Selected text does not leave your Mac.

Action script

The script below is included so you can inspect how the action works before downloading it.

JavaScript definition
function run(selected_text) {
  const normalized = selected_text.replace(/\r\n|[\n\r\u000B\u000C\u0085\u2028\u2029]/g, "\n");
  const block = normalized.match(/^(`{3,})[^\n]*\n([\s\S]*?)\n?\1[ \t]*$/);
  if (block) return block[2];

  if (!/[\r\n]/.test(normalized)) {
    const inline = normalized.match(/^(\s*)(`+)([\s\S]*?)\2(\s*)$/);
    if (inline) return inline[1] + inline[3] + inline[4];

    const leading = (normalized.match(/^\s*/) || [""])[0];
    const trailing = (normalized.match(/\s*$/) || [""])[0];
    const content = normalized.slice(leading.length, normalized.length - trailing.length);
    const runs = content.match(/`+/g) || [];
    const fenceLength = Math.max(1, ...runs.map((run) => run.length + 1));
    const fence = "`".repeat(fenceLength);
    const needsPadding = content.startsWith("`") || content.endsWith("`");
    return leading + fence + (needsPadding ? " " : "") + content
      + (needsPadding ? " " : "") + fence + trailing;
  }

  const runs = normalized.match(/`+/g) || [];
  const fenceLength = Math.max(3, ...runs.map((run) => run.length + 1));
  const fence = "`".repeat(fenceLength);
  return fence + "\n" + normalized + (normalized.endsWith("\n") ? "" : "\n") + fence;
}