Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitMurf/7c9ad7cd9efb1c00886547556513fc86 to your computer and use it in GitHub Desktop.
Save GitMurf/7c9ad7cd9efb1c00886547556513fc86 to your computer and use it in GitHub Desktop.
<%*
//Set to false if you do NOT want to ask for a custom title or whether foldable or not
const bCustomTitle = true;
const bFoldable = true;
const calloutOptions = ['note', 'abstract', 'summary', 'tldr', 'info', 'todo', 'tip', 'hint', 'important', 'success', 'check', 'done', 'question', 'help', 'faq', 'warning', 'caution', 'attention', 'failure', 'fail', 'missing', 'danger', 'error', 'bug', 'example', 'quote', 'cite'];
let calloutType = await tp.system.suggester(calloutOptions, calloutOptions, false);
if(!calloutType) { return };
calloutType = calloutType.toUpperCase();
const editor = app.workspace.activeLeaf.view.editor;
if(editor.somethingSelected() === false) {
let getCur = editor.getCursor();
let curLineNum = getCur.line;
editor.setSelection({ line: curLineNum, ch: 0 }, { line: curLineNum, ch: editor.getLine(curLineNum).length });
}
let getLine = editor.getSelection();
//Check if empty line above or below to decide whether to add empty lines
let anchorLn = editor.listSelections()[0].anchor.line;
let headLn = editor.listSelections()[0].head.line;
let startLn = anchorLn > headLn ? headLn : anchorLn;
let endLn = anchorLn > headLn ? anchorLn : headLn;
let lineAbove = startLn > 0 ? editor.getLine(startLn - 1) : "";
let lineBelow = endLn < editor.lastLine() ? editor.getLine(endLn + 1) : "";
let foldable = 0;
if(bFoldable) {
if(getLine !== "") {
const foldableOptions = ['*Not foldable', 'Expanded by default', 'Collapsed by default'];
const foldableValues = [0, 1, 2];
//Comment the next line if you don't want to ask about foldable callouts
foldable = await tp.system.suggester(foldableOptions, foldableValues, false);
}
if(foldable === null) { foldable = 0 };
}
let custTitle = "";
if(bCustomTitle) {
custTitle = await tp.system.prompt('Custom Title for your Callout', calloutType);
if(custTitle === null) { custTitle = "" };
}
let splitLines = getLine.split("\n");
let finalStr = "";
let calloutTypeTitle = `>[!${calloutType}]`;
switch(foldable) {
case 1:
calloutTypeTitle = `${calloutTypeTitle}+`;
break
case 2:
calloutTypeTitle = `${calloutTypeTitle}-`;
break;
}
if(custTitle !== "" && custTitle.toUpperCase() !== calloutType.toUpperCase()) {
calloutTypeTitle = `${calloutTypeTitle} ${custTitle}`;
}
let newLine = `${calloutTypeTitle}`;
if(getLine !== "") {
splitLines.forEach(eachLine => {
finalStr = finalStr === "" ? `>${eachLine}` : `${finalStr}\n>${eachLine}`;
});
newLine = `${calloutTypeTitle}\n${finalStr}`;
}
newLine = lineAbove !== "" ? `\n${newLine}` : newLine;
newLine = lineBelow !== "" ? `${newLine}\n` : newLine;
tR = newLine;
%>
@HEmile
Copy link

HEmile commented Mar 15, 2022

This works great, thanks Murf!

@GitMurf
Copy link
Author

GitMurf commented Mar 15, 2022

This works great, thanks Murf!

No problem! Although I didn't realize that the Admonitions plugin already has this type of functionality 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment