forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
href.js
75 lines (72 loc) · 1.97 KB
/
href.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
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
74
75
// @flow
import defineFunction, {ordargument} from "../defineFunction";
import buildCommon from "../buildCommon";
import {assertNodeType} from "../parseNode";
import {assertType} from "../utils";
import {MathNode} from "../mathMLTree";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
defineFunction({
type: "href",
names: ["\\href"],
props: {
numArgs: 2,
argTypes: ["url", "original"],
allowedInText: true,
},
handler: ({parser}, args) => {
const body = args[1];
const href = assertNodeType(args[0], "url").url;
return {
type: "href",
mode: parser.mode,
href,
body: ordargument(body),
};
},
htmlBuilder: (group, options) => {
const elements = html.buildExpression(group.body, options, false);
return buildCommon.makeAnchor(group.href, [], elements, options);
},
mathmlBuilder: (group, options) => {
const math = mml.buildExpressionRow(group.body, options);
assertType(math, MathNode).setAttribute("href", group.href);
return math;
},
});
defineFunction({
type: "href",
names: ["\\url"],
props: {
numArgs: 1,
argTypes: ["url"],
allowedInText: true,
},
handler: ({parser}, args) => {
const href = assertNodeType(args[0], "url").url;
const chars = [];
for (let i = 0; i < href.length; i++) {
let c = href[i];
if (c === "~") {
c = "\\textasciitilde";
}
chars.push({
type: "textord",
mode: "text",
text: c,
});
}
const body = {
type: "text",
mode: parser.mode,
font: "\\texttt",
body: chars,
};
return {
type: "href",
mode: parser.mode,
href,
body: ordargument(body),
};
},
});