as outlined in this post I’m using the following code snippet provided by hkr to convert an HTML fragment to plain text:
// convert an HTML fragment to plain text
var tempDiv = document.createElement('div');
tempDiv.innerHTML = '{{example}}';
document.documentElement.appendChild(tempDiv);
var singleColumnCSV = tempDiv.innerText;
document.documentElement.removeChild(tempDiv);
This conversion removes all HTML tags.
But I would like to preserve certain HTML tags.
For example the tags <b> which I’m using to highlight stressed syllables.
Is there a way to preserve certain html tags <b>?
ssnoyes from the Anki subreddit kindly provided the solution:
tempDiv.innerHTML = '{{example}}'
tempDiv.innerHTML = tempdiv.innerHTML.replace(/(\<b>)(.*?)(\<\/b>)/g, "<b>$2</b>");
var singleColumnCSV = tempDiv.innerText
singleColumnCSV = singleColumnCSV.replace(/<b>/g, "<b>").replace(/</b>/g, "</b>");
If you want to preserve more tags, just add this after the 2nd line:
tempDiv.innerHTML = tempDiv.innerHTML.replace(/(\<TAG>)(.*?)(\<\/TAG>)/g, "<TAG>$2</TAG>");