Update Core.js

This commit is contained in:
simonpipe 2026-07-09 18:13:57 +02:00
parent 9c94f81b07
commit 72bae28400

145
Core.js
View file

@ -5,8 +5,8 @@
class SimonUp { class SimonUp {
constructor() { constructor() {
this.plugins = []; this.plugins = [];
this.headings = []; // Speichert alle gefundenen Überschriften für das TOC this.headings = [];
this.counters = [0, 0, 0, 0, 0, 0]; // Zähler für die hierarchischen Ebenen 1 bis 6 this.counters = [0, 0, 0, 0, 0, 0];
} }
/** /**
@ -26,15 +26,23 @@ class SimonUp {
let html = text; let html = text;
for (const plugin of this.plugins) { if (this.plugins && this.plugins.length > 0) {
if (plugin.before) html = plugin.before(html); for (let i = 0; i < this.plugins.length; i++) {
if (this.plugins[i].before) {
html = this.plugins[i].before(html);
}
}
} }
html = this._parseBlocks(html); html = this._parseBlocks(html);
html = this._injectTOC(html); html = this._injectTOC(html);
for (const plugin of this.plugins) { if (this.plugins && this.plugins.length > 0) {
if (plugin.after) html = plugin.after(html); for (let i = 0; i < this.plugins.length; i++) {
if (this.plugins[i].after) {
html = this.plugins[i].after(html);
}
}
} }
return html; return html;
@ -86,10 +94,10 @@ class SimonUp {
if (trimmed === '___') { if (trimmed === '___') {
result.push('<hr>'); result.push('<hr>');
} else if (trimmed.startsWith('- ')) { } else if (trimmed.startsWith('- ')) {
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)) {
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 {
result.push(this._parseInline(line)); result.push(this._parseInline(line));
} }
@ -107,40 +115,48 @@ class SimonUp {
switch (type) { switch (type) {
case 'toc': case 'toc':
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';
if (meta) tocWrapper += `<h2>${meta}</h2>\n`; if (meta) {
if (content.trim()) { tocWrapper += '<h2>' + meta + '</h2>\n';
const intro = lines.map(l => this._parseInline(l)).join('<br>');
tocWrapper += `<p class="toc-intro">${intro}</p>\n`;
} }
tocWrapper += `\n</div>`; if (content.trim()) {
let self = this;
const intro = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
tocWrapper += '<p class="toc-intro">' + intro + '</p>\n';
}
tocWrapper += '\n</div>';
} }
return tocWrapper; return tocWrapper;
case 'c': case 'c':
const langClass = meta ? ` class="language-${meta}"` : ''; const langClass = meta ? ' class="language-' + meta + '"' : '';
const escapedCode = content.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); const escapedCode = content.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return `<pre><code${langClass}>${escapedCode}</code></pre>`; return '<pre><code' + langClass + '>' + escapedCode + '</code></pre>';
case 'q': case 'q':
return `<blockquote>${lines.map(l => this._parseInline(l)).join('<br>')}</blockquote>`; let selfQ = this;
return '<blockquote>' + lines.map(function(l) { return selfQ._parseInline(l); }).join('<br>') + '</blockquote>';
case 'b': case 'b':
return `<div class="bible-block">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`; let selfB = this;
return '<div class="bible-block">' + lines.map(function(l) { return selfB._parseInline(l); }).join('<br>') + '</div>';
case 'f': case 'f':
return `<details>\n<summary>${meta || 'Details'}</summary>\n<p>${lines.map(l => this._parseInline(l)).join('<br>')}</p>\n</details>`; let selfF = this;
return '<details>\n<summary>' + (meta || 'Details') + '</summary>\n<p>' + lines.map(function(l) { return selfF._parseInline(l); }).join('<br>') + '</p>\n</details>';
case 'i': case 'i':
return `<div class="infobox" style="border-left: 4px solid ${meta || '#ff6347'}; padding: 10px; margin: 10px 0; background: #f8f9fa;">\n${lines.map(l => this._parseInline(l)).join('<br>')}\n</div>`; let selfI = this;
return '<div class="infobox" style="border-left: 4px solid ' + (meta || '#ff6347') + '; padding: 10px; margin: 10px 0; background: #f8f9fa;">\n' + lines.map(function(l) { return selfI._parseInline(l); }).join('<br>') + '\n</div>';
case 't': case 't':
return this._renderTable(meta, lines); return this._renderTable(meta, lines);
default: default:
return `<div class="block-${type}">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`; let selfDef = this;
return '<div class="block-' + type + '">' + lines.map(function(l) { return selfDef._parseInline(l); }).join('<br>') + '</div>';
} }
} }
@ -151,13 +167,15 @@ class SimonUp {
let tableHtml = '<table>\n'; let tableHtml = '<table>\n';
let currentCells = []; let currentCells = [];
let cellBuffer = []; let cellBuffer = [];
let self = this;
for (let line of lines) { for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let parts = line.split('|'); let parts = line.split('|');
for (let i = 0; i < parts.length; i++) { for (let j = 0; j < parts.length; j++) {
let part = parts[i]; let part = parts[j];
if (i === parts.length - 1) { if (j === parts.length - 1) {
if (part.trim() !== '') { if (part.trim() !== '') {
cellBuffer.push(part); cellBuffer.push(part);
} }
@ -173,7 +191,10 @@ class SimonUp {
} }
if (line.endsWith('|') && currentCells.length > 0) { if (line.endsWith('|') && currentCells.length > 0) {
tableHtml += '<tr>\n' + currentCells.map(c => `<td>${this._parseInline(c.trim()).replace(/\n/g, '<br>')}</td>`).join('\n') + '\n</tr>\n'; let rowContent = currentCells.map(function(c) {
return '<td>' + self._parseInline(c.trim()).replace(/\n/g, '<br>') + '</td>';
}).join('\n');
tableHtml += '<tr>\n' + rowContent + '\n</tr>\n';
currentCells = []; currentCells = [];
} }
} }
@ -189,14 +210,15 @@ class SimonUp {
let html = text; let html = text;
const escapes = []; const escapes = [];
html = html.replace(/\\(.)/g, (match, char) => { html = html.replace(/\\(.)/g, function(match, char) {
escapes.push(char); escapes.push(char);
return ``; return '';
}); });
html = html.replace(/\/\/(\s|$)/g, '$1'); html = html.replace(/\/\/(\s|$)/g, '$1');
const processHeading = (pattern, callback) => { let self = this;
const processHeading = function(pattern, callback) {
let match; let match;
while ((match = pattern.exec(html)) !== null) { while ((match = pattern.exec(html)) !== null) {
const fullMatch = match[0]; const fullMatch = match[0];
@ -216,22 +238,22 @@ class SimonUp {
} }
}; };
processHeading(/^([1-6])x#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])x#\s*(.*)/g, function(level, title, rest) {
const numStr = this._getAutoNumber(level); const numStr = self._getAutoNumber(level);
const fullTitle = `${numStr} ${title}`; const fullTitle = numStr + ' ' + title;
const id = this._slugify(fullTitle); const id = self._slugify(fullTitle);
this.headings.push({ level, text: fullTitle, id }); self.headings.push({ level: level, text: fullTitle, id: id });
return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`; return '<h' + level + ' id="' + id + '">' + fullTitle + '</h' + level + '>' + rest;
}); });
processHeading(/^([1-6])-#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])-#\s*(.*)/g, function(level, title, rest) {
return `<h${level}>${title}</h${level}>${rest}`; return '<h' + level + '>' + title + '</h' + level + '>' + rest;
}); });
processHeading(/^([1-6])#\s*(.*)/g, (level, title, rest) => { processHeading(/^([1-6])#\s*(.*)/g, function(level, title, rest) {
const id = this._slugify(title); const id = self._slugify(title);
this.headings.push({ level, text: title, id }); self.headings.push({ level: level, text: title, id: id });
return `<h${level} id="${id}">${title}</h${level}>${rest}`; return '<h' + level + ' id="' + id + '">' + title + '</h' + level + '>' + rest;
}); });
const inlineRules = [ const inlineRules = [
@ -246,20 +268,20 @@ class SimonUp {
{ tag: 'code', symbol: '`' } { tag: 'code', symbol: '`' }
]; ];
// Hier nutzen wir nun eine absolut sichere String-Verkettung für den RegExp-Konstruktor for (let k = 0; k < inlineRules.length; k++) {
for (const rule of inlineRules) { let rule = inlineRules[k];
let regex = new RegExp(rule.symbol + '(.+?)\\x2F' + rule.symbol, 'g'); let regex = new RegExp(rule.symbol + '(.+?)\\x2F' + 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] + '>');
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, ''); html = html.replace(//g, '');
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, function(m, dir, content) {
return `<div style="text-align: ${alignMap[dir]};">${content}</div>`; return '<div style="text-align: ' + alignMap[dir] + ';">' + content + '</div>';
}); });
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;">');
@ -270,7 +292,9 @@ class SimonUp {
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>');
html = html.replace(//g, (match, idx) => escapes[idx]); html = html.replace(//g, function(match, idx) {
return escapes[idx];
});
return html; return html;
} }
@ -281,7 +305,9 @@ class SimonUp {
_getAutoNumber(level) { _getAutoNumber(level) {
let idx = level - 1; let idx = level - 1;
this.counters[idx]++; this.counters[idx]++;
for (let i = idx + 1; i < 6; i++) this.counters[i] = 0; for (let i = idx + 1; i < 6; i++) {
this.counters[i] = 0;
}
let activeParts = this.counters.slice(0, level); let activeParts = this.counters.slice(0, level);
return activeParts.join('.') + '.'; return activeParts.join('.') + '.';
} }
@ -305,14 +331,15 @@ class SimonUp {
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 (const heading of this.headings) { for (let i = 0; i < this.headings.length; i++) {
let heading = this.headings[i];
if (heading.level > currentLevel) { if (heading.level > currentLevel) {
tocListHtml += '<ul>\n'.repeat(heading.level - currentLevel); tocListHtml += '<ul>\n'.repeat(heading.level - currentLevel);
} else if (heading.level < currentLevel) { } else if (heading.level < currentLevel) {
tocListHtml += '</ul>\n'.repeat(currentLevel - heading.level); tocListHtml += '</ul>\n'.repeat(currentLevel - heading.level);
} }
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) {
@ -332,22 +359,24 @@ class SimonUp {
let listBuffer = []; let listBuffer = [];
let currentListType = null; let currentListType = null;
const flushList = () => { const flushList = function() {
if (listBuffer.length > 0) { if (listBuffer.length > 0) {
processed.push(`<${currentListType}>\n` + listBuffer.map(item => ` <li>${item}</li>`).join('\n') + `\n</${currentListType}>`); let items = listBuffer.map(function(item) { return ' <li>' + item + '</li>'; }).join('\n');
processed.push('<' + currentListType + '>\n' + items + '\n</' + currentListType + '>');
listBuffer = []; listBuffer = [];
currentListType = null; currentListType = null;
} }
}; };
const flushParagraph = () => { const flushParagraph = function() {
if (paragraphBuffer.length > 0) { if (paragraphBuffer.length > 0) {
processed.push(`<p>${paragraphBuffer.join('<br>')}</p>`); processed.push('<p>' + paragraphBuffer.join('<br>') + '</p>');
paragraphBuffer = []; paragraphBuffer = [];
} }
}; };
for (let element of elements) { for (let i = 0; i < elements.length; i++) {
let element = elements[i];
let isUlItem = element.startsWith('<ul-item>'); let isUlItem = element.startsWith('<ul-item>');
let isOlItem = element.startsWith('<ol-item>'); let isOlItem = element.startsWith('<ol-item>');