diff --git a/cssToHtml.js b/cssToHtml.js index 5561ff6..9f821fd 100644 --- a/cssToHtml.js +++ b/cssToHtml.js @@ -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' @@ -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 } diff --git a/elementOfHtml.js b/elementOfHtml.js index 48b5621..7ffff2b 100644 --- a/elementOfHtml.js +++ b/elementOfHtml.js @@ -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 += '' - } + if (this.#selfCloseTag) return + + this.#string += '' } searchInnerElements(elements, searchIndex) { for (++searchIndex; searchIndex < elements.length; searchIndex++) { diff --git a/formatWorker.js b/formatWorker.js deleted file mode 100644 index 9607bcb..0000000 --- a/formatWorker.js +++ /dev/null @@ -1,6 +0,0 @@ -import { runAsWorker } from 'synckit' -import * as prettier from "prettier" - -runAsWorker(async (newContent) => { - return prettier.format(newContent, { parser: "html" }) -}) \ No newline at end of file diff --git a/package.json b/package.json index 7d3e2d1..66bb165 100644 --- a/package.json +++ b/package.json @@ -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" }