Skip to content

Commit 84824fc

Browse files
committed
Format the CSS using cssbeautify
Also: Avoid processing empty strings. Trim the API result.
1 parent c94b5d2 commit 84824fc

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@heroicons/react": "^1.0.6",
1313
"autoprefixer": "^10.4.7",
14+
"cssbeautify": "^0.3.1",
1415
"next": "13.4.9",
1516
"postcss": "^8.4.25",
1617
"postcss-js": "^4.0.1",
@@ -22,6 +23,7 @@
2223
"tailwindcss": "^3.0.24"
2324
},
2425
"devDependencies": {
26+
"@types/cssbeautify": "^0.3.5",
2527
"@types/node": "^17.0.36",
2628
"@types/react": "^18.0.9",
2729
"eslint": "8.16.0",

pages/api/generate-css.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import postcss from "postcss";
22
import tailwindcss from "tailwindcss";
33
import autoprefixer from "autoprefixer";
44
import { NextApiRequest, NextApiResponse } from "next";
5+
import cssbeautify from "cssbeautify";
56

67
interface RequestBody {
78
classes: string;
@@ -41,11 +42,14 @@ export default async function handler(
4142
css = "no: result;";
4243
}
4344

44-
// Format the CSS
45-
css = css.replace(/^\s+/gm, " ");
46-
css = css.replace(/^\s*\.generated/gm, ".generated");
45+
// Format the CSS using cssbeautify
46+
css = cssbeautify(css, {
47+
indent: " ",
48+
openbrace: "end-of-line",
49+
autosemicolon: true,
50+
});
4751

48-
return css;
52+
return css.trim();
4953
});
5054

5155
res.status(200).send(result);

pages/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function App() {
1919
useEffect(() => {
2020
setResult('');
2121
setResultJSS('');
22+
if (!input.trim()) return;
2223

2324
const handler = setTimeout(async () => {
2425
const resultCss = await getConvertedClasses(input);
@@ -87,7 +88,7 @@ export default function App() {
8788
value={result}
8889
readOnly
8990
></textarea>
90-
<CopyToClipboard text={result.trim()} onCopy={() => toast.success('Copied!')}>
91+
<CopyToClipboard text={result} onCopy={() => toast.success('Copied!')}>
9192
<ClipboardCopyIcon className="w-6 h-6 mt-3 text-gray-500 cursor-pointer md:mr-1" />
9293
</CopyToClipboard>
9394
</div>

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
dependencies:
191191
tslib "^2.4.0"
192192

193+
"@types/cssbeautify@^0.3.5":
194+
version "0.3.5"
195+
resolved "https://registry.yarnpkg.com/@types/cssbeautify/-/cssbeautify-0.3.5.tgz#b44fe836901b4277be27027b90ca1f9f54fd2538"
196+
integrity sha512-bkxuJdUu7Liw14EouI8IFNvJXvd5IioAZdDvlCFBXv+Sel8uPK9rcxfvOchQKqw2AtppxRvcYaWmimi0t0Yuow==
197+
193198
"@types/json5@^0.0.29":
194199
version "0.0.29"
195200
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
@@ -596,6 +601,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2:
596601
shebang-command "^2.0.0"
597602
which "^2.0.1"
598603

604+
cssbeautify@^0.3.1:
605+
version "0.3.1"
606+
resolved "https://registry.yarnpkg.com/cssbeautify/-/cssbeautify-0.3.1.tgz#12dd1f734035c2e6faca67dcbdcef74e42811397"
607+
integrity sha512-ljnSOCOiMbklF+dwPbpooyB78foId02vUrTDogWzu6ca2DCNB7Kc/BHEGBnYOlUYtwXvSW0mWTwaiO2pwFIoRg==
608+
599609
cssesc@^3.0.0:
600610
version "3.0.0"
601611
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee"

0 commit comments

Comments
 (0)