Update Core.js
This commit is contained in:
parent
72bae28400
commit
91efaa66b1
1 changed files with 51 additions and 36 deletions
87
Core.js
87
Core.js
|
|
@ -94,10 +94,12 @@ class SimonUp {
|
|||
if (trimmed === '___') {
|
||||
result.push('<hr>');
|
||||
} else if (trimmed.startsWith('- ')) {
|
||||
result.push('<ul-item>' + this._parseInline(line.replace(/^\s*-\s+/, '')) + '</ul-item>');
|
||||
let insideUl = this._parseInline(line.replace(/^\s*-\s+/, ''));
|
||||
result.push(`<ul-item>${insideUl}</ul-item>`);
|
||||
} else if (/^\d+\.\s+/.test(trimmed)) {
|
||||
let cleanLine = line.replace(/^\s*\d+\.\s+/, '');
|
||||
result.push('<ol-item>' + this._parseInline(cleanLine) + '</ol-item>');
|
||||
let insideOl = this._parseInline(cleanLine);
|
||||
result.push(`<ol-item>${insideOl}</ol-item>`);
|
||||
} else {
|
||||
result.push(this._parseInline(line));
|
||||
}
|
||||
|
|
@ -112,6 +114,7 @@ class SimonUp {
|
|||
*/
|
||||
_renderBlock(type, meta, lines) {
|
||||
const content = lines.join('\n');
|
||||
let self = this;
|
||||
|
||||
switch (type) {
|
||||
case 'toc':
|
||||
|
|
@ -119,44 +122,45 @@ class SimonUp {
|
|||
if (meta || content.trim()) {
|
||||
tocWrapper = '<div class="simonup-toc-wrapper">\n';
|
||||
if (meta) {
|
||||
tocWrapper += '<h2>' + meta + '</h2>\n';
|
||||
tocWrapper += `<h2>${meta}</h2>\n`;
|
||||
}
|
||||
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 += `<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':
|
||||
let selfQ = this;
|
||||
return '<blockquote>' + lines.map(function(l) { return selfQ._parseInline(l); }).join('<br>') + '</blockquote>';
|
||||
const qContent = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
|
||||
return `<blockquote>${qContent}</blockquote>`;
|
||||
|
||||
case 'b':
|
||||
let selfB = this;
|
||||
return '<div class="bible-block">' + lines.map(function(l) { return selfB._parseInline(l); }).join('<br>') + '</div>';
|
||||
const bContent = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
|
||||
return `<div class="bible-block">${bContent}</div>`;
|
||||
|
||||
case 'f':
|
||||
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>';
|
||||
const fSummary = meta || 'Details';
|
||||
const fContent = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
|
||||
return `<details>\n<summary>${fSummary}</summary>\n<p>${fContent}</p>\n</details>`;
|
||||
|
||||
case 'i':
|
||||
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>';
|
||||
const iColor = meta || '#ff6347';
|
||||
const iContent = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
|
||||
return `<div class="infobox" style="border-left: 4px solid ${iColor}; padding: 10px; margin: 10px 0; background: #f8f9fa;">\n${iContent}\n</div>`;
|
||||
|
||||
case 't':
|
||||
return this._renderTable(meta, lines);
|
||||
|
||||
default:
|
||||
let selfDef = this;
|
||||
return '<div class="block-' + type + '">' + lines.map(function(l) { return selfDef._parseInline(l); }).join('<br>') + '</div>';
|
||||
const defContent = lines.map(function(l) { return self._parseInline(l); }).join('<br>');
|
||||
return `<div class="block-${type}">${defContent}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -192,9 +196,10 @@ class SimonUp {
|
|||
|
||||
if (line.endsWith('|') && currentCells.length > 0) {
|
||||
let rowContent = currentCells.map(function(c) {
|
||||
return '<td>' + self._parseInline(c.trim()).replace(/\n/g, '<br>') + '</td>';
|
||||
let parsedCell = self._parseInline(c.trim()).replace(/\n/g, '<br>');
|
||||
return `<td>${parsedCell}</td>`;
|
||||
}).join('\n');
|
||||
tableHtml += '<tr>\n' + rowContent + '\n</tr>\n';
|
||||
tableHtml += `<tr>\n${rowContent}\n</tr>\n`;
|
||||
currentCells = [];
|
||||
}
|
||||
}
|
||||
|
|
@ -212,7 +217,7 @@ class SimonUp {
|
|||
const escapes = [];
|
||||
html = html.replace(/\\(.)/g, function(match, char) {
|
||||
escapes.push(char);
|
||||
return '';
|
||||
return ``;
|
||||
});
|
||||
|
||||
html = html.replace(/\/\/(\s|$)/g, '$1');
|
||||
|
|
@ -240,20 +245,20 @@ class SimonUp {
|
|||
|
||||
processHeading(/^([1-6])x#\s*(.*)/g, function(level, title, rest) {
|
||||
const numStr = self._getAutoNumber(level);
|
||||
const fullTitle = numStr + ' ' + title;
|
||||
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;
|
||||
return `<h${level} id="${id}">${fullTitle}</h${level}>${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, 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;
|
||||
return `<h${level} id="${id}">${title}</h${level}>${rest}`;
|
||||
});
|
||||
|
||||
const inlineRules = [
|
||||
|
|
@ -270,18 +275,20 @@ class SimonUp {
|
|||
|
||||
for (let k = 0; k < inlineRules.length; k++) {
|
||||
let rule = inlineRules[k];
|
||||
let tagSimple = rule.tag.split(' ')[0];
|
||||
|
||||
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</${tagSimple}>`);
|
||||
|
||||
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</${tagSimple}>`);
|
||||
}
|
||||
|
||||
html = html.replace(//g, '');
|
||||
|
||||
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>';
|
||||
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;">');
|
||||
|
|
@ -325,8 +332,12 @@ class SimonUp {
|
|||
* INTERN: Erzeugt die fertige HTML-Liste des TOC und injiziert sie in den Block
|
||||
*/
|
||||
_injectTOC(html) {
|
||||
if (!html.includes('')) return html;
|
||||
if (this.headings.length === 0) return html.replace('', '');
|
||||
if (!html.includes('')) {
|
||||
return html;
|
||||
}
|
||||
if (this.headings.length === 0) {
|
||||
return html.replace('', '');
|
||||
}
|
||||
|
||||
let tocListHtml = '<div class="simonup-toc">\n<ul>\n';
|
||||
let currentLevel = 1;
|
||||
|
|
@ -334,16 +345,19 @@ class SimonUp {
|
|||
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);
|
||||
let diffUp = heading.level - currentLevel;
|
||||
tocListHtml += '<ul>\n'.repeat(diffUp);
|
||||
} else if (heading.level < currentLevel) {
|
||||
tocListHtml += '</ul>\n'.repeat(currentLevel - heading.level);
|
||||
let diffDown = currentLevel - heading.level;
|
||||
tocListHtml += '</ul>\n'.repeat(diffDown);
|
||||
}
|
||||
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) {
|
||||
tocListHtml += '</ul>\n'.repeat(currentLevel - 1);
|
||||
let diffFinal = currentLevel - 1;
|
||||
tocListHtml += '</ul>\n'.repeat(diffFinal);
|
||||
}
|
||||
tocListHtml += '</ul>\n</div>';
|
||||
|
||||
|
|
@ -361,8 +375,8 @@ class SimonUp {
|
|||
|
||||
const flushList = function() {
|
||||
if (listBuffer.length > 0) {
|
||||
let items = listBuffer.map(function(item) { return ' <li>' + item + '</li>'; }).join('\n');
|
||||
processed.push('<' + currentListType + '>\n' + items + '\n</' + currentListType + '>');
|
||||
let items = listBuffer.map(function(item) { return ` <li>${item}</li>`; }).join('\n');
|
||||
processed.push(`<${currentListType}>\n${items}\n</${currentListType}>`);
|
||||
listBuffer = [];
|
||||
currentListType = null;
|
||||
}
|
||||
|
|
@ -370,7 +384,8 @@ class SimonUp {
|
|||
|
||||
const flushParagraph = function() {
|
||||
if (paragraphBuffer.length > 0) {
|
||||
processed.push('<p>' + paragraphBuffer.join('<br>') + '</p>');
|
||||
let pContent = paragraphBuffer.join('<br>');
|
||||
processed.push(`<p>${pContent}</p>`);
|
||||
paragraphBuffer = [];
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue