Replacing `<` and `>` to `&lt;` and `&gt;`

What I’m trying to do

I have a custom template that I use to place text with in a <pre> block, especially code snippets. I prefer more control w/ a custom <pre> template as opposed to ``` i.e., I can highlight, italicize, emphasize, etc.

The issue is what I copy templated C++ code, or any code for that matter, that uses < and > the editor view doesn’t render unless I go full on out to Preview Mode. I would like to have a template (or whatever) solution to selecting the code that I am about to place under a block and replace each instance of < > with &lt; &gt;. Right now I am resorting to a Python script that does this separately.

Things I have tried

I believe my question is similar to Templater: replacing " " with "_", but I keep failing in trying to leverage it.

Solution: User Scripts - Templater

function encodeHTML(text) {
  const encodedText = text.replace(/</g, "&lt;").replace(/>/g, "&gt;");
  return encodedText;
}

module.exports = encodeHTML;

NOTE: hello is the script name containing ^^ function.
Create a new template with the following content:

<pre><font face="Source Code Pro, monospace" size="4"><%tp.user.hello(tp.file.selection())%><br></font></pre>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.