Update Core.js
This commit is contained in:
parent
9c94f81b07
commit
72bae28400
1 changed files with 87 additions and 58 deletions
145
Core.js
145
Core.js
|
|
@ -5,8 +5,8 @@
|
|||
class SimonUp {
|
||||
constructor() {
|
||||
this.plugins = [];
|
||||
this.headings = []; // Speichert alle gefundenen Überschriften für das TOC
|
||||
this.counters = [0, 0, 0, 0, 0, 0]; // Zähler für die hierarchischen Ebenen 1 bis 6
|
||||
this.headings = [];
|
||||
this.counters = [0, 0, 0, 0, 0, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -26,15 +26,23 @@ class SimonUp {
|
|||
|
||||
let html = text;
|
||||
|
||||
for (const plugin of this.plugins) {
|
||||
if (plugin.before) html = plugin.before(html);
|
||||
if (this.plugins && this.plugins.length > 0) {
|
||||
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._injectTOC(html);
|
||||
|
||||
for (const plugin of this.plugins) {
|
||||
if (plugin.after) html = plugin.after(html);
|
||||
if (this.plugins && this.plugins.length > 0) {
|
||||
for (let i = 0; i < this.plugins.length; i++) {
|
||||
if (this.plugins[i].after) {
|
||||
html = this.plugins[i].after(html);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return html;
|
||||
|
|
@ -86,10 +94,10 @@ class SimonUp {
|
|||
if (trimmed === '___') {
|
||||
result.push('<hr>');
|
||||
} 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)) {
|
||||
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 {
|
||||
result.push(this._parseInline(line));
|
||||
}
|
||||
|
|
@ -107,40 +115,48 @@ class SimonUp {
|
|||
|
||||
switch (type) {
|
||||
case 'toc':
|
||||
let tocWrapper = ``;
|
||||
let tocWrapper = '';
|
||||
if (meta || content.trim()) {
|
||||
tocWrapper = `<div class="simonup-toc-wrapper">\n`;
|
||||
if (meta) tocWrapper += `<h2>${meta}</h2>\n`;
|
||||
if (content.trim()) {
|
||||
const intro = lines.map(l => this._parseInline(l)).join('<br>');
|
||||
tocWrapper += `<p class="toc-intro">${intro}</p>\n`;
|
||||
tocWrapper = '<div class="simonup-toc-wrapper">\n';
|
||||
if (meta) {
|
||||
tocWrapper += '<h2>' + meta + '</h2>\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;
|
||||
|
||||
case 'c':
|
||||
const langClass = meta ? ` class="language-${meta}"` : '';
|
||||
const langClass = meta ? ' class="language-' + meta + '"' : '';
|
||||
const escapedCode = content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
return `<pre><code${langClass}>${escapedCode}</code></pre>`;
|
||||
return '<pre><code' + langClass + '>' + escapedCode + '</code></pre>';
|
||||
|
||||
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':
|
||||
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':
|
||||
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':
|
||||
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':
|
||||
return this._renderTable(meta, lines);
|
||||
|
||||
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 currentCells = [];
|
||||
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('|');
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
let part = parts[i];
|
||||
for (let j = 0; j < parts.length; j++) {
|
||||
let part = parts[j];
|
||||
|
||||
if (i === parts.length - 1) {
|
||||
if (j === parts.length - 1) {
|
||||
if (part.trim() !== '') {
|
||||
cellBuffer.push(part);
|
||||
}
|
||||
|
|
@ -173,7 +191,10 @@ class SimonUp {
|
|||
}
|
||||
|
||||
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 = [];
|
||||
}
|
||||
}
|
||||
|
|
@ -189,14 +210,15 @@ class SimonUp {
|
|||
let html = text;
|
||||
|
||||
const escapes = [];
|
||||
html = html.replace(/\\(.)/g, (match, char) => {
|
||||
html = html.replace(/\\(.)/g, function(match, char) {
|
||||
escapes.push(char);
|
||||
return ``;
|
||||
return '';
|
||||
});
|
||||
|
||||
html = html.replace(/\/\/(\s|$)/g, '$1');
|
||||
|
||||
const processHeading = (pattern, callback) => {
|
||||
let self = this;
|
||||
const processHeading = function(pattern, callback) {
|
||||
let match;
|
||||
while ((match = pattern.exec(html)) !== null) {
|
||||
const fullMatch = match[0];
|
||||
|
|
@ -216,22 +238,22 @@ class SimonUp {
|
|||
}
|
||||
};
|
||||
|
||||
processHeading(/^([1-6])x#\s*(.*)/g, (level, title, rest) => {
|
||||
const numStr = this._getAutoNumber(level);
|
||||
const fullTitle = `${numStr} ${title}`;
|
||||
const id = this._slugify(fullTitle);
|
||||
this.headings.push({ level, text: fullTitle, id });
|
||||
return `<h${level} id="${id}">${fullTitle}</h${level}>${rest}`;
|
||||
processHeading(/^([1-6])x#\s*(.*)/g, function(level, title, rest) {
|
||||
const numStr = self._getAutoNumber(level);
|
||||
const fullTitle = numStr + ' ' + title;
|
||||
const id = self._slugify(fullTitle);
|
||||
self.headings.push({ level: level, text: fullTitle, id: id });
|
||||
return '<h' + level + ' id="' + id + '">' + fullTitle + '</h' + level + '>' + rest;
|
||||
});
|
||||
|
||||
processHeading(/^([1-6])-#\s*(.*)/g, (level, title, rest) => {
|
||||
return `<h${level}>${title}</h${level}>${rest}`;
|
||||
processHeading(/^([1-6])-#\s*(.*)/g, function(level, title, rest) {
|
||||
return '<h' + level + '>' + title + '</h' + level + '>' + rest;
|
||||
});
|
||||
|
||||
processHeading(/^([1-6])#\s*(.*)/g, (level, title, rest) => {
|
||||
const id = this._slugify(title);
|
||||
this.headings.push({ level, text: title, id });
|
||||
return `<h${level} id="${id}">${title}</h${level}>${rest}`;
|
||||
processHeading(/^([1-6])#\s*(.*)/g, function(level, title, rest) {
|
||||
const id = self._slugify(title);
|
||||
self.headings.push({ level: level, text: title, id: id });
|
||||
return '<h' + level + ' id="' + id + '">' + title + '</h' + level + '>' + rest;
|
||||
});
|
||||
|
||||
const inlineRules = [
|
||||
|
|
@ -246,20 +268,20 @@ class SimonUp {
|
|||
{ tag: 'code', symbol: '`' }
|
||||
];
|
||||
|
||||
// Hier nutzen wir nun eine absolut sichere String-Verkettung für den RegExp-Konstruktor
|
||||
for (const rule of inlineRules) {
|
||||
for (let k = 0; k < inlineRules.length; k++) {
|
||||
let rule = inlineRules[k];
|
||||
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');
|
||||
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, '');
|
||||
|
||||
const alignMap = { l: 'left', r: 'right', c: 'center', j: 'justify' };
|
||||
html = html.replace(/>([lrcj])\s+(.+?)\s*\/>/g, (m, dir, content) => {
|
||||
return `<div style="text-align: ${alignMap[dir]};">${content}</div>`;
|
||||
const alignMap = { 'l': 'left', 'r': 'right', 'c': 'center', 'j': 'justify' };
|
||||
html = html.replace(/>([lrcj])\s+(.+?)\s*\/>/g, function(m, dir, content) {
|
||||
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;">');
|
||||
|
|
@ -270,7 +292,9 @@ class SimonUp {
|
|||
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(//g, (match, idx) => escapes[idx]);
|
||||
html = html.replace(//g, function(match, idx) {
|
||||
return escapes[idx];
|
||||
});
|
||||
|
||||
return html;
|
||||
}
|
||||
|
|
@ -281,7 +305,9 @@ class SimonUp {
|
|||
_getAutoNumber(level) {
|
||||
let idx = level - 1;
|
||||
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);
|
||||
return activeParts.join('.') + '.';
|
||||
}
|
||||
|
|
@ -305,14 +331,15 @@ class SimonUp {
|
|||
let tocListHtml = '<div class="simonup-toc">\n<ul>\n';
|
||||
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) {
|
||||
tocListHtml += '<ul>\n'.repeat(heading.level - currentLevel);
|
||||
} else if (heading.level < currentLevel) {
|
||||
tocListHtml += '</ul>\n'.repeat(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) {
|
||||
|
|
@ -332,22 +359,24 @@ class SimonUp {
|
|||
let listBuffer = [];
|
||||
let currentListType = null;
|
||||
|
||||
const flushList = () => {
|
||||
const flushList = function() {
|
||||
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 = [];
|
||||
currentListType = null;
|
||||
}
|
||||
};
|
||||
|
||||
const flushParagraph = () => {
|
||||
const flushParagraph = function() {
|
||||
if (paragraphBuffer.length > 0) {
|
||||
processed.push(`<p>${paragraphBuffer.join('<br>')}</p>`);
|
||||
processed.push('<p>' + paragraphBuffer.join('<br>') + '</p>');
|
||||
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 isOlItem = element.startsWith('<ol-item>');
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue