|
// Remove unnecessary (singular) page elements by id |
|
const idsToRemove = [ |
|
'sidebar', |
|
'menu-bar-hover-placeholder', |
|
'menu-bar', |
|
'search-wrapper', |
|
]; |
|
idsToRemove.forEach((selector) => { |
|
const elem = document.getElementById(selector); |
|
if (elem) { |
|
elem.remove(); |
|
} |
|
}); |
|
|
|
// Remove unnecessary anchors from header tag elements |
|
Array.from(document.querySelectorAll(':is(h1,h2,h3,h4,h5,h6) a')).forEach( |
|
(a) => { |
|
const span = document.createElement('span'); |
|
span.innerText = a.innerText; |
|
a.parentNode.replaceChild(span, a); |
|
} |
|
); |
|
// Fix anchor ids on headings |
|
Array.from(document.querySelectorAll('a :is(h1,h2,h3,h4,h5,h6)')).forEach( |
|
(heading) => { |
|
const a = heading.parentNode; |
|
heading.id = a.id; |
|
a.parentNode.replaceChild(heading, a); |
|
} |
|
); |
|
|
|
// Create 'Table of Contents' section |
|
const secondSection = document.getElementsByTagName('h1')[1]; |
|
const tableOfContents = document.createElement('ol'); |
|
secondSection.parentNode.insertBefore(tableOfContents, secondSection); |
|
let lastParent = tableOfContents; |
|
const headings = Array.from(document.querySelectorAll('h1, h2')); |
|
headings.forEach((heading, index) => { |
|
const link = document.createElement('a'); |
|
link.setAttribute('href', `#${heading.id}`); |
|
link.textContent = heading.textContent; |
|
|
|
const li = document.createElement('li'); |
|
li.setAttribute('class', heading.tagName.toLowerCase()); |
|
li.appendChild(link); |
|
|
|
if ( |
|
heading.tagName.toLowerCase() === 'h1' && |
|
headings[index + 1] && |
|
headings[index + 1].tagName.toLowerCase() === 'h1' |
|
) { |
|
tableOfContents.appendChild(li); |
|
} else if (heading.tagName.toLowerCase() === 'h1') { |
|
tableOfContents.appendChild(li); |
|
const subParent = document.createElement('ol'); |
|
tableOfContents.appendChild(subParent); |
|
lastParent = subParent; |
|
} else { |
|
lastParent.appendChild(li); |
|
} |
|
}); |
|
const tocHeader = document.createElement('h1'); |
|
tocHeader.innerText = 'Table of Contents'; |
|
tableOfContents.parentNode.insertBefore(tocHeader, tableOfContents); |
|
|
|
// Remove buttons (i.e. `copy`, `run` on rust playground) |
|
Array.from(document.getElementsByClassName('buttons')).forEach((el) => |
|
el.remove() |
|
); |
|
|
|
// Use absolute URLs instead of relative |
|
Array.from(document.getElementsByTagName('img')).forEach((img) => { |
|
if (img.src.includes('https://rustwasm.github.io/docs/images/')) { |
|
img.src = `https://rustwasm.github.io/docs/book/images/game-of-life/${img.src |
|
.split(/[/ ]+/) |
|
.pop()}`; |
|
return; |
|
} |
|
if (!img.getAttribute('src').startsWith('http')) { |
|
img.src = img.src; |
|
} |
|
}); |
|
|
|
// Move out Ferris from syntax highlighter |
|
Array.from( |
|
document.querySelectorAll('pre .ferris-container img.ferris') |
|
).forEach((ferris) => { |
|
const parentCodeBlock = ferris.closest('pre'); |
|
parentCodeBlock.parentNode.insertBefore(ferris, parentCodeBlock); |
|
}); |
|
|
|
// Flatten code block elements |
|
Array.from(document.querySelectorAll('pre pre')).forEach((pre) => { |
|
pre.parentNode.replaceWith(pre); |
|
}); |
|
|
|
// Remove syntax highlighter |
|
Array.from(document.querySelectorAll('pre code')).forEach((code) => { |
|
const match = /language-([^ ]*)/gi.exec(code.className); |
|
if (match && match[1]) { |
|
code.removeAttribute('class'); |
|
code.parentNode.className = `${match[1] === 'console' ? 'bash' : match[1]}`; |
|
} |
|
|
|
code.innerText = code.textContent; // replace elements by text |
|
}); |
|
|
|
// Remove scripts |
|
Array.from(document.getElementsByTagName('script')).forEach((scripts) => |
|
scripts.remove() |
|
); |
|
|
|
// Unfold details HTML element |
|
Array.from(document.querySelectorAll('details summary')).forEach((summary) => { |
|
summary.remove(); |
|
}); |
|
Array.from(document.getElementsByTagName('details')).forEach((details) => { |
|
details.replaceWith(...details.childNodes); |
|
}); |
|
|
|
// Reset the wrapper |
|
document.getElementById('page-wrapper').classList.remove('page-wrapper'); |
|
document |
|
.getElementsByTagName('main')[0] |
|
.setAttribute('style', 'max-width:none;'); |
|
document |
|
.getElementsByClassName('page')[0] |
|
.setAttribute('style', 'margin-top: 0;'); |