@doenet/doenetml-iframe 0.7.25-dev.508 → 0.7.25-dev.510

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +123 -11
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -28959,8 +28959,19 @@ function getMediaLicenseDisplay(code, version2) {
28959
28959
  return { kind: info.kind, label, url: info.url(resolvedVersion) };
28960
28960
  }
28961
28961
  const mathjaxConfig$1 = {
28962
+ loader: {
28963
+ // The `units` package is neither bundled into the combined component
28964
+ // we load nor reachable through `autoload`, so it has to be requested
28965
+ // by name. MathJax fetches it during startup, from the same place it
28966
+ // gets the rest of its components.
28967
+ load: ["[tex]/units"]
28968
+ },
28962
28969
  tex: {
28963
28970
  tags: "ams",
28971
+ // `[+]` adds to MathJax's default package list rather than replacing
28972
+ // it. `units` supplies `\units`, `\unitfrac`, and `\nicefrac`, so that
28973
+ // units can be typeset semantically instead of hand-spaced.
28974
+ packages: { "[+]": ["units"] },
28964
28975
  macros: {
28965
28976
  lt: "<",
28966
28977
  gt: ">",
@@ -42018,7 +42029,8 @@ function waitForExistingMathJax(timeoutMs) {
42018
42029
  }
42019
42030
  function injectMathJax(src, config2, { force = false } = {}) {
42020
42031
  return new Promise((resolve, reject2) => {
42021
- if (config2 && (force || window.MathJax == null)) {
42032
+ const bootsOnOurConfig = !config2 || force || window.MathJax == null;
42033
+ if (config2 && bootsOnOurConfig) {
42022
42034
  window.MathJax = config2;
42023
42035
  }
42024
42036
  const script = document.createElement("script");
@@ -42027,7 +42039,10 @@ function injectMathJax(src, config2, { force = false } = {}) {
42027
42039
  script.async = true;
42028
42040
  script.setAttribute(DOENET_MATHJAX_SCRIPT_ATTR, "true");
42029
42041
  script.addEventListener("load", () => {
42030
- resolve(window.MathJax);
42042
+ const engine = window.MathJax;
42043
+ resolve(
42044
+ bootsOnOurConfig ? engine : primeUnconfiguredEngine(engine, config2)
42045
+ );
42031
42046
  });
42032
42047
  script.addEventListener("error", () => {
42033
42048
  reject2(
@@ -42037,6 +42052,83 @@ function injectMathJax(src, config2, { force = false } = {}) {
42037
42052
  document.head.appendChild(script);
42038
42053
  });
42039
42054
  }
42055
+ function texConfig(config2) {
42056
+ const tex2 = config2?.tex;
42057
+ return tex2 && typeof tex2 === "object" ? tex2 : void 0;
42058
+ }
42059
+ function texDefsFromConfig(config2) {
42060
+ const macros = texConfig(config2)?.macros;
42061
+ if (!macros || typeof macros !== "object") {
42062
+ return "";
42063
+ }
42064
+ const defs = [];
42065
+ for (const [name2, value] of Object.entries(
42066
+ macros
42067
+ )) {
42068
+ if (!/^[A-Za-z]+$/.test(name2)) {
42069
+ continue;
42070
+ }
42071
+ let replacement;
42072
+ let argCount = 0;
42073
+ if (typeof value === "string") {
42074
+ replacement = value;
42075
+ } else if (Array.isArray(value) && value.length <= 2 && typeof value[0] === "string") {
42076
+ replacement = value[0];
42077
+ argCount = typeof value[1] === "number" ? value[1] : 0;
42078
+ } else {
42079
+ continue;
42080
+ }
42081
+ if (!Number.isInteger(argCount) || argCount < 0 || argCount > 9) {
42082
+ continue;
42083
+ }
42084
+ const parameters = Array.from(
42085
+ { length: argCount },
42086
+ (_unused, index2) => `#${index2 + 1}`
42087
+ ).join("");
42088
+ defs.push(`\\def\\${name2}${parameters}{${replacement}}`);
42089
+ }
42090
+ return defs.join("");
42091
+ }
42092
+ function texPackagesFromConfig(config2) {
42093
+ const packages = texConfig(config2)?.packages;
42094
+ if (!packages || typeof packages !== "object" || Array.isArray(packages)) {
42095
+ return [];
42096
+ }
42097
+ const added = packages["[+]"];
42098
+ if (!Array.isArray(added)) {
42099
+ return [];
42100
+ }
42101
+ return added.filter(
42102
+ (name2) => typeof name2 === "string" && /^[\w-]+$/.test(name2)
42103
+ );
42104
+ }
42105
+ async function convertForSideEffect(engine, tex2) {
42106
+ if (typeof engine.tex2mmlPromise !== "function") {
42107
+ return;
42108
+ }
42109
+ try {
42110
+ await engine.tex2mmlPromise(tex2);
42111
+ } catch (reason) {
42112
+ console.warn(
42113
+ `DoenetViewer: could not prime MathJax with "${tex2}"; math relying on it may not render as it does on doenet.org.`,
42114
+ reason
42115
+ );
42116
+ }
42117
+ }
42118
+ async function primeUnconfiguredEngine(engine, config2) {
42119
+ try {
42120
+ await engine.startup?.promise;
42121
+ } catch {
42122
+ }
42123
+ const defs = texDefsFromConfig(config2);
42124
+ if (defs) {
42125
+ await convertForSideEffect(engine, defs);
42126
+ }
42127
+ for (const packageName of texPackagesFromConfig(config2)) {
42128
+ await convertForSideEffect(engine, `\\require{${packageName}}`);
42129
+ }
42130
+ return engine;
42131
+ }
42040
42132
  function createMathJaxPromise(options) {
42041
42133
  const {
42042
42134
  config: config2,
@@ -42045,16 +42137,25 @@ function createMathJaxPromise(options) {
42045
42137
  timeoutMs = DEFAULT_TIMEOUT_MS
42046
42138
  } = options;
42047
42139
  if (isMathJaxEngine(window.MathJax)) {
42048
- return Promise.resolve(window.MathJax);
42140
+ return primeUnconfiguredEngine(window.MathJax, config2);
42049
42141
  }
42050
42142
  if (useExistingMathJax || findMathJaxScript()) {
42051
- return waitForExistingMathJax(timeoutMs).catch((reason) => {
42052
- console.warn(
42053
- "DoenetViewer: a host-provided MathJax did not become usable in time; falling back to loading Doenet's own copy.",
42054
- reason
42055
- );
42056
- return injectMathJax(src, config2, { force: true });
42057
- });
42143
+ return waitForExistingMathJax(timeoutMs).then(
42144
+ // The host's engine: ours to use, not ours to have configured.
42145
+ (engine) => primeUnconfiguredEngine(engine, config2),
42146
+ // The takeover copy is one we load and stage `config` on ourselves,
42147
+ // so it needs no priming. Handling the rejection here rather than in
42148
+ // a trailing `.catch` also keeps a priming failure above — which is
42149
+ // never fatal — from being mistaken for a host that never loaded,
42150
+ // and answered by injecting a second engine over the host's.
42151
+ (reason) => {
42152
+ console.warn(
42153
+ "DoenetViewer: a host-provided MathJax did not become usable in time; falling back to loading Doenet's own copy.",
42154
+ reason
42155
+ );
42156
+ return injectMathJax(src, config2, { force: true });
42157
+ }
42158
+ );
42058
42159
  }
42059
42160
  return injectMathJax(src, config2);
42060
42161
  }
@@ -68183,8 +68284,19 @@ Object.fromEntries(
68183
68284
  mediaLicenses.map((info) => [info.code.toLowerCase(), info])
68184
68285
  );
68185
68286
  const mathjaxConfig = {
68287
+ loader: {
68288
+ // The `units` package is neither bundled into the combined component
68289
+ // we load nor reachable through `autoload`, so it has to be requested
68290
+ // by name. MathJax fetches it during startup, from the same place it
68291
+ // gets the rest of its components.
68292
+ load: ["[tex]/units"]
68293
+ },
68186
68294
  tex: {
68187
68295
  tags: "ams",
68296
+ // `[+]` adds to MathJax's default package list rather than replacing
68297
+ // it. `units` supplies `\units`, `\unitfrac`, and `\nicefrac`, so that
68298
+ // units can be typeset semantically instead of hand-spaced.
68299
+ packages: { "[+]": ["units"] },
68188
68300
  macros: {
68189
68301
  lt: "<",
68190
68302
  gt: ">",
@@ -68594,7 +68706,7 @@ function ExternalVirtualKeyboard({
68594
68706
  }
68595
68707
  );
68596
68708
  }
68597
- const version = "0.7.25-dev.508";
68709
+ const version = "0.7.25-dev.510";
68598
68710
  const latestDoenetmlVersion = version;
68599
68711
  function subscribeToPinnedTheme() {
68600
68712
  return () => {