import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class MarkdownService { transformCustomBlocks(content: string): string { if (!content) return ''; return content .replace(/\[gallery\]\s*([\s\S]*?)\s*\[\/gallery\]/g, (_, inner: string) => { const images = inner .split('\n') .map((line) => this.sanitizeImageUrl(line)) .filter((url): url is string => url !== null) .map((url) => `Gallery image`) .join(''); return ``; }) .replace( /\[center\]\s*([\s\S]*?)\s*\[\/center\]/g, (_, inner: string) => `
${inner}
` ); } private sanitizeImageUrl(rawUrl: string): string | null { const trimmed = rawUrl.trim(); if (trimmed.startsWith('/')) { return trimmed; } try { const parsed = new URL(trimmed); if (parsed.protocol === 'https:' || parsed.protocol === 'http:') { return parsed.href; } } catch { return null; } return null; } private escapeHtmlAttr(value: string): string { return value .replace(/&/g, '&') .replace(/"/g, '"') .replace(//g, '>'); } }