Update Core.js

This commit is contained in:
simonpipe 2026-07-09 18:07:34 +02:00
parent 353a32f041
commit 2096a1fd9a

58
Core.js
View file

@ -26,7 +26,6 @@ class SimonUp {
let html = text; let html = text;
// "Before"-Plugins ausführen
for (const plugin of this.plugins) { for (const plugin of this.plugins) {
if (plugin.before) html = plugin.before(html); if (plugin.before) html = plugin.before(html);
} }
@ -34,7 +33,6 @@ class SimonUp {
html = this._parseBlocks(html); html = this._parseBlocks(html);
html = this._injectTOC(html); html = this._injectTOC(html);
// "After"-Plugins ausführen
for (const plugin of this.plugins) { for (const plugin of this.plugins) {
if (plugin.after) html = plugin.after(html); if (plugin.after) html = plugin.after(html);
} }
@ -56,7 +54,6 @@ class SimonUp {
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
let line = lines[i]; let line = lines[i];
// 1. Block-Start erkennen (&typ meta)
if (!inBlock && line.startsWith('&') && line.trim().length > 1) { if (!inBlock && line.startsWith('&') && line.trim().length > 1) {
inBlock = true; inBlock = true;
const fullHeader = line.substring(1).trim(); const fullHeader = line.substring(1).trim();
@ -73,7 +70,6 @@ class SimonUp {
continue; continue;
} }
// 2. Block-Ende erkennen (einzelnes &)
if (inBlock && line.trim() === '&') { if (inBlock && line.trim() === '&') {
result.push(this._renderBlock(blockType, blockMeta, blockBuffer)); result.push(this._renderBlock(blockType, blockMeta, blockBuffer));
inBlock = false; inBlock = false;
@ -82,30 +78,24 @@ class SimonUp {
continue; continue;
} }
// 3. Inhalt sammeln oder freie Zeilen verarbeiten
if (inBlock) { if (inBlock) {
blockBuffer.push(line); blockBuffer.push(line);
} else { } else {
let trimmed = line.trim(); let trimmed = line.trim();
if (trimmed === '___') { if (trimmed === '___') {
// Horizontale Linie
result.push('<hr>'); result.push('<hr>');
} else if (trimmed.startsWith('- ')) { } else if (trimmed.startsWith('- ')) {
// Freier Listenpunkt (Ungeordnet)
result.push(`<ul-item>${this._parseInline(line.replace(/^\s*-\s+/, ''))}</ul-item>`); result.push(`<ul-item>${this._parseInline(line.replace(/^\s*-\s+/, ''))}</ul-item>`);
} else if (/^\d+\.\s+/.test(trimmed)) { } else if (/^\d+\.\s+/.test(trimmed)) {
// Freier Listenpunkt (Nummeriert)
let cleanLine = line.replace(/^\s*\d+\.\s+/, ''); let cleanLine = line.replace(/^\s*\d+\.\s+/, '');
result.push(`<ol-item>${this._parseInline(cleanLine)}</ol-item>`); result.push(`<ol-item>${this._parseInline(cleanLine)}</ol-item>`);
} else { } else {
// Normaler Fließtext
result.push(this._parseInline(line)); result.push(this._parseInline(line));
} }
} }
} }
// Freie Listenpunkte zusammenfassen und Absätze bauen
return this._processParagraphsAndLists(result); return this._processParagraphsAndLists(result);
} }
@ -117,7 +107,6 @@ class SimonUp {
switch (type) { switch (type) {
case 'toc': case 'toc':
// Platzhalter für das spätere Injektions-Verfahren hinterlassen
let tocWrapper = ``; let tocWrapper = ``;
if (meta || content.trim()) { if (meta || content.trim()) {
tocWrapper = `<div class="simonup-toc-wrapper">\n`; tocWrapper = `<div class="simonup-toc-wrapper">\n`;
@ -150,12 +139,6 @@ class SimonUp {
case 't': case 't':
return this._renderTable(meta, lines); return this._renderTable(meta, lines);
case '>':
return `<div style="margin-left: 2em;">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`;
case '>>':
return `<div style="margin-left: 4em;">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`;
default: default:
return `<div class="block-${type}">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`; return `<div class="block-${type}">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`;
} }
@ -169,7 +152,6 @@ class SimonUp {
let currentCells = []; let currentCells = [];
let cellBuffer = []; let cellBuffer = [];
// Parsing-Logik für Multi-line-Zellen via Pipe (|)
for (let line of lines) { for (let line of lines) {
let parts = line.split('|'); let parts = line.split('|');
for (let i = 0; i < parts.length; i++) { for (let i = 0; i < parts.length; i++) {
@ -206,17 +188,14 @@ class SimonUp {
if (!text) return ''; if (!text) return '';
let html = text; let html = text;
// 1. ESCAPE: Steuerzeichen sichern (Schutz vor doppelter Evaluierung)
const escapes = []; const escapes = [];
html = html.replace(/\\(.)/g, (match, char) => { html = html.replace(/\\(.)/g, (match, char) => {
escapes.push(char); escapes.push(char);
return ``; return ``;
}); });
// 2. UNIVERSAL-RESET: Verarbeitet den "//"-Reset im Satz
html = html.replace(/\/\/(\s|$)/g, '$1'); html = html.replace(/\/\/(\s|$)/g, '$1');
// 3. ÜBERSCHRIFTEN (V1.2 konform: Startet mit X#, schließt optional mit /# oder Zeilenende)
const processHeading = (pattern, callback) => { const processHeading = (pattern, callback) => {
let match; let match;
while ((match = pattern.exec(html)) !== null) { while ((match = pattern.exec(html)) !== null) {
@ -224,7 +203,6 @@ class SimonUp {
const level = parseInt(match[1]); const level = parseInt(match[1]);
const titleAndRest = match[2]; const titleAndRest = match[2];
// Schließzeichen /# suchen, sonst bis Zeilenende parsen
let title = titleAndRest; let title = titleAndRest;
let rest = ""; let rest = "";
const closeIdx = titleAndRest.indexOf('/#'); const closeIdx = titleAndRest.indexOf('/#');
@ -238,7 +216,6 @@ class SimonUp {
} }
}; };
// 3a. Automatische Nummerierung (1x# bis 6x#)
processHeading(/^([1-6])x#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])x#\s*(.*)/g, (level, title, rest) => {
const numStr = this._getAutoNumber(level); const numStr = this._getAutoNumber(level);
const fullTitle = `${numStr} ${title}`; const fullTitle = `${numStr} ${title}`;
@ -247,58 +224,50 @@ class SimonUp {
return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`; return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`;
}); });
// 3b. Nicht im TOC erwünscht (1-# bis 6-#)
processHeading(/^([1-6])-#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])-#\s*(.*)/g, (level, title, rest) => {
return `<h${level}>${title}</h${level}>${rest}`; return `<h${level}>${title}</h${level}>${rest}`;
}); });
// 3c. Standard-Überschrift (1# bis 6#)
processHeading(/^([1-6])#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])#\s*(.*)/g, (level, title, rest) => {
const id = this._slugify(title); const id = this._slugify(title);
this.headings.push({ level, text: title, id }); this.headings.push({ level, text: title, id });
return `<h${level} id="${id}">${title}</h${level}>${rest}`; return `<h${level} id="${id}">${title}</h${level}>${rest}`;
}); });
// 4. INLINE SCHLIESS-LOGIK (V1.2: ZeichenText/Zeichen)
const inlineRules = [ const inlineRules = [
{ tag: 'strong', symbol: '\\*\\*' }, // Fett (**Text/**) { tag: 'strong', symbol: '\\*\\*' },
{ tag: 'span style="font-weight: 500;"', symbol: '\\*' }, // Medium (*Text/*) { tag: 'span style="font-weight: 500;"', symbol: '\\*' },
{ tag: 'em', symbol: '%' }, // Kursiv (%Text/%) { tag: 'em', symbol: '%' },
{ tag: 'u', symbol: '_' }, // Unterstrichen (_Text/_) { tag: 'u', symbol: '_' },
{ tag: 'del', symbol: '~' }, // Durchgestrichen (~Text/~) { tag: 'del', symbol: '~' },
{ tag: 'mark', symbol: '=' }, // Highlight (=Text/=) { tag: 'mark', symbol: '=' },
{ tag: 'sup', symbol: '\\^' }, // Hochgestellt (^Text/^) { tag: 'sup', symbol: '\\^' },
{ tag: 'sub', symbol: '°' }, // Tiefgestellt (°Text/°) { tag: 'sub', symbol: '°' },
{ tag: 'code', symbol: '`' } // Inline-Code (`Text/`) { tag: 'code', symbol: '`' }
]; ];
inlineRules.forEach(rule => { inlineRules.forEach(rule => {
// Regulärer Match: Symbol -> Text -> /Symbol
let regex = new RegExp(`${rule.symbol}(.+?)\/${rule.symbol}`, 'g'); let regex = new RegExp(`${rule.symbol}(.+?)\/${rule.symbol}`, 'g');
html = html.replace(regex, `<${rule.tag}>$1</${rule.tag.split(' ')[0]}>`); html = html.replace(regex, `<${rule.tag}>$1</${rule.tag.split(' ')[0]}>`);
// Fallback für Universal-Reset: Offenes Symbol bis zum let resetRegex = new RegExp(`${rule.symbol}(.+?)(?=)`, 'g'); let resetRegex = new RegExp(`${rule.symbol}(.+?)(?=)`, 'g');
html = html.replace(resetRegex, `<${rule.tag}>$1</${rule.tag.split(' ')[0]}>`); html = html.replace(resetRegex, `<${rule.tag}>$1</${rule.tag.split(' ')[0]}>`);
}); });
html = html.replace(//g, ''); // Aufgeräumte Reset-Tags html = html.replace(//g, '');
// 5. AUSRICHTUNG IM FLUSS (>c Text />)
const alignMap = { l: 'left', r: 'right', c: 'center', j: 'justify' }; const alignMap = { l: 'left', r: 'right', c: 'center', j: 'justify' };
html = html.replace(/>([lrcj])\s+(.+?)\s*\/>/g, (m, dir, content) => { html = html.replace(/>([lrcj])\s+(.+?)\s*\/>/g, (m, dir, content) => {
return `<div style="text-align: ${alignMap[dir]};">${content}</div>`; return `<div style="text-align: ${alignMap[dir]};">${content}</div>`;
}); });
// 6. LINKS & MEDIEN
html = html.replace(/!\[([^\]]+)\s+w:(\d+)\]\(([^)]+)\)/g, '<img src="$3" alt="$1" style="width: $2px; height: auto;">'); html = html.replace(/!\[([^\]]+)\s+w:(\d+)\]\(([^)]+)\)/g, '<img src="$3" alt="$1" style="width: $2px; height: auto;">');
html = html.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">'); html = html.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">');
html = html.replace(/\[([^\]]+)\]\{([^}]+)\}/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>'); html = html.replace(/\[([^\]]+)\]\{([^}]+)\}/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>');
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>'); html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
// 7. FUSSNOTEN
html = html.replace(/\^x\[([^\]]+)\]\^/g, '<span class="footnote-auto" title="$1">[*]</span>'); html = html.replace(/\^x\[([^\]]+)\]\^/g, '<span class="footnote-auto" title="$1">[*]</span>');
html = html.replace(/\^(\d+)\[([^\]]+)\]\^/g, '<sup class="footnote-manual" title="$2">[$1]</sup>'); html = html.replace(/\^(\d+)\[([^\]]+)\]\^/g, '<sup class="footnote-manual" title="$2">[$1]</sup>');
// 8. ESCAPES wiederherstellen
html = html.replace(//g, (match, idx) => escapes[idx]); html = html.replace(//g, (match, idx) => escapes[idx]);
return html; return html;
@ -359,7 +328,7 @@ class SimonUp {
let processed = []; let processed = [];
let paragraphBuffer = []; let paragraphBuffer = [];
let listBuffer = []; let listBuffer = [];
let currentListType = null; // 'ul' oder 'ol' let currentListType = null;
const flushList = () => { const flushList = () => {
if (listBuffer.length > 0) { if (listBuffer.length > 0) {
@ -391,16 +360,13 @@ class SimonUp {
currentListType = type; currentListType = type;
listBuffer.push(content); listBuffer.push(content);
} else if (element.startsWith('<') && !/^(<a>|<strong>|<em>|<u>|<del>|<mark>|<sup>|<sub>|<span>|<code>|<img>|<h\d>)/i.test(element)) { } else if (element.startsWith('<') && !/^(<a>|<strong>|<em>|<u>|<del>|<mark>|<sup>|<sub>|<span>|<code>|<img>|<h\d>)/i.test(element)) {
// Strukturelles Block-Element (z.B. <table>, <blockquote>)
flushList(); flushList();
flushParagraph(); flushParagraph();
processed.push(element); processed.push(element);
} else if (element.trim() === '') { } else if (element.trim() === '') {
// Leerzeile bricht Listen und Absätze auf
flushList(); flushList();
flushParagraph(); flushParagraph();
} else { } else {
// Fließtext mündet im Absatz-Buffer
flushList(); flushList();
paragraphBuffer.push(element); paragraphBuffer.push(element);
} }