Skip to content

Commit f7504dd

Browse files
[fix] The formatter and formatting style have been changed.
1 parent dc17495 commit f7504dd

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

cssToHtml.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import fs from 'fs-extra'
22
import path from 'path'
3-
import { createSyncFn } from 'synckit'
3+
import format from "html-format"
44
import { parse } from '@adobe/css-tools'
55
import { createParser } from 'css-selector-parser'
66
import { ElementOfHtml } from './elementOfHtml.js'
77

8-
const syncFormatting = createSyncFn(path.resolve('./formatWorker.js'))
98

109
export 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
}

elementOfHtml.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff 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++) {

formatWorker.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
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
}

0 commit comments

Comments
 (0)