Files
dataroom/lib/utils/sanitize-html.ts
zeekayandMarc Seitz cb113713e0 fix: increase Q&A message limit to 4000 chars and surface validation errors
The admin was unable to reply to conversations because the 1000 character
limit was too restrictive for dataroom Q&A. This commit:

- Increases the default message character limit from 1000 to 4000
- Surfaces the actual validation error message (e.g. 'Content cannot be
  longer than 4000 characters') through the API instead of returning a
  generic 'Internal server error'
- Parses and displays the API error in the frontend toast notification
- Adds a character counter to the admin reply form with visual feedback
  when approaching/exceeding the limit
- Adds maxLength to viewer-side input fields for client-side enforcement

Co-authored-by: Marc Seitz <[email protected]>
2026-03-02 05:08:58 +00:00

36 lines
1004 B
TypeScript

import sanitizeHtml from "sanitize-html";
import { decodeHTML } from "entities";
const plainTextSanitizeConfig = {
allowedTags: [],
allowedAttributes: {},
};
const controlCharsRegex = /[\u0000-\u001F\u007F-\u009F]/g;
const invisibleControlRegex = /[\u200B-\u200D\uFEFF\u202A-\u202E\u2066-\u2069]/g;
export function sanitizePlainText(content: string) {
const sanitized = sanitizeHtml(content, plainTextSanitizeConfig);
const decoded = decodeHTML(sanitized).normalize("NFC");
return decoded
.replace(controlCharsRegex, " ")
.replace(invisibleControlRegex, "")
.trim();
}
export const MAX_MESSAGE_LENGTH = 4000;
export function validateContent(html: string, length: number = MAX_MESSAGE_LENGTH) {
if (html.length > length) {
throw new Error(`Content cannot be longer than ${length} characters`);
}
const sanitized = sanitizePlainText(html);
if (sanitized.length === 0 || sanitized === "") {
throw new Error("Content cannot be empty");
}
return sanitized;
}