UNPKG

2.06 kBJavaScriptView Raw
1// @flow
2import defineFunction, {ordargument} from "../defineFunction";
3import buildCommon from "../buildCommon";
4import {assertNodeType} from "../parseNode";
5import {MathNode} from "../mathMLTree";
6
7import * as html from "../buildHTML";
8import * as mml from "../buildMathML";
9
10defineFunction({
11 type: "href",
12 names: ["\\href"],
13 props: {
14 numArgs: 2,
15 argTypes: ["url", "original"],
16 allowedInText: true,
17 },
18 handler: ({parser}, args) => {
19 const body = args[1];
20 const href = assertNodeType(args[0], "url").url;
21 return {
22 type: "href",
23 mode: parser.mode,
24 href,
25 body: ordargument(body),
26 };
27 },
28 htmlBuilder: (group, options) => {
29 const elements = html.buildExpression(group.body, options, false);
30 return buildCommon.makeAnchor(group.href, [], elements, options);
31 },
32 mathmlBuilder: (group, options) => {
33 let math = mml.buildExpressionRow(group.body, options);
34 if (!(math instanceof MathNode)) {
35 math = new MathNode("mrow", [math]);
36 }
37 math.setAttribute("href", group.href);
38 return math;
39 },
40});
41
42defineFunction({
43 type: "href",
44 names: ["\\url"],
45 props: {
46 numArgs: 1,
47 argTypes: ["url"],
48 allowedInText: true,
49 },
50 handler: ({parser}, args) => {
51 const href = assertNodeType(args[0], "url").url;
52 const chars = [];
53 for (let i = 0; i < href.length; i++) {
54 let c = href[i];
55 if (c === "~") {
56 c = "\\textasciitilde";
57 }
58 chars.push({
59 type: "textord",
60 mode: "text",
61 text: c,
62 });
63 }
64 const body = {
65 type: "text",
66 mode: parser.mode,
67 font: "\\texttt",
68 body: chars,
69 };
70 return {
71 type: "href",
72 mode: parser.mode,
73 href,
74 body: ordargument(body),
75 };
76 },
77});