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

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

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -38,20 +38,51 @@
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 +
41 41   function build() {
42 42   var body = document.body;
43 43   if (!body || body.className.indexOf('viewbody') === -1) {
44 44   return; // edit, preview, or anything that is not a rendered page
45 45   }
55 + if (isWikiHome()) { return; }
46 46   var content = document.getElementById('xwikicontent');
47 47   if (!content) { return; }
48 48  
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]); }
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]);
54 54   }
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;
55 55   if (headings.length < MIN_SECTIONS) { return; }
56 56  
57 57   var made = [];
... ... @@ -76,7 +76,7 @@
76 76   // the table-of-contents macro still resolve to something.
77 77   if (h2.id) { details.id = h2.id; h2.removeAttribute('id'); }
78 78  
79 - content.insertBefore(details, h2);
110 + container.insertBefore(details, h2);
80 80  
81 81   // Move the heading and everything up to the next h2 into the body.
82 82   var node = h2;
... ... @@ -95,7 +95,7 @@
95 95   tools.className = 'alder-tools';
96 96   tools.appendChild(button('Expand all', true, made));
97 97   tools.appendChild(button('Collapse all', false, made));
98 - content.insertBefore(tools, made[0]);
129 + container.insertBefore(tools, made[0]);
99 99  
100 100   openFromHash();
101 101   window.addEventListener('hashchange', openFromHash);