Do not load the marked library or custom parser unless needed.
// In your routing module
path: 'converter',
loadChildren: () => import('./bh-converter/bh-converter.module').then(m => m.BhConverterModule)
Para hacer tu herramienta "BH Text to HTML" aún más potente, añade estas funcionalidades:
Instead of downloading a potentially unsafe "bh" library, here is the industry-standard way to handle "Text to HTML" in Angular, compliant with Mozilla browser standards.
The Problem:
You have a string: "Hello \n World".
You want it to render as: Hello <br> World.
The Solution: Create a custom Pipe. This is safer and more efficient than a generic library. descargar bh text to html mozilla angular
import Pipe, PipeTransform from '@angular/core'; import DomSanitizer, SafeHtml from '@angular/platform-browser';@Pipe( name: 'textToHtml' ) export class TextToHtmlPipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) {}
transform(value: string): SafeHtml // 1. Escape HTML characters to prevent XSS (Security First) let escapedValue = value .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'");
// 2. Convert plain text formatting (newlines) to HTML let htmlContent = escapedValue.replace(/\n/g, '<br>'); // 3. Bypass Angular sanitization ONLY because we sanitized it manually in step 1 return this.sanitizer.bypassSecurityTrustHtml(htmlContent);
}
Usage in Template:
<div [innerHTML]="userInput | textToHtml"></div>
git clone https://github.com/your-org/bh-text-to-html.git
cd bh-text-to-html
npm run build
npm link # creates a local package
Then in your Angular project:
npm link bh-text-to-html
// src/app/services/bh-converter.service.ts import Injectable from '@angular/core'; import Subject, Observable from 'rxjs';@Injectable( providedIn: 'root' ) export class BhConverterService private conversionSubject = new Subject<string>();
constructor() // Detect Firefox for specific handling const isFirefox = navigator.userAgent.includes('Firefox'); if (isFirefox) console.log('BH Converter: Firefox mode active'); Do not load the marked library or custom
convert(text: string, options?: preserveLines?: boolean ): Observable<string> // Simulate BH logic – replace with actual lib call try // Example if using window.BHTextToHTML // const html = window.BHTextToHTML.parse(text, options);
// Mock conversion (replace with real BH method) let html = text .replace(/\[b\](.*?)\[\/b\]/g, '<strong>$1</strong>') .replace(/\[i\](.*?)\[\/i\]/g, '<em>$1</em>') .replace(/\n/g, options?.preserveLines ? '<br>' : ' '); this.conversionSubject.next(html); return new Observable(sub => sub.next(html)); catch (err) console.error('BH conversion error', err); throw err;