MCPcopy
hub / github.com/marktext/marktext / inlineMath

Function inlineMath

packages/muya/src/inlineRenderer/renderer/inlineMath.ts:10–92  ·  view source on GitHub ↗
(this: Renderer, {
    h,
    cursor,
    block,
    token,
    outerClass,
}: ISyntaxRenderOptions & { token: CodeEmojiMathToken })

Source from the content-addressed store, hash-verified

8import 'katex/dist/katex.min.css';
9
10export default function inlineMath(this: Renderer, {
11 h,
12 cursor,
13 block,
14 token,
15 outerClass,
16}: ISyntaxRenderOptions & { token: CodeEmojiMathToken }) {
17 const className = this.getClassName(outerClass, block, token, cursor);
18 const { i18n } = this.muya;
19 const mathSelector
20 = className === CLASS_NAMES.MU_HIDE
21 ? `span.${className}.${CLASS_NAMES.MU_MATH}`
22 : `span.${CLASS_NAMES.MU_MATH}`;
23
24 const { start, end } = token.range;
25 const { marker } = token;
26
27 const startMarker = this.highlight(
28 h,
29 block,
30 start,
31 start + marker.length,
32 token,
33 );
34 const endMarker = this.highlight(h, block, end - marker.length, end, token);
35 const content = this.highlight(
36 h,
37 block,
38 start + marker.length,
39 end - marker.length,
40 token,
41 );
42
43 const { content: math, type } = token;
44
45 const { loadMathMap } = this;
46
47 const displayMode = false;
48 const key = `${math}_${type}`;
49 let mathVnode = null;
50 let previewSelector = `span.${CLASS_NAMES.MU_MATH_RENDER}`;
51 if (loadMathMap.has(key)) {
52 mathVnode = loadMathMap.get(key);
53 }
54 else {
55 try {
56 const html = katex.renderToString(math, {
57 displayMode,
58 });
59 mathVnode = htmlToVNode(html);
60 loadMathMap.set(key, mathVnode);
61 }
62 catch {
63 mathVnode = `<${i18n.t('Invalid Mathematical Formula')}>`;
64 previewSelector += `.${CLASS_NAMES.MU_MATH_ERROR}`;
65 }
66 }
67

Callers

nothing calls this directly

Calls 7

htmlToVNodeFunction · 0.90
hFunction · 0.85
highlightMethod · 0.80
getMethod · 0.80
tMethod · 0.80
hasMethod · 0.65
getClassNameMethod · 0.45

Tested by

no test coverage detected