rehype-katex.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. import {
  2. katex
  3. } from "./chunk-REIMVYFT.js";
  4. import {
  5. SKIP,
  6. convert,
  7. find,
  8. html,
  9. normalize,
  10. parse,
  11. parse2,
  12. svg,
  13. visitParents
  14. } from "./chunk-BDV6QPQQ.js";
  15. import "./chunk-DC5AMYBS.js";
  16. // node_modules/hast-util-parse-selector/lib/index.js
  17. var search = /[#.]/g;
  18. function parseSelector(selector, defaultTagName) {
  19. const value = selector || "";
  20. const props = {};
  21. let start = 0;
  22. let previous;
  23. let tagName;
  24. while (start < value.length) {
  25. search.lastIndex = start;
  26. const match = search.exec(value);
  27. const subvalue = value.slice(start, match ? match.index : value.length);
  28. if (subvalue) {
  29. if (!previous) {
  30. tagName = subvalue;
  31. } else if (previous === "#") {
  32. props.id = subvalue;
  33. } else if (Array.isArray(props.className)) {
  34. props.className.push(subvalue);
  35. } else {
  36. props.className = [subvalue];
  37. }
  38. start += subvalue.length;
  39. }
  40. if (match) {
  41. previous = match[0];
  42. start++;
  43. }
  44. }
  45. return {
  46. type: "element",
  47. // @ts-expect-error: tag name is parsed.
  48. tagName: tagName || defaultTagName || "div",
  49. properties: props,
  50. children: []
  51. };
  52. }
  53. // node_modules/hastscript/lib/create-h.js
  54. function createH(schema, defaultTagName, caseSensitive) {
  55. const adjust = caseSensitive ? createAdjustMap(caseSensitive) : void 0;
  56. function h2(selector, properties, ...children) {
  57. let node;
  58. if (selector === null || selector === void 0) {
  59. node = { type: "root", children: [] };
  60. const child = (
  61. /** @type {Child} */
  62. properties
  63. );
  64. children.unshift(child);
  65. } else {
  66. node = parseSelector(selector, defaultTagName);
  67. const lower = node.tagName.toLowerCase();
  68. const adjusted = adjust ? adjust.get(lower) : void 0;
  69. node.tagName = adjusted || lower;
  70. if (isChild(properties)) {
  71. children.unshift(properties);
  72. } else {
  73. for (const [key, value] of Object.entries(properties)) {
  74. addProperty(schema, node.properties, key, value);
  75. }
  76. }
  77. }
  78. for (const child of children) {
  79. addChild(node.children, child);
  80. }
  81. if (node.type === "element" && node.tagName === "template") {
  82. node.content = { type: "root", children: node.children };
  83. node.children = [];
  84. }
  85. return node;
  86. }
  87. return h2;
  88. }
  89. function isChild(value) {
  90. if (value === null || typeof value !== "object" || Array.isArray(value)) {
  91. return true;
  92. }
  93. if (typeof value.type !== "string") return false;
  94. const record = (
  95. /** @type {Record<string, unknown>} */
  96. value
  97. );
  98. const keys = Object.keys(value);
  99. for (const key of keys) {
  100. const value2 = record[key];
  101. if (value2 && typeof value2 === "object") {
  102. if (!Array.isArray(value2)) return true;
  103. const list = (
  104. /** @type {ReadonlyArray<unknown>} */
  105. value2
  106. );
  107. for (const item of list) {
  108. if (typeof item !== "number" && typeof item !== "string") {
  109. return true;
  110. }
  111. }
  112. }
  113. }
  114. if ("children" in value && Array.isArray(value.children)) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. function addProperty(schema, properties, key, value) {
  120. const info = find(schema, key);
  121. let result;
  122. if (value === null || value === void 0) return;
  123. if (typeof value === "number") {
  124. if (Number.isNaN(value)) return;
  125. result = value;
  126. } else if (typeof value === "boolean") {
  127. result = value;
  128. } else if (typeof value === "string") {
  129. if (info.spaceSeparated) {
  130. result = parse2(value);
  131. } else if (info.commaSeparated) {
  132. result = parse(value);
  133. } else if (info.commaOrSpaceSeparated) {
  134. result = parse2(parse(value).join(" "));
  135. } else {
  136. result = parsePrimitive(info, info.property, value);
  137. }
  138. } else if (Array.isArray(value)) {
  139. result = [...value];
  140. } else {
  141. result = info.property === "style" ? style(value) : String(value);
  142. }
  143. if (Array.isArray(result)) {
  144. const finalResult = [];
  145. for (const item of result) {
  146. finalResult.push(
  147. /** @type {number | string} */
  148. parsePrimitive(info, info.property, item)
  149. );
  150. }
  151. result = finalResult;
  152. }
  153. if (info.property === "className" && Array.isArray(properties.className)) {
  154. result = properties.className.concat(
  155. /** @type {Array<number | string> | number | string} */
  156. result
  157. );
  158. }
  159. properties[info.property] = result;
  160. }
  161. function addChild(nodes, value) {
  162. if (value === null || value === void 0) {
  163. } else if (typeof value === "number" || typeof value === "string") {
  164. nodes.push({ type: "text", value: String(value) });
  165. } else if (Array.isArray(value)) {
  166. for (const child of value) {
  167. addChild(nodes, child);
  168. }
  169. } else if (typeof value === "object" && "type" in value) {
  170. if (value.type === "root") {
  171. addChild(nodes, value.children);
  172. } else {
  173. nodes.push(value);
  174. }
  175. } else {
  176. throw new Error("Expected node, nodes, or string, got `" + value + "`");
  177. }
  178. }
  179. function parsePrimitive(info, name, value) {
  180. if (typeof value === "string") {
  181. if (info.number && value && !Number.isNaN(Number(value))) {
  182. return Number(value);
  183. }
  184. if ((info.boolean || info.overloadedBoolean) && (value === "" || normalize(value) === normalize(name))) {
  185. return true;
  186. }
  187. }
  188. return value;
  189. }
  190. function style(styles) {
  191. const result = [];
  192. for (const [key, value] of Object.entries(styles)) {
  193. result.push([key, value].join(": "));
  194. }
  195. return result.join("; ");
  196. }
  197. function createAdjustMap(values) {
  198. const result = /* @__PURE__ */ new Map();
  199. for (const value of values) {
  200. result.set(value.toLowerCase(), value);
  201. }
  202. return result;
  203. }
  204. // node_modules/hastscript/lib/svg-case-sensitive-tag-names.js
  205. var svgCaseSensitiveTagNames = [
  206. "altGlyph",
  207. "altGlyphDef",
  208. "altGlyphItem",
  209. "animateColor",
  210. "animateMotion",
  211. "animateTransform",
  212. "clipPath",
  213. "feBlend",
  214. "feColorMatrix",
  215. "feComponentTransfer",
  216. "feComposite",
  217. "feConvolveMatrix",
  218. "feDiffuseLighting",
  219. "feDisplacementMap",
  220. "feDistantLight",
  221. "feDropShadow",
  222. "feFlood",
  223. "feFuncA",
  224. "feFuncB",
  225. "feFuncG",
  226. "feFuncR",
  227. "feGaussianBlur",
  228. "feImage",
  229. "feMerge",
  230. "feMergeNode",
  231. "feMorphology",
  232. "feOffset",
  233. "fePointLight",
  234. "feSpecularLighting",
  235. "feSpotLight",
  236. "feTile",
  237. "feTurbulence",
  238. "foreignObject",
  239. "glyphRef",
  240. "linearGradient",
  241. "radialGradient",
  242. "solidColor",
  243. "textArea",
  244. "textPath"
  245. ];
  246. // node_modules/hastscript/lib/index.js
  247. var h = createH(html, "div");
  248. var s = createH(svg, "g", svgCaseSensitiveTagNames);
  249. // node_modules/web-namespaces/index.js
  250. var webNamespaces = {
  251. html: "http://www.w3.org/1999/xhtml",
  252. mathml: "http://www.w3.org/1998/Math/MathML",
  253. svg: "http://www.w3.org/2000/svg",
  254. xlink: "http://www.w3.org/1999/xlink",
  255. xml: "http://www.w3.org/XML/1998/namespace",
  256. xmlns: "http://www.w3.org/2000/xmlns/"
  257. };
  258. // node_modules/hast-util-from-dom/lib/index.js
  259. function fromDom(tree, options) {
  260. return transform(tree, options || {}) || { type: "root", children: [] };
  261. }
  262. function transform(node, options) {
  263. const transformed = one(node, options);
  264. if (transformed && options.afterTransform)
  265. options.afterTransform(node, transformed);
  266. return transformed;
  267. }
  268. function one(node, options) {
  269. switch (node.nodeType) {
  270. case 1: {
  271. const domNode = (
  272. /** @type {Element} */
  273. node
  274. );
  275. return element(domNode, options);
  276. }
  277. // Ignore: Attr (2).
  278. case 3: {
  279. const domNode = (
  280. /** @type {Text} */
  281. node
  282. );
  283. return text(domNode);
  284. }
  285. // Ignore: CDATA (4).
  286. // Removed: Entity reference (5)
  287. // Removed: Entity (6)
  288. // Ignore: Processing instruction (7).
  289. case 8: {
  290. const domNode = (
  291. /** @type {Comment} */
  292. node
  293. );
  294. return comment(domNode);
  295. }
  296. case 9: {
  297. const domNode = (
  298. /** @type {Document} */
  299. node
  300. );
  301. return root(domNode, options);
  302. }
  303. case 10: {
  304. return doctype();
  305. }
  306. case 11: {
  307. const domNode = (
  308. /** @type {DocumentFragment} */
  309. node
  310. );
  311. return root(domNode, options);
  312. }
  313. default: {
  314. return void 0;
  315. }
  316. }
  317. }
  318. function root(node, options) {
  319. return { type: "root", children: all(node, options) };
  320. }
  321. function doctype() {
  322. return { type: "doctype" };
  323. }
  324. function text(node) {
  325. return { type: "text", value: node.nodeValue || "" };
  326. }
  327. function comment(node) {
  328. return { type: "comment", value: node.nodeValue || "" };
  329. }
  330. function element(node, options) {
  331. const space = node.namespaceURI;
  332. const x = space === webNamespaces.svg ? s : h;
  333. const tagName = space === webNamespaces.html ? node.tagName.toLowerCase() : node.tagName;
  334. const content = (
  335. // @ts-expect-error: DOM types are wrong, content can exist.
  336. space === webNamespaces.html && tagName === "template" ? node.content : node
  337. );
  338. const attributes = node.getAttributeNames();
  339. const properties = {};
  340. let index = -1;
  341. while (++index < attributes.length) {
  342. properties[attributes[index]] = node.getAttribute(attributes[index]) || "";
  343. }
  344. return x(tagName, properties, all(content, options));
  345. }
  346. function all(node, options) {
  347. const nodes = node.childNodes;
  348. const children = [];
  349. let index = -1;
  350. while (++index < nodes.length) {
  351. const child = transform(nodes[index], options);
  352. if (child !== void 0) {
  353. children.push(child);
  354. }
  355. }
  356. return children;
  357. }
  358. // node_modules/hast-util-from-html-isomorphic/lib/browser.js
  359. var parser = new DOMParser();
  360. function fromHtmlIsomorphic(value, options) {
  361. const node = (options == null ? void 0 : options.fragment) ? parseFragment(value) : parser.parseFromString(value, "text/html");
  362. return (
  363. /** @type {Root} */
  364. fromDom(node)
  365. );
  366. }
  367. function parseFragment(value) {
  368. const template = document.createElement("template");
  369. template.innerHTML = value;
  370. return template.content;
  371. }
  372. // node_modules/unist-util-find-after/lib/index.js
  373. var findAfter = (
  374. // Note: overloads like this are needed to support optional generics.
  375. /**
  376. * @type {(
  377. * (<Kind extends UnistParent, Check extends Test>(parent: Kind, index: Child<Kind> | number, test: Check) => Matches<Child<Kind>, Check> | undefined) &
  378. * (<Kind extends UnistParent>(parent: Kind, index: Child<Kind> | number, test?: null | undefined) => Child<Kind> | undefined)
  379. * )}
  380. */
  381. /**
  382. * @param {UnistParent} parent
  383. * @param {UnistNode | number} index
  384. * @param {Test} [test]
  385. * @returns {UnistNode | undefined}
  386. */
  387. (function(parent, index, test) {
  388. const is = convert(test);
  389. if (!parent || !parent.type || !parent.children) {
  390. throw new Error("Expected parent node");
  391. }
  392. if (typeof index === "number") {
  393. if (index < 0 || index === Number.POSITIVE_INFINITY) {
  394. throw new Error("Expected positive finite number as index");
  395. }
  396. } else {
  397. index = parent.children.indexOf(index);
  398. if (index < 0) {
  399. throw new Error("Expected child node or index");
  400. }
  401. }
  402. while (++index < parent.children.length) {
  403. if (is(parent.children[index], index, parent)) {
  404. return parent.children[index];
  405. }
  406. }
  407. return void 0;
  408. })
  409. );
  410. // node_modules/hast-util-is-element/lib/index.js
  411. var convertElement = (
  412. // Note: overloads in JSDoc can’t yet use different `@template`s.
  413. /**
  414. * @type {(
  415. * (<Condition extends TestFunction>(test: Condition) => (element: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is Element & Predicate<Condition, Element>) &
  416. * (<Condition extends string>(test: Condition) => (element: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is Element & {tagName: Condition}) &
  417. * ((test?: null | undefined) => (element?: unknown, index?: number | null | undefined, parent?: Parents | null | undefined, context?: unknown) => element is Element) &
  418. * ((test?: Test) => Check)
  419. * )}
  420. */
  421. /**
  422. * @param {Test | null | undefined} [test]
  423. * @returns {Check}
  424. */
  425. (function(test) {
  426. if (test === null || test === void 0) {
  427. return element2;
  428. }
  429. if (typeof test === "string") {
  430. return tagNameFactory(test);
  431. }
  432. if (typeof test === "object") {
  433. return anyFactory(test);
  434. }
  435. if (typeof test === "function") {
  436. return castFactory(test);
  437. }
  438. throw new Error("Expected function, string, or array as `test`");
  439. })
  440. );
  441. function anyFactory(tests) {
  442. const checks = [];
  443. let index = -1;
  444. while (++index < tests.length) {
  445. checks[index] = convertElement(tests[index]);
  446. }
  447. return castFactory(any);
  448. function any(...parameters) {
  449. let index2 = -1;
  450. while (++index2 < checks.length) {
  451. if (checks[index2].apply(this, parameters)) return true;
  452. }
  453. return false;
  454. }
  455. }
  456. function tagNameFactory(check) {
  457. return castFactory(tagName);
  458. function tagName(element3) {
  459. return element3.tagName === check;
  460. }
  461. }
  462. function castFactory(testFunction) {
  463. return check;
  464. function check(value, index, parent) {
  465. return Boolean(
  466. looksLikeAnElement(value) && testFunction.call(
  467. this,
  468. value,
  469. typeof index === "number" ? index : void 0,
  470. parent || void 0
  471. )
  472. );
  473. }
  474. }
  475. function element2(element3) {
  476. return Boolean(
  477. element3 && typeof element3 === "object" && "type" in element3 && element3.type === "element" && "tagName" in element3 && typeof element3.tagName === "string"
  478. );
  479. }
  480. function looksLikeAnElement(value) {
  481. return value !== null && typeof value === "object" && "type" in value && "tagName" in value;
  482. }
  483. // node_modules/hast-util-to-text/lib/index.js
  484. var searchLineFeeds = /\n/g;
  485. var searchTabOrSpaces = /[\t ]+/g;
  486. var br = convertElement("br");
  487. var cell = convertElement(isCell);
  488. var p = convertElement("p");
  489. var row = convertElement("tr");
  490. var notRendered = convertElement([
  491. // List from: <https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements>
  492. "datalist",
  493. "head",
  494. "noembed",
  495. "noframes",
  496. "noscript",
  497. // Act as if we support scripting.
  498. "rp",
  499. "script",
  500. "style",
  501. "template",
  502. "title",
  503. // Hidden attribute.
  504. hidden,
  505. // From: <https://html.spec.whatwg.org/multipage/rendering.html#flow-content-3>
  506. closedDialog
  507. ]);
  508. var blockOrCaption = convertElement([
  509. "address",
  510. // Flow content
  511. "article",
  512. // Sections and headings
  513. "aside",
  514. // Sections and headings
  515. "blockquote",
  516. // Flow content
  517. "body",
  518. // Page
  519. "caption",
  520. // `table-caption`
  521. "center",
  522. // Flow content (legacy)
  523. "dd",
  524. // Lists
  525. "dialog",
  526. // Flow content
  527. "dir",
  528. // Lists (legacy)
  529. "dl",
  530. // Lists
  531. "dt",
  532. // Lists
  533. "div",
  534. // Flow content
  535. "figure",
  536. // Flow content
  537. "figcaption",
  538. // Flow content
  539. "footer",
  540. // Flow content
  541. "form,",
  542. // Flow content
  543. "h1",
  544. // Sections and headings
  545. "h2",
  546. // Sections and headings
  547. "h3",
  548. // Sections and headings
  549. "h4",
  550. // Sections and headings
  551. "h5",
  552. // Sections and headings
  553. "h6",
  554. // Sections and headings
  555. "header",
  556. // Flow content
  557. "hgroup",
  558. // Sections and headings
  559. "hr",
  560. // Flow content
  561. "html",
  562. // Page
  563. "legend",
  564. // Flow content
  565. "li",
  566. // Lists (as `display: list-item`)
  567. "listing",
  568. // Flow content (legacy)
  569. "main",
  570. // Flow content
  571. "menu",
  572. // Lists
  573. "nav",
  574. // Sections and headings
  575. "ol",
  576. // Lists
  577. "p",
  578. // Flow content
  579. "plaintext",
  580. // Flow content (legacy)
  581. "pre",
  582. // Flow content
  583. "section",
  584. // Sections and headings
  585. "ul",
  586. // Lists
  587. "xmp"
  588. // Flow content (legacy)
  589. ]);
  590. function toText(tree, options) {
  591. const options_ = options || {};
  592. const children = "children" in tree ? tree.children : [];
  593. const block = blockOrCaption(tree);
  594. const whitespace = inferWhitespace(tree, {
  595. whitespace: options_.whitespace || "normal",
  596. breakBefore: false,
  597. breakAfter: false
  598. });
  599. const results = [];
  600. if (tree.type === "text" || tree.type === "comment") {
  601. results.push(
  602. ...collectText(tree, {
  603. whitespace,
  604. breakBefore: true,
  605. breakAfter: true
  606. })
  607. );
  608. }
  609. let index = -1;
  610. while (++index < children.length) {
  611. results.push(
  612. ...renderedTextCollection(
  613. children[index],
  614. // @ts-expect-error: `tree` is a parent if we’re here.
  615. tree,
  616. {
  617. whitespace,
  618. breakBefore: index ? void 0 : block,
  619. breakAfter: index < children.length - 1 ? br(children[index + 1]) : block
  620. }
  621. )
  622. );
  623. }
  624. const result = [];
  625. let count;
  626. index = -1;
  627. while (++index < results.length) {
  628. const value = results[index];
  629. if (typeof value === "number") {
  630. if (count !== void 0 && value > count) count = value;
  631. } else if (value) {
  632. if (count !== void 0 && count > -1) {
  633. result.push("\n".repeat(count) || " ");
  634. }
  635. count = -1;
  636. result.push(value);
  637. }
  638. }
  639. return result.join("");
  640. }
  641. function renderedTextCollection(node, parent, info) {
  642. if (node.type === "element") {
  643. return collectElement(node, parent, info);
  644. }
  645. if (node.type === "text") {
  646. return info.whitespace === "normal" ? collectText(node, info) : collectPreText(node);
  647. }
  648. return [];
  649. }
  650. function collectElement(node, parent, info) {
  651. const whitespace = inferWhitespace(node, info);
  652. const children = node.children || [];
  653. let index = -1;
  654. let items = [];
  655. if (notRendered(node)) {
  656. return items;
  657. }
  658. let prefix;
  659. let suffix;
  660. if (br(node)) {
  661. suffix = "\n";
  662. } else if (row(node) && // @ts-expect-error: something up with types of parents.
  663. findAfter(parent, node, row)) {
  664. suffix = "\n";
  665. } else if (p(node)) {
  666. prefix = 2;
  667. suffix = 2;
  668. } else if (blockOrCaption(node)) {
  669. prefix = 1;
  670. suffix = 1;
  671. }
  672. while (++index < children.length) {
  673. items = items.concat(
  674. renderedTextCollection(children[index], node, {
  675. whitespace,
  676. breakBefore: index ? void 0 : prefix,
  677. breakAfter: index < children.length - 1 ? br(children[index + 1]) : suffix
  678. })
  679. );
  680. }
  681. if (cell(node) && // @ts-expect-error: something up with types of parents.
  682. findAfter(parent, node, cell)) {
  683. items.push(" ");
  684. }
  685. if (prefix) items.unshift(prefix);
  686. if (suffix) items.push(suffix);
  687. return items;
  688. }
  689. function collectText(node, info) {
  690. const value = String(node.value);
  691. const lines = [];
  692. const result = [];
  693. let start = 0;
  694. while (start <= value.length) {
  695. searchLineFeeds.lastIndex = start;
  696. const match = searchLineFeeds.exec(value);
  697. const end = match && "index" in match ? match.index : value.length;
  698. lines.push(
  699. // Any sequence of collapsible spaces and tabs immediately preceding or
  700. // following a segment break is removed.
  701. trimAndCollapseSpacesAndTabs(
  702. // […] ignoring bidi formatting characters (characters with the
  703. // Bidi_Control property [UAX9]: ALM, LTR, RTL, LRE-RLO, LRI-PDI) as if
  704. // they were not there.
  705. value.slice(start, end).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""),
  706. start === 0 ? info.breakBefore : true,
  707. end === value.length ? info.breakAfter : true
  708. )
  709. );
  710. start = end + 1;
  711. }
  712. let index = -1;
  713. let join;
  714. while (++index < lines.length) {
  715. if (lines[index].charCodeAt(lines[index].length - 1) === 8203 || index < lines.length - 1 && lines[index + 1].charCodeAt(0) === 8203) {
  716. result.push(lines[index]);
  717. join = void 0;
  718. } else if (lines[index]) {
  719. if (typeof join === "number") result.push(join);
  720. result.push(lines[index]);
  721. join = 0;
  722. } else if (index === 0 || index === lines.length - 1) {
  723. result.push(0);
  724. }
  725. }
  726. return result;
  727. }
  728. function collectPreText(node) {
  729. return [String(node.value)];
  730. }
  731. function trimAndCollapseSpacesAndTabs(value, breakBefore, breakAfter) {
  732. const result = [];
  733. let start = 0;
  734. let end;
  735. while (start < value.length) {
  736. searchTabOrSpaces.lastIndex = start;
  737. const match = searchTabOrSpaces.exec(value);
  738. end = match ? match.index : value.length;
  739. if (!start && !end && match && !breakBefore) {
  740. result.push("");
  741. }
  742. if (start !== end) {
  743. result.push(value.slice(start, end));
  744. }
  745. start = match ? end + match[0].length : end;
  746. }
  747. if (start !== end && !breakAfter) {
  748. result.push("");
  749. }
  750. return result.join(" ");
  751. }
  752. function inferWhitespace(node, info) {
  753. if (node.type === "element") {
  754. const properties = node.properties || {};
  755. switch (node.tagName) {
  756. case "listing":
  757. case "plaintext":
  758. case "xmp": {
  759. return "pre";
  760. }
  761. case "nobr": {
  762. return "nowrap";
  763. }
  764. case "pre": {
  765. return properties.wrap ? "pre-wrap" : "pre";
  766. }
  767. case "td":
  768. case "th": {
  769. return properties.noWrap ? "nowrap" : info.whitespace;
  770. }
  771. case "textarea": {
  772. return "pre-wrap";
  773. }
  774. default:
  775. }
  776. }
  777. return info.whitespace;
  778. }
  779. function hidden(node) {
  780. return Boolean((node.properties || {}).hidden);
  781. }
  782. function isCell(node) {
  783. return node.tagName === "td" || node.tagName === "th";
  784. }
  785. function closedDialog(node) {
  786. return node.tagName === "dialog" && !(node.properties || {}).open;
  787. }
  788. // node_modules/rehype-katex/lib/index.js
  789. var emptyOptions = {};
  790. var emptyClasses = [];
  791. function rehypeKatex(options) {
  792. const settings = options || emptyOptions;
  793. return function(tree, file) {
  794. visitParents(tree, "element", function(element3, parents) {
  795. const classes = Array.isArray(element3.properties.className) ? element3.properties.className : emptyClasses;
  796. const languageMath = classes.includes("language-math");
  797. const mathDisplay = classes.includes("math-display");
  798. const mathInline = classes.includes("math-inline");
  799. let displayMode = mathDisplay;
  800. if (!languageMath && !mathDisplay && !mathInline) {
  801. return;
  802. }
  803. let parent = parents[parents.length - 1];
  804. let scope = element3;
  805. if (element3.tagName === "code" && languageMath && parent && parent.type === "element" && parent.tagName === "pre") {
  806. scope = parent;
  807. parent = parents[parents.length - 2];
  808. displayMode = true;
  809. }
  810. if (!parent) return;
  811. const value = toText(scope, { whitespace: "pre" });
  812. let result;
  813. try {
  814. result = katex.renderToString(value, {
  815. ...settings,
  816. displayMode,
  817. throwOnError: true
  818. });
  819. } catch (error) {
  820. const cause = (
  821. /** @type {Error} */
  822. error
  823. );
  824. const ruleId = cause.name.toLowerCase();
  825. file.message("Could not render math with KaTeX", {
  826. ancestors: [...parents, element3],
  827. cause,
  828. place: element3.position,
  829. ruleId,
  830. source: "rehype-katex"
  831. });
  832. try {
  833. result = katex.renderToString(value, {
  834. ...settings,
  835. displayMode,
  836. strict: "ignore",
  837. throwOnError: false
  838. });
  839. } catch {
  840. result = [
  841. {
  842. type: "element",
  843. tagName: "span",
  844. properties: {
  845. className: ["katex-error"],
  846. style: "color:" + (settings.errorColor || "#cc0000"),
  847. title: String(error)
  848. },
  849. children: [{ type: "text", value }]
  850. }
  851. ];
  852. }
  853. }
  854. if (typeof result === "string") {
  855. const root2 = fromHtmlIsomorphic(result, { fragment: true });
  856. result = /** @type {Array<ElementContent>} */
  857. root2.children;
  858. }
  859. const index = parent.children.indexOf(scope);
  860. parent.children.splice(index, 1, ...result);
  861. return SKIP;
  862. });
  863. };
  864. }
  865. export {
  866. rehypeKatex as default
  867. };
  868. //# sourceMappingURL=rehype-katex.js.map