Updated 10112022-134435
https://davidblue.wtf/htmlcomment
when I say I published this @draftsapp action... I literally just duplicated @sylumer's Insert Highlight action and changed the text in two action steps...
already finding it quite handy though! https://directory.getdrafts.com/a/2Dj
hey Drafts users! give this a whirl for me before I list it publicly, would ya? https://t.co/UOmODGtkFP
— 𝗗 𝗔 𝗩 𝗢 𝗗 (@NeoYokel) October 10, 2022
hey Drafts users! give this a whirl for me before I list it publicly, would ya? https://directory.getdrafts.com/a/2Dj
Before I list it publicly, I'd love it if any of y'all get a chance to try this HTML Comment Action.
Wraps selected text in HTML comment tags (''.) If no text is selected, the cursor is placed between the same.
Wraps selected text in HTML comment tags (''.) If no text is selected, the cursor is placed between the same.
This action was made possible by @sylumer's Insert Highlight Action. You'll note, in fact - at least as of initial release - it includes his Script step, entirely unmodified (including the comments!)
// Set up variables for prefix and suffix
let strPrefix;
let strSuffix;
if(draft.getTemplateTag("WRAP") == "")
\{
// No WRAP tag defined, so we must use PREFIX and SUFFIX
// One of both of them may not be defined in which case the prefix
// and/or suffix will be an empty string, allowing just for prefixes
// or just for suffixes.
strPrefix = draft.getTemplateTag("PREFIX");
strSuffix = draft.getTemplateTag("SUFFIX");
}
else
\{
// Wrap tag is defined, so prefix and suffix are identical
strPrefix = draft.getTemplateTag("WRAP");
strSuffix = strPrefix;
}
// Get selection range details from editor
let rngSelected = editor.getSelectedRange();
// Check if we are adding or undoing the insertion
// If the text before and after is the prefix/suffix, extend the selection
let strBeforeSelection = editor.getTextInRange(rngSelected\[0] - strPrefix.length, strPrefix.length);
let strAfterSelection = editor.getTextInRange(rngSelected\[0] + rngSelected\[1], strSuffix.length);
if(strBeforeSelection == strPrefix && strAfterSelection == strSuffix)
\{
// Extend the range and update thje variable for the selection range
editor.setSelectedRange(rngSelected\[0] - strPrefix.length, strPrefix.length + rngSelected\[1] + strSuffix.length);
rngSelected = editor.getSelectedRange();
}
// Get selection content
let strSelection = editor.getSelectedText();
if(strSelection.startsWith(strPrefix) && strSelection.endsWith(strSuffix))
\{
// Remove the selection and position the cursor
editor.setSelectedText(strSelection.substring(strPrefix.length, strSelection.length - strSuffix.length ));
rngSelected = editor.getSelectedRange();
editor.setSelectedRange(rngSelected\[0] + rngSelected\[1], 0);
}
else
\{
//Add the selection
// Replace the selection and position the cursor.
if (!strSelection \|| strSelection.length == 0)
\{
editor.setSelectedText(strPrefix + strSuffix);
editor.setSelectedRange(rngSelected\[0] + strPrefix.length, 0);
}
else
\{
editor.setSelectedText(strPrefix + strSelection + strSuffix);
editor.setSelectedRange(rngSelected\[0] + rngSelected\[1] + strPrefix.length + strSuffix.length, 0);
}
}
// Activate the editor for the selection
editor.activate();
drafts://action?data=%7B%22notificationType%22:1,%22disposition%22:0,%22assignTags%22:%5B%5D,%22icon%22:%22chat-alert%22,%22groupDisposition%22:0,%22uuid%22:%221515523E-5618-4FCD-9530-80ACF9B78000%22,%22visibility%22:480,%22name%22:%22HTML%20Comment%22,%22groupUUID%22:%22896E9936-31AB-4722-A67D-AA578EABE763%22,%22tintColor%22:%22gray%22,%22backingPlatforms%22:3,%22keyCommand%22:%7B%22optionKey%22:true,%22input%22:%22C%22,%22controlKey%22:false,%22commandKey%22:false,%22type%22:%22action%22,%22discoverabilityTitle%22:%22HTML%20Comment%22,%22shiftKey%22:true%7D,%22keyUseIcon%22:true,%22backingIsSeparator%22:false,%22shortName%22:%22%22,%22backingActionType%22:%22action%22,%22actionDescription%22:%22Wraps%20selected%20text%20in%20HTML%20comment%20tags%20('~%3C!--%20%20--%3E~'.)%20If%20no%20text%20is%20selected,%20the%20cursor%20is%20placed%20between%20the%20same.%22,%22logLevel%22:1,%22steps%22:%5B%7B%22platforms%22:3,%22data%22:%7B%22template%22:%22%3C!--%20%22,%22name%22:%22PREFIX%22,%22templateType%22:%22legacy%22%7D,%22type%22:%22defineTemplateTag%22,%22isEnabled%22:true,%22uuid%22:%22C6E7C648-31AB-4F4F-A571-13ACBCAB6EE1%22%7D,%7B%22platforms%22:3,%22data%22:%7B%22name%22:%22SUFFIX%22,%22template%22:%22%20--%3E%22,%22templateType%22:%22legacy%22%7D,%22type%22:%22defineTemplateTag%22,%22isEnabled%22:true,%22uuid%22:%2261E46E30-1FE1-4F2C-B0AF-AD11D6C63E05%22%7D,%7B%22platforms%22:3,%22data%22:%7B%22script%22:%22%5C/%5C/%20Set%20up%20variables%20for%20prefix%20and%20suffix%5Cnlet%20strPrefix;%5Cnlet%20strSuffix;%5Cn%5Cnif(draft.getTemplateTag(%5C%22WRAP%5C%22)%20%3D%3D%20%5C%22%5C%22)%5Cn%7B%5Cn%5Ct%5C/%5C/%20No%20WRAP%20tag%20defined,%20so%20we%20must%20use%20PREFIX%20and%20SUFFIX%5Cn%5Ct%5C/%5C/%20One%20of%20both%20of%20them%20may%20not%20be%20defined%20in%20which%20case%20the%20prefix%20%5Cn%5Ct%5C/%5C/%20and%5C/or%20suffix%20will%20be%20an%20empty%20string,%20allowing%20just%20for%20prefixes%5Cn%5Ct%5C/%5C/%20or%20just%20for%20suffixes.%5Cn%5CtstrPrefix%20%3D%20draft.getTemplateTag(%5C%22PREFIX%5C%22);%5Cn%5CtstrSuffix%20%3D%20draft.getTemplateTag(%5C%22SUFFIX%5C%22);%5Cn%7D%5Cnelse%5Cn%7B%5Cn%5Ct%5C/%5C/%20Wrap%20tag%20is%20defined,%20so%20prefix%20and%20suffix%20are%20identical%5Cn%5CtstrPrefix%20%3D%20draft.getTemplateTag(%5C%22WRAP%5C%22);%5Cn%5CtstrSuffix%20%3D%20strPrefix;%5Cn%7D%5Cn%5Cn%5C/%5C/%20Get%20selection%20range%20details%20from%20editor%5Cnlet%20rngSelected%20%3D%20editor.getSelectedRange();%5Cn%5Cn%5C/%5C/%20Check%20if%20we%20are%20adding%20or%20undoing%20the%20insertion%5Cn%5C/%5C/%20If%20the%20text%20before%20and%20after%20is%20the%20prefix%5C/suffix,%20extend%20the%20selection%5Cnlet%20strBeforeSelection%20%3D%20editor.getTextInRange(rngSelected%5B0%5D%20-%20strPrefix.length,%20strPrefix.length);%5Cnlet%20strAfterSelection%20%3D%20editor.getTextInRange(rngSelected%5B0%5D%20+%20rngSelected%5B1%5D,%20strSuffix.length);%5Cnif(strBeforeSelection%20%3D%3D%20strPrefix%20%26%26%20strAfterSelection%20%3D%3D%20strSuffix)%5Cn%7B%5Cn%5Ct%5C/%5C/%20Extend%20the%20range%20and%20update%20thje%20variable%20for%20the%20selection%20range%5Cn%5Cteditor.setSelectedRange(rngSelected%5B0%5D%20-%20strPrefix.length,%20strPrefix.length%20+%20rngSelected%5B1%5D%20+%20strSuffix.length);%5Cn%5CtrngSelected%20%3D%20editor.getSelectedRange();%5Cn%7D%5Cn%5Cn%5C/%5C/%20Get%20selection%20content%5Cnlet%20strSelection%20%3D%20editor.getSelectedText();%5Cn%5Cnif(strSelection.startsWith(strPrefix)%20%26%26%20strSelection.endsWith(strSuffix))%5Cn%7B%5Cn%5Ct%5C/%5C/%20Remove%20the%20selection%20and%20position%20the%20cursor%5Cn%5Cteditor.setSelectedText(strSelection.substring(strPrefix.length,%20strSelection.length%20-%20strSuffix.length%20));%5Cn%5CtrngSelected%20%3D%20editor.getSelectedRange();%5Cn%5Cteditor.setSelectedRange(rngSelected%5B0%5D%20+%20rngSelected%5B1%5D,%200);%5Cn%7D%5Cnelse%5Cn%7B%5Cn%5Ct%5C/%5C/Add%20the%20selection%5Cn%5Cn%5Ct%5C/%5C/%20Replace%20the%20selection%20and%20position%20the%20cursor.%5Cn%5Ctif%20(!strSelection%20%7C%7C%20strSelection.length%20%3D%3D%200)%5Cn%5Ct%7B%5Cn%5Ct%5Cteditor.setSelectedText(strPrefix%20+%20strSuffix);%5Cn%5Ct%5Cteditor.setSelectedRange(rngSelected%5B0%5D%20+%20strPrefix.length,%200);%5Cn%5Ct%7D%5Cn%5Ctelse%5Cn%5Ct%7B%5Cn%5Ct%5Cteditor.setSelectedText(strPrefix%20+%20strSelection%20+%20strSuffix);%5Cn%5Ct%5Cteditor.setSelectedRange(rngSelected%5B0%5D%20+%20rngSelected%5B1%5D%20+%20strPrefix.length%20+%20strSuffix.length,%200);%5Cn%5Ct%7D%5Cn%7D%5Cn%5Cn%5C/%5C/%20Activate%20the%20editor%20for%20the%20selection%5Cneditor.activate();%5Cn%22,%22allowAsync%22:%22false%22%7D,%22type%22:%22script%22,%22isEnabled%22:true,%22uuid%22:%22A10FE2EF-5573-4A1B-A173-8E6FC05C00E7%22%7D%5D,%22shouldConfirm%22:false%7D