Skip to content

Commit a1dda34

Browse files
committed
feat(tasty): debug css prettifier * 2
1 parent 11a8463 commit a1dda34

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/tasty/debug.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ function prettifyCSS(css: string): string {
1212
return '';
1313
}
1414

15-
let formatted = css
16-
// Normalize whitespace first
17-
.replace(/\s+/g, ' ')
18-
.trim()
19-
// Add newlines after opening braces
20-
.replace(/\s*\{\s*/g, ' {\n')
21-
// Add newlines after semicolons
22-
.replace(/;\s*/g, ';\n')
23-
// Add newlines before closing braces
24-
.replace(/\s*\}\s*/g, '\n}\n')
25-
// Handle comma-separated selectors (but not inside strings)
26-
.replace(/,(?![^"]*"[^"]*$)/g, ',\n')
27-
// Clean up media queries
28-
.replace(/@media\s+([^{]+?)\s*\{/g, '@media $1 {');
15+
// First, normalize whitespace but preserve structure
16+
let formatted = css.replace(/\s+/g, ' ').trim();
17+
18+
// Add newlines after opening braces
19+
formatted = formatted.replace(/\s*\{\s*/g, ' {\n');
20+
21+
// Add newlines after semicolons (but not inside strings or functions)
22+
formatted = formatted.replace(/;(?![^"']*["'][^"']*$)(?![^()]*\))/g, ';\n');
23+
24+
// Add newlines before closing braces
25+
formatted = formatted.replace(/\s*\}\s*/g, '\n}\n');
26+
27+
// Handle comma-separated selectors (only outside of property values)
28+
// This regex looks for commas that are:
29+
// 1. Not inside quotes
30+
// 2. Not inside parentheses (CSS functions)
31+
// 3. Not followed by a colon (not in a property value)
32+
formatted = formatted.replace(
33+
/,(?![^"']*["'][^"']*$)(?![^()]*\))(?=.*:.*\{|.*\{)/g,
34+
',\n',
35+
);
2936

3037
// Process line by line for proper indentation
3138
const lines = formatted.split('\n');
@@ -54,12 +61,17 @@ function prettifyCSS(css: string): string {
5461
return result;
5562
});
5663

57-
// Clean up the result
58-
return formattedLines
64+
// Clean up the result and ensure proper spacing
65+
let result = formattedLines
5966
.filter((line) => line.trim()) // Remove empty lines
6067
.join('\n')
6168
.replace(/\n{3,}/g, '\n\n') // Max 2 consecutive newlines
6269
.trim();
70+
71+
// Final cleanup: ensure single spaces in function calls
72+
result = result.replace(/,\s+/g, ', ');
73+
74+
return result;
6375
}
6476

6577
/**

0 commit comments

Comments
 (0)