Skip to content

Commit c929256

Browse files
committed
[docusaurus] Add feature for math equations
1 parent 7bef7aa commit c929256

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

openedu_builder/plugins/docusaurus.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def _parse_config_options(self):
8585
or isinstance(x[list(x.keys())[0]], dict)
8686
]
8787
config_template_args["copyright_string"] = self.config.get("copyright_string")
88+
config_template_args["math"] = self.config.get("math", False)
8889

8990
return config_template_args
9091

@@ -464,6 +465,22 @@ def run(self):
464465
except FileNotFoundError:
465466
log.info("index.js already removed")
466467

468+
if self.config.get("math", False):
469+
math_command = [
470+
"npm",
471+
"install",
472+
"--save",
473+
"remark-math@3",
474+
"rehype-katex@5",
475+
"hast-util-is-element@1.1.0",
476+
]
477+
p = subprocess.run(math_command, capture_output=True)
478+
if p.returncode != 0:
479+
log.error(f"Command {math_command} failed with code {p.returncode}")
480+
log.error(f"STDOUT: {p.stdout.decode('utf-8')}")
481+
log.error(f"STDERR: {p.stderr.decode('utf-8')}")
482+
raise PluginRunError(f"Error while installing math dependencies command")
483+
467484
p = subprocess.run(self.build_command, capture_output=True)
468485
if p.returncode != 0:
469486
log.error(f"Command {self.build_command} failed with code {p.returncode}")

openedu_builder/plugins/docusaurus_templates/config.jinja2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
const lightCodeTheme = require('prism-react-renderer/themes/github');
55
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
6+
{% if math -%}
7+
const math = require('remark-math');
8+
const katex = require('rehype-katex');
9+
{%- endif %}
610

711
/** @type {import('@docusaurus/types').Config} */
812
const config = {
@@ -19,6 +23,10 @@ const config = {
1923
routeBasePath: '/',
2024
{% endif -%}
2125
sidebarPath: require.resolve('./sidebars.js'),
26+
{%- if math %}
27+
remarkPlugins: [math],
28+
rehypePlugins: [katex],
29+
{%- endif %}
2230
},
2331
blog: false,
2432
theme: {
@@ -28,6 +36,18 @@ const config = {
2836
],
2937
],
3038

39+
{%- if math %}
40+
stylesheets: [
41+
{
42+
href: 'https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css',
43+
type: 'text/css',
44+
integrity:
45+
'sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM',
46+
crossorigin: 'anonymous',
47+
},
48+
],
49+
{% endif -%}
50+
3151
themeConfig:
3252
/** @type {import('@docusaurus/preset-classic').ThemeConfig} */
3353
({

0 commit comments

Comments
 (0)