Template for insert callout

This template is so much fun. Thanks!

I wanted to use a Template Hotkey (similar to the core Insert callout command) for one specific callout that I was using a lot and skip the prompts. Select the text, type the hotkey, done.

Here are two versions: one that asks for the folded state, and the barebones one that that simply wraps the text you’ve selected. Change the !tbox bit at the bottom to whatever callout you want to use.

Fold or not? → wrap →

<%*
//get selection
noteContent = tp.file.selection();
//return fold
const fold = await tp.system.suggester(['None', 'Expanded', 'Collapsed'], ['', '+', '-'], true, 'Select callout fold option.');
//get array of lines
lines = noteContent.split('\n')
//make a new string with > prepended to each line
let newContent = "";
lines.forEach(l => {
	newContent += '> ' + l + "\n";
})
//remove the last newline character
//inset your desired callout after the !
newContent = newContent.replace(/\n$/, "");
//define callout header
header = ">[!tbox]"+fold + "\n"
// Return the complete callout block
return header + newContent;
%>

Wrap only →

<%*
//get selection
noteContent = tp.file.selection();
//get array of lines
lines = noteContent.split('\n')
//make a new string with > prepended to each line
let newContent = "";
lines.forEach(l => {
	newContent += '> ' + l + "\n";
})
//remove the last newline character
//inset your desired callout after the !
newContent = newContent.replace(/\n$/, "");
//define callout header
header = ">[!tbox]"+ "\n"
// Return the complete callout block
return header + newContent;
%>
1 Like

Hi everyone - I recently released a plugin called Callout Toggles that’s very similar to this template. Feel free to check it out and see if it’s useful for you!

I have a humble request to make - would it be possible for you to kindly consider using the first line of the selected text as the title? In my usual writing process, I tend to formulate the title before delving into the main content

@calmwaves - Callout Toggles will use the first line as the title if the first line is a Markdown heading (e.g. ## My custom title). I had the same thought as you here, it’s often nice to write the title before adding the callout syntax.

Thanks. I simplified it a little:

  1. Includes only the default callout types.
  2. Simply places the cursor after e.g.[!tip] instead of asking for title or fold state.
  3. No emoji overload.
Templater script

Insert_Callout.md (1.1 KB)

<%*
const editor = app.workspace.activeLeaf.view.editor;
const selection = editor.getSelection();
const cursor = editor.getCursor();

const callouts = {
    note: '✏️ note',
    abstract: '📋 abstract / summary / tldr',
    info: 'ℹ info',
    todo: '✔️ todo',
    tip: '🔥 tip / hint / important',
    success: '✔ success / check / done',
    question: '❓ question / help / faq',
    warning: '⚠ warning / caution / attention',
    failure: '❌ failure / fail / missing',
    danger: '⚡ danger / error',
    bug: '🪲 bug',
    example: '📑 example',
    quote: '💬 quote / cite',
};

const type = await tp.system.suggester(
    Object.values(callouts), 
    Object.keys(callouts), 
    true, 
    'Select callout type.'
);

if (selection) {
    const lines = selection.split('\n').map(line => '> ' + line).join('\n');
    const callout = `> [!${type}]`;
    editor.replaceSelection(callout + ' \n' + lines);
    editor.setCursor(cursor.line, callout.length);
} else {
    const callout = `> [!${type}]`;
    editor.replaceRange(callout, cursor);
    editor.setCursor(cursor.line, cursor.ch + callout.length);
}
%>
1 Like