ソースを参照

Fix midword link wrapping issues

Adds <wbr> which is suggested linebreaks to each class reference link
in the sidebar once the page is ready.
Adam Scott 2 年 前
コミット
2b06a3822d
2 ファイル変更25 行追加1 行削除
  1. 24 0
      _static/js/custom.js
  2. 1 1
      conf.py

+ 24 - 0
_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](?!$))|(?<!^)[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.

+ 1 - 1
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'})
 ]