I posted this question in an old thread that was related, but I’ve concluded that it’s better to start a new thread.
I don’t have much experience with running JS from Python, but my intuition would be that running node.js as a subprocess would be the easiest approach (because it doesn’t involve any experimental/tricky code)
This is what we’ve had to do in past (shell out to node.js) (tried with MiniRacer and other runtimes in Ruby with limited success), but that’s admittedly pretty slow. What we do now is run a separate node process with a simple expressjs service for importing html server-side. For manipulation we have a simple ruby library that lets us manipulate the json.
Thanks for sharing your experience. I’ve also used direct JSON manipulation on the documents, but I’ve occasionally produced docs that violate the schema when the editor tries to load them. It would be nice to avoid that by using the editor code on the server side.
That is the tricky part of schema’s having executable JS in them. It’d be nice if we could validate with something like the jsonschema spec that’s supported across various runtimes and languages, but I think I get why that’s not practical.
from execjs import get
runtime = get('Node')
context = runtime.compile('''
prosemirrorConverters = require('prosemirrorConverters');
htmlToJson = prosemirrorConverters.htmlToJson;
jsonToText = prosemirrorConverters.jsonToText;
jsonToHtml = prosemirrorConverters.jsonToHtml;
''' % (
os.path.join(BASE_DIR, prosemirror_dist), # ProseMirror - src js files hosted here in python static files
os.path.join(BASE_DIR, 'src/util'), # ProseMirrorConverter
os.path.join(BASE_DIR, 'src/lib'), # jsdom
# Example usage
# Runs jsonToText which is loaded above and runs with 1st argument `text_json` json object
node.plain_text = context.call('jsonToText', node.text_json)\
But it was messy (and also incurred a large cost, based on what I read about
execjs) so I decided to host a nodeJS server in parallel with my python server, which is much more maintable and usable.
A colleague of mine has done some great work to translate ProseMirror’s models and transforms to Python. It is passing the full test suite from the upstream implementation, and we will be keeping the library up to date with any changes since we will be using it in production soon. You can see it here: https://github.com/fellowinsights/prosemirror-py
Wow. Thank you very much. It will be very useful to be able to manipulate ProseMirror docs on the server side with confidence that schema integrity will be maintained.
Hello Marijn and @sciyoshi,
I have an update on this topic.
I tried the Python port of ProseMirror, but the problem for me is that my schema contains JS code that as far as I can tell will interfere with using it in Python. I could create a parallel schema as pure JSON w/o the code, but I don’t like that from a maintenance standpoint.
Fortunately I found a very nice small JS interpreter called quickjs for which a kind person has written a python binding. I’ve done a bit of testing with this combo and I find that I can load the ProseMirror model libraries and create schema and nodes.
I will continue to work on using quickjs on the server side and post an update if it works for me.