joelniklaus HF Staff Cursor commited on
Commit
305f4f3
·
1 Parent(s): 1834e40

fix footer to merge references and footnotes from all chapters into one list

Browse files
Files changed (1) hide show
  1. app/src/components/Footer.astro +74 -16
app/src/components/Footer.astro CHANGED
@@ -129,36 +129,94 @@ const { citationText, bibtex, licence, doi } = Astro.props as Props;
129
  // Prevent multiple runs
130
  if (footer.dataset.processed === "true") return false;
131
 
132
- const findFirstOutsideFooter = (selectors) => {
133
- // Try contentRoot first, then document.body
134
- const searchRoots = [contentRoot, document.body].filter(Boolean);
 
 
 
 
 
135
 
 
 
 
 
 
136
  for (const root of searchRoots) {
137
  for (const sel of selectors) {
138
- const el = root.querySelector(sel);
139
- if (el && !footer.contains(el)) return el;
 
140
  }
141
  }
142
- return null;
 
 
 
143
  };
144
 
145
- const referencesEl = findFirstOutsideFooter([
146
- "#bibliography-references-list",
147
- "[data-bibliography-block]",
148
- "#references",
149
- "#refs",
150
- ".references",
151
- ".bibliography",
152
- ]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
- // Try multiple selectors for footnotes
155
- const footnotesEl = findFirstOutsideFooter([
156
  "[data-built-footnotes]",
157
  ".footnotes",
158
  "section.footnotes",
159
  "div.footnotes",
160
  ]);
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  const movedRefs = moveIntoFooter(referencesEl, "References");
163
  const movedNotes = moveIntoFooter(footnotesEl, "Footnotes");
164
 
 
129
  // Prevent multiple runs
130
  if (footer.dataset.processed === "true") return false;
131
 
132
+ const refSelectors = [
133
+ "#bibliography-references-list",
134
+ "[data-bibliography-block]",
135
+ "#references",
136
+ "#refs",
137
+ ".references",
138
+ ".bibliography",
139
+ ];
140
 
141
+ // Find ALL top-level reference/footnote sections (each chapter generates its own).
142
+ // Filters out descendants to avoid matching both a container and its inner list.
143
+ const findAllOutsideFooter = (selectors) => {
144
+ const found = [];
145
+ const searchRoots = [contentRoot, document.body].filter(Boolean);
146
  for (const root of searchRoots) {
147
  for (const sel of selectors) {
148
+ root.querySelectorAll(sel).forEach((el) => {
149
+ if (!footer.contains(el) && !found.includes(el)) found.push(el);
150
+ });
151
  }
152
  }
153
+ // Remove any element that is a descendant of another element in the list
154
+ return found.filter(
155
+ (el) => !found.some((other) => other !== el && other.contains(el)),
156
+ );
157
  };
158
 
159
+ const allRefsEls = findAllOutsideFooter(refSelectors);
160
+
161
+ // Merge all reference sections into the first one, deduplicating by id
162
+ let referencesEl = null;
163
+ if (allRefsEls.length > 0) {
164
+ referencesEl = allRefsEls[0];
165
+ if (allRefsEls.length > 1) {
166
+ // Collect existing ids from the first section
167
+ const seenIds = new Set();
168
+ referencesEl.querySelectorAll("li[id]").forEach((li) => seenIds.add(li.id));
169
+ // Find or create the <ol> in the first section
170
+ let targetOl = referencesEl.querySelector("ol");
171
+ if (!targetOl) {
172
+ targetOl = document.createElement("ol");
173
+ targetOl.className = "references";
174
+ referencesEl.appendChild(targetOl);
175
+ }
176
+ // Merge entries from remaining sections
177
+ for (let i = 1; i < allRefsEls.length; i++) {
178
+ allRefsEls[i].querySelectorAll("li").forEach((li) => {
179
+ if (!li.id || !seenIds.has(li.id)) {
180
+ if (li.id) seenIds.add(li.id);
181
+ targetOl.appendChild(li);
182
+ }
183
+ });
184
+ allRefsEls[i].remove();
185
+ }
186
+ }
187
+ }
188
 
189
+ // Merge all footnote sections the same way
190
+ const allFootnoteEls = findAllOutsideFooter([
191
  "[data-built-footnotes]",
192
  ".footnotes",
193
  "section.footnotes",
194
  "div.footnotes",
195
  ]);
196
 
197
+ let footnotesEl = null;
198
+ if (allFootnoteEls.length > 0) {
199
+ footnotesEl = allFootnoteEls[0];
200
+ if (allFootnoteEls.length > 1) {
201
+ const seenIds = new Set();
202
+ footnotesEl.querySelectorAll("li[id]").forEach((li) => seenIds.add(li.id));
203
+ let targetOl = footnotesEl.querySelector("ol");
204
+ if (!targetOl) {
205
+ targetOl = document.createElement("ol");
206
+ footnotesEl.appendChild(targetOl);
207
+ }
208
+ for (let i = 1; i < allFootnoteEls.length; i++) {
209
+ allFootnoteEls[i].querySelectorAll("li").forEach((li) => {
210
+ if (!li.id || !seenIds.has(li.id)) {
211
+ if (li.id) seenIds.add(li.id);
212
+ targetOl.appendChild(li);
213
+ }
214
+ });
215
+ allFootnoteEls[i].remove();
216
+ }
217
+ }
218
+ }
219
+
220
  const movedRefs = moveIntoFooter(referencesEl, "References");
221
  const movedNotes = moveIntoFooter(footnotesEl, "Footnotes");
222