-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex.js
46 lines (37 loc) · 1.58 KB
/
latex.js
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
const { Div, RawBlock, stdio } = require("pandoc-filter");
/**
* Convert div.<env_name> to LaTeX environment <env_name> with contents parsed as Markdown.
* Convert CodeBlock with language <env_name> to LaTeX environment <env_name> with contents unparsed.
* @param ele source pandoc AST element
* @return replacement pandoc AST element
*/
processLatex = (ele) => {
if (!["Div", "CodeBlock"].includes(ele.t)) return;
const [attr, childrenOrCode] = ele.c; // when Div, we have children: Block[]; when CodeBlock, we have code: string
/**
* @type {[string, string[], [string, string][]]}
* attrs is key-value pairs
*/
const [id, classList, attrs] = attr;
const attrsMap = Object.fromEntries(attrs);
if (classList.length !== 1) return; // Not implemented
const [env] = classList; // Assuming that, by the next line, this is a latex env, this is that env; the name is thus appropriate
const labelText = id ? `\\label{${id}}` : "";
const bracketArgs = attrsMap["bracket-args"] ?? attrsMap["sbr"];
const bracketArgsStr = bracketArgs ? `[${bracketArgs}]` : "";
if (ele.t === "Div")
return Div(
["", [], []],
[
RawBlock("latex", `\\begin{${env}}${bracketArgsStr}${labelText}`),
...childrenOrCode, // Leave this to get processed as markdown
RawBlock("latex", `\\end{${env}}`),
]
);
if (ele.t === "CodeBlock") {
if (["latex", "tex"].includes(env))
return RawBlock("latex", childrenOrCode);
return RawBlock("latex", `\\begin{${env}}${labelText}${childrenOrCode}\\end{${env}}`);
}
};
stdio(processLatex);