Skip to content

Commit f771318

Browse files
committed
fix #7738 -- better math rendering when printing jupyter notebooks
- loading katex css was broken, and we were stripping the katex html and falling back to crappy mathml. Now we do not strip anything, properly load the katex css, and things work properly. Note that weird cloudflare's cdn is behind on katex, so we switched to jsdeliver for katex css, and also for antd. We do not switch for codemirror, since I can't find codemirror5 css on jsdeliver.
1 parent d4753e1 commit f771318

File tree

6 files changed

+12
-17
lines changed

6 files changed

+12
-17
lines changed

src/packages/frontend/frame-editors/jupyter-editor/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export class JupyterEditorActions extends BaseActions<JupyterEditorState> {
233233
}
234234

235235
print(_id): void {
236-
this.jupyter_actions.show_nbconvert_dialog("cocalc-html");
236+
this.jupyter_actions.show_nbconvert_dialog("cocalc-pdf");
237237
}
238238

239239
async format(id: string): Promise<void> {

src/packages/frontend/jupyter/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ export function commands(actions: AllActions): {
630630

631631
"nbconvert latex pdf": {
632632
i: "tex",
633-
m: "PDF via LaTeX (.pdf)",
633+
m: "PDF via LaTeX and nbconvert (minimal image support) (.pdf)",
634634
f: () => actions.jupyter_actions?.show_nbconvert_dialog("pdf"),
635635
r: true,
636636
},

src/packages/frontend/jupyter/main.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Top-level react component, which ties everything together
88
*/
99

1010
import * as immutable from "immutable";
11-
1211
import {
1312
CSS,
1413
React,

src/packages/frontend/jupyter/nbconvert.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,13 @@ export const NBConvert: React.FC<NBConvertProps> = React.memo(
235235
<div>
236236
<br />
237237
<Button
238+
type="primary"
238239
onClick={() => {
239240
actions.file_action("open_file", target_path);
240241
close();
241242
}}
242243
>
243-
Edit exported file...
244+
Open Exported File
244245
</Button>
245246
</div>
246247
);

src/packages/frontend/jupyter/nbviewer/export.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import NBViewer from "./nbviewer";
1616
import { renderToString } from "react-dom/server";
1717
import { createElement, CSSProperties } from "react";
1818
import { FileContext } from "@cocalc/frontend/lib/file-context";
19-
import cheerio from "cheerio";
2019

2120
export default function exportToHTML({
2221
content,
@@ -43,18 +42,14 @@ export default function exportToHTML({
4342
[notebook],
4443
);
4544
let body = renderToString(element);
46-
const $ = cheerio.load(body);
47-
$(".katex-html").remove();
48-
body = $.html();
49-
5045
const { codemirror, antd, katex } = getVersions();
5146

5247
return `<html>
5348
<head><meta charset="utf-8" />
5449
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5550
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/${codemirror}/codemirror.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
56-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/${katex}/katex.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
57-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/antd/${antd}/antd.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
51+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@${katex}/dist/katex.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
52+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/antd@${antd}/dist/antd.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
5853
</head>
5954
<body>
6055
${body}
@@ -63,14 +58,14 @@ ${body}
6358
}
6459

6560
// TODO regarding antd -- see https://github.com/sagemathinc/cocalc/issues/6305
66-
// For some reason the css for antd beyond version 4.24.7 is not available.
67-
// Thus for now we are just hardcoding version 4.24.7. This is of course much
61+
// Due to a change in architecture, the css for antd beyond version 4.24.16 will
62+
// never be available.
63+
// Thus for now we are just hardcoding version 4.24.16. This is of course much
6864
// better than being compltely broken, but not ideal. The CSS we actually use
69-
// for Jupyter notebooks from antd is very minimal, except in maybe widgets, which
70-
// aren't relevant for printing (yet?).
65+
// for Jupyter notebooks from antd is very minimal, so maybe this won't be a problem.
7166
function getVersions() {
7267
return {
73-
antd: "4.24.7", // require("antd").version
68+
antd: "4.24.16", // require("antd").version
7469
codemirror: require("codemirror/package").version,
7570
katex: require("katex").version,
7671
};

src/packages/project/jupyter/convert/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function nbconvert(opts: NbconvertParams): Promise<void> {
2929

3030
if (to == "cocalc-html" || to == "cocalc-pdf") {
3131
// We use our own internal cocalc conversion, since I'm tired of weird subtle issues
32-
// with upstream nbconvert...
32+
// with upstream nbconvert, and we can also be much faster.
3333
const ipynb = join(opts.directory ?? "", parseSource(opts.args)); // make relative to home directory
3434
const html = await ipynbToHtml(ipynb);
3535
if (to == "cocalc-html") {

0 commit comments

Comments
 (0)