Skip to content

Commit c3e21b8

Browse files
Implement lazy loading for KaTeX and texmath modules
Added lazy loading for KaTeX and markdown-it-texmath modules.
1 parent 56aadab commit c3e21b8

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/pages/markdownPreview/renderer.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
import katex from "katex";
21
import markdownIt from "markdown-it";
32
import anchor from "markdown-it-anchor";
43
import { full as markdownItEmoji } from "markdown-it-emoji";
54
import markdownItFootnote from "markdown-it-footnote";
65
import MarkdownItGitHubAlerts from "markdown-it-github-alerts";
76
import markdownItTaskLists from "markdown-it-task-lists";
8-
import markdownItTexmath from "markdown-it-texmath";
97
import Url from "utils/Url";
108

119
const EXTERNAL_LINK_PATTERN = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
1210
const IMAGE_PLACEHOLDER =
1311
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
1412

13+
let katexModulePromise = null;
14+
let mdItTexmathModulePromise = null;
15+
16+
async function getKatexAndTexmathModule() {
17+
if (!katexModulePromise) {
18+
katexModulePromise = import("katex")
19+
.then(({ default: katex }) => katex)
20+
.catch((error) => {
21+
katexModulePromise = null;
22+
throw error;
23+
});
24+
}
25+
26+
if (!mdItTexmathModulePromise) {
27+
mdItTexmathModulePromise = import("markdown-it-texmath")
28+
.then(({ default: markdownItTexmath }) => markdownItTexmath)
29+
.catch((error) => {
30+
mdItTexmathModulePromise = null;
31+
throw error;
32+
});
33+
}
34+
35+
return { katexModulePromise, mdItTexmathModulePromise };
36+
}
37+
1538
function slugify(text) {
1639
return text
1740
.trim()
@@ -113,6 +136,9 @@ function collectTokens(tokens, callback) {
113136
}
114137

115138
function createMarkdownIt() {
139+
140+
const { katexModulePromise: katex, mdItTexmathModulePromise: markdownItTexmath } = await getKatexAndTexmathModule();
141+
116142
const md = markdownIt({
117143
html: true,
118144
linkify: true,

0 commit comments

Comments
 (0)