Skip to content

Commit

Permalink
🚸 [editor] User prettier to format the output JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-liu committed Feb 12, 2025
1 parent c4a357e commit 4f328f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
16 changes: 16 additions & 0 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"clsx": "^2.1.1",
"esbuild-wasm": "^0.25.0",
"monaco-editor": "^0.52.2",
"prettier": "^3.5.0",
"prism-react-renderer": "^2.4.1",
"react": "^19.0.0",
"react-dom": "^19.0.0"
Expand Down
17 changes: 14 additions & 3 deletions docs/src/editor/karabiner.worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as esbuild from 'esbuild-wasm'
import { format } from 'prettier'
import prettierBabelPlugin from 'prettier/plugins/babel'
import prettierEstreePlugin from 'prettier/plugins/estree'

import * as lib from '../../../dist/index'

Expand Down Expand Up @@ -26,9 +29,17 @@ self.onmessage = async (event) => {
}),
{ description: '', manipulators: [] },
)
self.postMessage({ input, output: JSON.stringify(config, null, 2) })
format(JSON.stringify(config), {
parser: 'json',
plugins: [prettierBabelPlugin, prettierEstreePlugin],
}).then(
(output) => self.postMessage({ input, output }),
(error) => self.postMessage({ input, output: error?.message }),
)
} catch (e) {
let error = (e as Error)?.message || 'Unknown Error'
self.postMessage({ input, output: JSON.stringify({ error }, null, 2) })
self.postMessage({
input,
output: (e as Error)?.message || 'Unknown Error',
})
}
}

0 comments on commit 4f328f9

Please sign in to comment.