← GPT-AI-LP
CODE · 0.5 KB

src/document-text.js

Workspace snapshot · 08/31 23:51

export function normalizeDocumentText(value, maxLength = 60000) {
  const normalized = String(value || "")
    .replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/gu, " ")
    .replace(/\r\n?/gu, "\n")
    .split("\n")
    .map((line) => line.replace(/[\t ]+/gu, " ").trimEnd())
    .join("\n")
    .replace(/\n{4,}/gu, "\n\n\n")
    .trim();

  return {
    text: normalized.slice(0, maxLength),
    originalLength: normalized.length,
    truncated: normalized.length > maxLength,
  };
}