-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysisFormatter.ts
More file actions
40 lines (30 loc) · 1.73 KB
/
Copy pathanalysisFormatter.ts
File metadata and controls
40 lines (30 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Enforces a strict layout for the Tactical Analysis text.
// Fixes AI artifacts like "## HeaderText" (glued), "/ Header", or missing newlines.
export const formatTacticalAnalysis = (text: string): string => {
if (!text) return "";
let formatted = text;
// 0. Pre-cleaning: Remove starting slashes that AI sometimes adds (e.g. "/ Comportamento")
formatted = formatted.replace(/^\s*\/\s*/gm, '');
// List of known section headers in Supported Languages to enforce
const headers = [
"Comportamento", "Sinergia", "Fraqueza", "Dica do Mestre", "Motivação", // PT
"Behavior", "Synergy", "Weakness", "GM Tips", "Motivation", // EN
"Comportamiento", "Sinergia", "Debilidad", "Consejo del GM" // ES
];
headers.forEach(header => {
// 1. Fix "Glued" Headers (e.g., "## ComportamentoO monstro")
// Pattern: ## (Header) (Any non-whitespace/newline char)
const gluedRegex = new RegExp(`(#{1,3}|\\*\\*|^)\\s*(${header})([^\\n\\r\\s])`, 'gi');
formatted = formatted.replace(gluedRegex, '$1 $2\n\n$3');
// 2. Enforce Newlines around Headers and Standardize Level 2 Header (##)
// This catches: ## Header, **Header**, Header:
const standardRegex = new RegExp(`(?:^|\\n)\\s*(?:#{1,3}|\\*\\*|\\/)\\s*(${header})\\s*(?:\\*\\*|:)?\\s*`, 'gi');
formatted = formatted.replace(standardRegex, '\n\n## $1\n\n');
});
// 3. Cleanup excessive newlines (max 2)
formatted = formatted.replace(/\n{3,}/g, '\n\n');
// 4. Ensure bolding isn't broken across lines
formatted = formatted.replace(/(\*\*|__)\n+(\*\*|__)/g, '$1$2\n');
// 5. Remove any leading newlines from the very start
return formatted.trim();
};