49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
export function stripHtml(html: string): string {
|
|
if (!html) return ''
|
|
return html
|
|
.replace(/<[^>]*>/g, '')
|
|
.replace(/ /g, ' ')
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
}
|
|
|
|
export function extractFirstSentence(text: string, maxLen = 200): string {
|
|
const cleaned = stripHtml(text)
|
|
const match = cleaned.match(/^[^.!?]+[.!?]/)
|
|
if (match) return match[0].slice(0, maxLen)
|
|
return cleaned.slice(0, maxLen)
|
|
}
|
|
|
|
export function stripHtmlTags(html: string): string {
|
|
return stripHtml(html)
|
|
}
|
|
|
|
export function parseQuillList(html: string): string[] {
|
|
if (!html) return []
|
|
const items: string[] = []
|
|
const liRegex = /<li[^>]*>([\s\S]*?)<\/li>/gi
|
|
let match
|
|
while ((match = liRegex.exec(html)) !== null) {
|
|
const text = match[1].replace(/<[^>]*>/g, '').trim()
|
|
if (text) items.push(text)
|
|
}
|
|
return items
|
|
}
|
|
|
|
export function criteriaToJson(html: string): string {
|
|
return JSON.stringify(parseQuillList(html))
|
|
}
|
|
|
|
export function criteriaFromJson(json: string): string[] {
|
|
try {
|
|
return JSON.parse(json)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|