CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/jupyter/convert/ipynb-to-html.ts
Views: 687
1
/*
2
Some React-based nbconvert functionality.
3
4
Note that this doesn't actually use upstream nbconvert itself at all!
5
6
- html: using react; very fast and uses no command line calls; html depends on cdn.
7
*/
8
9
import * as fs from "fs";
10
import { join, parse } from "path";
11
12
const { readFile, writeFile } = fs.promises;
13
14
export default async function ipynbToHtml(path: string): Promise<string> {
15
// This toHtml is expensive to import (due to the frontend being quite large),
16
// so we don't import it at the module level:
17
const { default: toHtml } = await import(
18
"@cocalc/frontend/jupyter/nbviewer/export"
19
);
20
21
const content = (await readFile(path)).toString();
22
const html = toHtml({ content, style: { margin: "30px 30px 0 0" } });
23
const outfile = htmlPath(path);
24
await writeFile(outfile, html);
25
return outfile;
26
}
27
28
export function htmlPath(path: string): string {
29
const { dir, name } = parse(path);
30
return join(dir, name + ".html");
31
}
32
33