Update Core.js

This commit is contained in:
simonpipe 2026-07-09 23:21:48 +02:00
parent f444ae5735
commit 0428c0d37f

23
Core.js
View file

@ -34,7 +34,10 @@ class SimonUp {
} }
} }
// Phase 1: Blöcke und Inline-Strukturen parsen (sammelt auch alle Überschriften)
html = this._parseBlocks(html); html = this._parseBlocks(html);
// Phase 2: Das Inhaltsverzeichnis nachträglich in die vorbereitete Box einsetzen
html = this._injectTOC(html); html = this._injectTOC(html);
if (this.plugins && this.plugins.length > 0) { if (this.plugins && this.plugins.length > 0) {
@ -126,6 +129,7 @@ class SimonUp {
const intro = lines.map(function(l) { return self._parseInline(l); }).join('<br>'); const intro = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
tocWrapper += `<p class="toc-intro">${intro}</p>\n`; tocWrapper += `<p class="toc-intro">${intro}</p>\n`;
} }
// HIER IST JETZT DER ECHTE PLATZHALTER:
tocWrapper += '\n'; tocWrapper += '\n';
tocWrapper += '</div>'; tocWrapper += '</div>';
return tocWrapper; return tocWrapper;
@ -270,7 +274,7 @@ class SimonUp {
const numStr = self._getAutoNumber(level); const numStr = self._getAutoNumber(level);
const fullTitle = `${numStr} ${title}`; const fullTitle = `${numStr} ${title}`;
const id = self._slugify(fullTitle); const id = self._slugify(fullTitle);
self.headings.push({ level: level, text: fullTitle, id: id }); self.headings.push({ level: level, text: title, id: id }); // Speichert nur den puren Titel ohne Nummer für das TOC Link-Markup
return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`; return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`;
}); });
@ -331,19 +335,20 @@ class SimonUp {
* INTERN: Erzeugt die fertige HTML-Liste des TOC und injiziert sie in den Block * INTERN: Erzeugt die fertige HTML-Liste des TOC und injiziert sie in den Block
*/ */
_injectTOC(html) { _injectTOC(html) {
// HIER STEHT JETZT DEFINITIV DAS RECHTE TOKEN:
const placeholder = ''; const placeholder = '';
if (!html.includes(placeholder)) { if (!html.includes(placeholder)) {
return html; return html;
} }
if (this.headings.length === 0) { if (this.headings.length === 0) {
return html.replace(placeholder, ''); return html.replace(placeholder, '');
} }
let tocListHtml = '<div class="simonup-toc">\n<ul>\n'; let tocListHtml = '<div class="simonup-toc">\n<ul>\n';
let currentLevel = 1; let currentLevel = 1;
for (let i = 0; i < this.headings.length; i++) { for (let i = 0; i < this.headings.length; i++) {
let heading = this.headings[i]; let heading = this.headings[i];
if (heading.level > currentLevel) { if (heading.level > currentLevel) {
@ -356,13 +361,13 @@ class SimonUp {
currentLevel = heading.level; currentLevel = heading.level;
tocListHtml += `<li><a href="#${heading.id}">${heading.text}</a></li>\n`; tocListHtml += `<li><a href="#${heading.id}">${heading.text}</a></li>\n`;
} }
if (currentLevel > 1) { if (currentLevel > 1) {
let diffFinal = currentLevel - 1; let diffFinal = currentLevel - 1;
tocListHtml += '</ul>\n'.repeat(diffFinal); tocListHtml += '</ul>\n'.repeat(diffFinal);
} }
tocListHtml += '</ul>\n</div>'; tocListHtml += '</ul>\n</div>';
return html.replace(placeholder, tocListHtml); return html.replace(placeholder, tocListHtml);
} }
@ -395,7 +400,7 @@ class SimonUp {
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
let element = elements[i]; let element = elements[i];
if (!element) continue; if (!element) continue;
let isUlItem = element.startsWith("<ul-item>"); let isUlItem = element.startsWith("<ul-item>");
let isOlItem = element.startsWith("<ol-item>"); let isOlItem = element.startsWith("<ol-item>");
@ -403,7 +408,7 @@ class SimonUp {
flushParagraph(); flushParagraph();
let type = isUlItem ? "ul" : "ol"; let type = isUlItem ? "ul" : "ol";
let content = isUlItem ? element.replace(/<\/?ul-item>/g, "") : element.replace(/<\/?ol-item>/g, ""); let content = isUlItem ? element.replace(/<\/?ul-item>/g, "") : element.replace(/<\/?ol-item>/g, "");
if (currentListType && currentListType !== type) { if (currentListType && currentListType !== type) {
flushList(); flushList();
} }