Skip to content

Handle Math_span and Math_block constructs #886

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

Merged
merged 1 commit into from
Aug 31, 2022
Merged

Handle Math_span and Math_block constructs #886

merged 1 commit into from
Aug 31, 2022

Conversation

gpetiot
Copy link
Collaborator

@gpetiot gpetiot commented Jul 5, 2022

This PR is based on #811 (I don't have the rights to push to it)

  • rebased on top of main branch of odoc-parser to handle Math_span and Math_block
  • vendored the katex files similarly to what is done for highlight.js (no need to check for the integrity of the files once they are vendored)

@jonludlam
Copy link
Member

Looking good so far! I think there are a few things we still need to do on this:

  1. We need some tests
  2. I think we're missing the fonts in the vendored files
  3. There's no distinction between display and inline maths
  4. It'd be nice to only add the scripts to the html if the page actually contains some math content.

@jonludlam
Copy link
Member

I've released odoc-parser 2.0.0 with math support, so the CI should be a little happier now!

Copy link
Collaborator

@Julow Julow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Some comments:

  • Katex complains with

    downloadable font: download failed (font-family: "KaTeX_Size1" style:normal weight:400 stretch:100 src index:0): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Size1-Regular.woff2
    downloadable font: download failed (font-family: "KaTeX_Main" style:normal weight:400 stretch:100 src index:0): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Main-Regular.woff2
    downloadable font: download failed (font-family: "KaTeX_Size1" style:normal weight:400 stretch:100 src index:1): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Size1-Regular.woff
    downloadable font: download failed (font-family: "KaTeX_Main" style:normal weight:400 stretch:100 src index:1): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Main-Regular.woff
    downloadable font: download failed (font-family: "KaTeX_Size1" style:normal weight:400 stretch:100 src index:2): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Size1-Regular.ttf
    downloadable font: download failed (font-family: "KaTeX_Main" style:normal weight:400 stretch:100 src index:2): status=2152857618 source: file:///home/juloo/w/odoc/test/generators/html/fonts/KaTeX_Main-Regular.ttf 
    

    There should be a way to specify a font family to use/not use. Otherwise, we'll need to vendor it too.

  • Create symlinks inside the generator tests output directory, to be able to open the browser on the test results:

    ln -s ../../../src/vendor/katex.min.css test/generators/html/katex.min.css
    ln -s ../../../src/vendor/katex.min.js test/generators/html/katex.min.js
    
  • When a block doesn't render (for whatever reason), the result is unreadable. The example in the tests renders to this:

    % \f is defined as #1f(#2) using the macro \f\relax{x} = \int_{-\infty}^\infty \f\hat\xi\,e^{2 \pi i \xi x} \,d\xi
    

    I suggest wrapping math in a <code> element until they are rendered.

  • In the test, the second math element doesn't render, perhaps due to the font issue ?

@gpetiot gpetiot marked this pull request as draft July 12, 2022 19:11
@gpetiot gpetiot marked this pull request as ready for review July 22, 2022 17:43
Copy link
Collaborator

@Julow Julow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When a block fails to render, the result is still a mess. Intermediate elements should be <code> for inline elements and <pre> for blocks instead of <span> for both currently.

  • Are all these font files necessary ? Can we get away with shipping just the .ttfs or fetching them from the internet ?

@gpetiot
Copy link
Collaborator Author

gpetiot commented Jul 29, 2022

  • Are all these font files necessary ? Can we get away with shipping just the .ttfs or fetching them from the internet ?

Based on https://katex.org/docs/font.html I would be more tempted to only keep the .woff files (and they're smaller than ttf).

@dbuenzli
Copy link
Contributor

Based on https://katex.org/docs/font.html I would be more tempted to only keep the .woff files (and they're smaller than ttf).

Just keep the .woff2 fonts, browser support is widespread enough.

@gpetiot gpetiot requested a review from Julow July 29, 2022 18:04
@jonludlam
Copy link
Member

The block math example is a bit broken - in the original katex example it relies on a macro declared as an option to the katex renderer (which can be seen by clicking the cog in the example):

{
    "\\f": "#1f(#2)"
}

Since I don't think we want a way to provide out-of-band macros like this we could just use \newcommand which appears to be supported - so the example becomes:

% \f is defined as #1f(#2) using the macro
\newcommand{\f}[2]{#1f(#2)}
\f\relax{x} = \int_{-\infty}^\infty
\f\hat\xi\,e^{2 \pi i \xi x}
\,d\xi

then it renders as it does on the katex home page.

@jonludlam jonludlam mentioned this pull request Aug 22, 2022
@giltho
Copy link
Contributor

giltho commented Aug 22, 2022

PR lgtm (although I'm not an odoc expert at all).
Thank you so much for taking over my half-baked work!

Curious @jonludlam why are you unhappy with out-of-band macros?
I'd actually be happy to be able to provide a macro file on the side, in whichever format, which would be injected everywhere

@jonludlam
Copy link
Member

Curious @jonludlam why are you unhappy with out-of-band macros? I'd actually be happy to be able to provide a macro file on the side, in whichever format, which would be injected everywhere

Let's just say I'm happy not to have to support this until we've got an extremely compelling use case :-)

@giltho
Copy link
Contributor

giltho commented Aug 24, 2022

I think that's fair yes :)

@gpetiot gpetiot requested review from jonludlam and removed request for Julow August 26, 2022 16:18
@jonludlam
Copy link
Member

I think this is good now, thanks!

@jonludlam jonludlam merged commit a52dd8e into ocaml:master Aug 31, 2022
@gpetiot gpetiot deleted the katex-support-2 branch August 31, 2022 11:33
mseri pushed a commit to ocaml/opam-repository that referenced this pull request Dec 13, 2022
CHANGES:

Additions
- New unstable option `--as-json` for the HTML renderer that emits HTML
  fragments (preamble, content) together with metadata (table of contents,
  breadcrumbs, whether katex is used) in JSON format. (@sabine, ocaml/odoc#908)
- New maths support via `{m ... }` and `{math ... }` tags. (@giltho, @gpetiot, ocaml/odoc#886)
- Various optimisations (@jonludlam, ocaml/odoc#870, ocaml/odoc#883)
- Better handling of alerts and deprecation notices. (@panglesd, ocaml/odoc#828)
- Handle language tags on code blocks (@Julow, ocaml/odoc#848)

Bugfixes
- Shadowing issues (@jonludlam, ocaml/odoc#853)
- Layout fixes and improvements (@panglesd, ocaml/odoc#832, ocaml/odoc#839, ocaml/odoc#847)
- Handle comments on class constraints and inherit (@Julow, ocaml/odoc#844)
- Disable the missing root warning (@jonludlam, ocaml/odoc#881)
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

Successfully merging this pull request may close these issues.

5 participants