Skip to content

Instantly share code, notes, and snippets.

@k1000
Last active March 1, 2019 18:08
Show Gist options
  • Save k1000/cb04734ef423ddab85a58c67dc0b85c9 to your computer and use it in GitHub Desktop.
Save k1000/cb04734ef423ddab85a58c67dc0b85c9 to your computer and use it in GitHub Desktop.
// remove hard spaces
let bodyHTML = document.body.innerHTML;
bodyHTML = bodyHTML.replace(/ /gim, ' ');
bodyHTML = bodyHTML.replace(/\u00a0/gim, ' '); //  
bodyHTML = bodyHTML.replace(' ', ' ');
bodyHTML = bodyHTML.replace(/<[^/>]+>[ \n\r\t]*<\/[^>]+>/gim, ''); // remove empry tags
bodyHTML = bodyHTML.replace(/,([^ ])/gim, ', $1'); // fixes commas not followed by space
//bodyHTML = bodyHTML.replace(/”([^ ])/gim, '” $1'); // fixes not followed by space
//bodyHTML = bodyHTML.replace(/\.([^ <\d])/gim, '\. $1'); // fixes dot stops not followed by space
//bodyHTML = bodyHTML.replace(/\:([^ ])/gim, '\: $1');
//bodyHTML = bodyHTML.replace(/\;([^ ])/gim, '\; $1');
document.body.innerHTML = bodyHTML;
const linker = (accumulator, link) => accumulator + link.outerHTML;
function replaceTags(query, newTag, exeptions, strip) {
let newEle = null;
let eleHTML = '';
let links = [];
let linksHTML = '';
let newDom = null;
let [tag, tagClass] = newTag.split('.')
let elements = document.querySelectorAll(query)
for (ele of elements) {
newEle = document.createElement(tag);
if (ele.id !== '') {
newEle.id = ele.id;
}
if (tagClass !== undefined) {
newEle.className = tagClass
}
if (strip !== undefined) {
newEle.innerHTML = ele.innerHTML
} else {
eleHTML = ele.innerHTML
links = Array.from(ele.querySelectorAll(exeptions))
if (links.length == 0) {
linksHTML = ''
} else {
linksHTML = links.reduce(linker)
}
newEle.innerHTML = ele.textContent + linksHTML
}
if (newEle.innerHTML == '') {
newDom = ele.parentNode.replaceChild(document.createTextNode(" "), ele);
//newDom = ele.parentNode.removeChild( ele);
} else {
newDom = ele.parentNode.replaceChild(newEle, ele);
}
}
}
let replace = [
['p.Tittle', 'h2', 'a'],
['p.Podtyt-', 'h3.subtitle', 'a'],
['span.char-style-override-14', 'em'],
['p.Podtyt-2', 'h3', 'a'],
['p.podtyt-3', 'h4', 'a'],
['p.podtyt4', 'h5', 'a'],
['p.tekst2', 'p.colofon', false],
['p.Normal', 'p', '', false],
['p.tekst', 'p', '', false],
['p.tekst1', 'p.opening', '', false],
['p.tibetan', 'p.tibetan', 'a', false]
]
for (toReplace of replace)
replaceTags(...toReplace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment