Files
NightSpire/web-next/lib/forum-sanitize.ts
T
Inna 1de1002938 Foro: edición/borrado de posts, moderación de temas y búsqueda
Portado desde forum/db.py + forum/permissions.py de Django:
- lib/forum-perm.ts: forumIsModerator (gmlevel >= FORUM_MOD_GMLEVEL) y
  canEditPost (autor o moderador).
- Edición y borrado lógico de posts (autor o mod), respetando tema bloqueado,
  con validación de longitud mínima (plainLength).
- Moderación de temas (solo mod): bloquear/desbloquear, fijar/no fijar, borrar.
- Búsqueda de temas por título/contenido (searchTopics/countSearchTopics)
  con página /forum/search y buscador en el índice.
- API: PATCH/DELETE /api/forum/post, POST /api/forum/moderate (401/403 correctos).
- UI: PostActions, TopicModBar, ForumSearchBox; marca "editado" y "[Fijado]".
- i18n: 22 claves nuevas en es/en.

Verificado: build OK, /forum y /forum/search 200, APIs 401 sin sesión.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 00:28:39 +00:00

37 lines
1.2 KiB
TypeScript

import sanitizeHtml from 'sanitize-html'
// Misma allowlist que forum/sanitize.py (nh3) para el HTML de los mensajes.
export function cleanPostHtml(html: string): string {
if (!html) return ''
return sanitizeHtml(html, {
allowedTags: [
'p', 'br', 'hr', 'span', 'div',
'strong', 'b', 'em', 'i', 'u', 's', 'strike', 'sub', 'sup',
'ul', 'ol', 'li', 'blockquote', 'code', 'pre',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'a', 'img',
'table', 'thead', 'tbody', 'tr', 'th', 'td',
],
allowedAttributes: {
a: ['href', 'title', 'target'],
img: ['src', 'alt', 'title', 'width', 'height'],
span: ['style'],
div: ['style'],
td: ['colspan', 'rowspan'],
th: ['colspan', 'rowspan'],
},
allowedSchemes: ['http', 'https', 'mailto'],
transformTags: {
a: sanitizeHtml.simpleTransform('a', { rel: 'noopener noreferrer nofollow' }),
},
})
}
/** Longitud del texto plano (sin etiquetas) — para validar longitud mínima. */
export function plainLength(html: string): number {
return sanitizeHtml(html || '', { allowedTags: [], allowedAttributes: {} })
.replace(/&nbsp;/g, ' ')
.replace(/\s+/g, ' ')
.trim().length
}