Skip to content

Instantly share code, notes, and snippets.

@Gro-Tsen
Created April 2, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gro-Tsen/9d613d232abe7b7cac2f97251b3a2a33 to your computer and use it in GitHub Desktop.
Save Gro-Tsen/9d613d232abe7b7cac2f97251b3a2a33 to your computer and use it in GitHub Desktop.
// JavaScript functions to replace "th" by the letter thorn throughout
// a document. Use this with `thornifyDoc(window.document)`.
// Written by David A. Madore, 2022-04-02. Public Domain.
function thornifyString(s) {
return s.replace(/T[Hh]/g, "\u00DE").replace(/th/g, "\u00FE");
}
function thornifyDoc(doc) {
var body = doc.body;
var walker = doc.createTreeWalker(body, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false);
var node = walker.nextNode();
while ( node ) {
if ( node.nodeType == Node.TEXT_NODE ) {
var thisnode = node;
node = walker.nextNode();
var str = thornifyString(thisnode.nodeValue);
thisnode.parentNode.replaceChild(doc.createTextNode(str), thisnode);
} else if ( node.nodeType == Node.ELEMENT_NODE
&& ( node.localName == "pre"
|| node.localName == "code"
|| node.localName == "script"
|| node.localName == "style" ) ) {
do {
node = walker.nextSibling();
if ( node )
break;
node = walker.parentNode();
if ( ! node )
break;
} while ( true );
} else
node = walker.nextNode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment