Actually I found that Harlowe does let me call out to JavaScript, except that you can't pass a variable. Instead you need to use text: to force it into a string. Then the JavaScript code can do the paranoid checking for backticks and additionally add spaces at the front in back just in case the string starts or ends with a backtick.
(set: $quoted to harloweVerbatim((text: $userInput)))
function harloweVerbatim(str) {
if (str === undefined || str == "") return "";
var n = str.length;
var count = 0;
var max = 0;
var delim = "`";
for (var i = 0; i < n; ++i) {
var ch = str.charAt(i);
if (ch == "`") {
if (++count > max) {
++max;
delim += "`";
}
} else {
count = 0;
}
}
var pre = "";
var post = "";
if (str.charAt(0) == "`") pre = " ";
if (str.charAt(n-1) == "`") post = " ";
return delim + pre + str + post + delim;
}
window.harloweVerbatim = harloweVerbatim;