Skip to content

Commit

Permalink
[fix] The formatter and formatting style have been changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulyanov-programmer committed Oct 10, 2024
1 parent dc17495 commit f7504dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
20 changes: 15 additions & 5 deletions cssToHtml.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from 'fs-extra'
import path from 'path'
import { createSyncFn } from 'synckit'
import format from "html-format"
import { parse } from '@adobe/css-tools'
import { createParser } from 'css-selector-parser'
import { ElementOfHtml } from './elementOfHtml.js'

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

export class CssToHtml {
static ENCODING = 'utf8'
Expand Down Expand Up @@ -101,20 +100,31 @@ export class CssToHtml {
if (this.#writeInFile) {
newContent = this.#html.substring(0, contentStartIndex)

if (contentStartIndex)
newContent += '\n'

for (let element of this.#elements) {
newContent += element.string + '\n'
newContent += element.string
}

newContent += this.#html.substring(contentEndIndex)
}
else {
for (let element of this.#elements) {
newContent += element.string + '\n'
newContent += element.string
}
}

if (this.format) {
return syncFormatting(newContent)
try {
return format(newContent, ' ')
}
catch (error) {
throw new Error(
`An error occurred during formatting, check your code!
Perhaps this happened because the code that was converted turned out to be incorrect.`
)
}
} else {
return newContent
}
Expand Down
13 changes: 8 additions & 5 deletions elementOfHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,23 @@ export class ElementOfHtml {
this.#string += this.textBefore ? ` ${this.textBefore}` : ''
}
#addTextAfter() {
this.#string += this.textAfter ? `${this.textAfter}` : ''
this.#string += this.textAfter ? `${this.textAfter}\n` : '\n'
}
#addText() {
this.#string += this.text ? `${this.text}` : ''
}
#addInnerElements() {
if (this.innerElements.length > 0) {
this.#string += '\n'
}
for (let innerElement of this.innerElements) {
this.#string += '\n' + innerElement.string
this.#string += innerElement.string
}
}
#createEndString() {
if (!this.#selfCloseTag) {
this.#string += '</' + this.tag + '>'
}
if (this.#selfCloseTag) return

this.#string += '</' + this.tag + '>'
}
searchInnerElements(elements, searchIndex) {
for (++searchIndex; searchIndex < elements.length; searchIndex++) {
Expand Down
6 changes: 0 additions & 6 deletions formatWorker.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"@adobe/css-tools": "^4.4.0",
"css-selector-parser": "^3.0.5",
"fs-extra": "^11.2.0",
"prettier": "^3.3.3",
"synckit": "^0.9.2"
"html-format": "^1.1.7"
},
"license": "ISC"
}

0 comments on commit f7504dd

Please sign in to comment.