| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- function stripCodeFences(content) {
- return String(content || '')
- .replace(/```(?:html)?\s*/gi, '')
- .replace(/```/g, '')
- .trim()
- }
- function escapeHtml(content) {
- return String(content || '')
- .replace(/&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/"/g, '"')
- .replace(/'/g, ''')
- }
- function plainTextToHtml(content) {
- return String(content || '')
- .split(/\n{2,}/)
- .map(part => part.trim())
- .filter(Boolean)
- .map(part => `<p>${escapeHtml(part).replace(/\n/g, '<br>')}</p>`)
- .join('')
- }
- export function prepareAIWritingEditorHtml(content) {
- let html = stripCodeFences(content)
- if (!html) return ''
- const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i)
- if (bodyMatch) {
- html = bodyMatch[1].trim()
- }
- const firstContentTag = html.search(/<(article|section|main|div|h[1-6]|p|table|ul|ol)\b/i)
- if (firstContentTag > 0 && html.slice(0, firstContentTag).trim()) {
- html = html.slice(firstContentTag)
- }
- html = html
- .replace(/<!DOCTYPE[^>]*>/gi, '')
- .replace(/<html[^>]*>/gi, '')
- .replace(/<\/html>/gi, '')
- .replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
- .replace(/<body[^>]*>/gi, '')
- .replace(/<\/body>/gi, '')
- .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
- .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
- .replace(/<meta[^>]*>/gi, '')
- .replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
- .trim()
- if (!/<[^>]+>/.test(html)) {
- return plainTextToHtml(html)
- }
- return html
- }
|