When imputing an escaped character in a field in the HTML editor, it is automatically unescaped (unless it one of , <, >, &). The same thing happens when importing with Allow HTML in fields enabled. E.g. ' is changed to be stored as '.
How can I avoid this?
One thing I can do is to input it as &#39; and in javascript replace this with ' so it is correctly displayed, but is there a way to have it mimic the behaviour as with , <, >, &? I do not see why it is being unescaped.
I am accessing the field in javascript e.g. var front = '{{Front}}' and it breaks if any 'appears in the field. Right now I replaced it with ` inside the field, but I also use other characters inside the field such as | and ; as delimiters for lists of variable size that I want to split with.
Or alternatively you could do something like this:
<div id="output"></div>
<div id="data" style="display: none">{{Front}}</div>
<script>
// Get the raw field data
const data = document.getElementById("data").innerHTML;
// Do stuff with the field data
const output = data.split("|").join("<br />");
document.getElementById("output").innerHTML = output;
</script>
What I mean is it does not work if an item itself contains | characters. E.g. if I wanted the string item 1 | item 2 | item 2 extra | item 3
it would not be possible to have this string stored in Anki as it would automatically unescape the | into | which would split item 2 into two separate items.
Because anki changes Hello | "World" | is a | string. from the non-html view to Hello | "World" | is a &#124; string. if you view it as html. Maybe that’s a suitable workaround in your case?
I guess you could escape it in another way e.g. URI encoding:
Template
<div id="output"></div>
<div id="data" style="display: none">{{Front}}</div>
<script>
// Get the raw field data
const data = document.getElementById("data").innerHTML;
// Do stuff with the field data
const output = data.split("|").join("<br />");
document.getElementById("output").innerHTML = decodeURIComponent(output);
</script>
Yeah as you’ll see from my first message, this is also the solution I found with first splitting and then manually replacing with the wanted character, but I was mostly asking to find out why the characters are even escaped and if it could be avoided.