File tree Expand file tree Collapse file tree 4 files changed +24
-18
lines changed Expand file tree Collapse file tree 4 files changed +24
-18
lines changed Original file line number Diff line number Diff line change 11import fs from 'fs-extra'
22import path from 'path'
3- import { createSyncFn } from 'synckit'
3+ import format from "html-format"
44import { parse } from '@adobe/css-tools'
55import { createParser } from 'css-selector-parser'
66import { ElementOfHtml } from './elementOfHtml.js'
77
8- const syncFormatting = createSyncFn ( path . resolve ( './formatWorker.js' ) )
98
109export class CssToHtml {
1110 static ENCODING = 'utf8'
@@ -101,20 +100,31 @@ export class CssToHtml {
101100 if ( this . #writeInFile) {
102101 newContent = this . #html. substring ( 0 , contentStartIndex )
103102
103+ if ( contentStartIndex )
104+ newContent += '\n'
105+
104106 for ( let element of this . #elements) {
105- newContent += element . string + '\n'
107+ newContent += element . string
106108 }
107109
108110 newContent += this . #html. substring ( contentEndIndex )
109111 }
110112 else {
111113 for ( let element of this . #elements) {
112- newContent += element . string + '\n'
114+ newContent += element . string
113115 }
114116 }
115117
116118 if ( this . format ) {
117- return syncFormatting ( newContent )
119+ try {
120+ return format ( newContent , ' ' )
121+ }
122+ catch ( error ) {
123+ throw new Error (
124+ `An error occurred during formatting, check your code!
125+ Perhaps this happened because the code that was converted turned out to be incorrect.`
126+ )
127+ }
118128 } else {
119129 return newContent
120130 }
Original file line number Diff line number Diff line change @@ -76,20 +76,23 @@ export class ElementOfHtml {
7676 this . #string += this . textBefore ? ` ${ this . textBefore } ` : ''
7777 }
7878 #addTextAfter( ) {
79- this . #string += this . textAfter ? `${ this . textAfter } ` : ''
79+ this . #string += this . textAfter ? `${ this . textAfter } \n ` : '\n '
8080 }
8181 #addText( ) {
8282 this . #string += this . text ? `${ this . text } ` : ''
8383 }
8484 #addInnerElements( ) {
85+ if ( this . innerElements . length > 0 ) {
86+ this . #string += '\n'
87+ }
8588 for ( let innerElement of this . innerElements ) {
86- this . #string += '\n' + innerElement . string
89+ this . #string += innerElement . string
8790 }
8891 }
8992 #createEndString( ) {
90- if ( ! this . #selfCloseTag) {
91- this . #string += '</' + this . tag + '>'
92- }
93+ if ( this . #selfCloseTag) return
94+
95+ this . #string += '</' + this . tag + '>'
9396 }
9497 searchInnerElements ( elements , searchIndex ) {
9598 for ( ++ searchIndex ; searchIndex < elements . length ; searchIndex ++ ) {
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2828 "@adobe/css-tools" : " ^4.4.0" ,
2929 "css-selector-parser" : " ^3.0.5" ,
3030 "fs-extra" : " ^11.2.0" ,
31- "prettier" : " ^3.3.3" ,
32- "synckit" : " ^0.9.2"
31+ "html-format" : " ^1.1.7"
3332 },
3433 "license" : " ISC"
3534}
You can’t perform that action at this time.
0 commit comments