@pretextbook/remark-pretext 0.0.5 → 0.0.7

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.
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
- import { unified as R } from "unified";
2
- import j from "remark-parse";
3
- import B from "remark-directive";
4
- import { visit as z } from "unist-util-visit";
5
- const I = {
1
+ import { unified as j } from "unified";
2
+ import B from "remark-parse";
3
+ import z from "remark-directive";
4
+ import { isTitlelessDivisionType as ie, divisionTypeAtRelativeDepth as se, isTopLevelDivisionType as le } from "@pretextbook/ptxast";
5
+ import { visit as W } from "unist-util-visit";
6
+ const S = {
6
7
  // theorem-like (requires statement wrapping)
7
8
  theorem: { type: "theorem", category: "theorem-like", requiresStatement: !0 },
8
9
  lemma: { type: "lemma", category: "theorem-like", requiresStatement: !0 },
@@ -43,14 +44,14 @@ const I = {
43
44
  solution: { type: "solution", category: "solution-like", requiresStatement: !1 },
44
45
  hint: { type: "hint", category: "solution-like", requiresStatement: !1 },
45
46
  answer: { type: "answer", category: "solution-like", requiresStatement: !1 }
46
- }, gt = I;
47
- function se(e) {
48
- return I[e.toLowerCase()];
47
+ }, St = S;
48
+ function ce(e) {
49
+ return S[e.toLowerCase()];
49
50
  }
50
- const W = new Set(
51
- Object.entries(I).filter(([e, t]) => t.category === "proof-like" || t.category === "solution-like").map(([e]) => e)
51
+ const H = new Set(
52
+ Object.entries(S).filter(([e, t]) => t.category === "proof-like" || t.category === "solution-like").map(([e]) => e)
52
53
  );
53
- function x(e, t, n) {
54
+ function b(e, t, n) {
54
55
  return {
55
56
  type: "element",
56
57
  name: e,
@@ -58,96 +59,96 @@ function x(e, t, n) {
58
59
  children: t
59
60
  };
60
61
  }
61
- function C(e) {
62
+ function D(e) {
62
63
  return e.type === "containerDirective" && e.name === "task";
63
64
  }
64
- function ie(e) {
65
- return e.type === "containerDirective" && W.has(e.name);
65
+ function ae(e) {
66
+ return e.type === "containerDirective" && H.has(e.name);
66
67
  }
67
- function D(e) {
68
+ function w(e) {
68
69
  const t = [];
69
70
  for (const n of e)
70
- n.name === "ul" || n.name === "ol" ? t.length > 0 && t[t.length - 1]?.name === "p" ? t[t.length - 1].children.push(n) : t.push(x("p", [n])) : t.push(n);
71
+ n.name === "ul" || n.name === "ol" ? t.length > 0 && t[t.length - 1]?.name === "p" ? t[t.length - 1].children.push(n) : t.push(b("p", [n])) : t.push(n);
71
72
  return t;
72
73
  }
73
- function le(e, t, n, o, r, s, i) {
74
+ function ue(e, t, n, o, r, i, s) {
74
75
  const l = [];
75
76
  if (n && l.push(n), e.hasNestedTasks) {
76
- const c = o.findIndex(C);
77
+ const c = o.findIndex(D);
77
78
  if (c !== -1) {
78
79
  const a = [];
79
80
  for (let p = 0; p < o.length; p++)
80
- C(o[p]) && a.push(p);
81
- const u = a[a.length - 1], d = o.slice(0, c), m = o.slice(c).filter(C), v = [], y = [], L = [];
81
+ D(o[p]) && a.push(p);
82
+ const u = a[a.length - 1], d = o.slice(0, c), m = o.slice(c).filter(D), k = [], y = [], F = [];
82
83
  for (let p = c; p < o.length; p++) {
83
84
  const f = o[p];
84
- if (!C(f)) {
85
+ if (!D(f)) {
85
86
  if (p < u) {
86
- v.push(f);
87
+ k.push(f);
87
88
  continue;
88
89
  }
89
- if (ie(f)) {
90
- L.push(f);
90
+ if (ae(f)) {
91
+ F.push(f);
91
92
  continue;
92
93
  }
93
94
  y.push(f);
94
95
  }
95
96
  }
96
- if (v.length > 0 && (r.messages ?? (r.messages = [])).push({
97
+ if (k.length > 0 && (r.messages ?? (r.messages = [])).push({
97
98
  type: "warning",
98
- reason: `Dropped ${v.length} non-task node(s) between task directives in ${e.type}.`,
99
+ reason: `Dropped ${k.length} non-task node(s) between task directives in ${e.type}.`,
99
100
  category: "dropped-content-between-tasks",
100
- position: v[0].position?.start
101
+ position: k[0].position?.start
101
102
  }), d.length > 0) {
102
- const p = D(
103
- d.map((f) => s(f, r)).filter((f) => f !== null)
103
+ const p = w(
104
+ d.map((f) => i(f, r)).filter((f) => f !== null)
104
105
  );
105
- p.length > 0 && l.push(x("introduction", p));
106
+ p.length > 0 && l.push(b("introduction", p));
106
107
  }
107
108
  for (const p of m) {
108
- const f = i(p, r);
109
+ const f = s(p, r);
109
110
  f && l.push(f);
110
111
  }
111
112
  if (y.length > 0) {
112
- const p = D(
113
- y.map((f) => s(f, r)).filter((f) => f !== null)
113
+ const p = w(
114
+ y.map((f) => i(f, r)).filter((f) => f !== null)
114
115
  );
115
- p.length > 0 && l.push(x("conclusion", p));
116
+ p.length > 0 && l.push(b("conclusion", p));
116
117
  }
117
- for (const p of L) {
118
- const f = i(p, r);
118
+ for (const p of F) {
119
+ const f = s(p, r);
119
120
  f && l.push(f);
120
121
  }
121
122
  } else
122
- F(l, e, o, r, s, i);
123
+ N(l, e, o, r, i, s);
123
124
  } else
124
- F(l, e, o, r, s, i);
125
- return x(e.type, l, t);
125
+ N(l, e, o, r, i, s);
126
+ return b(e.type, l, t);
126
127
  }
127
- function F(e, t, n, o, r, s) {
128
+ function N(e, t, n, o, r, i) {
128
129
  if (t.requiresStatement) {
129
- const i = [], l = [];
130
+ const s = [], l = [];
130
131
  for (const c of n)
131
- if (c.type === "containerDirective" && W.has(c.name)) {
132
- const a = s(c, o);
132
+ if (c.type === "containerDirective" && H.has(c.name)) {
133
+ const a = i(c, o);
133
134
  a && l.push(a);
134
135
  } else
135
- i.push(c);
136
- if (i.length > 0) {
137
- const c = D(
138
- i.map((a) => r(a, o)).filter((a) => a !== null)
136
+ s.push(c);
137
+ if (s.length > 0) {
138
+ const c = w(
139
+ s.map((a) => r(a, o)).filter((a) => a !== null)
139
140
  );
140
- c.length > 0 && e.push(x("statement", c));
141
+ c.length > 0 && e.push(b("statement", c));
141
142
  }
142
143
  e.push(...l);
143
144
  } else {
144
- const i = D(
145
+ const s = w(
145
146
  n.map((l) => r(l, o)).filter((l) => l !== null)
146
147
  );
147
- e.push(...i);
148
+ e.push(...s);
148
149
  }
149
150
  }
150
- function H(e) {
151
+ function U(e) {
151
152
  return { type: "text", value: e };
152
153
  }
153
154
  function h(e, t, n) {
@@ -159,77 +160,81 @@ function h(e, t, n) {
159
160
  };
160
161
  }
161
162
  function g(e, t, n) {
162
- return h(e, [H(t)], n);
163
+ return h(e, [U(t)], n);
163
164
  }
164
- function U(e) {
165
+ function Z(e) {
165
166
  return { type: "comment", value: e.replace(/--/g, "- -") };
166
167
  }
167
168
  function $(e, t) {
168
169
  const n = e?.position, o = n?.start?.offset, r = n?.end?.offset;
169
170
  return t.source && o != null && r != null ? t.source.slice(o, r) : "";
170
171
  }
171
- function Z(e, t, n) {
172
- return h("TODO", [U(`todo: ${t}`), g("pre", n)], {
172
+ function X(e, t, n) {
173
+ return h("TODO", [Z(`todo: ${t}`), g("pre", n)], {
173
174
  type: e
174
175
  });
175
176
  }
176
- function ce(e, t, n) {
177
- return h("TODO", [U(`todo: ${t}`), g("c", n)], {
177
+ function pe(e, t, n) {
178
+ return h("TODO", [Z(`todo: ${t}`), g("c", n)], {
178
179
  type: e
179
180
  });
180
181
  }
181
- function X(e) {
182
+ function M(e) {
182
183
  const t = [];
183
184
  for (const n of e)
184
185
  n.name === "ul" || n.name === "ol" ? t.length > 0 && t[t.length - 1]?.name === "p" ? t[t.length - 1].children.push(n) : t.push(h("p", [n])) : t.push(n);
185
186
  return t;
186
187
  }
187
- function N(e, t) {
188
- return ae(e, t).tree;
189
- }
190
- function ae(e, t) {
191
- const n = [], o = { ancestors: [], depth: 0, messages: n, source: t }, r = e.children;
188
+ function O(e, t, n) {
189
+ return fe(e, t, n).tree;
190
+ }
191
+ function fe(e, t, n) {
192
+ const o = [], r = {
193
+ ancestors: [],
194
+ depth: 0,
195
+ messages: o,
196
+ source: t,
197
+ topLevelDivision: n?.topLevelDivision ?? "chapter",
198
+ topLevelAttributes: n?.topLevelAttributes,
199
+ topLevelAttributesApplied: n?.topLevelAttributes ? { done: !1 } : void 0
200
+ }, i = e.children;
201
+ if (ie(r.topLevelDivision)) {
202
+ const l = V(r), c = M(
203
+ q(i, { ...r, depth: 1 }, !0)
204
+ );
205
+ return { tree: { type: "root", children: [h(r.topLevelDivision, c, l)] }, messages: o };
206
+ }
192
207
  return {
193
- tree: { type: "root", children: K(r, o) },
194
- messages: n
208
+ tree: { type: "root", children: q(i, r) },
209
+ messages: o
195
210
  };
196
211
  }
197
- const O = [
198
- "chapter",
199
- "section",
200
- "subsection",
201
- "subsubsection",
202
- "paragraphs"
203
- ];
204
- function ue(e) {
205
- return O[Math.min(e - 1, O.length - 1)];
206
- }
207
- function K(e, t, n = !1) {
212
+ function q(e, t, n = !1) {
208
213
  let o = 7;
209
214
  for (const a of e)
210
215
  a.type === "heading" && (o = Math.min(o, a.depth));
211
216
  if (o === 7)
212
- return w(e, t, n);
217
+ return C(e, t, n);
213
218
  const r = [];
214
- let s = null, i = [], l = [];
219
+ let i = null, s = [], l = [];
215
220
  const c = () => {
216
- s && (r.push(pe(s, l, t)), s = null, l = []);
221
+ i && (r.push(he(i, l, t)), i = null, l = []);
217
222
  };
218
223
  for (const a of e)
219
- a.type === "heading" && a.depth === o ? (!s && i.length > 0 && (r.push(
220
- ...w(i, t, n)
221
- ), i = []), c(), s = a, l = []) : s ? l.push(a) : i.push(a);
222
- return !s && i.length > 0 && r.push(
223
- ...w(i, t, n)
224
+ a.type === "heading" && a.depth === o ? (!i && s.length > 0 && (r.push(
225
+ ...C(s, t, n)
226
+ ), s = []), c(), i = a, l = []) : i ? l.push(a) : s.push(a);
227
+ return !i && s.length > 0 && r.push(
228
+ ...C(s, t, n)
224
229
  ), c(), r;
225
230
  }
226
- function w(e, t, n = !1) {
231
+ function C(e, t, n = !1) {
227
232
  const o = [];
228
233
  for (let r = 0; r < e.length; r += 1) {
229
- const s = e[r], i = T(s, t);
230
- if (i !== null) {
231
- if (i.name === "p" && i.children.length === 1 && i.children[0].name === "md") {
232
- const l = i.children[0], c = o[o.length - 1], a = r + 1 < e.length ? T(e[r + 1], t) : null, u = a?.name === "p" ? a.children : null;
234
+ const i = e[r], s = E(i, t);
235
+ if (s !== null) {
236
+ if (s.name === "p" && s.children.length === 1 && s.children[0].name === "md") {
237
+ const l = s.children[0], c = o[o.length - 1], a = r + 1 < e.length ? E(e[r + 1], t) : null, u = a?.name === "p" ? a.children : null;
233
238
  if (c?.name === "p")
234
239
  c.children.push(l), u && (c.children.push(
235
240
  ...u
@@ -240,111 +245,125 @@ function w(e, t, n = !1) {
240
245
  }
241
246
  continue;
242
247
  }
243
- if ((i.name === "ul" || i.name === "ol") && o.length > 0) {
248
+ if ((s.name === "ul" || s.name === "ol") && o.length > 0) {
244
249
  const l = o[o.length - 1];
245
250
  if (l?.name === "p") {
246
- l.children.push(i);
251
+ l.children.push(s);
247
252
  continue;
248
253
  }
249
254
  }
250
- o.push(i);
255
+ o.push(s);
251
256
  }
252
257
  }
253
- return n ? X(o) : o;
258
+ return n ? M(o) : o;
254
259
  }
255
- function pe(e, t, n) {
256
- const o = ue(e.depth), r = h("title", b(e.children, n)), s = fe(e), i = K(
260
+ function he(e, t, n) {
261
+ const o = se(
262
+ n.topLevelDivision,
263
+ e.depth
264
+ ), r = h("title", x(e.children, n)), i = me(e, n), s = q(
257
265
  t,
258
266
  { ...n, depth: n.depth + 1 },
259
267
  !0
260
- ), l = X(i);
261
- return h(o, [r, ...l], s);
268
+ ), l = M(s);
269
+ return h(o, [r, ...l], i);
262
270
  }
263
- function fe(e) {
271
+ function de(e) {
264
272
  const t = e.data;
265
273
  return t?.id ? { "xml:id": t.id } : void 0;
266
274
  }
267
- const he = {
268
- paragraph: (e, t) => Y(e, t),
269
- blockquote: (e, t) => de(e, t),
270
- list: (e, t) => me(e, t),
271
- code: (e, t) => ye(e),
272
- containerDirective: (e, t) => V(e, t)
275
+ function me(e, t) {
276
+ const n = de(e), o = V(t);
277
+ if (!(!n && !o))
278
+ return { ...o, ...n };
279
+ }
280
+ function V(e) {
281
+ if (e.depth !== 0) return;
282
+ const t = e.topLevelAttributesApplied;
283
+ if (!(!e.topLevelAttributes || !t || t.done))
284
+ return t.done = !0, e.topLevelAttributes;
285
+ }
286
+ const ge = {
287
+ paragraph: (e, t) => K(e, t),
288
+ blockquote: (e, t) => ye(e, t),
289
+ list: (e, t) => ve(e, t),
290
+ code: (e, t) => be(e),
291
+ containerDirective: (e, t) => Y(e, t)
273
292
  };
274
- function T(e, t) {
275
- const n = he[e.type];
293
+ function E(e, t) {
294
+ const n = ge[e.type];
276
295
  return n ? n(e, t) : (t.messages && t.messages.push({
277
296
  type: "warning",
278
297
  reason: `Unknown block type: ${e.type}`,
279
298
  category: "unknown-block-type"
280
- }), Z(
299
+ }), X(
281
300
  "unknown-block-type",
282
301
  `unknown block type "${e.type}"`,
283
302
  $(e, t)
284
303
  ));
285
304
  }
286
- function Y(e, t) {
287
- return h("p", b(e.children, t));
305
+ function K(e, t) {
306
+ return h("p", x(e.children, t));
288
307
  }
289
- function de(e, t) {
308
+ function ye(e, t) {
290
309
  const n = e.children.filter((o) => o.type === "paragraph").map((o) => {
291
310
  const r = {
292
311
  ...t,
293
312
  ancestors: [...t.ancestors, e],
294
313
  depth: t.depth + 1
295
314
  };
296
- return Y(o, r);
315
+ return K(o, r);
297
316
  });
298
317
  return h("blockquote", n);
299
318
  }
300
- function me(e, t) {
319
+ function ve(e, t) {
301
320
  const n = e.children.map((o) => {
302
321
  const r = {
303
322
  ...t,
304
323
  ancestors: [...t.ancestors, e],
305
324
  depth: t.depth + 1
306
325
  };
307
- return ge(o, r);
326
+ return ke(o, r);
308
327
  });
309
328
  return h(e.ordered ? "ol" : "ul", n);
310
329
  }
311
- function ge(e, t) {
312
- const n = w(
330
+ function ke(e, t) {
331
+ const n = C(
313
332
  e.children,
314
333
  t
315
334
  );
316
335
  return h("li", n);
317
336
  }
318
- function ye(e, t) {
337
+ function be(e, t) {
319
338
  return g(
320
339
  "program",
321
340
  e.value,
322
341
  e.lang ? { language: e.lang } : void 0
323
342
  );
324
343
  }
325
- function ke(e, t) {
344
+ function xe(e, t) {
326
345
  const n = e.value.split(/\\\\|[\r\n]+/).map((o) => o.trim()).filter((o) => o.length > 0);
327
346
  return n.length > 1 ? h(
328
347
  "md",
329
348
  n.map((o) => g("mrow", o))
330
349
  ) : g("md", n[0] ?? "");
331
350
  }
332
- function ve(e, t) {
333
- return e.meta === "inline" ? g("m", e.value) : ke(e);
351
+ function De(e, t) {
352
+ return e.meta === "inline" ? g("m", e.value) : xe(e);
334
353
  }
335
- function V(e, t) {
336
- const n = se(e.name);
354
+ function Y(e, t) {
355
+ const n = ce(e.name);
337
356
  if (!n)
338
357
  return t.messages && t.messages.push({
339
358
  type: "warning",
340
359
  reason: `Unknown directive: ${e.name}`,
341
360
  category: "unknown-directive"
342
- }), Z(
361
+ }), X(
343
362
  "unknown-directive",
344
363
  `unknown directive "${e.name}"`,
345
364
  $(e, t)
346
365
  );
347
- const o = be(e), r = e.children, { title: s, body: i } = xe(
366
+ const o = we(e), r = e.children, { title: i, body: s } = Ce(
348
367
  r,
349
368
  t
350
369
  ), l = {
@@ -352,56 +371,56 @@ function V(e, t) {
352
371
  ancestors: [...t.ancestors, e],
353
372
  depth: t.depth + 1
354
373
  };
355
- return le(
374
+ return ue(
356
375
  n,
357
376
  o,
358
- s,
359
377
  i,
378
+ s,
360
379
  l,
361
- (c, a) => T(c, a),
362
- (c, a) => V(c, a)
380
+ (c, a) => E(c, a),
381
+ (c, a) => Y(c, a)
363
382
  );
364
383
  }
365
- function xe(e, t) {
384
+ function Ce(e, t) {
366
385
  const n = e[0];
367
386
  return n?.type === "paragraph" && n.data?.directiveLabel === !0 ? {
368
387
  title: h(
369
388
  "title",
370
- b(n.children, t)
389
+ x(n.children, t)
371
390
  ),
372
391
  body: e.slice(1)
373
392
  } : { title: null, body: e };
374
393
  }
375
- function be(e) {
394
+ function we(e) {
376
395
  const t = e.attributes ?? {}, n = {};
377
396
  for (const [o, r] of Object.entries(t))
378
397
  r != null && (n[o === "id" ? "xml:id" : o] = r);
379
398
  return Object.keys(n).length > 0 ? n : void 0;
380
399
  }
381
- function Ce(e, t) {
400
+ function Le(e, t) {
382
401
  return !t || e.position?.start?.offset === null || e.position?.start?.offset === void 0 ? "asterisk" : t.charAt(e.position.start.offset) === "_" ? "underscore" : "asterisk";
383
402
  }
384
- const we = {
385
- text: (e, t) => H(e.value),
403
+ const Se = {
404
+ text: (e, t) => U(e.value),
386
405
  emphasis: (e, t) => {
387
- const o = Ce(e, t.source) === "underscore" ? "term" : "em";
388
- return h(o, b(e.children, t));
406
+ const o = Le(e, t.source) === "underscore" ? "term" : "em";
407
+ return h(o, x(e.children, t));
389
408
  },
390
- strong: (e, t) => h("alert", b(e.children, t)),
409
+ strong: (e, t) => h("alert", x(e.children, t)),
391
410
  inlineCode: (e, t) => g("c", e.value),
392
- math: (e, t) => ve(e)
411
+ math: (e, t) => De(e)
393
412
  };
394
- function b(e, t) {
395
- return e.flatMap((n) => De(n, t)).filter((n) => n !== null);
413
+ function x(e, t) {
414
+ return e.flatMap((n) => Ie(n, t)).filter((n) => n !== null);
396
415
  }
397
- function De(e, t) {
398
- const n = we[e.type];
416
+ function Ie(e, t) {
417
+ const n = Se[e.type];
399
418
  if (!n)
400
419
  return t.messages && t.messages.push({
401
420
  type: "warning",
402
421
  reason: `Unknown inline type: ${e.type}`,
403
422
  category: "unknown-inline-type"
404
- }), ce(
423
+ }), pe(
405
424
  "unknown-inline-type",
406
425
  `unknown inline type "${e.type}"`,
407
426
  $(e, t)
@@ -409,39 +428,39 @@ function De(e, t) {
409
428
  const o = { ...t, ancestors: [...t.ancestors, e] };
410
429
  return n(e, o);
411
430
  }
412
- const Se = [
431
+ const qe = [
413
432
  // Display math (check these first, before single $)
414
433
  { open: "$$", close: "$$", isDisplay: !0 },
415
434
  { open: "\\[", close: "\\]", isDisplay: !0 },
416
435
  // Inline math
417
436
  { open: "$", close: "$", isDisplay: !1 },
418
437
  { open: "\\(", close: "\\)", isDisplay: !1 }
419
- ], G = "PTX_MATH_TOKEN_", q = new RegExp(`${G}(\\d+)`, "g");
438
+ ], G = "PTX_MATH_TOKEN_", I = new RegExp(`${G}(\\d+)`, "g");
420
439
  function J(e, t) {
421
440
  let n = null;
422
- for (const o of Se) {
441
+ for (const o of qe) {
423
442
  const r = e.indexOf(o.open, t);
424
443
  if (r === -1) continue;
425
- const s = e.indexOf(o.close, r + o.open.length);
426
- if (s !== -1 && !(o.open === "$" && (r > 0 && e[r - 1] === "$" || r + 1 < e.length && e[r + 1] === "$")) && (!n || r < n.index)) {
427
- const i = e.substring(r + o.open.length, s);
428
- n = { index: r, pattern: o, content: i };
444
+ const i = e.indexOf(o.close, r + o.open.length);
445
+ if (i !== -1 && !(o.open === "$" && (r > 0 && e[r - 1] === "$" || r + 1 < e.length && e[r + 1] === "$")) && (!n || r < n.index)) {
446
+ const s = e.substring(r + o.open.length, i);
447
+ n = { index: r, pattern: o, content: s };
429
448
  }
430
449
  }
431
450
  return n;
432
451
  }
433
- function Ie(e, t) {
452
+ function Ee(e, t) {
434
453
  let n = t;
435
454
  for (; n < e.length; ) {
436
455
  const o = e.indexOf("`", n);
437
456
  if (o === -1) return null;
438
- let r = 0, s = o;
439
- for (; s < e.length && e[s] === "`"; )
440
- r++, s++;
441
- const i = "`".repeat(r);
442
- let l = s;
457
+ let r = 0, i = o;
458
+ for (; i < e.length && e[i] === "`"; )
459
+ r++, i++;
460
+ const s = "`".repeat(r);
461
+ let l = i;
443
462
  for (; l < e.length; ) {
444
- const c = e.indexOf(i, l);
463
+ const c = e.indexOf(s, l);
445
464
  if (c === -1) break;
446
465
  const a = c + r;
447
466
  if (a >= e.length || e[a] !== "`")
@@ -452,30 +471,30 @@ function Ie(e, t) {
452
471
  }
453
472
  return null;
454
473
  }
455
- function qe(e) {
474
+ function Te(e) {
456
475
  const t = /* @__PURE__ */ new Map();
457
476
  let n = "", o = 0, r = 0;
458
477
  for (; o < e.length; ) {
459
- const s = Ie(e, o), i = J(e, o);
460
- if (s && (!i || s.start < i.index)) {
461
- n += e.substring(o, s.end), o = s.end;
478
+ const i = Ee(e, o), s = J(e, o);
479
+ if (i && (!s || i.start < s.index)) {
480
+ n += e.substring(o, i.end), o = i.end;
462
481
  continue;
463
482
  }
464
- if (!i) {
483
+ if (!s) {
465
484
  n += e.substring(o);
466
485
  break;
467
486
  }
468
- n += e.substring(o, i.index);
487
+ n += e.substring(o, s.index);
469
488
  const l = `${G}${r}`;
470
489
  n += l, t.set(l, {
471
490
  type: "math",
472
- value: i.content,
473
- meta: i.pattern.isDisplay ? "display" : "inline"
474
- }), o = i.index + i.pattern.open.length + i.content.length + i.pattern.close.length, r += 1;
491
+ value: s.content,
492
+ meta: s.pattern.isDisplay ? "display" : "inline"
493
+ }), o = s.index + s.pattern.open.length + s.content.length + s.pattern.close.length, r += 1;
475
494
  }
476
495
  return { markdown: n, tokens: t };
477
496
  }
478
- function Te(e) {
497
+ function Ae(e) {
479
498
  const t = [];
480
499
  let n = 0;
481
500
  for (; n < e.length; ) {
@@ -495,57 +514,57 @@ function Te(e) {
495
514
  }
496
515
  return t.length === 0 ? [{ type: "text", value: e }] : t;
497
516
  }
498
- function Ee(e, t) {
517
+ function $e(e, t) {
499
518
  const n = [];
500
519
  let o = 0;
501
- q.lastIndex = 0;
502
- let r = q.exec(e);
520
+ I.lastIndex = 0;
521
+ let r = I.exec(e);
503
522
  for (; r; ) {
504
- const s = r.index, i = r[0];
505
- s > o && n.push({ type: "text", value: e.substring(o, s) });
506
- const l = t.get(i);
507
- l ? n.push(l) : n.push({ type: "text", value: i }), o = s + i.length, r = q.exec(e);
523
+ const i = r.index, s = r[0];
524
+ i > o && n.push({ type: "text", value: e.substring(o, i) });
525
+ const l = t.get(s);
526
+ l ? n.push(l) : n.push({ type: "text", value: s }), o = i + s.length, r = I.exec(e);
508
527
  }
509
528
  return o < e.length && n.push({ type: "text", value: e.substring(o) }), n.length === 0 ? [{ type: "text", value: e }] : n;
510
529
  }
511
530
  function Q(e) {
512
- z(e, "paragraph", (t) => {
531
+ W(e, "paragraph", (t) => {
513
532
  const n = t.children || [], o = [];
514
533
  for (const r of n)
515
534
  if (r.type === "text") {
516
- const s = Te(r.value);
517
- o.push(...s);
535
+ const i = Ae(r.value);
536
+ o.push(...i);
518
537
  } else
519
538
  o.push(r);
520
539
  t.children = o;
521
540
  });
522
541
  }
523
542
  function Me(e, t) {
524
- z(e, "paragraph", (n) => {
543
+ W(e, "paragraph", (n) => {
525
544
  const o = n.children || [], r = [];
526
- for (const s of o)
527
- if (s.type === "text") {
528
- const i = Ee(s.value, t);
529
- r.push(...i);
545
+ for (const i of o)
546
+ if (i.type === "text") {
547
+ const s = $e(i.value, t);
548
+ r.push(...s);
530
549
  } else
531
- r.push(s);
550
+ r.push(i);
532
551
  n.children = r;
533
552
  });
534
553
  }
535
- const yt = () => (e) => {
554
+ const It = () => (e) => {
536
555
  Q(e);
537
556
  };
538
- function $e(e) {
557
+ function Fe(e) {
539
558
  const t = e.trim();
540
559
  if (!t.startsWith(":")) return null;
541
560
  let n = 0;
542
- for (let s = 0; s < t.length && t[s] === ":"; s++)
561
+ for (let i = 0; i < t.length && t[i] === ":"; i++)
543
562
  n++;
544
563
  if (n < 3) return null;
545
564
  const o = t.slice(n).trim(), r = o.match(/^([a-zA-Z0-9_-]+)(\[[^\]]*\])?(\{[^}]*\})?$/);
546
565
  return o === "" ? { colons: n, label: null, lineIndex: -1, isOpen: !1 } : r ? { colons: n, label: r[0], lineIndex: -1, isOpen: !0 } : null;
547
566
  }
548
- function Le(e) {
567
+ function Ne(e) {
549
568
  const t = [], n = [];
550
569
  for (const o of e)
551
570
  if (o.isOpen) {
@@ -559,8 +578,8 @@ function Le(e) {
559
578
  parent: null
560
579
  };
561
580
  if (t.length > 0) {
562
- const s = t[t.length - 1];
563
- r.parent = s, s.children.push(r);
581
+ const i = t[t.length - 1];
582
+ r.parent = i, i.children.push(r);
564
583
  } else
565
584
  n.push(r);
566
585
  t.push(r);
@@ -579,22 +598,22 @@ function ee(e) {
579
598
  }
580
599
  return e.finalColons;
581
600
  }
582
- function Fe(e, t, n) {
583
- const o = e.slice(), r = [], s = t.slice();
584
- for (; s.length > 0; ) {
585
- const i = s.shift();
586
- r.push(i), s.push(...i.children);
601
+ function Oe(e, t, n) {
602
+ const o = e.slice(), r = [], i = t.slice();
603
+ for (; i.length > 0; ) {
604
+ const s = i.shift();
605
+ r.push(s), i.push(...s.children);
587
606
  }
588
- for (const i of r) {
589
- if (i.openLineIndex >= 0) {
590
- const l = o[i.openLineIndex], c = l.match(/^:+/)?.[0].length || 3, a = l.slice(c);
591
- o[i.openLineIndex] = ":".repeat(i.finalColons) + a;
607
+ for (const s of r) {
608
+ if (s.openLineIndex >= 0) {
609
+ const l = o[s.openLineIndex], c = l.match(/^:+/)?.[0].length || 3, a = l.slice(c);
610
+ o[s.openLineIndex] = ":".repeat(s.finalColons) + a;
592
611
  }
593
- i.closeLineIndex !== null && i.closeLineIndex >= 0 && (o[i.closeLineIndex] = ":".repeat(i.finalColons));
612
+ s.closeLineIndex !== null && s.closeLineIndex >= 0 && (o[s.closeLineIndex] = ":".repeat(s.finalColons));
594
613
  }
595
614
  return o;
596
615
  }
597
- function Ne(e) {
616
+ function _e(e) {
598
617
  const t = e.split(`
599
618
  `), n = [];
600
619
  let o = !1, r = null;
@@ -607,28 +626,28 @@ function Ne(e) {
607
626
  }
608
627
  if (o)
609
628
  continue;
610
- const d = $e(c);
629
+ const d = Fe(c);
611
630
  d && n.push({ ...d, lineIndex: l });
612
631
  }
613
632
  if (n.length === 0)
614
633
  return e;
615
- const s = Le(n);
616
- for (const l of s)
634
+ const i = Ne(n);
635
+ for (const l of i)
617
636
  ee(l);
618
- return Fe(t, s).join(`
637
+ return Oe(t, i).join(`
619
638
  `);
620
639
  }
621
- function Oe(e) {
640
+ function Re(e) {
622
641
  const t = e.trim();
623
642
  if (!t.endsWith(":")) return null;
624
643
  const n = t.slice(0, -1);
625
644
  if (!/^[a-zA-Z]/.test(n)) return null;
626
645
  const o = n.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)$/);
627
646
  if (!o) return null;
628
- const [, r, s] = o;
629
- return s && !/^(\[[^\]]*\]|\{[^}]*\})*$/.test(s) || !I[r.toLowerCase()] ? null : n;
647
+ const [, r, i] = o;
648
+ return i && !/^(\[[^\]]*\]|\{[^}]*\})*$/.test(i) || !S[r.toLowerCase()] ? null : n;
630
649
  }
631
- function E(e) {
650
+ function T(e) {
632
651
  let t = 0;
633
652
  for (let n = 0; n < e.length; n++)
634
653
  if (e[n] === " ")
@@ -639,17 +658,17 @@ function E(e) {
639
658
  break;
640
659
  return t;
641
660
  }
642
- function Ae(e, t) {
643
- const n = E(e[t]);
661
+ function Pe(e, t) {
662
+ const n = T(e[t]);
644
663
  for (let o = t + 1; o < e.length; o++)
645
664
  if (!te(e[o]))
646
- return E(e[o]) > n;
665
+ return T(e[o]) > n;
647
666
  return !1;
648
667
  }
649
668
  function te(e) {
650
669
  return e.trim() === "";
651
670
  }
652
- function Pe(e, t) {
671
+ function je(e, t) {
653
672
  let n = 0, o = 0;
654
673
  for (; o < e.length && n < t; )
655
674
  if (e[o] === " ")
@@ -660,7 +679,7 @@ function Pe(e, t) {
660
679
  break;
661
680
  return e.slice(o);
662
681
  }
663
- function _e(e) {
682
+ function Be(e) {
664
683
  const t = e.split(`
665
684
  `), n = [], o = {
666
685
  level: 0,
@@ -669,22 +688,22 @@ function _e(e) {
669
688
  codeFenceMarker: null
670
689
  };
671
690
  for (let r = 0; r < t.length; r++) {
672
- const s = t[r], i = s.trim(), l = E(s), c = i.match(/^(`{3,}|~{3,})/);
691
+ const i = t[r], s = i.trim(), l = T(i), c = s.match(/^(`{3,}|~{3,})/);
673
692
  if (c) {
674
693
  const u = c[1];
675
- o.inCodeFence ? o.codeFenceMarker && i.startsWith(o.codeFenceMarker) && (o.inCodeFence = !1, o.codeFenceMarker = null) : (o.inCodeFence = !0, o.codeFenceMarker = u), n.push(s);
694
+ o.inCodeFence ? o.codeFenceMarker && s.startsWith(o.codeFenceMarker) && (o.inCodeFence = !1, o.codeFenceMarker = null) : (o.inCodeFence = !0, o.codeFenceMarker = u), n.push(i);
676
695
  continue;
677
696
  }
678
697
  if (o.inCodeFence) {
679
- n.push(s);
698
+ n.push(i);
680
699
  continue;
681
700
  }
682
- if (te(s)) {
701
+ if (te(i)) {
683
702
  n.push("");
684
703
  continue;
685
704
  }
686
- const a = Oe(i);
687
- if (a && Ae(t, r)) {
705
+ const a = Re(s);
706
+ if (a && Pe(t, r)) {
688
707
  for (; o.openDirectives.length > 0 && !(o.openDirectives[o.openDirectives.length - 1].indentLevel < l); ) {
689
708
  const y = o.openDirectives.pop();
690
709
  y && n.push(":".repeat(y.colonCount));
@@ -703,9 +722,9 @@ function _e(e) {
703
722
  }
704
723
  if (o.openDirectives.length > 0) {
705
724
  const u = o.openDirectives[o.openDirectives.length - 1];
706
- u.bodyBaseIndent < 0 && (u.bodyBaseIndent = l), n.push(Pe(s, u.bodyBaseIndent));
725
+ u.bodyBaseIndent < 0 && (u.bodyBaseIndent = l), n.push(je(i, u.bodyBaseIndent));
707
726
  } else
708
- n.push(s);
727
+ n.push(i);
709
728
  o.level = l;
710
729
  }
711
730
  }
@@ -716,81 +735,99 @@ function _e(e) {
716
735
  return n.join(`
717
736
  `);
718
737
  }
719
- const Re = function() {
720
- return function(t, n) {
721
- if (typeof n?.value == "string") {
722
- const o = _e(n.value), r = Ne(o), s = qe(r), l = R().data("micromarkExtensions", [{ disable: { null: ["codeIndented"] } }]).use(j).use(B).parse(s.markdown);
723
- return Me(l, s.tokens), N(l, s.markdown);
738
+ const ze = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/, We = /^division:\s*(\S+)\s*$/m, He = /^xmlid:\s*(\S+)\s*$/m, Ue = /^label:\s*(\S+)\s*$/m, Ze = /^component:\s*(\S+)\s*$/m;
739
+ function Xe(e) {
740
+ const t = e.match(ze);
741
+ if (!t)
742
+ return { body: e };
743
+ const n = t[1], o = e.slice(t[0].length), r = n.match(We)?.[1], i = r && le(r) ? r : void 0, s = {}, l = n.match(He)?.[1], c = n.match(Ue)?.[1], a = n.match(Ze)?.[1];
744
+ return l && (s["xml:id"] = l), c && (s.label = c), a && (s.component = a), {
745
+ division: i,
746
+ attributes: Object.keys(s).length > 0 ? s : void 0,
747
+ body: o
748
+ };
749
+ }
750
+ const Ve = function(e) {
751
+ return function(n, o) {
752
+ if (typeof o?.value == "string") {
753
+ const r = Xe(o.value), i = e?.topLevelDivision ?? r.division ?? "chapter", s = e?.topLevelAttributes ?? r.attributes, l = Be(r.body), c = _e(l), a = Te(c), d = j().data("micromarkExtensions", [{ disable: { null: ["codeIndented"] } }]).use(B).use(z).parse(a.markdown);
754
+ return Me(d, a.tokens), O(d, a.markdown, {
755
+ topLevelDivision: i,
756
+ topLevelAttributes: s
757
+ });
724
758
  }
725
- return Q(t), N(t);
759
+ return Q(n), O(n, void 0, {
760
+ topLevelDivision: e?.topLevelDivision ?? "chapter",
761
+ topLevelAttributes: e?.topLevelAttributes
762
+ });
726
763
  };
727
- }, je = /["&'<>`]/g, Be = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ze = (
764
+ }, Ke = /["&'<>`]/g, Ye = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, Ge = (
728
765
  // eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
729
766
  /[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g
730
- ), We = /[|\\{}()[\]^$+*?.]/g, A = /* @__PURE__ */ new WeakMap();
731
- function He(e, t) {
767
+ ), Je = /[|\\{}()[\]^$+*?.]/g, _ = /* @__PURE__ */ new WeakMap();
768
+ function Qe(e, t) {
732
769
  if (e = e.replace(
733
- t.subset ? Ue(t.subset) : je,
770
+ t.subset ? et(t.subset) : Ke,
734
771
  o
735
772
  ), t.subset || t.escapeOnly)
736
773
  return e;
737
- return e.replace(Be, n).replace(ze, o);
738
- function n(r, s, i) {
774
+ return e.replace(Ye, n).replace(Ge, o);
775
+ function n(r, i, s) {
739
776
  return t.format(
740
777
  (r.charCodeAt(0) - 55296) * 1024 + r.charCodeAt(1) - 56320 + 65536,
741
- i.charCodeAt(s + 2),
778
+ s.charCodeAt(i + 2),
742
779
  t
743
780
  );
744
781
  }
745
- function o(r, s, i) {
782
+ function o(r, i, s) {
746
783
  return t.format(
747
784
  r.charCodeAt(0),
748
- i.charCodeAt(s + 1),
785
+ s.charCodeAt(i + 1),
749
786
  t
750
787
  );
751
788
  }
752
789
  }
753
- function Ue(e) {
754
- let t = A.get(e);
755
- return t || (t = Ze(e), A.set(e, t)), t;
790
+ function et(e) {
791
+ let t = _.get(e);
792
+ return t || (t = tt(e), _.set(e, t)), t;
756
793
  }
757
- function Ze(e) {
794
+ function tt(e) {
758
795
  const t = [];
759
796
  let n = -1;
760
797
  for (; ++n < e.length; )
761
- t.push(e[n].replace(We, "\\$&"));
798
+ t.push(e[n].replace(Je, "\\$&"));
762
799
  return new RegExp("(?:" + t.join("|") + ")", "g");
763
800
  }
764
- function Xe(e) {
801
+ function nt(e) {
765
802
  return "&#x" + e.toString(16).toUpperCase() + ";";
766
803
  }
767
- function Ke(e, t) {
768
- return He(e, Object.assign({ format: Xe }, t));
804
+ function ot(e, t) {
805
+ return Qe(e, Object.assign({ format: nt }, t));
769
806
  }
770
- const Ye = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
771
- function k(e, t, n) {
772
- const o = Ve(e);
807
+ const rt = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
808
+ function v(e, t, n) {
809
+ const o = it(e);
773
810
  return n ? o.replace(n, r) : r(o);
774
- function r(s) {
775
- return Ke(s, { subset: t });
811
+ function r(i) {
812
+ return ot(i, { subset: t });
776
813
  }
777
814
  }
778
- function Ve(e) {
779
- return String(e || "").replace(Ye, "");
815
+ function it(e) {
816
+ return String(e || "").replace(rt, "");
780
817
  }
781
- const Ge = /]]>/g, Je = [">"];
782
- function Qe(e) {
783
- return "<![CDATA[" + k(e.value, Je, Ge) + "]]>";
818
+ const st = /]]>/g, lt = [">"];
819
+ function ct(e) {
820
+ return "<![CDATA[" + v(e.value, lt, st) + "]]>";
784
821
  }
785
- function et(e) {
786
- return "<!--" + k(e.value, ["-"]) + "-->";
822
+ function at(e) {
823
+ return "<!--" + v(e.value, ["-"]) + "-->";
787
824
  }
788
- const tt = [" ", `
825
+ const ut = [" ", `
789
826
  `, " ", '"', "&", "'", "/", "<", "=", ">"];
790
- function S(e) {
791
- return k(e, tt);
827
+ function L(e) {
828
+ return v(e, ut);
792
829
  }
793
- function P(e, t) {
830
+ function R(e, t) {
794
831
  const n = String(e);
795
832
  if (typeof t != "string")
796
833
  throw new TypeError("Expected character");
@@ -799,50 +836,50 @@ function P(e, t) {
799
836
  o++, r = n.indexOf(t, r + t.length);
800
837
  return o;
801
838
  }
802
- function M(e, t) {
839
+ function A(e, t) {
803
840
  const n = String(e);
804
841
  let o = t.options.quote || '"';
805
842
  if (t.options.quoteSmart) {
806
843
  const r = o === '"' ? "'" : '"';
807
- P(n, o) > P(n, r) && (o = r);
844
+ R(n, o) > R(n, r) && (o = r);
808
845
  }
809
- return o + k(n, ["<", "&", o]) + o;
846
+ return o + v(n, ["<", "&", o]) + o;
810
847
  }
811
- function nt(e, t) {
812
- const n = S(e.name), o = e.public, r = e.system;
813
- let s = "<!DOCTYPE";
814
- return n !== "" && (s += " " + n), o ? s += " PUBLIC " + M(o, t) : r && (s += " SYSTEM"), r && (s += " " + M(r, t)), s + ">";
848
+ function pt(e, t) {
849
+ const n = L(e.name), o = e.public, r = e.system;
850
+ let i = "<!DOCTYPE";
851
+ return n !== "" && (i += " " + n), o ? i += " PUBLIC " + A(o, t) : r && (i += " SYSTEM"), r && (i += " " + A(r, t)), i + ">";
815
852
  }
816
- const ot = {}.hasOwnProperty;
817
- function rt(e, t) {
818
- const n = S(e.name), o = re(e, t), r = e.attributes || {}, s = o ? !1 : t.options.closeEmptyElements, i = [];
853
+ const ft = {}.hasOwnProperty;
854
+ function ht(e, t) {
855
+ const n = L(e.name), o = re(e, t), r = e.attributes || {}, i = o ? !1 : t.options.closeEmptyElements, s = [];
819
856
  let l;
820
857
  for (l in r)
821
- if (ot.call(r, l)) {
858
+ if (ft.call(r, l)) {
822
859
  const c = r[l];
823
- c != null && i.push(S(l) + "=" + M(c, t));
860
+ c != null && s.push(L(l) + "=" + A(c, t));
824
861
  }
825
- return "<" + n + (i.length === 0 ? "" : " " + i.join(" ")) + (s ? (t.options.tightClose ? "" : " ") + "/" : "") + ">" + o + (s ? "" : "</" + n + ">");
862
+ return "<" + n + (s.length === 0 ? "" : " " + s.join(" ")) + (i ? (t.options.tightClose ? "" : " ") + "/" : "") + ">" + o + (i ? "" : "</" + n + ">");
826
863
  }
827
- const st = /\?>/g, it = [">"];
828
- function lt(e) {
829
- const t = S(e.name) || "x", n = k(e.value, it, st);
864
+ const dt = /\?>/g, mt = [">"];
865
+ function gt(e) {
866
+ const t = L(e.name) || "x", n = v(e.value, mt, dt);
830
867
  return "<?" + t + (n ? " " + n : "") + "?>";
831
868
  }
832
- const ct = ["&", "<"];
869
+ const yt = ["&", "<"];
833
870
  function ne(e) {
834
- return k(e.value, ct);
871
+ return v(e.value, yt);
835
872
  }
836
- function at(e, t) {
873
+ function vt(e, t) {
837
874
  return t.options.allowDangerousXml ? e.value : ne(e);
838
875
  }
839
- const ut = {}.hasOwnProperty, _ = {
840
- cdata: Qe,
841
- comment: et,
842
- doctype: nt,
843
- element: rt,
844
- instruction: lt,
845
- raw: at,
876
+ const kt = {}.hasOwnProperty, P = {
877
+ cdata: ct,
878
+ comment: at,
879
+ doctype: pt,
880
+ element: ht,
881
+ instruction: gt,
882
+ raw: vt,
846
883
  root: re,
847
884
  text: ne
848
885
  };
@@ -850,9 +887,9 @@ function oe(e, t) {
850
887
  const n = e && e.type;
851
888
  if (!n)
852
889
  throw new Error("Expected node, not `" + e + "`");
853
- if (!ut.call(_, n))
890
+ if (!kt.call(P, n))
854
891
  throw new Error("Cannot compile unknown node `" + n + "`");
855
- const o = _[n];
892
+ const o = P[n];
856
893
  return o(e, t);
857
894
  }
858
895
  function re(e, t) {
@@ -863,7 +900,7 @@ function re(e, t) {
863
900
  r[o] = oe(n[o], t);
864
901
  return r.join("");
865
902
  }
866
- function pt(e, t) {
903
+ function bt(e, t) {
867
904
  const n = { options: {} };
868
905
  if (typeof n.options.quote == "string" && n.options.quote !== '"' && n.options.quote !== "'")
869
906
  throw new Error(
@@ -872,15 +909,15 @@ function pt(e, t) {
872
909
  const o = Array.isArray(e) ? { type: "root", children: e } : e;
873
910
  return oe(o, n);
874
911
  }
875
- function kt(e) {
876
- const t = R().use(j).use(B).use(Re), n = t.parse(e), o = t.runSync(n, { value: e });
877
- return pt(o.children);
912
+ function qt(e, t) {
913
+ const n = j().use(B).use(z).use(Ve, t), o = n.parse(e), r = n.runSync(o, { value: e });
914
+ return bt(r.children);
878
915
  }
879
916
  export {
880
- gt as DIRECTIVE_MAP,
881
- W as PROOF_SOLUTION_NAMES,
882
- kt as markdownToPretext,
883
- N as mdastToPtxast,
884
- yt as remarkMath,
885
- Re as remarkPretext
917
+ St as DIRECTIVE_MAP,
918
+ H as PROOF_SOLUTION_NAMES,
919
+ qt as markdownToPretext,
920
+ O as mdastToPtxast,
921
+ It as remarkMath,
922
+ Ve as remarkPretext
886
923
  };