Files
itzmineydev/src/app/shared/services/markdown.service.ts
T
itzMiney 7651374e3d
Frontend CI/CD / test-and-build (push) Successful in 25s
Frontend CI/CD / deploy (push) Successful in 26s
refactor: "minor" changes.. to a bit of everything, yea
- Restructure en.json & de.json into nested keys and update all templates
- Extract shared markdown logic into MarkdownService
- Fix SSR route configuration breaking Express REST requests for /admin
- Fix terminal emulator mobile layout shrink-to-fit bug on About page
- Refresh Socials & Portfolio layouts, styles, and buttons (extracted osu.svg, added Club Penguin button)
- Update Privacy Policy content and clean up its template layout
- Let Gemini write this commit message because I am too lazy to write up all the changes myself
2026-11-19 01:19:31 +01:00

53 lines
1.3 KiB
TypeScript

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) => `<img src="${this.escapeHtmlAttr(url)}" alt="Gallery image" loading="lazy">`)
.join('');
return `<div class="gallery">${images}</div>`;
})
.replace(
/\[center\]\s*([\s\S]*?)\s*\[\/center\]/g,
(_, inner: string) => `<div class="centered-text">${inner}</div>`
);
}
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, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
}