add support for > and syntax

This commit is contained in:
m15o 2024-08-24 15:13:00 +02:00
parent 3f19a2f889
commit 81ca01ef1a
1 changed files with 7 additions and 6 deletions

View File

@ -587,7 +587,7 @@ SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// xtx
const sref = l => l.replace(/{/g, "&#123;").replace(/}/g, "&#125;").replace(/#\[\[([^\n]+?)\]\]/g, '#$1').replace(/\[\[([^\n]+?)\]\]/g, (match, p1) => `<a href="#${p1}">${p1}</a>`);
const sref = l => l.replace(/{/g, "&#123;").replace(/}/g, "&#125;").replace(/#\[\[([^\n]+?)\]\]/g, '#$1').replace(/`([^`]+)`/g, '<code>$1</code>').replace(/\[\[([^\n]+?)\]\]/g, (match, p1) => `<a href="#${p1}">${p1}</a>`);
const escapeHtml = unsafe => unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
function toHTML(text) {
@ -595,19 +595,20 @@ SOFTWARE.
const lines = text.split('\n');
for (let i = 0; i < lines.length; i++) {
let l = lines[i];
if (l.startsWith('# ')) rv += '<h1>' + sref(l.slice(2)) + '</h1>';
else if (l.startsWith('## ')) rv += '<h2>' + sref(l.slice(3)) + '</h2>';
else if (l.startsWith('### ')) rv += '<h3>' + sref(l.slice(4)) + '</h3>';
if (l.startsWith('# ')) rv += `<h1>${sref(l.slice(2))}</h1>`;
else if (l.startsWith('## ')) rv += `<h2>${sref(l.slice(3))}</h2>`;
else if (l.startsWith('### ')) rv += `<h3>${sref(l.slice(4))}</h3>`;
else if (l.startsWith('> ')) rv += `<blockquote>${sref(l.slice(2))}</blockquote>`;
else if (l.startsWith('* ')) {
!isList && (isList = true, rv += '<ul>');
rv += '<li>' + sref(l.slice(2)) + '</li>';
rv += `<li>${sref(l.slice(2))}</li>`;
} else if (l.startsWith(' ')) {
!isPre && (isPre = true, rv += '<pre>');
rv += escapeHtml(l.slice(2)) + '\n';
} else {
isList && (isList = false, rv += '</ul>');
isPre && (isPre = false, rv += '</pre>');
l && (rv += '<p>' + sref(l) + '</p>');
l && (rv += `<p>${sref(l)}</p>`);
}
}
isList && (rv += '</ul>');