penrose icon indicating copy to clipboard operation
penrose copied to clipboard

Run in non-browser environments

Open hyperupcall opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

I ran through the steps in Language API, but it seems it requires a browser environment:

$ node index.js
/home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/node_modules/.pnpm/[email protected]/node_modules/mathjax-full/js/adaptors/browserAdaptor.js:6
    return new HTMLAdaptor_js_1.HTMLAdaptor(window);
                                            ^

ReferenceError: window is not defined
    at browserAdaptor (/home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/node_modules/.pnpm/[email protected]/node_modules/mathjax-full/js/adaptors/browserAdaptor.js:6:45)
    at mathjaxInit (file:///home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/node_modules/.pnpm/@[email protected]/node_modules/@penrose/core/dist/utils/CollectLabels.js:12:21)
    at compile (file:///home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/node_modules/.pnpm/@[email protected]/node_modules/@penrose/core/dist/index.js:193:25)
    at async doPenrose (file:///home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/index.js:69:19)
    at async file:///home/edwin/Documents/Repositories/big-blocks/marked-penrose/testing/index.js:85:1

Node.js v18.18.0

Describe the solution you'd like

Have the ability to run Penrose within Node.js, without a browser environment

Describe alternatives you've considered

Not allowing penrose diagrams to be generated server-side / without the browser.

Additional context

N/A

hyperupcall avatar Dec 21 '23 20:12 hyperupcall

Great question, thanks for opening this issue! This is definitely possible; for instance, in our @penrose/roger CLI we achieve this via jsdom; @wodeni do we have any doc pages about this?

samestep avatar Dec 21 '23 21:12 samestep

Yes, thanks for asking @hyperupcall! This page has an example of using jsdom. Our hope is to make core browser/node agnostic, and will look into configuring MathJax automatically based on the environment. In the meantime, let us know if you bump into any problems with jsdom!

wodeni avatar Dec 21 '23 22:12 wodeni

Another possible issue is the Canvas API, which we use for text measurement. We use node-canvas to shim that, see example in roger.

wodeni avatar Dec 21 '23 22:12 wodeni

Thank you for the quick reply! Admittedly I skipped over the "bundler" page, and using something like global-jsdom makes sense. I'll take a look at using global-jsdom - it should solve most problems. Like you mention, in the future, it would be good to make core brower/node agnostic.

hyperupcall avatar Dec 22 '23 20:12 hyperupcall