Skip to content

Instantly share code, notes, and snippets.

@pickleat
Created May 22, 2020 00:57
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 pickleat/e66075084ac0f5716ac7d2a91a550287 to your computer and use it in GitHub Desktop.
Save pickleat/e66075084ac0f5716ac7d2a91a550287 to your computer and use it in GitHub Desktop.
A more elegant solution to formatting
useEffect( () => {
// formats the Markdown
function styleChildren(children) {
children.forEach((child) => {
let tagIt = child.tagName
child.classList = blogStyle[tagIt]
if(tagIt === 'CODE' && child.innerText.length > 15){child.classList = longCode[tagIt]}
if(tagIt === 'CODE' && child.innerText.length <= 15){child.classList = shortCode[tagIt]}
if(child.children) {
let grandChildren = [...child.children]
styleChildren(grandChildren)
}
})
}
let post = document.getElementById('post-data')
let postChildren = [...post.children]
styleChildren(postChildren)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment