Last modified by wiki admin on 2026/08/26 01:04

From version 7.1
edited by wiki admin
on 2026/08/24 22:31
Change comment: There is no comment for this version
To version 5.1
edited by wiki admin
on 2026/08/24 22:13
Change comment: There is no comment for this version

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -38,51 +38,20 @@
38 38   return svg;
39 39   }
40 40  
41 - function isWikiHome() {
42 - // The employees home page has several H2s (departments, article status,
43 - // publishing workflow). Collapsing a landing page into rows hides the
44 - // thing people came for, so skip it. Matches both URL schemes this
45 - // deployment serves: /bin/view/Main/ and /wiki/<name>/view/Main/.
46 - var path = (window.location.pathname || '').replace(/\/+$/, '');
47 - return /\/view\/Main(\/WebHome)?$/.test(path);
48 - }
49 -
50 50   function build() {
51 51   var body = document.body;
52 52   if (!body || body.className.indexOf('viewbody') === -1) {
53 53   return; // edit, preview, or anything that is not a rendered page
54 54   }
55 - if (isWikiHome()) { return; }
56 56   var content = document.getElementById('xwikicontent');
57 57   if (!content) { return; }
58 58  
59 - // Find the element the section headings actually live in. XWiki wraps
60 - // rendered content in its own container (.f-accordion-panel on this
61 - // instance), so the headings are NOT direct children of #xwikicontent --
62 - // an earlier version of this script looked only at content.children, found
63 - // nothing on every real article, and silently did nothing. The Preview
64 - // page hid that, because hand-written wiki markup does put headings at the
65 - // top level.
66 - var found = content.querySelectorAll('h2');
67 - if (found.length < MIN_SECTIONS) { return; }
68 -
69 - // Group headings that share a parent, using the parent holding the most of
70 - // them. Taking every h2 regardless of depth would tear headings out of
71 - // tables and macro boxes they belong to.
72 - var byParent = [];
73 - for (var f = 0; f < found.length; f++) {
74 - var parent = found[f].parentNode;
75 - var slot = null;
76 - for (var b = 0; b < byParent.length; b++) {
77 - if (byParent[b].parent === parent) { slot = byParent[b]; break; }
78 - }
79 - if (!slot) { slot = { parent: parent, items: [] }; byParent.push(slot); }
80 - slot.items.push(found[f]);
49 + // Only top-level headings. An h2 nested inside a table or a macro box is
50 + // part of that structure and must not be torn out of it.
51 + var headings = [];
52 + for (var i = 0; i < content.children.length; i++) {
53 + if (content.children[i].tagName === 'H2') { headings.push(content.children[i]); }
81 81   }
82 - byParent.sort(function (a, b) { return b.items.length - a.items.length; });
83 -
84 - var container = byParent[0].parent;
85 - var headings = byParent[0].items;
86 86   if (headings.length < MIN_SECTIONS) { return; }
87 87  
88 88   var made = [];
... ... @@ -107,7 +107,7 @@
107 107   // the table-of-contents macro still resolve to something.
108 108   if (h2.id) { details.id = h2.id; h2.removeAttribute('id'); }
109 109  
110 - container.insertBefore(details, h2);
79 + content.insertBefore(details, h2);
111 111  
112 112   // Move the heading and everything up to the next h2 into the body.
113 113   var node = h2;
... ... @@ -126,7 +126,7 @@
126 126   tools.className = 'alder-tools';
127 127   tools.appendChild(button('Expand all', true, made));
128 128   tools.appendChild(button('Collapse all', false, made));
129 - container.insertBefore(tools, made[0]);
98 + content.insertBefore(tools, made[0]);
130 130  
131 131   openFromHash();
132 132   window.addEventListener('hashchange', openFromHash);