@refinex-md/markdown 0.1.0-canary.0

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 ADDED
@@ -0,0 +1,944 @@
1
+ import { Fragment as C } from "prosemirror-model";
2
+ import I from "remark-frontmatter";
3
+ import P from "remark-gfm";
4
+ import D from "remark-math";
5
+ import Q from "remark-parse";
6
+ import ee from "remark-stringify";
7
+ import { unified as F } from "unified";
8
+ import { parse as te, stringify as re } from "yaml";
9
+ import { createRefinexSchema as z, createRefinexDoc as ae, validateDoc as ne, createBlockId as le } from "@refinex-md/model";
10
+ import { defaultSchema as it } from "@refinex-md/model";
11
+ const se = {
12
+ bullet: "-",
13
+ emphasis: "*",
14
+ strong: "*",
15
+ bulletOrdered: ".",
16
+ incrementListMarker: !0,
17
+ fences: !0,
18
+ listItemIndent: "one",
19
+ rule: "-",
20
+ ruleRepetition: 3,
21
+ setext: !1
22
+ };
23
+ function et(e, t = {}) {
24
+ return N(e, t).docNode;
25
+ }
26
+ function tt(e, t = {}) {
27
+ return x(e, t).markdown;
28
+ }
29
+ function rt(e, t = {}) {
30
+ return N(e, t);
31
+ }
32
+ function at(e, t = {}) {
33
+ return x(e, t);
34
+ }
35
+ function N(e, t = {}) {
36
+ const r = z({ extensions: t.schemaExtensions }), a = [], n = oe(ie(e), a), { frontmatter: l, children: s } = ue(n, t, a), { nodes: i, extensionNames: c } = pe(s, r, t, a), u = r.nodes.doc.create(null, i.length > 0 ? i : void 0), o = ae(u, {
37
+ schema: {
38
+ extensions: [
39
+ ...c,
40
+ ...(t.schemaExtensions ?? []).map((f) => ({ name: f.name, version: f.version }))
41
+ ]
42
+ },
43
+ metadata: {
44
+ source: t.source ?? "markdown-import",
45
+ ...de(l)
46
+ }
47
+ }), d = ne(o, {
48
+ mode: "repair",
49
+ extensions: t.schemaExtensions,
50
+ createId: le
51
+ });
52
+ d.ok || a.push(h("DOC_VALIDATION_FAILED", "error", "$.content", "Markdown produced an invalid RefinexDoc."));
53
+ for (const f of d.issues)
54
+ f.code !== "BLOCK_ID_MISSING" && f.severity !== "info" && a.push(h("ROUNDTRIP_LOSSY", f.severity, f.path, f.message));
55
+ const m = d.normalized ?? d.doc ?? o;
56
+ return {
57
+ doc: m,
58
+ docNode: r.nodeFromJSON(m.content),
59
+ issues: a,
60
+ frontmatter: l
61
+ };
62
+ }
63
+ function ie(e) {
64
+ return e.replace(
65
+ /(^|\n)([^\n$]*?[::])?\s*\$\$\s*([^\n$][^\n]*?)\s*\$\$(?=\n|$)/g,
66
+ (t, r, a, n) => `${r}${a ? `${a.trim()}
67
+
68
+ ` : ""}$$
69
+ ${n.trim()}
70
+ $$`
71
+ );
72
+ }
73
+ function x(e, t = {}) {
74
+ const r = z({ extensions: t.schemaExtensions }), a = [], n = ye(e, r), s = { type: "root", children: fe(n, t, a) }, i = ke(e, t, a);
75
+ return i && s.children.unshift({ type: "yaml", value: i }), { markdown: ce(s, t), issues: a };
76
+ }
77
+ function nt(e = {}) {
78
+ return {
79
+ parse(t, r = {}) {
80
+ return N(t, {
81
+ schemaExtensions: e.schemaExtensions,
82
+ markdownExtensions: e.markdownExtensions,
83
+ ...e.parse,
84
+ ...r
85
+ });
86
+ },
87
+ serialize(t, r = {}) {
88
+ return x(t, {
89
+ schemaExtensions: e.schemaExtensions,
90
+ markdownExtensions: e.markdownExtensions,
91
+ ...e.serialize,
92
+ ...r
93
+ });
94
+ }
95
+ };
96
+ }
97
+ function S() {
98
+ return {
99
+ name: "rich_blocks",
100
+ version: "1.0.0",
101
+ nodes: {
102
+ image_block: {
103
+ parse({ schema: e }, t) {
104
+ var a;
105
+ if (t.type !== "paragraph" || !e.nodes.image_block) return null;
106
+ const r = t.children.length === 1 && ((a = t.children[0]) == null ? void 0 : a.type) === "image" ? t.children[0] : null;
107
+ return r ? e.nodes.image_block.create({
108
+ src: r.url,
109
+ alt: r.alt ?? null,
110
+ title: r.title ?? null
111
+ }) : null;
112
+ },
113
+ serialize(e, t) {
114
+ return we(e, t);
115
+ },
116
+ fallback: "paragraph"
117
+ },
118
+ callout: {
119
+ parse(e, t) {
120
+ return Me(e, t);
121
+ },
122
+ serialize(e, t) {
123
+ return Te(e, t);
124
+ },
125
+ fallback: "paragraph"
126
+ },
127
+ task_list: {
128
+ parse(e, t) {
129
+ return Ae(e, t);
130
+ },
131
+ serialize(e, t) {
132
+ return Ee(e, t);
133
+ },
134
+ fallback: "paragraph"
135
+ },
136
+ toggle_list: {
137
+ parse() {
138
+ return null;
139
+ },
140
+ serialize(e, t) {
141
+ return $e(e, t);
142
+ },
143
+ fallback: "paragraph"
144
+ },
145
+ divider: {
146
+ parse({ schema: e }, t) {
147
+ return t.type === "thematicBreak" && e.nodes.divider ? e.nodes.divider.create({ variant: "solid" }) : null;
148
+ },
149
+ serialize(e, t) {
150
+ return t.attrs.variant === "solid" || !t.attrs.variant ? { type: "thematicBreak" } : v("divider", { variant: t.attrs.variant }, [{ type: "thematicBreak" }]);
151
+ },
152
+ fallback: "paragraph"
153
+ },
154
+ table: {
155
+ parse(e, t) {
156
+ return L(e, t);
157
+ },
158
+ serialize(e, t) {
159
+ return xe(e, t);
160
+ },
161
+ fallback: "code_block"
162
+ },
163
+ math_block: {
164
+ parse({ schema: e }, t) {
165
+ return e.nodes.math_block ? t.type === "math" ? e.nodes.math_block.create({ tex: t.value }) : t.type === "code" && t.lang === "math" ? e.nodes.math_block.create({ tex: t.value }) : null : null;
166
+ },
167
+ serialize(e, t) {
168
+ return t.attrs.numbered || t.attrs.label || t.attrs.align === "left" ? v("math_block", {
169
+ numbered: !!t.attrs.numbered,
170
+ label: t.attrs.label,
171
+ align: t.attrs.align
172
+ }, [{ type: "math", value: t.attrs.tex }]) : { type: "math", value: t.attrs.tex };
173
+ },
174
+ fallback: "code_block"
175
+ },
176
+ embed_block: {
177
+ parse({ schema: e }, t) {
178
+ if (t.type !== "paragraph" || !e.nodes.embed_block) return null;
179
+ const r = t.children[0];
180
+ return t.children.length !== 1 || (r == null ? void 0 : r.type) !== "link" ? null : e.nodes.embed_block.create({
181
+ url: r.url,
182
+ title: w(r.children),
183
+ embedType: "bookmark",
184
+ status: "unresolved"
185
+ });
186
+ },
187
+ serialize(e, t) {
188
+ const r = typeof t.attrs.title == "string" && t.attrs.title.length > 0 ? t.attrs.title : t.attrs.url;
189
+ return {
190
+ type: "paragraph",
191
+ children: [{
192
+ type: "link",
193
+ url: t.attrs.url,
194
+ title: null,
195
+ children: [{ type: "text", value: r }]
196
+ }]
197
+ };
198
+ },
199
+ fallback: "paragraph"
200
+ }
201
+ }
202
+ };
203
+ }
204
+ function oe(e, t) {
205
+ try {
206
+ return F().use(Q).use(I, ["yaml"]).use(P, { singleTilde: !1 }).use(D).parse(e);
207
+ } catch (r) {
208
+ return t.push(h("MARKDOWN_PARSE_FAILED", "error", "$", r instanceof Error ? r.message : "Markdown parse failed.")), { type: "root", children: [] };
209
+ }
210
+ }
211
+ function ce(e, t) {
212
+ const r = { ...se, ...t.format };
213
+ return String(F().use(P, { singleTilde: !1, tablePipeAlign: !1, tableCellPadding: !0 }).use(D).use(I, ["yaml"]).use(ee, r).stringify(e)).replace(/(<!-- refinex:block-id blk_[A-Za-z0-9_-]{8,36} -->)\n\n/g, `$1
214
+ `).replace(/^(---\n[\s\S]*?\n---)\n\n/, `$1
215
+ `);
216
+ }
217
+ function ue(e, t, r) {
218
+ const a = e.children[0];
219
+ if (!a || a.type !== "yaml") return { frontmatter: {}, children: e.children };
220
+ if (t.frontmatter === "drop") return { frontmatter: {}, children: e.children.slice(1) };
221
+ try {
222
+ const n = te(a.value);
223
+ return g(n) ? { frontmatter: n, children: e.children.slice(1) } : (r.push(h("FRONTMATTER_INVALID", "warn", "$.frontmatter", "YAML frontmatter must be an object.")), { frontmatter: { frontmatterRaw: a.value }, children: e.children.slice(1) });
224
+ } catch (n) {
225
+ return r.push(h("FRONTMATTER_INVALID", "warn", "$.frontmatter", n instanceof Error ? n.message : "Invalid YAML frontmatter.")), { frontmatter: { frontmatterRaw: a.value }, children: e.children.slice(1) };
226
+ }
227
+ }
228
+ function de(e) {
229
+ const t = {};
230
+ for (const r of ["title", "createdAt", "updatedAt", "createdBy", "updatedBy"])
231
+ typeof e[r] == "string" && (t[r] = e[r]);
232
+ return Object.keys(e).length > 0 && (t.custom = { frontmatter: e }), t;
233
+ }
234
+ function pe(e, t, r, a) {
235
+ const n = [], l = [];
236
+ let s;
237
+ for (let i = 0; i < e.length; i += 1) {
238
+ const c = e[i];
239
+ if (c.type === "html") {
240
+ const o = Se(c.value);
241
+ if (o) {
242
+ r.blockIdMode === "preserve-comments" && (s = o);
243
+ continue;
244
+ }
245
+ const d = Oe(c.value);
246
+ if (d) {
247
+ const m = [];
248
+ let f = i + 1;
249
+ for (; f < e.length; ) {
250
+ const y = e[f];
251
+ if (y.type === "html" && Le(y.value)) break;
252
+ m.push(y), f += 1;
253
+ }
254
+ if (f < e.length) {
255
+ n.push(...Re(d, m, t, r, a, `$.children[${String(i)}]`, s, l)), s = void 0, i = f;
256
+ continue;
257
+ }
258
+ }
259
+ }
260
+ const u = _(c, t, r, a, `$.children[${String(i)}]`, s, l);
261
+ s = void 0, n.push(...u);
262
+ }
263
+ return { nodes: n, extensionNames: l };
264
+ }
265
+ function _(e, t, r, a, n, l, s) {
266
+ const i = ve(e, t, r, a);
267
+ if (i)
268
+ return s.push(...i.extensionNames), Array.isArray(i.node) ? i.node : [i.node];
269
+ switch (e.type) {
270
+ case "paragraph":
271
+ return ze(e) ? (a.push(h("HTML_FALLBACK", "warn", n, "HTML block is preserved as html code block.")), [t.nodes.code_block.create({ language: "html", ...p(l) }, b(t, w(e.children)))]) : [t.nodes.paragraph.create(p(l), k(e.children, t, a, n))];
272
+ case "heading":
273
+ return [t.nodes.heading.create({ level: e.depth, ...p(l) }, k(e.children, t, a, n))];
274
+ case "blockquote": {
275
+ const c = e.children.flatMap((u, o) => _(u, t, r, a, `${n}.children[${String(o)}]`, void 0, s));
276
+ return [t.nodes.blockquote.create(p(l), c.length > 0 ? c : [t.nodes.paragraph.create(p())])];
277
+ }
278
+ case "code": {
279
+ const c = j(e.lang);
280
+ return [t.nodes.code_block.create({
281
+ language: c,
282
+ displayMode: W(),
283
+ ...p(l)
284
+ }, b(t, e.value))];
285
+ }
286
+ case "math":
287
+ return t.nodes.math_block ? [t.nodes.math_block.create({ tex: e.value, ...p(l) })] : [t.nodes.code_block.create({ language: "math", ...p(l) }, b(t, e.value))];
288
+ case "thematicBreak":
289
+ return [t.nodes.horizontal_rule.create(p(l))];
290
+ case "list":
291
+ return [U(e, t, r, a, n)];
292
+ case "table":
293
+ return a.push(h("GFM_TABLE_FALLBACK", "warn", n, "GFM table is preserved as markdown-table code block until table nodes exist.")), [t.nodes.code_block.create(
294
+ { language: "markdown-table", ...p(l), metadata: { markdown: { type: "table", table: e } } },
295
+ b(t, Fe(e))
296
+ )];
297
+ case "html":
298
+ return a.push(h("HTML_FALLBACK", "warn", n, "HTML block is preserved as html code block.")), [t.nodes.code_block.create({ language: "html", ...p(l) }, b(t, e.value))];
299
+ case "yaml":
300
+ return [];
301
+ default:
302
+ return be(e, t, r, a, n);
303
+ }
304
+ }
305
+ function U(e, t, r, a, n) {
306
+ if (!e.ordered && e.children.some((i) => typeof i.checked == "boolean") && t.nodes.task_list && t.nodes.task_item)
307
+ return t.nodes.task_list.create({ metadata: null }, e.children.map((i, c) => {
308
+ var o;
309
+ const u = i.children.flatMap((d, m) => _(d, t, r, a, `${n}.children[${String(c)}].children[${String(m)}]`, void 0, []));
310
+ return (u.length === 0 || ((o = u[0]) == null ? void 0 : o.type.name) !== "paragraph") && u.unshift(t.nodes.paragraph.create(p())), t.nodes.task_item.create(
311
+ { checked: i.checked === !0, ...p() },
312
+ u
313
+ );
314
+ }));
315
+ const s = e.children.map((i, c) => {
316
+ var o;
317
+ typeof i.checked == "boolean" && a.push(h("GFM_TASK_LIST_ITEM", "warn", `${n}.children[${String(c)}]`, "Task list item state is stored in list item metadata."));
318
+ const u = i.children.flatMap((d, m) => _(d, t, r, a, `${n}.children[${String(c)}].children[${String(m)}]`, void 0, []));
319
+ return (u.length === 0 || ((o = u[0]) == null ? void 0 : o.type.name) !== "paragraph") && u.unshift(t.nodes.paragraph.create(p())), t.nodes.list_item.create(
320
+ { ...p(), metadata: typeof i.checked == "boolean" ? { task: { checked: i.checked } } : null },
321
+ u
322
+ );
323
+ });
324
+ return e.ordered ? t.nodes.ordered_list.create({ order: e.start ?? 1, metadata: null }, s) : t.nodes.bullet_list.create({ metadata: null }, s);
325
+ }
326
+ function k(e, t, r, a, n = []) {
327
+ return e.flatMap((l, s) => he(l, t, r, `${a}.children[${String(s)}]`, n));
328
+ }
329
+ function he(e, t, r, a, n) {
330
+ switch (e.type) {
331
+ case "text":
332
+ return e.value.length > 0 ? [t.text(e.value, n)] : [];
333
+ case "strong":
334
+ return k(e.children, t, r, a, [...n, t.marks.bold.create()]);
335
+ case "emphasis":
336
+ return k(e.children, t, r, a, [...n, t.marks.italic.create()]);
337
+ case "delete":
338
+ return k(e.children, t, r, a, [...n, t.marks.strikethrough.create()]);
339
+ case "inlineCode":
340
+ return [t.text(e.value, [t.marks.code.create()])];
341
+ case "inlineMath":
342
+ return t.nodes.math_inline ? [t.nodes.math_inline.create({ tex: e.value })] : (r.push(h("MATH_PARSE_WARNING", "warn", a, "Inline math is preserved as plain text because math nodes are not enabled.")), [t.text(`$${e.value}$`, n)]);
343
+ case "link":
344
+ return k(e.children, t, r, a, [...n, t.marks.link.create({ href: e.url, title: e.title ?? null })]);
345
+ case "image":
346
+ return [t.nodes.image.create({ src: e.url, alt: e.alt ?? null, title: e.title ?? null, metadata: null, permissions: null })];
347
+ case "break":
348
+ return [t.nodes.hard_break.create()];
349
+ case "html":
350
+ return r.push(h("HTML_FALLBACK", "warn", a, "Inline HTML is preserved as text.")), [t.text(e.value, n)];
351
+ }
352
+ }
353
+ function fe(e, t, r) {
354
+ const a = [];
355
+ return e.forEach((n) => {
356
+ const l = O(n, t, r), s = Array.isArray(l) ? l : [l];
357
+ for (const i of s)
358
+ t.blockIds === "html-comments" && typeof n.attrs.blockId == "string" && a.push({ type: "html", value: `<!-- refinex:block-id ${n.attrs.blockId} -->` }), a.push(i);
359
+ }), a;
360
+ }
361
+ function O(e, t, r) {
362
+ var n, l, s, i, c;
363
+ const a = (n = t.markdownExtensions) == null ? void 0 : n.find((u) => {
364
+ var o;
365
+ return (o = u.nodes) == null ? void 0 : o[e.type.name];
366
+ });
367
+ if ((s = (l = a == null ? void 0 : a.nodes) == null ? void 0 : l[e.type.name]) != null && s.serialize)
368
+ try {
369
+ const u = (c = (i = a.nodes[e.type.name]).serialize) == null ? void 0 : c.call(i, {
370
+ node: e,
371
+ issues: r,
372
+ serializeInline: (o) => M(o, t, r)
373
+ }, e);
374
+ if (u) return u;
375
+ } catch (u) {
376
+ r.push(h("EXTENSION_CODEC_FAILED", "error", `$.${e.type.name}`, u instanceof Error ? u.message : "Extension codec failed."));
377
+ }
378
+ switch (e.type.name) {
379
+ case "paragraph":
380
+ return { type: "paragraph", children: $(M(e, t, r)) };
381
+ case "heading":
382
+ return { type: "heading", depth: We(e.attrs.level), children: $(M(e, t, r)) };
383
+ case "blockquote":
384
+ return { type: "blockquote", children: T(e, t, r) };
385
+ case "code_block":
386
+ return ge(e);
387
+ case "horizontal_rule":
388
+ return { type: "thematicBreak" };
389
+ case "bullet_list":
390
+ case "ordered_list":
391
+ return me(e, t, r);
392
+ default:
393
+ return _e(e, t, r);
394
+ }
395
+ }
396
+ function M(e, t, r) {
397
+ const a = [];
398
+ return e.forEach((n) => {
399
+ if (n.type.name === "hard_break") {
400
+ a.push({ type: "break" });
401
+ return;
402
+ }
403
+ if (n.type.name === "image") {
404
+ a.push({
405
+ type: "image",
406
+ url: n.attrs.src,
407
+ alt: n.attrs.alt ?? void 0,
408
+ title: n.attrs.title ?? null
409
+ });
410
+ return;
411
+ }
412
+ if (n.type.name === "math_inline") {
413
+ a.push({ type: "inlineMath", value: n.attrs.tex });
414
+ return;
415
+ }
416
+ if (!n.isText) return;
417
+ if (n.marks.find((i) => i.type.name === "code")) {
418
+ a.push({ type: "inlineCode", value: n.text ?? "" });
419
+ return;
420
+ }
421
+ let s = { type: "text", value: n.text ?? "" };
422
+ for (const i of [...n.marks].reverse())
423
+ switch (i.type.name) {
424
+ case "bold":
425
+ s = { type: "strong", children: [s] };
426
+ break;
427
+ case "italic":
428
+ s = { type: "emphasis", children: [s] };
429
+ break;
430
+ case "strikethrough":
431
+ s = { type: "delete", children: [s] };
432
+ break;
433
+ case "link":
434
+ s = {
435
+ type: "link",
436
+ url: i.attrs.href,
437
+ title: i.attrs.title ?? null,
438
+ children: [s]
439
+ };
440
+ break;
441
+ case "underline":
442
+ r.push(h("MARK_UNSUPPORTED", "warn", "$.marks.underline", "Underline has no portable Markdown representation and was dropped."));
443
+ break;
444
+ }
445
+ a.push(s);
446
+ }), a;
447
+ }
448
+ function me(e, t, r) {
449
+ const a = [];
450
+ return e.forEach((n) => {
451
+ const l = T(n, t, r), s = Ue(n);
452
+ a.push({
453
+ type: "listItem",
454
+ checked: s,
455
+ spread: !1,
456
+ children: l
457
+ });
458
+ }), {
459
+ type: "list",
460
+ ordered: e.type.name === "ordered_list",
461
+ start: e.type.name === "ordered_list" ? e.attrs.order : void 0,
462
+ spread: !1,
463
+ children: a
464
+ };
465
+ }
466
+ function j(e) {
467
+ const t = String(e ?? "").trim().toLowerCase();
468
+ return t.length > 0 ? t : null;
469
+ }
470
+ function W(e) {
471
+ return "source";
472
+ }
473
+ function ge(e) {
474
+ const t = je(e);
475
+ return (t == null ? void 0 : t.type) === "table" && t.table ? t.table : {
476
+ type: "code",
477
+ lang: e.attrs.language ?? void 0,
478
+ value: e.textContent
479
+ };
480
+ }
481
+ function T(e, t, r) {
482
+ const a = [];
483
+ return e.forEach((n) => {
484
+ const l = O(n, t, r);
485
+ a.push(...Array.isArray(l) ? l : [l]);
486
+ }), a;
487
+ }
488
+ function ke(e, t, r) {
489
+ var l;
490
+ if (t.frontmatter === "drop" || !V(e)) return;
491
+ const a = {
492
+ ...(l = e.metadata.custom) == null ? void 0 : l.frontmatter,
493
+ title: e.metadata.title,
494
+ createdAt: e.metadata.createdAt,
495
+ updatedAt: e.metadata.updatedAt,
496
+ createdBy: e.metadata.createdBy,
497
+ updatedBy: e.metadata.updatedBy
498
+ }, n = Object.fromEntries(Object.entries(a).filter(([, s]) => s !== void 0));
499
+ if (Object.keys(n).length !== 0)
500
+ try {
501
+ return re(n).trimEnd();
502
+ } catch (s) {
503
+ r.push(h("FRONTMATTER_UNSERIALIZABLE", "warn", "$.metadata", s instanceof Error ? s.message : "Frontmatter is not serializable."));
504
+ return;
505
+ }
506
+ }
507
+ function ye(e, t) {
508
+ return Ke(e) ? e : V(e) ? t.nodeFromJSON(e.content) : t.nodeFromJSON(e);
509
+ }
510
+ function be(e, t, r, a, n) {
511
+ return r.unknownNodePolicy === "error" ? (a.push(h("MDAST_NODE_UNSUPPORTED", "error", n, `Unsupported mdast node "${e.type}".`)), []) : (a.push(h("MDAST_NODE_UNSUPPORTED", "warn", n, `Unsupported mdast node "${e.type}" was dropped.`)), r.unknownNodePolicy === "warn-and-paragraph" ? [t.nodes.paragraph.create(p(), t.text(R(e)))] : []);
512
+ }
513
+ function _e(e, t, r) {
514
+ return t.unsupportedNodePolicy === "error" ? (r.push(h("PMAST_NODE_UNSUPPORTED", "error", `$.${e.type.name}`, `Unsupported ProseMirror node "${e.type.name}".`)), []) : (r.push(h("PMAST_NODE_UNSUPPORTED", "warn", `$.${e.type.name}`, `Unsupported ProseMirror node "${e.type.name}" was dropped.`)), t.unsupportedNodePolicy === "warn-and-code-fence" ? [{ type: "code", lang: "json", value: JSON.stringify(e.toJSON(), null, 2) }] : []);
515
+ }
516
+ function ve(e, t, r, a) {
517
+ for (const n of r.markdownExtensions ?? [])
518
+ for (const l of Object.values(n.nodes ?? {}))
519
+ if (l.parse)
520
+ try {
521
+ const s = l.parse({ schema: t, issues: a }, e);
522
+ if (s) {
523
+ const i = new Set((r.schemaExtensions ?? []).map((c) => c.name));
524
+ return {
525
+ node: s,
526
+ extensionNames: i.has(n.name) ? [{ name: n.name, version: n.version }] : []
527
+ };
528
+ }
529
+ } catch (s) {
530
+ a.push(h("EXTENSION_CODEC_FAILED", "error", `$.${e.type}`, s instanceof Error ? s.message : "Extension codec failed."));
531
+ }
532
+ }
533
+ function $(e) {
534
+ const t = [];
535
+ for (const r of e) {
536
+ const a = t[t.length - 1];
537
+ if ((a == null ? void 0 : a.type) === "text" && r.type === "text") {
538
+ a.value += r.value;
539
+ continue;
540
+ }
541
+ if ((a == null ? void 0 : a.type) === "strong" && r.type === "strong") {
542
+ a.children.push(...r.children);
543
+ continue;
544
+ }
545
+ if ((a == null ? void 0 : a.type) === "emphasis" && r.type === "emphasis") {
546
+ a.children.push(...r.children);
547
+ continue;
548
+ }
549
+ if ((a == null ? void 0 : a.type) === "delete" && r.type === "delete") {
550
+ a.children.push(...r.children);
551
+ continue;
552
+ }
553
+ t.push(r);
554
+ }
555
+ return t;
556
+ }
557
+ function we(e, t) {
558
+ const r = {
559
+ type: "image",
560
+ url: t.attrs.src,
561
+ alt: t.attrs.alt ?? void 0,
562
+ title: t.attrs.title ?? null
563
+ }, a = $(e.serializeInline(t)), n = a.length > 0, l = {
564
+ align: t.attrs.align,
565
+ widthPercent: t.attrs.widthPercent,
566
+ aspectRatio: t.attrs.aspectRatio,
567
+ status: t.attrs.status
568
+ }, s = (l.align === "center" || l.align == null) && (l.widthPercent === 100 || l.widthPercent == null) && !l.aspectRatio && (l.status === "ready" || l.status == null), i = { type: "paragraph", children: [r] };
569
+ return !n && s ? i : v("image_block", l, [
570
+ i,
571
+ ...n ? [{ type: "paragraph", children: a }] : []
572
+ ]);
573
+ }
574
+ function Me(e, t) {
575
+ var o;
576
+ if (t.type !== "blockquote" || !e.schema.nodes.callout) return null;
577
+ const r = t.children[0];
578
+ if ((r == null ? void 0 : r.type) !== "paragraph") return null;
579
+ const a = w(r.children).match(/^\[!(NOTE|INFO|TIP|IMPORTANT|WARNING|CAUTION|ERROR)\]\s*([\s\S]*)$/i);
580
+ if (!a) return null;
581
+ const n = K(a[1]), l = ((o = a[2]) == null ? void 0 : o.trim()) || "", [s, ...i] = l.split(/\n+/), c = s || null, u = [];
582
+ i.length > 0 ? u.push(e.schema.nodes.paragraph.create(p(), e.schema.text(i.join(`
583
+ `)))) : c && u.push(e.schema.nodes.paragraph.create(p(), e.schema.text(c)));
584
+ for (const d of t.children.slice(1))
585
+ u.push(...A(e, d));
586
+ return e.schema.nodes.callout.create(
587
+ { kind: n, title: c, ...p() },
588
+ u.length > 0 ? u : [e.schema.nodes.paragraph.create(p())]
589
+ );
590
+ }
591
+ function Te(e, t) {
592
+ const r = K(t.attrs.kind).toUpperCase(), a = typeof t.attrs.title == "string" && t.attrs.title.length > 0 ? ` ${t.attrs.title}` : "", n = T(t, { markdownExtensions: [S()] }, e.issues), l = n[0];
593
+ return !a && (l == null ? void 0 : l.type) === "paragraph" ? {
594
+ type: "blockquote",
595
+ children: [
596
+ {
597
+ type: "paragraph",
598
+ children: [
599
+ { type: "text", value: `[!${r}]
600
+ ` },
601
+ ...l.children
602
+ ]
603
+ },
604
+ ...n.slice(1)
605
+ ]
606
+ } : {
607
+ type: "blockquote",
608
+ children: [
609
+ { type: "paragraph", children: [{ type: "text", value: `[!${r}]${a}` }] },
610
+ ...n
611
+ ]
612
+ };
613
+ }
614
+ function K(e) {
615
+ const t = String(e ?? "note").toLowerCase();
616
+ return t === "tip" || t === "important" || t === "warning" || t === "caution" ? t : t === "error" ? "caution" : "note";
617
+ }
618
+ function Ae(e, t) {
619
+ return t.type !== "list" || t.ordered || !e.schema.nodes.task_list || !e.schema.nodes.task_item || !t.children.some((r) => typeof r.checked == "boolean") ? null : e.schema.nodes.task_list.create({ metadata: null }, t.children.map((r) => {
620
+ var n;
621
+ const a = r.children.flatMap((l) => A(e, l));
622
+ return (a.length === 0 || ((n = a[0]) == null ? void 0 : n.type.name) !== "paragraph") && a.unshift(e.schema.nodes.paragraph.create(p())), e.schema.nodes.task_item.create({ checked: r.checked === !0, ...p() }, a);
623
+ }));
624
+ }
625
+ function Ee(e, t) {
626
+ const r = [];
627
+ return t.forEach((a) => {
628
+ r.push({
629
+ type: "listItem",
630
+ checked: a.attrs.checked === !0,
631
+ spread: !1,
632
+ children: T(a, { markdownExtensions: [S()] }, e.issues)
633
+ });
634
+ }), { type: "list", ordered: !1, spread: !1, children: r };
635
+ }
636
+ function $e(e, t) {
637
+ const r = [], a = H(e, t, r);
638
+ return v("toggle_list", { items: r }, [a]);
639
+ }
640
+ function H(e, t, r) {
641
+ const a = [];
642
+ return t.forEach((n) => {
643
+ var i;
644
+ const l = { collapsed: n.attrs.collapsed === !0 };
645
+ r.push(l);
646
+ const s = [];
647
+ n.forEach((c) => {
648
+ if (c.type.name === "toggle_list") {
649
+ const d = [];
650
+ s.push(H(e, c, d)), l.children = d;
651
+ return;
652
+ }
653
+ const u = O(c, { markdownExtensions: [S()] }, e.issues), o = Array.isArray(u) ? u : [u];
654
+ s.push(...o);
655
+ }), (s.length === 0 || ((i = s[0]) == null ? void 0 : i.type) !== "paragraph") && s.unshift({ type: "paragraph", children: [] }), a.push({ type: "listItem", checked: null, spread: s.length > 1, children: s });
656
+ }), { type: "list", ordered: !1, spread: a.some((n) => n.spread), children: a };
657
+ }
658
+ function Ne(e, t, r) {
659
+ if (!e.schema.nodes.toggle_list || !e.schema.nodes.toggle_item) return null;
660
+ const a = t.find((l) => l.type === "list" && !l.ordered);
661
+ if (!a) return null;
662
+ const n = G(r.items);
663
+ return e.schema.nodes.toggle_list.create(
664
+ { metadata: null },
665
+ a.children.map((l, s) => q(e, l, n[s]))
666
+ );
667
+ }
668
+ function q(e, t, r) {
669
+ var l;
670
+ const a = [], n = (r == null ? void 0 : r.children) ?? [];
671
+ return t.children.forEach((s) => {
672
+ if (s.type === "list" && !s.ordered && n.length > 0) {
673
+ a.push(e.schema.nodes.toggle_list.create(
674
+ { metadata: null },
675
+ s.children.map((i, c) => q(e, i, n[c]))
676
+ ));
677
+ return;
678
+ }
679
+ a.push(...A(e, s));
680
+ }), (a.length === 0 || ((l = a[0]) == null ? void 0 : l.type.name) !== "paragraph") && a.unshift(e.schema.nodes.paragraph.create(p())), e.schema.nodes.toggle_item.create(
681
+ { collapsed: (r == null ? void 0 : r.collapsed) === !0, ...p() },
682
+ a
683
+ );
684
+ }
685
+ function G(e) {
686
+ return Array.isArray(e) ? e.map((t) => {
687
+ if (!g(t)) return { collapsed: !1 };
688
+ const r = G(t.children);
689
+ return {
690
+ collapsed: t.collapsed === !0,
691
+ ...r.length > 0 ? { children: r } : {}
692
+ };
693
+ }) : [];
694
+ }
695
+ function L(e, t) {
696
+ const r = e.schema;
697
+ if (t.type !== "table" || !r.nodes.table || !r.nodes.table_row || !r.nodes.table_cell || !r.nodes.table_header) return null;
698
+ const a = t.children.map((n, l) => r.nodes.table_row.create(null, n.children.map((s, i) => {
699
+ var o;
700
+ const c = l === 0 ? r.nodes.table_header : r.nodes.table_cell, u = ((o = t.align) == null ? void 0 : o[i]) ?? null;
701
+ return c.create({ align: u }, [
702
+ r.nodes.paragraph.create(p(), k(s.children, r, e.issues, "$.table"))
703
+ ]);
704
+ })));
705
+ return r.nodes.table.create({ ...p(), layout: "auto", columnWidths: null, caption: null }, a);
706
+ }
707
+ function xe(e, t) {
708
+ const r = [], a = [];
709
+ let n = !1;
710
+ t.forEach((s, i, c) => {
711
+ const u = [];
712
+ s.forEach((o, d, m) => {
713
+ var f;
714
+ (o.attrs.colspan !== 1 || o.attrs.rowspan !== 1 || o.childCount !== 1 || ((f = o.firstChild) == null ? void 0 : f.type.name) !== "paragraph") && (n = !0), Ce(t, c, o.type.name) || (n = !0), Be(o) && (n = !0), c === 0 && (a[m] = J(o.attrs.align)), u.push({ type: "tableCell", children: o.firstChild ? M(o.firstChild, {}, e.issues) : [] });
715
+ }), r.push({ type: "tableRow", children: u });
716
+ });
717
+ const l = { type: "table", align: a, children: r };
718
+ return n || t.attrs.caption || t.attrs.columnWidths ? (e.issues.push(h("TABLE_COMPLEX_MARKDOWN_FALLBACK", "warn", "$.table", "Complex table attributes require Refinex block wrapper.")), v("table", {
719
+ caption: t.attrs.caption,
720
+ columnWidths: t.attrs.columnWidths,
721
+ layout: t.attrs.layout,
722
+ cells: Ie(t)
723
+ }, [l])) : l;
724
+ }
725
+ function A(e, t) {
726
+ switch (t.type) {
727
+ case "paragraph":
728
+ return [e.schema.nodes.paragraph.create(p(), k(t.children, e.schema, e.issues, "$.rich"))];
729
+ case "heading":
730
+ return [e.schema.nodes.heading.create({ level: t.depth, ...p() }, k(t.children, e.schema, e.issues, "$.rich"))];
731
+ case "code": {
732
+ const r = j(t.lang);
733
+ return [e.schema.nodes.code_block.create({
734
+ language: r,
735
+ displayMode: W(),
736
+ ...p()
737
+ }, b(e.schema, t.value))];
738
+ }
739
+ case "math":
740
+ return e.schema.nodes.math_block ? [e.schema.nodes.math_block.create({ tex: t.value, ...p() })] : [];
741
+ case "list":
742
+ return [U(t, e.schema, {}, e.issues, "$.rich")];
743
+ case "thematicBreak":
744
+ return [e.schema.nodes.horizontal_rule.create(p())];
745
+ case "blockquote":
746
+ return [e.schema.nodes.blockquote.create(p(), t.children.flatMap((r) => A(e, r)))];
747
+ case "table": {
748
+ const r = L(e, t);
749
+ return r ? [r] : [];
750
+ }
751
+ case "html":
752
+ return [e.schema.nodes.code_block.create({ language: "html", ...p() }, b(e.schema, t.value))];
753
+ }
754
+ }
755
+ function v(e, t, r) {
756
+ const a = Object.fromEntries(Object.entries(t).filter(([, n]) => n != null));
757
+ return [
758
+ { type: "html", value: `<!-- refinex:block ${JSON.stringify({ type: e, attrs: a })} -->` },
759
+ ...r,
760
+ { type: "html", value: "<!-- /refinex:block -->" }
761
+ ];
762
+ }
763
+ function J(e) {
764
+ return e === "left" || e === "right" || e === "center" ? e : null;
765
+ }
766
+ function p(e) {
767
+ return {
768
+ blockId: e ?? null,
769
+ metadata: null,
770
+ permissions: null
771
+ };
772
+ }
773
+ function b(e, t) {
774
+ return t.length > 0 ? e.text(t) : void 0;
775
+ }
776
+ function Se(e) {
777
+ const t = /^<!--\s*refinex:block-id\s+(blk_[A-Za-z0-9_-]{8,36})\s*-->$/.exec(e.trim());
778
+ return t == null ? void 0 : t[1];
779
+ }
780
+ function Oe(e) {
781
+ const t = /^<!--\s*refinex:block\s+(\{[\s\S]*\})\s*-->$/.exec(e.trim());
782
+ if (t)
783
+ try {
784
+ const r = JSON.parse(t[1]);
785
+ return !g(r) || typeof r.type != "string" ? void 0 : {
786
+ type: r.type,
787
+ attrs: g(r.attrs) ? r.attrs : {}
788
+ };
789
+ } catch {
790
+ return;
791
+ }
792
+ }
793
+ function Le(e) {
794
+ return /^<!--\s*\/refinex:block\s*-->$/.test(e.trim());
795
+ }
796
+ function Re(e, t, r, a, n, l, s, i) {
797
+ var u;
798
+ const c = { ...e.attrs, ...p(s) };
799
+ switch (e.type) {
800
+ case "image_block": {
801
+ const o = t[0], d = (o == null ? void 0 : o.type) === "paragraph" && ((u = o.children[0]) == null ? void 0 : u.type) === "image" ? o.children[0] : void 0;
802
+ if (!d || !r.nodes.image_block) break;
803
+ const m = t.find((f, y) => y > 0 && f.type === "paragraph");
804
+ return [r.nodes.image_block.create({
805
+ ...c,
806
+ src: d.url,
807
+ alt: d.alt ?? null,
808
+ title: d.title ?? null
809
+ }, m ? k(m.children, r, n, l) : void 0)];
810
+ }
811
+ case "divider":
812
+ if (r.nodes.divider) return [r.nodes.divider.create(c)];
813
+ break;
814
+ case "math_block": {
815
+ const o = t.find((d) => d.type === "math" || d.type === "code" || d.type === "paragraph");
816
+ if (r.nodes.math_block && o)
817
+ return [r.nodes.math_block.create({
818
+ ...c,
819
+ tex: "value" in o ? o.value : R(o)
820
+ })];
821
+ break;
822
+ }
823
+ case "table": {
824
+ const o = t.find((d) => d.type === "table");
825
+ if (o) {
826
+ const d = L({ schema: r, issues: n }, o);
827
+ if (d) return [De(r, d, c)];
828
+ }
829
+ break;
830
+ }
831
+ case "toggle_list": {
832
+ const o = Ne({ schema: r, issues: n }, t, e.attrs);
833
+ if (o) return [o];
834
+ break;
835
+ }
836
+ case "embed_block":
837
+ if (r.nodes.embed_block) return [r.nodes.embed_block.create(c)];
838
+ break;
839
+ }
840
+ return n.push(h("RICH_BLOCK_ATTR_FALLBACK", "warn", l, `Refinex block "${e.type}" could not be restored and its content was imported normally.`)), t.flatMap((o, d) => _(o, r, a, n, `${l}.children[${String(d)}]`, void 0, i));
841
+ }
842
+ function Ce(e, t, r) {
843
+ if (t === 0) return r === "table_header";
844
+ if (r !== "table_cell" || e.childCount === 0) return !1;
845
+ for (let a = 0; a < e.child(0).childCount; a += 1)
846
+ if (e.child(0).child(a).type.name !== "table_header") return !1;
847
+ return !0;
848
+ }
849
+ function Be(e) {
850
+ return !!(e.attrs.background || e.attrs.colwidth || e.attrs.metadata || e.attrs.colspan !== void 0 && e.attrs.colspan !== 1 || e.attrs.rowspan !== void 0 && e.attrs.rowspan !== 1);
851
+ }
852
+ function Ie(e) {
853
+ const t = [];
854
+ return e.forEach((r) => {
855
+ const a = [];
856
+ r.forEach((n) => {
857
+ a.push({
858
+ type: n.type.name,
859
+ attrs: Pe(n)
860
+ });
861
+ }), t.push(a);
862
+ }), t;
863
+ }
864
+ function Pe(e) {
865
+ return Object.fromEntries(Object.entries({
866
+ align: J(e.attrs.align),
867
+ background: e.attrs.background ?? null,
868
+ colspan: e.attrs.colspan === 1 ? null : e.attrs.colspan,
869
+ rowspan: e.attrs.rowspan === 1 ? null : e.attrs.rowspan,
870
+ colwidth: e.attrs.colwidth ?? null,
871
+ metadata: e.attrs.metadata ?? null
872
+ }).filter(([, t]) => t != null));
873
+ }
874
+ function De(e, t, r) {
875
+ const a = Array.isArray(r.cells) ? r.cells : [], n = [];
876
+ t.forEach((i, c, u) => {
877
+ const o = [], d = Array.isArray(a[u]) ? a[u] : [];
878
+ i.forEach((m, f, y) => {
879
+ const E = g(d[y]) ? d[y] : {}, Y = E.type === "table_header" ? "table_header" : "table_cell", X = e.nodes[Y] ?? m.type, Z = {
880
+ ...m.attrs,
881
+ ...g(E.attrs) ? E.attrs : {}
882
+ };
883
+ o.push(X.create(Z, m.content, m.marks));
884
+ }), n.push(i.type.create(i.attrs, B(o), i.marks));
885
+ });
886
+ const { cells: l, ...s } = r;
887
+ return t.type.create({ ...t.attrs, ...s }, B(n), t.marks);
888
+ }
889
+ function B(e) {
890
+ return e.length > 0 ? C.fromArray([...e]) : C.empty;
891
+ }
892
+ function Fe(e) {
893
+ return e.children.map((r) => r.children.map((a) => w(a.children))).map((r) => r.join(" ")).join(`
894
+ `);
895
+ }
896
+ function ze(e) {
897
+ let t = !1;
898
+ return e.children.length > 0 && e.children.every((r) => r.type === "html" ? (t = !0, !0) : r.type === "text") && t;
899
+ }
900
+ function w(e) {
901
+ return e.map((t) => "value" in t ? t.value : "children" in t ? w(t.children) : t.type === "image" ? t.alt ?? "" : "").join("");
902
+ }
903
+ function R(e) {
904
+ return "value" in e ? e.value : "children" in e ? e.children.map(R).join("") : "";
905
+ }
906
+ function Ue(e) {
907
+ const t = e.attrs.metadata;
908
+ if (!g(t)) return;
909
+ const r = t.task;
910
+ if (!(!g(r) || typeof r.checked != "boolean"))
911
+ return r.checked;
912
+ }
913
+ function je(e) {
914
+ const t = e.attrs.metadata;
915
+ if (!(!g(t) || !g(t.markdown)))
916
+ return t.markdown;
917
+ }
918
+ function We(e) {
919
+ return typeof e == "number" && e >= 1 && e <= 6 ? e : 1;
920
+ }
921
+ function V(e) {
922
+ return g(e) && e.refinexDocVersion === "1.0.0" && g(e.content);
923
+ }
924
+ function Ke(e) {
925
+ return g(e) && typeof e.type == "object" && typeof e.toJSON == "function";
926
+ }
927
+ function g(e) {
928
+ return typeof e == "object" && e !== null && !Array.isArray(e);
929
+ }
930
+ function h(e, t, r, a) {
931
+ return { code: e, severity: t, path: r, message: a };
932
+ }
933
+ export {
934
+ nt as createMarkdownTransformer,
935
+ S as createRichBlockMarkdownExtension,
936
+ it as defaultSchema,
937
+ at as docToMarkdown,
938
+ rt as markdownToDoc,
939
+ et as parseMarkdown,
940
+ N as parseMarkdownDocument,
941
+ tt as serializeMarkdown,
942
+ x as serializeMarkdownDocument
943
+ };
944
+ //# sourceMappingURL=index.js.map