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 lines. Running it on matching code markup removes the markup.

CategoryMarkdown
Action typeJavaScript
ResultReplace or copy text
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. Review the result, then replace the selection or copy the new text.

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 configuration

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

JavaScript definition
function run(selected_text) {
  const block = selected_text.match(/^(`{3,})[^\n]*\n([\s\S]*?)\n?\1[ \t]*$/);
  if (block) return block[2];

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

    const leading = (selected_text.match(/^\s*/) || [""])[0];
    const trailing = (selected_text.match(/\s*$/) || [""])[0];
    const content = selected_text.slice(leading.length, selected_text.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 normalized = selected_text.replace(/\r\n?|\n/g, "\n");
  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;
}