From: Thomas Walker Lynch Date: Thu, 14 May 2026 16:53:05 +0000 (+0000) Subject: now breaks certain elements over page boundaries, fixes scroll on refresh X-Git-Url: https://git.reasoningtechnology.com/%28%5B%5E?a=commitdiff_plain;h=80052435d61df6d64592a2c79188718f0bdd0619;p=RT-style-JS_public now breaks certain elements over page boundaries, fixes scroll on refresh --- diff --git a/0pus_Harmony b/0pus_Harmony deleted file mode 100644 index e69de29..0000000 diff --git a/0pus_RT-style-JS_public b/0pus_RT-style-JS_public new file mode 100644 index 0000000..e69de29 diff --git a/consumer/release/RT/element/TOC.js b/consumer/release/RT/element/TOC.js index eb246f5..fb6b10a 100644 --- a/consumer/release/RT/element/TOC.js +++ b/consumer/release/RT/element/TOC.js @@ -10,7 +10,14 @@ Default (No attribute): Context Aware. Looks backwards for the nearest heading H(N). Targets H(N+1). Stops at the next H(N). + +First heading 1 1 + First heading 2 2 + Next heading 2 2 +Next heading 2 3 + */ + window.StyleRT = window.StyleRT || {}; window.StyleRT.TOC = function(){ @@ -20,83 +27,136 @@ window.StyleRT.TOC = function(){ TOC_seq.forEach( (container ,TOC_index) => { container.style.display = 'block'; - // 1. Determine Target Level - const attr_level = parseInt( container.getAttribute('level') ); - let target_level; - - if( !isNaN(attr_level) ){ - // EXPLICIT MODE - target_level = attr_level; - if(debug.log) debug.log('TOC' ,`TOC #${TOC_index} explicit target: H${target_level}`); - } else { - // IMPLICIT / CONTEXT MODE - let context_level = 0; // Default 0 (Root) - let prev = container.previousElementSibling; - while(prev){ - const match = prev.tagName.match(/^H([1-6])$/); - if(match){ - context_level = parseInt( match[1] ); - break; - } - prev = prev.previousElementSibling; - } - target_level = context_level + 1; - if(debug.log) debug.log('TOC' ,`TOC #${TOC_index} context implied target: H${target_level}`); + // 1. Parse attribute: single number N or range A-B + const attr_val = container.getAttribute('level'); + let start_level, end_level; + + if (attr_val) { + const rangeMatch = attr_val.match(/^(\d)-(\d)$/); + if (rangeMatch) { + const a = parseInt(rangeMatch[1]); + const b = parseInt(rangeMatch[2]); + if (a >= 1 && a <= 6 && b >= 1 && b <= 6 && a <= b) { + start_level = a; + end_level = b; + if (debug.log) debug.log('TOC', `TOC #${TOC_index} range: H${a}-H${b}`); + } else { + if (debug.log) debug.log('TOC', `Invalid range "${attr_val}" → implicit mode`); + } + } else { + const single = parseInt(attr_val); + if (!isNaN(single) && single >= 1 && single <= 6) { + start_level = single; + end_level = single; + if (debug.log) debug.log('TOC', `TOC #${TOC_index} single level: H${single}`); + } else { + if (debug.log) debug.log('TOC', `Invalid level "${attr_val}" → implicit mode`); + } + } + } + + // 2. Implicit mode (no attribute or invalid) + if (start_level === undefined || end_level === undefined) { + let context_level = 0; + let prev = container.previousElementSibling; + while (prev) { + const match = prev.tagName.match(/^H([1-6])$/); + if (match) { + context_level = parseInt(match[1]); + break; + } + prev = prev.previousElementSibling; + } + const target_level = Math.min(context_level + 1, 6); + start_level = target_level; + end_level = target_level; + if (debug.log) debug.log('TOC', `TOC #${TOC_index} implicit target: H${target_level}`); } - // Stop condition: Stop if we hit a heading that is a "parent" or "sibling" of the context. - // Mathematically: Stop if found_level < target_level. - const stop_threshold = target_level; + // 3. Collect all matching headings until a higher-level heading stops us + const headings = []; + let next_el = container.nextElementSibling; + while (next_el) { + const match = next_el.tagName.match(/^H([1-6])$/); + if (match) { + const found_level = parseInt(match[1]); + + // Stop if we hit a heading that is a parent of the lowest level we collect + if (found_level < start_level) break; + + // Collect if within the requested range + if (found_level >= start_level && found_level <= end_level) { + // Ensure it has an id + if (!next_el.id) { + next_el.id = `TOC-ref-${TOC_index}-${found_level}-${headings.length}`; + } + headings.push({ el: next_el, level: found_level }); + } + } + next_el = next_el.nextElementSibling; + } - // 2. Setup Container + // 4. Build the container (title + list) container.innerHTML = ''; const title = document.createElement('h1'); - // Title logic: If targeting H1, the element serves as a Main TOC. Otherwise the element serves as a Section TOC. - title.textContent = target_level === 1 ? 'Table of Contents' : 'Section Contents'; + title.textContent = start_level === 1 ? 'Table of Contents' : 'Section Contents'; title.style.textAlign = 'center'; container.appendChild(title); - const list = document.createElement('ul'); - list.style.listStyle = 'none'; - list.style.paddingLeft = '0'; - container.appendChild(list); + if (headings.length === 0) return; // nothing to show - // 3. Scan Forward - let next_el = container.nextElementSibling; - while(next_el){ - const match = next_el.tagName.match(/^H([1-6])$/); - if(match){ - const found_level = parseInt( match[1] ); + // Top-level list + const topList = document.createElement('ul'); + topList.style.listStyle = 'none'; + topList.style.paddingLeft = '0'; + container.appendChild(topList); + + // Stack of