25 lines
659 B
TypeScript
25 lines
659 B
TypeScript
const RICH_TEXT_TAG = /<(?:p|h[1-6]|ul|ol|li|blockquote|strong|em|u|s|a|br|hr|span)\b/i
|
|
|
|
export function escapeRichText(value: string) {
|
|
return value
|
|
.replaceAll('&', '&')
|
|
.replaceAll('<', '<')
|
|
.replaceAll('>', '>')
|
|
.replaceAll('"', '"')
|
|
.replaceAll("'", ''')
|
|
}
|
|
|
|
export function toEditorHtml(value: string) {
|
|
if (!value.trim()) return '<p></p>'
|
|
if (RICH_TEXT_TAG.test(value)) return value
|
|
|
|
return value
|
|
.split(/\n{2,}/)
|
|
.map(paragraph => `<p>${escapeRichText(paragraph).replaceAll('\n', '<br>')}</p>`)
|
|
.join('')
|
|
}
|
|
|
|
export function toDisplayHtml(value: string) {
|
|
return toEditorHtml(value || '')
|
|
}
|