import { sanitize } from 'isomorphic-dompurify'
/**
* Formats a bio string: escapes HTML, autolinks URLs, converts newlines to
,
* then sanitizes with DOMPurify (only and
allowed).
*/
export function formatBio(bio: string): string {
if (!bio) return ''
// Step 1: Escape HTML entities
let result = bio
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
// Step 2: Autolink URLs (only http:// and https://)
// Display text strips protocol prefix and trailing slash for cleaner appearance.
result = result.replace(/https?:\/\/[^\s<]+/g, (url) => {
const display = url.replace(/^https?:\/\//, '').replace(/\/$/, '')
return `${display}`
})
// Step 3: Convert newlines to
result = result.replace(/\n/g, '
')
// Step 4: Sanitize (only allow and
)
return sanitize(result, {
ALLOWED_TAGS: ['a', 'br'],
ALLOWED_ATTR: ['href', 'rel'],
})
}