<%*
// Simple Link Wrapper
// If URL is selected: [$CursorHere](URL)
// If word is selected: [WORD]($CursorHere)
function isValidUrl(urlString) {
try {
new URL(urlString);
return true;
} catch {
return false;
}
}
let selection = tp.file.selection();
if (selection) {
if (isValidUrl(selection)) {
// Selected text is a URL - create [CURSOR](URL)
tR += `[${tp.file.cursor(1)}](${selection})`;
} else {
// Selected text is a word/phrase - create [WORD](CURSOR)
tR += `[${selection}](${tp.file.cursor(2)})`;
}
} else {
// Nothing selected - create empty link template
tR += `[${tp.file.cursor(1)}]()`;
}
%>
try this