From a8e3f5c31b9739b7520a85e798cc62a8baa677c4 Mon Sep 17 00:00:00 2001 From: Julie Blanc Date: Thu, 9 Apr 2026 22:15:00 +0200 Subject: [PATCH] change behaviour followingNotes (before next title or next ol) --- .../plugins/followingNotes/followingNotes.js | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/csspageweaver/plugins/followingNotes/followingNotes.js b/csspageweaver/plugins/followingNotes/followingNotes.js index 15b4c17..6dd0fc4 100644 --- a/csspageweaver/plugins/followingNotes/followingNotes.js +++ b/csspageweaver/plugins/followingNotes/followingNotes.js @@ -27,19 +27,38 @@ export default class followingNotes extends Handler { createCallandMarker(content, this.notesClass, newNotesClass); + const containerMap = new Map(); + let notes = content.querySelectorAll(this.notesClass); notes.forEach(function (note) { let paragraph = note.closest("p"); if (!paragraph) return; - let container = paragraph.nextElementSibling; - if (!container || !container.classList.contains("container-following-note")) { - container = document.createElement("div"); - container.classList.add("container-following-note"); - paragraph.after(container); + // Find next boundary: heading or ol[type="1"] + let boundary = null; + let sibling = paragraph.nextElementSibling; + while (sibling) { + if (sibling.matches('h1, h2, h3, h4, h5, h6, ol[type="1"]')) { + boundary = sibling; + break; + } + sibling = sibling.nextElementSibling; } - container.appendChild(note); + const mapKey = boundary || paragraph.parentElement; + + if (!containerMap.has(mapKey)) { + let container = document.createElement("div"); + container.classList.add("container-following-note"); + if (boundary) { + boundary.before(container); + } else { + paragraph.parentElement.appendChild(container); + } + containerMap.set(mapKey, container); + } + + containerMap.get(mapKey).appendChild(note); });