CodeMirror 6 + prettier?

Hi, I’m hoping to use prettier to format code in Codemirror 6. I’m not tied to Prettier specifically; I just need to format the editor’s code automatically on load and save. Could someone point me in the right direction?

Find a pretty-printer that you can run in the browser (or set it up as an HTTP service), take code from editor (view.state.doc.toString()), run it through the pretty-printer, and then either update the entire document if something changed (easy, but crude), or find the precise changes (through diffing or maybe the pretty-printing library will tell you) and apply those via view.dispatch({changes: ....}).