Files
KaTeX/src/functions/href.js
Ashish Myles 1506dc1d88 Flatten a bunch of non-pervasive ParseNode types (part 1) (#1551)
* Flatten "url" ParseNode.

* Flatten "href" ParseNode.

* Flatten "verb" ParseNode.

* Flatten "tag" ParseNode.

* Flatten "cr" ParseNode.

* Flatten "delimsizing" ParseNode.

* Flatten "middle" ParseNode.

* Flatten "leftright" ParseNode.

* Flatten "leftright-right" ParseNode.

* Flatten "mathchoice" ParseNode.

* Remove unused ParseNode type "mod".

* Flatten "mclass" ParseNode.

* Flatten "font" ParseNode.

* Flatten "phantom" ParseNode.

* Flatten "hphantom" ParseNode.

* Flatten "vphantom" ParseNode.
2018-08-06 11:49:43 +09:00

79 lines
2.1 KiB
JavaScript

// @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 new 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",
value: c,
});
}
const body = {
type: "text",
mode: parser.mode,
value: {
type: "text",
font: "\\texttt",
body: chars,
},
};
return {
type: "href",
mode: parser.mode,
href,
body: ordargument(body),
};
},
});