Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Created May 1, 2018 18:39
Show Gist options
  • Select an option

  • Save NickDeckerDevs/8414d704966947a47de982af8205cfb7 to your computer and use it in GitHub Desktop.

Select an option

Save NickDeckerDevs/8414d704966947a47de982af8205cfb7 to your computer and use it in GitHub Desktop.
add nbsp to the last word of certain elements that you choose.
// target your items to be checked for oprhans,
// avoid header and footer areas to keep script quick
let contentToCheck = [
'.content-wrapper p',
'.content-wrapper h1',
'.content-wrapper h2',
'.content-wrapper h3',
'.content-wrapper h4'
];
for(let i = 0; i < contentToCheck.length; i++) {
let items = document.querySelectorAll(contentToCheck[i]);
for(let j = 0; j < items.length; j++) {
let item = items[j];
let html = item.innerHTML;
// 5 words is when we split
if((html.split(' ').length - 1) > 4) {
item.innerHTML = html.replace(/ ([^ ]*)$/,'&nbsp;$1');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment