-
Notifications
You must be signed in to change notification settings - Fork 61
/
math.ts
73 lines (66 loc) · 2.08 KB
/
math.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Handler, ITexSerializer } from './types.js';
function addMacrosToState(value: string, state: ITexSerializer) {
if (!state.options.math) return;
Object.entries(state.options.math).forEach(([k, v]) => {
if (value.includes(k)) state.data.mathPlugins[k] = v;
});
}
type MathPlugins = ITexSerializer['data']['mathPlugins'];
/**
* Add any required recursive commands found, for example,
* if only `\aMat` was included in the content, but sill requires other commands:
*
* ```javascript
* {
* '\aNrm': "a",
* '\aMat': '[\mathrm{\aNrm}]',
* }
* ```
*/
export function withRecursiveCommands(
state: ITexSerializer,
plugins = state.data.mathPlugins,
): MathPlugins {
if (!state.options.math) return plugins;
const pluginsList = Object.entries(plugins);
const addedPlugins: MathPlugins = {};
Object.entries(state.options.math).forEach(([k, v]) => {
if (plugins[k]) return;
pluginsList.forEach(([, value]) => {
if (value.includes(k)) addedPlugins[k] = v;
});
});
const newPlugins = { ...addedPlugins, ...plugins };
if (Object.keys(addedPlugins).length === 0) return newPlugins;
return withRecursiveCommands(state, newPlugins);
}
const math: Handler = (node, state) => {
const { label, enumerated } = node;
state.usePackages('amsmath');
addMacrosToState(node.value, state);
if (state.data.isInTable) {
state.write('\\(\\displaystyle ');
state.write(node.value);
state.write(' \\)');
} else {
// TODO: AMS math
state.write(`\\begin{equation${enumerated === false ? '*' : ''}}\n`);
if (label) {
state.write(`\\label{${label}}`);
}
state.ensureNewLine();
state.write(node.value);
state.ensureNewLine(true);
state.write(`\\end{equation${enumerated === false ? '*' : ''}}`);
}
if (!state.data.isInTable) state.closeBlock(node);
};
const inlineMath: Handler = (node, state) => {
state.usePackages('amsmath');
addMacrosToState(node.value, state);
state.write('$');
state.text(node.value, true);
state.write('$');
};
const MATH_HANDLERS = { math, inlineMath };
export default MATH_HANDLERS;