function escapeHtml(input: string): string {
return input
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
function renderInline(text: string): string {
const escaped = escapeHtml(text);
// Minimal inline formatting: **bold** and `code`
const withBold = escaped.replace(/\*\*([^*]+)\*\*/g, '$1');
return withBold.replace(/`([^`]+)`/g, '$1');
}
function slugify(input: string): string {
return input
.toLowerCase()
.trim()
.replace(/[`*_~]/g, '')
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}
function countLeadingSpaces(line: string): number {
let count = 0;
for (const ch of line) {
if (ch === ' ') count += 1;
else if (ch === '\t') count += 2;
else break;
}
return count;
}
export interface TocEntry {
id: string;
label: string;
level: number;
}
export interface MarkdownRenderResult {
html: string;
toc: TocEntry[];
}
/**
* Safe markdown renderer for release notes.
*
* This intentionally supports a small subset and escapes all HTML first,
* which makes it safe to inject with {@html ...}.
*/
export function renderMarkdownWithToc(markdown: string | null | undefined): MarkdownRenderResult {
if (!markdown) {
return { html: '', toc: [] };
}
const lines = markdown.split('\n');
const html: string[] = [];
const toc: TocEntry[] = [];
// Stack of list levels currently open (each entry is the level number)
const openLists: number[] = [];
const closeListsToLevel = (targetLevel: number) => {
while (openLists.length > targetLevel) {
html.push('');
openLists.pop();
}
};
const openListToLevel = (targetLevel: number) => {
while (openLists.length < targetLevel) {
html.push('
${renderInline(trimmed)}
`); } closeListsToLevel(0); return { html: html.join('\n'), toc }; } export function renderMarkdownSafe(markdown: string | null | undefined): string { return renderMarkdownWithToc(markdown).html; } export function formatReleaseDate(isoDate: string): string { const date = new Date(isoDate); if (Number.isNaN(date.getTime())) return isoDate; return new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'short', day: 'numeric', }).format(date); }