@@ -6,7 +6,7 @@ import { saveState } from "./state";
66import * as prettier from "prettier/standalone" ;
77import * as parserHtml from "prettier/plugins/html" ;
88import * as parserCss from "prettier/plugins/postcss" ;
9- import * as parserFlow from "prettier/plugins/flow" ;
9+ import * as parserBabel from "prettier/plugins/babel" ; // Use Babel instead of Flow
1010import * as prettierPluginEstree from "prettier/plugins/estree" ;
1111
1212export function runCode ( ) : void {
@@ -58,13 +58,13 @@ export function runCode(): void {
5858 "</style>" ,
5959 "<script>" ,
6060 consoleInterceptor ,
61- "</script>" ,
61+ "<\ /script>" ,
6262 "</head><body>" ,
6363 html ,
64- "</body>" ,
64+ "<\ /body>" ,
6565 "<script>" ,
6666 js ,
67- "</script></html>" ,
67+ "<\ /script><\ /html>" ,
6868 ] . join ( "" ) ;
6969 }
7070
@@ -87,42 +87,97 @@ export function runCode(): void {
8787
8888export async function formatCode ( ) : Promise < void > {
8989 try {
90- const formattedHtml = await prettier . format (
91- editors . html . view . state . doc . toString ( ) ,
92- {
93- parser : "html" ,
94- plugins : [ parserHtml ] ,
95- printWidth : 100 ,
96- tabWidth : 2 ,
97- htmlWhitespaceSensitivity : "css" ,
90+ // Format each editor separately with error handling
91+ let formattedHtml = editors . html . view . state . doc . toString ( ) ;
92+ let formattedCss = editors . css . view . state . doc . toString ( ) ;
93+ let formattedJs = editors . js . view . state . doc . toString ( ) ;
94+
95+ // Format HTML
96+ try {
97+ if ( formattedHtml . trim ( ) ) {
98+ // First normalize the HTML by removing extra whitespace
99+ const normalizedHtml = formattedHtml . trim ( ) . replace ( / ^ \s + / gm, '' ) ;
100+
101+ formattedHtml = await prettier . format ( normalizedHtml , {
102+ parser : "html" ,
103+ plugins : [ parserHtml ] ,
104+ printWidth : 120 ,
105+ tabWidth : 4 ,
106+ htmlWhitespaceSensitivity : "ignore" ,
107+ bracketSameLine : true ,
108+ singleAttributePerLine : false ,
109+ } ) ;
110+
111+ // Ensure the formatted HTML is well-formed
112+ formattedHtml = formattedHtml . replace ( / > \n \s * \n / g, '>\n' ) ;
113+
114+ // Remove two spaces from the beginning of each line
115+ formattedHtml = formattedHtml . replace ( / ^ / gm, '' ) ;
116+
98117 }
99- ) ;
100- const formattedCss = await prettier . format (
101- editors . css . view . state . doc . toString ( ) ,
102- {
103- parser : "css" ,
104- plugins : [ parserCss ] ,
105- printWidth : 100 ,
106- tabWidth : 2 ,
118+ } catch ( error ) {
119+ console . warn ( "HTML formatting skipped:" , error ) ;
120+ // Keep original HTML if formatting fails
121+ }
122+
123+ // Format CSS
124+ try {
125+ if ( formattedCss . trim ( ) ) {
126+ formattedCss = await prettier . format ( formattedCss , {
127+ parser : "css" ,
128+ plugins : [ parserCss ] ,
129+ printWidth : 100 ,
130+ tabWidth : 2 ,
131+ } ) ;
132+ // Remove two spaces from the beginning of each line
133+ formattedCss = formattedCss . replace ( / ^ / gm, '' ) ;
134+ }
135+ } catch ( error ) {
136+ console . warn ( "CSS formatting failed:" , error ) ;
137+ }
138+
139+ // Format JavaScript with better error handling
140+ try {
141+ if ( formattedJs . trim ( ) ) {
142+ formattedJs = await prettier . format ( formattedJs , {
143+ parser : "babel" , // Use babel instead of flow
144+ plugins : [
145+ parserBabel ,
146+ ( prettierPluginEstree as any ) . default || prettierPluginEstree ,
147+ ] ,
148+ printWidth : 100 ,
149+ tabWidth : 2 ,
150+ semi : true ,
151+ singleQuote : true ,
152+ trailingComma : "es5" ,
153+ bracketSpacing : true ,
154+ } ) ;
155+ // Remove two spaces from the beginning of each line
156+ formattedJs = formattedJs . replace ( / ^ / gm, '' ) ;
107157 }
108- ) ;
109- const formattedJs = await prettier . format (
110- editors . js . view . state . doc . toString ( ) ,
111- {
112- parser : "flow" ,
113- plugins : [
114- parserFlow ,
115- ( prettierPluginEstree as any ) . default || prettierPluginEstree ,
116- ] ,
117- printWidth : 100 ,
118- tabWidth : 2 ,
119- semi : true ,
120- singleQuote : true ,
121- trailingComma : "es5" ,
122- bracketSpacing : true ,
158+ } catch ( error ) {
159+ console . warn ( "JavaScript formatting failed:" , error ) ;
160+ // Try with a simpler parser as fallback
161+ try {
162+ formattedJs = await prettier . format ( formattedJs , {
163+ parser : "babel-ts" , // Alternative parser
164+ plugins : [
165+ parserBabel ,
166+ ( prettierPluginEstree as any ) . default || prettierPluginEstree ,
167+ ] ,
168+ printWidth : 100 ,
169+ tabWidth : 2 ,
170+ semi : true ,
171+ singleQuote : true ,
172+ } ) ;
173+ // Remove two spaces from the beginning of each line
174+ formattedJs = formattedJs . replace ( / ^ / gm, '' ) ;
175+ } catch ( fallbackError ) {
176+ console . warn ( "Fallback JavaScript formatting also failed:" , fallbackError ) ;
123177 }
124- ) ;
178+ }
125179
180+ // Update editors
126181 editors . html . view . dispatch ( {
127182 changes : {
128183 from : 0 ,
0 commit comments