Last active
May 14, 2022 14:53
-
-
Save obedparla/0c8d18741c86be8f68858f4491d37da7 to your computer and use it in GitHub Desktop.
Clean the HTML for the kindle highlights. Make it ready for a blog post with quotation marks and navigation to each header.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open the highlights HTML page on your browser and copy this code in the console of the developer tools | |
// Don't execute part of the script if you want to avoid some of the changes. | |
// Remove the highlights elements | |
const highlights = document.querySelectorAll('.m_noteHeading') | |
for(let highlight of highlights){ | |
highlight.parentNode.removeChild(highlight) | |
} | |
// Transform the sectionHeading into h2 elements | |
const headers = document.querySelectorAll('.m_sectionHeading') | |
for(let header of headers){ | |
const h2 = document.createElement('h2') | |
h2.innerHTML = header.innerHTML | |
h2.className = 'sectionHeading'; | |
header.parentNode.replaceChild(h2, header) | |
} | |
// Transform the div tags with noteText class into blockquote tag | |
const contents = document.querySelectorAll('.m_noteText') | |
for(let content of contents){ | |
const quote = document.createElement('blockquote') | |
quote.innerHTML = content.innerHTML | |
quote.className = 'noteText'; | |
content.parentNode.replaceChild(quote, content) | |
} | |
// Create a navigation linking to each header | |
// It will be added right at the top of the body, place the HTML wherever you wish | |
const h2s = document.querySelectorAll('h2') | |
const menu = document.createElement('nav') | |
menu.setAttribute('aria-label', 'content navigation') | |
document.body.insertBefore(menu, document.body.firstChild); | |
// Add all the a tags pointing to head header to the navigation menu | |
h2s.forEach((header, index) => { | |
const indexN = index+1; | |
header.id = indexN; | |
const p = document.createElement('p'); | |
p.innerHTML = `<a href='#${indexN}'>${indexN}. ${header.innerHTML}</a>`; | |
menu.appendChild(p); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment