From 2b06a3822d40ef4c72691c6ed8cb8a22d2564fa5 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Tue, 29 Nov 2022 12:58:14 -0500 Subject: [PATCH] Fix midword link wrapping issues Adds which is suggested linebreaks to each class reference link in the sidebar once the page is ready. --- _static/js/custom.js | 24 ++++++++++++++++++++++++ conf.py | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/_static/js/custom.js b/_static/js/custom.js index 7e6d51d4d..9fe65df2e 100644 --- a/_static/js/custom.js +++ b/_static/js/custom.js @@ -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](?!$))|(?$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. diff --git a/conf.py b/conf.py index bce06dceb..b08226da8 100644 --- a/conf.py +++ b/conf.py @@ -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'}) ]