Fix midword link wrapping issues

Adds <wbr> which is suggested linebreaks to each class reference link
in the sidebar once the page is ready.
This commit is contained in:
Adam Scott
2022-11-29 12:58:14 -05:00
committed by Yuri Sizov
parent 47506dd4b0
commit 2b06a3822d
2 changed files with 25 additions and 1 deletions

View File

@@ -250,6 +250,30 @@ $(document).ready(() => {
registerOnScrollEvent(mediaQuery);
});
// Add line-break suggestions to class refernece navigation items.
const classReferenceLinks = document.querySelectorAll('.wy-menu-vertical > ul:last-of-type .reference.internal');
for (const linkItem of classReferenceLinks) {
let textNode = null;
linkItem.childNodes.forEach(it => {
if (it.nodeType === Node.TEXT_NODE) {
// If this is a text node and if it needs to be updated, store a reference.
let text = it.textContent;
if (!(text.includes(" ") || text.length < 10)) {
textNode = it;
}
}
});
if (textNode != null) {
let text = textNode.textContent;
// Adds suggested line-breaks, if needed, and replace the original text.
text = text.replace(/((?:(?<=[a-z])[A-Z0-9](?!$))|(?<!^)[A-Z](?=[a-z]))/gm, '<wbr>$1');
linkItem.removeChild(textNode);
linkItem.insertAdjacentHTML('beforeend', text);
}
}
if (inDev) {
// Add a compatibility notice using JavaScript so it doesn't end up in the
// automatically generated `meta description` tag.

View File

@@ -196,7 +196,7 @@ if not on_rtd:
html_css_files.append("css/dev.css")
html_js_files = [
"js/custom.js?2", # Increment the number at the end when the file changes to bust the cache.
"js/custom.js?3", # Increment the number at the end when the file changes to bust the cache.
('https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js', {'defer': 'defer'}),
('js/algolia.js', {'defer': 'defer'})
]