diff --git a/markitect/document_manager.py b/markitect/document_manager.py index 2e4cfbab..4237b19a 100644 --- a/markitect/document_manager.py +++ b/markitect/document_manager.py @@ -748,11 +748,12 @@ class DocumentManager: // Multiple paragraphs - create separate sections const parentNode = section.parentNode; const sectionIndex = section.getAttribute('data-section'); + const nextSibling = section.nextSibling; // Remember position // Remove the original section parentNode.removeChild(section); - // Create separate sections for each paragraph + // Create separate sections for each paragraph and insert at correct position paragraphs.forEach((paragraph, index) => { const wrapper = document.createElement('div'); wrapper.innerHTML = marked.parse(paragraph.trim()); @@ -760,8 +761,8 @@ class DocumentManager: wrapper.setAttribute('data-section', sectionIndex + '_' + index); wrapper.setAttribute('data-edited', 'true'); - // Insert each new section - parentNode.appendChild(wrapper); + // Insert at the correct position (before nextSibling) + parentNode.insertBefore(wrapper, nextSibling); }); } else { // Single content block - create one wrapper