// WHY: the Content Editor runs on system Node without node_modules, so the user
// guide (docs/felhasznaloi-utmutato.md) is rendered by this small dependency-free
// markdown renderer instead of an external library.
// Supported subset: headings (#..####), bold, inline code, links, ul/ol lists,
// fenced code blocks, horizontal rules, paragraphs. HTML is escaped first.
function escapeHtml(value) {
return String(value)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
function renderInline(text) {
return escapeHtml(text)
.replace(/`([^`]+)`/g, '$1')
.replace(/\*\*([^*]+)\*\*/g, '$1')
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, '$1');
}
function renderMarkdown(markdown) {
const lines = String(markdown).split('\n');
const out = [];
let listTag = null; // 'ul' | 'ol'
let inCode = false;
const closeList = () => {
if (listTag) {
out.push(`${listTag}>`);
listTag = null;
}
};
for (const raw of lines) {
const line = raw.trimEnd();
if (line.trim().startsWith('```')) {
closeList();
out.push(inCode ? '' : '
');
inCode = !inCode;
continue;
}
if (inCode) {
out.push(escapeHtml(raw));
continue;
}
if (!line.trim()) {
closeList();
continue;
}
const heading = line.match(/^(#{1,4})\s+(.*)$/);
if (heading) {
closeList();
const level = heading[1].length;
out.push(`${renderInline(heading[2])} `);
continue;
}
if (/^(-{3,}|\*{3,})$/.test(line.trim())) {
closeList();
out.push('
');
continue;
}
const unordered = line.match(/^\s*[-*]\s+(.*)$/);
if (unordered) {
if (listTag !== 'ul') {
closeList();
out.push('');
listTag = 'ul';
}
out.push(`- ${renderInline(unordered[1])}
`);
continue;
}
const ordered = line.match(/^\s*\d+\.\s+(.*)$/);
if (ordered) {
if (listTag !== 'ol') {
closeList();
out.push('');
listTag = 'ol';
}
out.push(`- ${renderInline(ordered[1])}
`);
continue;
}
closeList();
out.push(`${renderInline(line)}
`);
}
closeList();
if (inCode) out.push('
');
return out.join('\n');
}
module.exports = { renderMarkdown, renderInline, escapeHtml };