Overview
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.
| Category | Markdown |
|---|---|
| Action type | JavaScript |
| Result | Replace or copy text |
| Internet | Not required |
Example
Action and outcome
| Action | Outcome |
|---|---|
| Markdown CodeSelected text: total += item | `total += item` |
Workflow
Use Markdown Code
Follow these steps to toggle Markdown code markup around selected text with ActionClip.
- Select the text you want to use.
- Choose Markdown Code from ActionClip.
- Review the result, then replace the selection or copy the new text.
Before you add it
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.
Inspect before installing
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;
}