Skip to content

NPM #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bkardell opened this issue Mar 10, 2025 · 6 comments
Open

NPM #10

bkardell opened this issue Mar 10, 2025 · 6 comments

Comments

@bkardell
Copy link

I honestly didn't know which repo to open this in, so I'm just reflecting the one from doersino#5

All of the examples here are using the DOM, but most repositories that have something like this provide a simple string transformation function API where I can say something like unicodemathml("1+▭(⟡(1&1/2/3/4/5))") and get back a string of MathML. This makes integration with a lot of packages very easy - but I don't see anything documented about such an API. I did some exploring and was able to find/modify (from peg) https://github.com/MurrayIII/UnicodeMathML/blob/main/utils/generate-parser.html#L32 to use the format commonjs, but when I call this method I get

{
  mathml: '<math display="block"><mstyle mathcolor="#F00"><mtext>⁅1+▭(⟡(1&1/2/3/4/5))⁆</mtext></mstyle></math>',
  details: {
    measurements: {
      parse: 0.20350000000001955,
      preprocess: 0.6372080000000437,
      transform: 0.584541999999999,
      pretty: 0.10495800000001054
    },
    intermediates: {
      parse: [Object],
      preprocess: [Object],
      transform: [Object],
      json: '{"unicodemath":{"content":[{"expr":[{"colored":{"color":"#F00","of":{"text":"⁅1+▭(⟡(1&1/2/3/4/5))⁆"}}}]}],"eqnumber":null}}'
    }
  }
}

Which is wrong. Perhaps there is a problem with my build or something?

Does such a thing exist? It would be great - I'm about to publish an article about how to embed maths in markdown with LaTex or AsciiMath - it would be super to be able to include Unicode Math if we can figure it out?

@MurrayIII
Copy link
Owner

MurrayIII commented Mar 11, 2025

The function to call is unicodemathml(unicodeMath), where unicodeMath is the UnicodeMath to convert to MathML. In the program the result is usually stored in a variable t and t.mathml is the MathML corresponding to unicodeMath. You can see it used, for example, in getting MathML for the function setUnicodeMath(uMath).

@bkardell
Copy link
Author

bkardell commented Mar 12, 2025

But as you can see above, the .mathml it is returning for me is incorrect, basically unparsed and stuffed into mtext.

@MurrayIII
Copy link
Owner

MurrayIII commented Mar 12, 2025

When I paste that expression into https://murrayiii.github.io/UnicodeMathML/playground/ I see

Image

What you see is the result when the peg parser fails to parse the UnicodeMath. Maybe the parser isn't loaded? The readme.md file (Click on About in the playground) has more details on integrating UnicodeMathML into Markdeep that should be useful for integrating it into Markdown.

In the code

function unicodemathml(unicodemath, displaystyle) {
...
    try {
        uast = parse(unicodemath);
    } catch (error) {
        // Display unparsable string in red
        uast = {unicodemath: {content: [{expr: [{colored: {color: '#F00', of: {text: unicodemath}}}]}], eqnumber: null}}
        autoBuildUp = false                 // If called for autobuildup, return failure
    }

the error isn't displayed. Might be worth setting a breakpoint to see what it is. In https://github.com/pegjs/pegjs/blob/master/docs/guides/javascript-api.md, it has

format₃ "bare" format of the generated parser ("amd", "bare", "commonjs", "es", "globals", or "umd")

where ₃ says: 'The format option is always set to "umd" when output is set to "parser" '

@MurrayIII
Copy link
Owner

MurrayIII commented Mar 13, 2025 via email

@bkardell
Copy link
Author

bkardell commented Mar 21, 2025

Most of these things use markdown-it and have a common integration strategy. You can read what I wrote here https://bkardell.com/blog/11tyMath.html

It explains how to do this with 11ty, but the basic integration would work with anything that uses markdown-it (which is a lot). 11ty is a really nice way to try this out in my opinion because it is really easy to set up.... But in the end it's even more complex than we need. What we need is just a single js file that you run from the command line with nodejs

/*
  ?? how do I get a functional unicodemathml?  I'd need to import or require it...
  currently that doesn't exist.
*/
console.log(unicodemathml("1+▭(⟡(1&1/2/3/4/5))"))

If that logs an object with actual MathML in it, we win.

As I said at the top

did some exploring and was able to find/modify (from peg) https://github.com/MurrayIII/UnicodeMathML/blob/main/utils/generate-parser.html#L32

but it didn't work.

@MurrayIII
Copy link
Owner

MurrayIII commented Mar 26, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants