markloom 0.6.0 → 0.7.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/collab.md +25 -13
- package/dist/ai/index.cjs +5 -4
- package/dist/ai/index.d.cts +5 -5
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +4 -3
- package/dist/{chunk-KYZJIOOL.js → chunk-4Q7PAR2T.js} +3671 -904
- package/dist/chunk-75EJ4ARB.cjs +642 -0
- package/dist/{chunk-U3JZDFL4.cjs → chunk-BMPCVT3B.cjs} +2 -34
- package/dist/{chunk-VSBIMMI7.js → chunk-EFYT2223.js} +3 -35
- package/dist/chunk-HXABDVLZ.cjs +297 -0
- package/dist/{chunk-I5VK4U7J.cjs → chunk-I2LDEVLA.cjs} +434 -80
- package/dist/{chunk-YBAT343I.js → chunk-MQLRZQZL.js} +263 -119
- package/dist/chunk-OVHASRID.cjs +4279 -0
- package/dist/chunk-PCSVBPOR.cjs +40 -0
- package/dist/{chunk-TB2OY3D4.js → chunk-PSAXWBNM.js} +413 -59
- package/dist/{chunk-QEVME6ZU.cjs → chunk-PZRWTLLB.cjs} +260 -161
- package/dist/chunk-QO7JEZDB.js +4279 -0
- package/dist/{chunk-EJSFZHGT.js → chunk-WSJUXPAG.js} +18 -2
- package/dist/{chunk-ACFYBYZJ.cjs → chunk-Y4IXQFWL.cjs} +4212 -1445
- package/dist/{chunk-CGYCJORO.js → chunk-YLVSMDIO.js} +220 -121
- package/dist/chunk-ZL6WT6OY.js +297 -0
- package/dist/{client-CLYq17SW.d.ts → client-BvvyHjhh.d.ts} +57 -7
- package/dist/{client-DAth8pPq.d.cts → client-Ciy7Fa_k.d.cts} +57 -7
- package/dist/collab/index.cjs +5 -4
- package/dist/collab/index.d.cts +13 -15
- package/dist/collab/index.d.ts +13 -15
- package/dist/collab/index.js +4 -3
- package/dist/{controller-BIOO_XKJ.d.cts → controller-CHznEhAR.d.cts} +186 -27
- package/dist/{controller-6AJ36G52.d.ts → controller-DLqI8_jm.d.ts} +186 -27
- package/dist/index.cjs +69 -7
- package/dist/index.d.cts +127 -8
- package/dist/index.d.ts +127 -8
- package/dist/index.js +80 -18
- package/dist/io/index.cjs +58 -59
- package/dist/io/index.d.cts +1 -1
- package/dist/io/index.d.ts +1 -1
- package/dist/io/index.js +15 -16
- package/dist/react/index.cjs +9 -8
- package/dist/react/index.d.cts +5 -5
- package/dist/react/index.d.ts +5 -5
- package/dist/react/index.js +8 -7
- package/dist/{rebase-batch-DlerAcFN.d.ts → rebase-batch-CSnv8FKp.d.ts} +1 -1
- package/dist/{rebase-batch-PjENQ742.d.cts → rebase-batch-DO1ygkx9.d.cts} +1 -1
- package/dist/{types-COUweEav.d.cts → types-BkHlmUFL.d.cts} +5 -1
- package/dist/{types-COUweEav.d.ts → types-BkHlmUFL.d.ts} +5 -1
- package/dist/{versions-9mG1epfB.d.cts → versions-DNFf8zVz.d.cts} +1 -1
- package/dist/{versions-Bubyo4pt.d.ts → versions-DOkOyh2J.d.ts} +1 -1
- package/dist/view/index.cjs +215 -17
- package/dist/view/index.d.cts +82 -9
- package/dist/view/index.d.ts +82 -9
- package/dist/view/index.js +212 -14
- package/dist/view/theme.css +349 -75
- package/dist/{suggest-Bk4zLzuv.d.ts → word-count-BPw9G2Bg.d.ts} +15 -5
- package/dist/{suggest-BkGGiPKA.d.cts → word-count-DNLn1h_t.d.cts} +15 -5
- package/package.json +11 -6
- package/dist/chunk-EADZKEU4.cjs +0 -498
- package/dist/chunk-SNFF6X45.js +0 -2347
- package/dist/chunk-TWD5T4YC.cjs +0 -2347
- package/dist/chunk-ZKQSQSSM.cjs +0 -24
|
@@ -0,0 +1,4279 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkHXABDVLZcjs = require('./chunk-HXABDVLZ.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _chunkPCSVBPORcjs = require('./chunk-PCSVBPOR.cjs');
|
|
14
|
+
|
|
15
|
+
// src/tree/schema.ts
|
|
16
|
+
var META = {
|
|
17
|
+
maxNodeTypes: 16,
|
|
18
|
+
// M1
|
|
19
|
+
maxDepth: 5,
|
|
20
|
+
// M2:容器嵌套上界。doc>quote>list>item 四层容器,item 里的块(code/table/…地板)是第五层,到顶;
|
|
21
|
+
// 持有文本的 inline 块(paragraph/heading)是「地板」不算层。
|
|
22
|
+
maxContentTypes: 8
|
|
23
|
+
// M3:单条产生式右部种类上界
|
|
24
|
+
};
|
|
25
|
+
function checkSchema(schema) {
|
|
26
|
+
const types = Object.keys(schema.nodes);
|
|
27
|
+
if (types.length > META.maxNodeTypes)
|
|
28
|
+
throw new Error(`[M1] node type \u603B\u6570 ${types.length} > ${META.maxNodeTypes}:type \u5F53 attr \u7528\u4E86`);
|
|
29
|
+
for (const [type, spec] of Object.entries(schema.nodes)) {
|
|
30
|
+
const childTypes = spec.content.filter((c) => c !== "text");
|
|
31
|
+
if (type !== "doc" && childTypes.length > META.maxContentTypes)
|
|
32
|
+
throw new Error(`[M3] ${type} \u5141\u8BB8 ${childTypes.length} \u79CD children > ${META.maxContentTypes}:\u4E07\u80FD\u5BB9\u5668`);
|
|
33
|
+
for (const c of childTypes) {
|
|
34
|
+
if (!schema.nodes[c])
|
|
35
|
+
throw new Error(`${type} \u7684 content \u5F15\u7528\u4E86\u672A\u5B9A\u4E49 type: ${c}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const depth = maxReachableDepth(schema);
|
|
39
|
+
if (depth > META.maxDepth)
|
|
40
|
+
throw new Error(`[M2] \u6587\u6CD5\u53EF\u8FBE\u5D4C\u5957\u6DF1\u5EA6 ${depth} > ${META.maxDepth}:\u75C5\u6001\u5D4C\u5957`);
|
|
41
|
+
}
|
|
42
|
+
function maxReachableDepth(schema) {
|
|
43
|
+
function walk(type, seen) {
|
|
44
|
+
const spec = schema.nodes[type];
|
|
45
|
+
if (!spec || spec.atom)
|
|
46
|
+
return 1;
|
|
47
|
+
if (spec.inline)
|
|
48
|
+
return 0;
|
|
49
|
+
let max = 0;
|
|
50
|
+
for (const c of spec.content) {
|
|
51
|
+
if (c === "text" || seen.has(c))
|
|
52
|
+
continue;
|
|
53
|
+
max = Math.max(max, walk(c, new Set(seen).add(c)));
|
|
54
|
+
}
|
|
55
|
+
return 1 + max;
|
|
56
|
+
}
|
|
57
|
+
return walk("doc", /* @__PURE__ */ new Set(["doc"]));
|
|
58
|
+
}
|
|
59
|
+
function validate(schema, doc) {
|
|
60
|
+
function check(node, typePath) {
|
|
61
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
62
|
+
return true;
|
|
63
|
+
const spec = schema.nodes[node.type];
|
|
64
|
+
if (!spec)
|
|
65
|
+
return `${typePath}: \u672A\u77E5 type "${node.type}"`;
|
|
66
|
+
if (spec.atom)
|
|
67
|
+
return true;
|
|
68
|
+
for (const child of node.children) {
|
|
69
|
+
const childType = _chunkPCSVBPORcjs.isText.call(void 0, child) ? "text" : child.type;
|
|
70
|
+
if (!spec.content.includes(childType))
|
|
71
|
+
return `${typePath}/${node.type}: \u4E0D\u5141\u8BB8 child "${childType}"`;
|
|
72
|
+
const r = check(child, `${typePath}/${node.type}`);
|
|
73
|
+
if (r !== true)
|
|
74
|
+
return r;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
return check(doc, "");
|
|
79
|
+
}
|
|
80
|
+
function validateAll(schema, doc) {
|
|
81
|
+
const out = [];
|
|
82
|
+
function check(node, path) {
|
|
83
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
84
|
+
return;
|
|
85
|
+
const spec = schema.nodes[node.type];
|
|
86
|
+
if (!spec) {
|
|
87
|
+
out.push({ path, reason: `\u672A\u77E5 type "${node.type}"` });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (spec.atom)
|
|
91
|
+
return;
|
|
92
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
93
|
+
const child = node.children[i];
|
|
94
|
+
const childType = _chunkPCSVBPORcjs.isText.call(void 0, child) ? "text" : child.type;
|
|
95
|
+
if (!spec.content.includes(childType))
|
|
96
|
+
out.push({ path: path.concat(i), reason: `${node.type} \u4E0D\u5141\u8BB8 child "${childType}"` });
|
|
97
|
+
check(child, path.concat(i));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
check(doc, []);
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
var CONTAINER_BLOCKS = ["paragraph", "heading", "list", "code", "table", "image", "raw", "quote"];
|
|
104
|
+
var basicSchema = {
|
|
105
|
+
marks: ["bold", "italic", "code", "strike", "link", "underline", "highlight", "superscript", "subscript", "textStyle", "comment"],
|
|
106
|
+
nodes: {
|
|
107
|
+
// 折叠内建 heading.attrs.collapsed(H1–H6 折到下一同级/上级标题),无独立 toggle 类型。
|
|
108
|
+
// math 行内 atom;embed 块级嵌入(youtube/twitch/audio/file 共用 kind attr)。
|
|
109
|
+
doc: { content: ["paragraph", "heading", "quote", "list", "code", "table", "image", "embed", "raw"] },
|
|
110
|
+
paragraph: { content: ["text", "mention", "hard_break", "math", "footnote_ref"], inline: true },
|
|
111
|
+
heading: { content: ["text", "mention", "hard_break", "math", "footnote_ref"], inline: true },
|
|
112
|
+
// attrs.depth + collapsed
|
|
113
|
+
code: { content: ["text"], inline: true, marks: [] },
|
|
114
|
+
// code 不收 mention/br/math:代码正文纯文本(\n 在叶内)
|
|
115
|
+
quote: { content: CONTAINER_BLOCKS },
|
|
116
|
+
// attrs.variant → 高亮块(isCallout)
|
|
117
|
+
list: { content: ["list_item"] },
|
|
118
|
+
list_item: { content: CONTAINER_BLOCKS },
|
|
119
|
+
table: { content: [], atom: true },
|
|
120
|
+
// 异拓扑黑盒(M4):内部二维模型自管
|
|
121
|
+
image: { content: [], atom: true },
|
|
122
|
+
// 块级 atom:无 children,url/alt/w/h 全在 attrs
|
|
123
|
+
embed: { content: [], atom: true },
|
|
124
|
+
// 块级 atom:youtube/twitch/audio/file,kind+src 在 attrs
|
|
125
|
+
mention: { content: [], atom: true, inline: true },
|
|
126
|
+
// 行内 atom:@提及,占 1 字符位不可下钻,id/label 在 attrs
|
|
127
|
+
hard_break: { content: [], atom: true, inline: true },
|
|
128
|
+
// 行内 atom:段内软换行 <br>,Shift+Enter;无 attrs
|
|
129
|
+
footnote_ref: { content: [], atom: true, inline: true },
|
|
130
|
+
// 行内 atom:脚注引用 [^x],label 在 attrs;定义 = quote + attrs.footnote(不占 type)
|
|
131
|
+
math: { content: [], atom: true, inline: true },
|
|
132
|
+
// 行内 atom:公式,latex + display(inline|block)在 attrs;块级公式=段内 display:block
|
|
133
|
+
raw: { content: ["text"], inline: true, marks: [] }
|
|
134
|
+
// mdToDoc 透传块(thematicBreak/html);源文本存单文本叶,attrs.mdType 定渲染
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/tree/content-space.ts
|
|
139
|
+
var CELL_EXCLUDED_FROM_BODY = ["heading", "table"];
|
|
140
|
+
function bodyBlockTypes(schema = basicSchema) {
|
|
141
|
+
const c = _optionalChain([schema, 'access', _2 => _2.nodes, 'access', _3 => _3.doc, 'optionalAccess', _4 => _4.content]);
|
|
142
|
+
if (!_optionalChain([c, 'optionalAccess', _5 => _5.length]))
|
|
143
|
+
throw new Error("schema.nodes.doc.content empty");
|
|
144
|
+
return c;
|
|
145
|
+
}
|
|
146
|
+
function cellBlockTypes(schema = basicSchema) {
|
|
147
|
+
const ban = new Set(CELL_EXCLUDED_FROM_BODY);
|
|
148
|
+
return bodyBlockTypes(schema).filter((t) => !ban.has(t));
|
|
149
|
+
}
|
|
150
|
+
function isBodyBlockType(type, schema = basicSchema) {
|
|
151
|
+
return bodyBlockTypes(schema).includes(type);
|
|
152
|
+
}
|
|
153
|
+
function isCellBlockTypeInSpace(type, schema = basicSchema) {
|
|
154
|
+
return cellBlockTypes(schema).includes(type);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/atom/table.ts
|
|
158
|
+
function tableShape(table) {
|
|
159
|
+
return { rows: Number(_nullishCoalesce(_optionalChain([table, 'access', _6 => _6.attrs, 'optionalAccess', _7 => _7.rows]), () => ( 0))), cols: Number(_nullishCoalesce(_optionalChain([table, 'access', _8 => _8.attrs, 'optionalAccess', _9 => _9.cols]), () => ( 0))) };
|
|
160
|
+
}
|
|
161
|
+
function colspanOf(cell) {
|
|
162
|
+
return _chunkPCSVBPORcjs.isText.call(void 0, cell) ? 1 : Math.max(1, Number(_nullishCoalesce(_optionalChain([cell, 'access', _10 => _10.attrs, 'optionalAccess', _11 => _11.colspan]), () => ( 1))));
|
|
163
|
+
}
|
|
164
|
+
function rowspanOf(cell) {
|
|
165
|
+
return _chunkPCSVBPORcjs.isText.call(void 0, cell) ? 1 : Math.max(1, Number(_nullishCoalesce(_optionalChain([cell, 'access', _12 => _12.attrs, 'optionalAccess', _13 => _13.rowspan]), () => ( 1))));
|
|
166
|
+
}
|
|
167
|
+
function buildGrid(table) {
|
|
168
|
+
const { rows, cols } = tableShape(table);
|
|
169
|
+
const owner = new Array(rows * cols).fill(-1);
|
|
170
|
+
const meta = /* @__PURE__ */ new Map();
|
|
171
|
+
const anchors = [];
|
|
172
|
+
let ci = 0;
|
|
173
|
+
for (let r = 0; r < rows; r++) {
|
|
174
|
+
for (let c = 0; c < cols; c++) {
|
|
175
|
+
if (owner[r * cols + c] !== -1)
|
|
176
|
+
continue;
|
|
177
|
+
const cell = table.children[ci];
|
|
178
|
+
if (cell === void 0)
|
|
179
|
+
continue;
|
|
180
|
+
const cs = Math.min(colspanOf(cell), cols - c);
|
|
181
|
+
const rs = Math.min(rowspanOf(cell), rows - r);
|
|
182
|
+
for (let dr = 0; dr < rs; dr++)
|
|
183
|
+
for (let dc = 0; dc < cs; dc++)
|
|
184
|
+
owner[(r + dr) * cols + (c + dc)] = ci;
|
|
185
|
+
meta.set(ci, { r, c, colspan: cs, rowspan: rs });
|
|
186
|
+
anchors.push(ci);
|
|
187
|
+
ci++;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
rows,
|
|
192
|
+
cols,
|
|
193
|
+
at: (r, c) => r >= 0 && r < rows && c >= 0 && c < cols ? owner[r * cols + c] : -1,
|
|
194
|
+
cellOf: (idx) => _nullishCoalesce(meta.get(idx), () => ( { r: 0, c: 0, colspan: 1, rowspan: 1 })),
|
|
195
|
+
anchors
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function cellCaret(tablePath, table, r, c, off = 0) {
|
|
199
|
+
const idx = buildGrid(table).at(r, c);
|
|
200
|
+
return [...tablePath, idx < 0 ? 0 : idx, 0, 0, off];
|
|
201
|
+
}
|
|
202
|
+
function caretCell(tablePath, table, caret) {
|
|
203
|
+
if (caret.length <= tablePath.length)
|
|
204
|
+
return null;
|
|
205
|
+
const i = caret[tablePath.length];
|
|
206
|
+
if (i < 0 || i >= table.children.length)
|
|
207
|
+
return null;
|
|
208
|
+
const { r, c } = buildGrid(table).cellOf(i);
|
|
209
|
+
return { r, c };
|
|
210
|
+
}
|
|
211
|
+
function emptyCell() {
|
|
212
|
+
return { type: "table_cell", children: [{ type: "paragraph", children: [{ text: "" }] }] };
|
|
213
|
+
}
|
|
214
|
+
var DEFAULT_COL_WIDTH = 120;
|
|
215
|
+
function makeTable(id, rows, cols) {
|
|
216
|
+
const children = [];
|
|
217
|
+
for (let i = 0; i < rows * cols; i++)
|
|
218
|
+
children.push(emptyCell());
|
|
219
|
+
const colWidths = Array.from({ length: cols }, () => DEFAULT_COL_WIDTH);
|
|
220
|
+
return { type: "table", id, attrs: { rows, cols, colWidths }, children };
|
|
221
|
+
}
|
|
222
|
+
function cellText(table, r, c) {
|
|
223
|
+
const idx = buildGrid(table).at(r, c);
|
|
224
|
+
const cell = idx < 0 ? void 0 : table.children[idx];
|
|
225
|
+
if (!cell || _chunkPCSVBPORcjs.isText.call(void 0, cell) || _chunkPCSVBPORcjs.isText.call(void 0, cell.children[0]))
|
|
226
|
+
return "";
|
|
227
|
+
const leaf = cell.children[0].children[0];
|
|
228
|
+
return leaf && _chunkPCSVBPORcjs.isText.call(void 0, leaf) ? leaf.text : "";
|
|
229
|
+
}
|
|
230
|
+
function nextCell(table, r, c, dir) {
|
|
231
|
+
const grid = buildGrid(table);
|
|
232
|
+
const n = table.children.length;
|
|
233
|
+
if (n === 0)
|
|
234
|
+
return { r, c };
|
|
235
|
+
const cur = grid.at(r, c);
|
|
236
|
+
const i = ((cur < 0 ? 0 : cur) + dir + n) % n;
|
|
237
|
+
const m = grid.cellOf(i);
|
|
238
|
+
return { r: m.r, c: m.c };
|
|
239
|
+
}
|
|
240
|
+
function isLastCell(table, r, c) {
|
|
241
|
+
const n = table.children.length;
|
|
242
|
+
return n > 0 && buildGrid(table).at(r, c) === n - 1;
|
|
243
|
+
}
|
|
244
|
+
function arrowCell(table, r, c, dir) {
|
|
245
|
+
const grid = buildGrid(table);
|
|
246
|
+
const idx = grid.at(r, c);
|
|
247
|
+
const cur = idx < 0 ? { r, c, colspan: 1, rowspan: 1 } : grid.cellOf(idx);
|
|
248
|
+
let tr = cur.r;
|
|
249
|
+
let tc = cur.c;
|
|
250
|
+
if (dir === "up")
|
|
251
|
+
tr = cur.r - 1;
|
|
252
|
+
else if (dir === "down")
|
|
253
|
+
tr = cur.r + cur.rowspan;
|
|
254
|
+
else if (dir === "left")
|
|
255
|
+
tc = cur.c - 1;
|
|
256
|
+
else tc = cur.c + cur.colspan;
|
|
257
|
+
const target = grid.at(tr, tc);
|
|
258
|
+
if (target < 0)
|
|
259
|
+
return null;
|
|
260
|
+
const a = grid.cellOf(target);
|
|
261
|
+
return { r: a.r, c: a.c };
|
|
262
|
+
}
|
|
263
|
+
function withSpan(cell, cs, rs) {
|
|
264
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, cell))
|
|
265
|
+
return cell;
|
|
266
|
+
const attrs = { ...cell.attrs };
|
|
267
|
+
if (cs > 1)
|
|
268
|
+
attrs.colspan = cs;
|
|
269
|
+
else delete attrs.colspan;
|
|
270
|
+
if (rs > 1)
|
|
271
|
+
attrs.rowspan = rs;
|
|
272
|
+
else delete attrs.rowspan;
|
|
273
|
+
return { ...cell, attrs };
|
|
274
|
+
}
|
|
275
|
+
function insertRow(table, tablePath, at) {
|
|
276
|
+
const { rows, cols } = tableShape(table);
|
|
277
|
+
const grid = buildGrid(table);
|
|
278
|
+
const children = table.children.map((c) => c);
|
|
279
|
+
const fresh = [];
|
|
280
|
+
for (let c = 0; c < cols; ) {
|
|
281
|
+
const idxAbove = at > 0 ? grid.at(at - 1, c) : -1;
|
|
282
|
+
const idxAt = at < rows ? grid.at(at, c) : -1;
|
|
283
|
+
if (idxAbove >= 0 && idxAbove === idxAt) {
|
|
284
|
+
const m = grid.cellOf(idxAbove);
|
|
285
|
+
children[idxAbove] = withSpan(children[idxAbove], m.colspan, m.rowspan + 1);
|
|
286
|
+
c += m.colspan;
|
|
287
|
+
} else {
|
|
288
|
+
const ref = idxAt >= 0 ? grid.cellOf(idxAt) : at > 0 && idxAbove >= 0 ? grid.cellOf(idxAbove) : null;
|
|
289
|
+
const w = ref ? ref.colspan : 1;
|
|
290
|
+
fresh.push(withSpan(emptyCell(), w, 1));
|
|
291
|
+
c += w;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const insAt = at < rows ? firstFlatAtRow(grid, table.children.length, at) : table.children.length;
|
|
295
|
+
children.splice(insAt, 0, ...fresh);
|
|
296
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs: { ...table.attrs, rows: rows + 1, cols }, children } };
|
|
297
|
+
}
|
|
298
|
+
function firstFlatAtRow(grid, n, r) {
|
|
299
|
+
for (let i = 0; i < n; i++) {
|
|
300
|
+
if (grid.cellOf(i).r === r)
|
|
301
|
+
return i;
|
|
302
|
+
}
|
|
303
|
+
return n;
|
|
304
|
+
}
|
|
305
|
+
function deleteRow(table, tablePath, r, doc) {
|
|
306
|
+
const { rows, cols } = tableShape(table);
|
|
307
|
+
if (rows <= 1)
|
|
308
|
+
return deleteTableBlock(tablePath, doc);
|
|
309
|
+
const grid = buildGrid(table);
|
|
310
|
+
const remove = /* @__PURE__ */ new Set();
|
|
311
|
+
const children = table.children.map((c) => c);
|
|
312
|
+
for (let c = 0; c < cols; ) {
|
|
313
|
+
const idx = grid.at(r, c);
|
|
314
|
+
if (idx < 0) {
|
|
315
|
+
c++;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const m = grid.cellOf(idx);
|
|
319
|
+
if (m.rowspan === 1) {
|
|
320
|
+
remove.add(idx);
|
|
321
|
+
} else if (m.r === r) {
|
|
322
|
+
children[idx] = withSpan(children[idx], m.colspan, m.rowspan - 1);
|
|
323
|
+
} else {
|
|
324
|
+
children[idx] = withSpan(children[idx], m.colspan, m.rowspan - 1);
|
|
325
|
+
}
|
|
326
|
+
c += m.colspan;
|
|
327
|
+
}
|
|
328
|
+
const kept = children.filter((_, i) => !remove.has(i));
|
|
329
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs: { ...table.attrs, rows: rows - 1, cols }, children: kept } };
|
|
330
|
+
}
|
|
331
|
+
function insertCol(table, tablePath, at) {
|
|
332
|
+
const { rows, cols } = tableShape(table);
|
|
333
|
+
const grid = buildGrid(table);
|
|
334
|
+
const children = table.children.map((c) => c);
|
|
335
|
+
const inserts = [];
|
|
336
|
+
for (let r = 0; r < rows; ) {
|
|
337
|
+
const idxLeft = at > 0 ? grid.at(r, at - 1) : -1;
|
|
338
|
+
const idxAt = at < cols ? grid.at(r, at) : -1;
|
|
339
|
+
if (idxLeft >= 0 && idxLeft === idxAt) {
|
|
340
|
+
const m = grid.cellOf(idxLeft);
|
|
341
|
+
children[idxLeft] = withSpan(children[idxLeft], m.colspan + 1, m.rowspan);
|
|
342
|
+
r += m.rowspan;
|
|
343
|
+
} else {
|
|
344
|
+
const ref = idxAt >= 0 ? grid.cellOf(idxAt) : idxLeft >= 0 ? grid.cellOf(idxLeft) : null;
|
|
345
|
+
const h = ref ? ref.rowspan : 1;
|
|
346
|
+
const anchorAt = idxAt >= 0 ? idxAt : nextFlatAfterRow(grid, table.children.length, r);
|
|
347
|
+
inserts.push({ flatIdx: anchorAt, cell: withSpan(emptyCell(), 1, h) });
|
|
348
|
+
r += h;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
inserts.sort((a, b) => b.flatIdx - a.flatIdx);
|
|
352
|
+
for (const ins of inserts)
|
|
353
|
+
children.splice(ins.flatIdx, 0, ins.cell);
|
|
354
|
+
const attrs = withColOp(table, cols + 1, { kind: "insert", at });
|
|
355
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs, children } };
|
|
356
|
+
}
|
|
357
|
+
function nextFlatAfterRow(grid, n, r) {
|
|
358
|
+
for (let i = 0; i < n; i++) {
|
|
359
|
+
if (grid.cellOf(i).r > r)
|
|
360
|
+
return i;
|
|
361
|
+
}
|
|
362
|
+
return n;
|
|
363
|
+
}
|
|
364
|
+
function deleteCol(table, tablePath, c, doc) {
|
|
365
|
+
const { rows, cols } = tableShape(table);
|
|
366
|
+
if (cols <= 1)
|
|
367
|
+
return deleteTableBlock(tablePath, doc);
|
|
368
|
+
const grid = buildGrid(table);
|
|
369
|
+
const remove = /* @__PURE__ */ new Set();
|
|
370
|
+
const children = table.children.map((cc) => cc);
|
|
371
|
+
for (let r = 0; r < rows; ) {
|
|
372
|
+
const idx = grid.at(r, c);
|
|
373
|
+
if (idx < 0) {
|
|
374
|
+
r++;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
const m = grid.cellOf(idx);
|
|
378
|
+
if (m.colspan === 1)
|
|
379
|
+
remove.add(idx);
|
|
380
|
+
else children[idx] = withSpan(children[idx], m.colspan - 1, m.rowspan);
|
|
381
|
+
r += m.rowspan;
|
|
382
|
+
}
|
|
383
|
+
const kept = children.filter((_, i) => !remove.has(i));
|
|
384
|
+
const attrs = withColOp(table, cols - 1, { kind: "delete", at: c });
|
|
385
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs, children: kept } };
|
|
386
|
+
}
|
|
387
|
+
function hasSpan(table) {
|
|
388
|
+
const grid = buildGrid(table);
|
|
389
|
+
return grid.anchors.some((i) => {
|
|
390
|
+
const m = grid.cellOf(i);
|
|
391
|
+
return m.colspan > 1 || m.rowspan > 1;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function moveRow(table, tablePath, from, to) {
|
|
395
|
+
const { rows, cols } = tableShape(table);
|
|
396
|
+
if (hasSpan(table))
|
|
397
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
398
|
+
const grid = Array.from({ length: rows }, (_, r) => table.children.slice(r * cols, (r + 1) * cols));
|
|
399
|
+
const [row] = grid.splice(from, 1);
|
|
400
|
+
grid.splice(to, 0, row);
|
|
401
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs: { ...table.attrs, rows, cols }, children: grid.flat() } };
|
|
402
|
+
}
|
|
403
|
+
function moveCol(table, tablePath, from, to) {
|
|
404
|
+
const { rows, cols } = tableShape(table);
|
|
405
|
+
if (hasSpan(table))
|
|
406
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
407
|
+
const children = [];
|
|
408
|
+
for (let r = 0; r < rows; r++) {
|
|
409
|
+
const row = table.children.slice(r * cols, (r + 1) * cols);
|
|
410
|
+
const [c] = row.splice(from, 1);
|
|
411
|
+
row.splice(to, 0, c);
|
|
412
|
+
children.push(...row);
|
|
413
|
+
}
|
|
414
|
+
const attrs = withColOp(table, cols, { kind: "move", from, to });
|
|
415
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs, children } };
|
|
416
|
+
}
|
|
417
|
+
function cellRectTable(table, r1, c1, r2, c2) {
|
|
418
|
+
const g = buildGrid(table);
|
|
419
|
+
const rows = r2 - r1 + 1;
|
|
420
|
+
const cols = c2 - c1 + 1;
|
|
421
|
+
const taken = new Array(rows * cols).fill(false);
|
|
422
|
+
const children = [];
|
|
423
|
+
for (let r = r1; r <= r2; r++) {
|
|
424
|
+
for (let c = c1; c <= c2; c++) {
|
|
425
|
+
if (taken[(r - r1) * cols + (c - c1)])
|
|
426
|
+
continue;
|
|
427
|
+
const idx = g.at(r, c);
|
|
428
|
+
const cell = table.children[idx];
|
|
429
|
+
if (idx < 0 || cell === void 0 || _chunkPCSVBPORcjs.isText.call(void 0, cell)) {
|
|
430
|
+
taken[(r - r1) * cols + (c - c1)] = true;
|
|
431
|
+
children.push(emptyCell());
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
const m = g.cellOf(idx);
|
|
435
|
+
const cs = Math.min(m.c + m.colspan - 1, c2) - c + 1;
|
|
436
|
+
const rs = Math.min(m.r + m.rowspan - 1, r2) - r + 1;
|
|
437
|
+
for (let dr = 0; dr < rs; dr++)
|
|
438
|
+
for (let dc = 0; dc < cs; dc++)
|
|
439
|
+
taken[(r - r1 + dr) * cols + (c - c1 + dc)] = true;
|
|
440
|
+
const { colspan: _cs, rowspan: _rs, ...rest } = _nullishCoalesce(cell.attrs, () => ( {}));
|
|
441
|
+
void _cs;
|
|
442
|
+
void _rs;
|
|
443
|
+
const attrs2 = { ...rest, ...cs > 1 ? { colspan: cs } : {}, ...rs > 1 ? { rowspan: rs } : {} };
|
|
444
|
+
const { id: _id, attrs: _a, ...plain } = cell;
|
|
445
|
+
void _id;
|
|
446
|
+
void _a;
|
|
447
|
+
children.push(Object.keys(attrs2).length ? { ...plain, attrs: attrs2 } : plain);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
const attrs = { rows, cols };
|
|
451
|
+
const widths = tableColWidths(table);
|
|
452
|
+
if (widths && widths.length === tableShape(table).cols)
|
|
453
|
+
attrs.colWidths = widths.slice(c1, c2 + 1);
|
|
454
|
+
if (Array.isArray(_optionalChain([table, 'access', _14 => _14.attrs, 'optionalAccess', _15 => _15.align])))
|
|
455
|
+
attrs.align = table.attrs.align.slice(c1, c2 + 1);
|
|
456
|
+
return { type: "table", attrs, children };
|
|
457
|
+
}
|
|
458
|
+
function tableColWidths(table) {
|
|
459
|
+
const w = _optionalChain([table, 'access', _16 => _16.attrs, 'optionalAccess', _17 => _17.colWidths]);
|
|
460
|
+
return Array.isArray(w) ? w.map(Number) : null;
|
|
461
|
+
}
|
|
462
|
+
function resolvedColWidths(table) {
|
|
463
|
+
const { cols } = tableShape(table);
|
|
464
|
+
const w = tableColWidths(table);
|
|
465
|
+
if (w && w.length === cols)
|
|
466
|
+
return w;
|
|
467
|
+
return Array.from({ length: cols }, () => DEFAULT_COL_WIDTH);
|
|
468
|
+
}
|
|
469
|
+
function setColWidths(table, tablePath, widths) {
|
|
470
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs: { ...table.attrs, colWidths: widths } } };
|
|
471
|
+
}
|
|
472
|
+
function setAlign(table, tablePath, col, value) {
|
|
473
|
+
const { cols } = tableShape(table);
|
|
474
|
+
const cur = Array.isArray(_optionalChain([table, 'access', _18 => _18.attrs, 'optionalAccess', _19 => _19.align])) ? table.attrs.align : [];
|
|
475
|
+
const align = Array.from({ length: cols }, (_, i) => _nullishCoalesce(cur[i], () => ( null)));
|
|
476
|
+
align[col] = value;
|
|
477
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs: { ...table.attrs, align } } };
|
|
478
|
+
}
|
|
479
|
+
function mergeCells(table, tablePath, r1, c1, r2, c2) {
|
|
480
|
+
const grid = buildGrid(table);
|
|
481
|
+
let top = Math.min(r1, r2);
|
|
482
|
+
let bot = Math.max(r1, r2);
|
|
483
|
+
let left = Math.min(c1, c2);
|
|
484
|
+
let right = Math.max(c1, c2);
|
|
485
|
+
let grew = true;
|
|
486
|
+
while (grew) {
|
|
487
|
+
grew = false;
|
|
488
|
+
for (const idx of grid.anchors) {
|
|
489
|
+
const m = grid.cellOf(idx);
|
|
490
|
+
const ir = m.r <= bot && m.r + m.rowspan - 1 >= top;
|
|
491
|
+
const ic = m.c <= right && m.c + m.colspan - 1 >= left;
|
|
492
|
+
if (!ir || !ic)
|
|
493
|
+
continue;
|
|
494
|
+
const nt = Math.min(top, m.r);
|
|
495
|
+
const nb = Math.max(bot, m.r + m.rowspan - 1);
|
|
496
|
+
const nl = Math.min(left, m.c);
|
|
497
|
+
const nr = Math.max(right, m.c + m.colspan - 1);
|
|
498
|
+
if (nt !== top || nb !== bot || nl !== left || nr !== right) {
|
|
499
|
+
top = nt;
|
|
500
|
+
bot = nb;
|
|
501
|
+
left = nl;
|
|
502
|
+
right = nr;
|
|
503
|
+
grew = true;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const covered = grid.anchors.filter((idx) => {
|
|
508
|
+
const m = grid.cellOf(idx);
|
|
509
|
+
return m.r >= top && m.r + m.rowspan - 1 <= bot && m.c >= left && m.c + m.colspan - 1 <= right;
|
|
510
|
+
});
|
|
511
|
+
if (covered.length <= 1)
|
|
512
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
513
|
+
const survivor = covered[0];
|
|
514
|
+
const survCell = table.children[survivor];
|
|
515
|
+
const paras = survCell.children.slice();
|
|
516
|
+
for (const idx of covered.slice(1)) {
|
|
517
|
+
const cell = table.children[idx];
|
|
518
|
+
for (const p of cell.children) {
|
|
519
|
+
if (!_chunkPCSVBPORcjs.isText.call(void 0, p) && p.type === "paragraph" && isNonEmptyPara(p))
|
|
520
|
+
paras.push(p);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
const merged = withSpan({ ...survCell, children: paras }, right - left + 1, bot - top + 1);
|
|
524
|
+
const removeSet = new Set(covered.slice(1));
|
|
525
|
+
const children = table.children.map((c, i) => i === survivor ? merged : c).filter((_, i) => !removeSet.has(i));
|
|
526
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, children } };
|
|
527
|
+
}
|
|
528
|
+
function isNonEmptyPara(p) {
|
|
529
|
+
return p.children.some((c) => _chunkPCSVBPORcjs.isText.call(void 0, c) && c.text !== "");
|
|
530
|
+
}
|
|
531
|
+
function splitCell(table, tablePath, r, c) {
|
|
532
|
+
const grid = buildGrid(table);
|
|
533
|
+
const idx = grid.at(r, c);
|
|
534
|
+
if (idx < 0)
|
|
535
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
536
|
+
const m = grid.cellOf(idx);
|
|
537
|
+
if (m.colspan === 1 && m.rowspan === 1)
|
|
538
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
539
|
+
const fill = m.colspan * m.rowspan - 1;
|
|
540
|
+
const children = table.children.map((cc) => cc);
|
|
541
|
+
children[idx] = withSpan(children[idx], 1, 1);
|
|
542
|
+
children.splice(idx + 1, 0, ...Array.from({ length: fill }, emptyCell));
|
|
543
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, children } };
|
|
544
|
+
}
|
|
545
|
+
function setCellStyle(table, tablePath, r, c, style) {
|
|
546
|
+
const idx = buildGrid(table).at(r, c);
|
|
547
|
+
if (idx < 0)
|
|
548
|
+
return { kind: "replaceNode", path: tablePath, node: table };
|
|
549
|
+
const cell = table.children[idx];
|
|
550
|
+
const attrs = { ...cell.attrs };
|
|
551
|
+
if ("bg" in style) {
|
|
552
|
+
if (style.bg)
|
|
553
|
+
attrs.bg = style.bg;
|
|
554
|
+
else delete attrs.bg;
|
|
555
|
+
}
|
|
556
|
+
if ("border" in style) {
|
|
557
|
+
if (style.border)
|
|
558
|
+
attrs.border = style.border;
|
|
559
|
+
else delete attrs.border;
|
|
560
|
+
}
|
|
561
|
+
const children = table.children.map((cc, i) => i === idx ? { ...cell, attrs } : cc);
|
|
562
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, children } };
|
|
563
|
+
}
|
|
564
|
+
function clearCells(table, tablePath, rect) {
|
|
565
|
+
const grid = buildGrid(table);
|
|
566
|
+
const children = table.children.map((cell, i) => {
|
|
567
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, cell) || cell.type !== "table_cell")
|
|
568
|
+
return cell;
|
|
569
|
+
const m = grid.cellOf(i);
|
|
570
|
+
const hit = m.r <= rect.r2 && m.r + m.rowspan - 1 >= rect.r1 && m.c <= rect.c2 && m.c + m.colspan - 1 >= rect.c1;
|
|
571
|
+
return hit ? { ...cell, children: [{ type: "paragraph", children: [{ text: "" }] }] } : cell;
|
|
572
|
+
});
|
|
573
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, children } };
|
|
574
|
+
}
|
|
575
|
+
function setFreeze(table, tablePath, freeze) {
|
|
576
|
+
const attrs = { ...table.attrs };
|
|
577
|
+
if ("rows" in freeze) {
|
|
578
|
+
if (freeze.rows)
|
|
579
|
+
attrs.freezeRows = freeze.rows;
|
|
580
|
+
else delete attrs.freezeRows;
|
|
581
|
+
}
|
|
582
|
+
if ("cols" in freeze) {
|
|
583
|
+
if (freeze.cols)
|
|
584
|
+
attrs.freezeCols = freeze.cols;
|
|
585
|
+
else delete attrs.freezeCols;
|
|
586
|
+
}
|
|
587
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs } };
|
|
588
|
+
}
|
|
589
|
+
function setHeaderCol(table, tablePath, on) {
|
|
590
|
+
const attrs = { ...table.attrs };
|
|
591
|
+
if (on)
|
|
592
|
+
attrs.headerCol = true;
|
|
593
|
+
else delete attrs.headerCol;
|
|
594
|
+
return { kind: "replaceNode", path: tablePath, node: { ...table, attrs } };
|
|
595
|
+
}
|
|
596
|
+
function mapCol(arr, op, fill) {
|
|
597
|
+
if (op.kind === "insert")
|
|
598
|
+
return arr.slice(0, op.at).concat(fill, arr.slice(op.at));
|
|
599
|
+
if (op.kind === "delete")
|
|
600
|
+
return arr.slice(0, op.at).concat(arr.slice(op.at + 1));
|
|
601
|
+
const a = arr.slice();
|
|
602
|
+
const [x] = a.splice(op.from, 1);
|
|
603
|
+
a.splice(op.to, 0, x);
|
|
604
|
+
return a;
|
|
605
|
+
}
|
|
606
|
+
function withColOp(table, cols, op) {
|
|
607
|
+
const attrs = { ...table.attrs, cols };
|
|
608
|
+
const oldCols = tableShape(table).cols;
|
|
609
|
+
const widths = _nullishCoalesce(tableColWidths(table), () => ( Array.from({ length: oldCols }, () => DEFAULT_COL_WIDTH)));
|
|
610
|
+
attrs.colWidths = mapCol(widths, op, DEFAULT_COL_WIDTH);
|
|
611
|
+
const align = Array.isArray(_optionalChain([table, 'access', _20 => _20.attrs, 'optionalAccess', _21 => _21.align])) ? table.attrs.align : null;
|
|
612
|
+
if (align)
|
|
613
|
+
attrs.align = mapCol(align, op, null);
|
|
614
|
+
return attrs;
|
|
615
|
+
}
|
|
616
|
+
function deleteTableBlock(tablePath, doc) {
|
|
617
|
+
const parent = tablePath.slice(0, -1);
|
|
618
|
+
const at = tablePath[tablePath.length - 1];
|
|
619
|
+
if (doc && parent.length === 0 && doc.children.length <= 1) {
|
|
620
|
+
const para = { type: "paragraph", children: [{ text: "" }] };
|
|
621
|
+
return { kind: "spliceChildren", path: [], at, remove: 1, insert: [para] };
|
|
622
|
+
}
|
|
623
|
+
return { kind: "spliceChildren", path: parent, at, remove: 1, insert: [] };
|
|
624
|
+
}
|
|
625
|
+
function tableAt(doc, tablePath) {
|
|
626
|
+
const n = _chunkHXABDVLZcjs.nodeAt.call(void 0, doc, tablePath);
|
|
627
|
+
return !_chunkPCSVBPORcjs.isText.call(void 0, n) && n.type === "table" ? n : null;
|
|
628
|
+
}
|
|
629
|
+
function checkTable(table) {
|
|
630
|
+
const out = [];
|
|
631
|
+
const { rows, cols } = tableShape(table);
|
|
632
|
+
if (rows * cols > 0) {
|
|
633
|
+
const grid = buildGrid(table);
|
|
634
|
+
let covered = 0;
|
|
635
|
+
let holes = 0;
|
|
636
|
+
for (let r = 0; r < rows; r++)
|
|
637
|
+
for (let c = 0; c < cols; c++)
|
|
638
|
+
grid.at(r, c) < 0 ? holes++ : covered++;
|
|
639
|
+
if (holes > 0)
|
|
640
|
+
out.push(`\u7F51\u683C\u6709 ${holes} \u4E2A\u672A\u8986\u76D6\u70B9(span \u4E4B\u548C < rows*cols \u6216 children \u4E0D\u8DB3)`);
|
|
641
|
+
if (grid.anchors.length !== table.children.length)
|
|
642
|
+
out.push(`\u951A\u70B9\u6570 ${grid.anchors.length} \u2260 children \u6570 ${table.children.length}(span \u4E4B\u548C > rows*cols)`);
|
|
643
|
+
void covered;
|
|
644
|
+
}
|
|
645
|
+
table.children.forEach((cell, i) => {
|
|
646
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, cell) || cell.type !== "table_cell") {
|
|
647
|
+
out.push(`children[${i}] \u4E0D\u662F table_cell`);
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
for (const child of cell.children) {
|
|
651
|
+
if (!_chunkPCSVBPORcjs.isText.call(void 0, child) && !isCellBlockType(child.type))
|
|
652
|
+
out.push(`children[${i}] cell \u5185\u975E\u6CD5\u5757: ${child.type}`);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
const widths = tableColWidths(table);
|
|
656
|
+
if (widths && widths.length !== cols)
|
|
657
|
+
out.push(`colWidths \u957F\u5EA6 ${widths.length} \u2260 cols ${cols}`);
|
|
658
|
+
const align = _optionalChain([table, 'access', _22 => _22.attrs, 'optionalAccess', _23 => _23.align]);
|
|
659
|
+
if (Array.isArray(align) && align.length !== cols)
|
|
660
|
+
out.push(`align \u957F\u5EA6 ${align.length} \u2260 cols ${cols}`);
|
|
661
|
+
return out;
|
|
662
|
+
}
|
|
663
|
+
var CELL_BLOCK_TYPES = cellBlockTypes();
|
|
664
|
+
function isCellBlockType(type) {
|
|
665
|
+
return isCellBlockTypeInSpace(type);
|
|
666
|
+
}
|
|
667
|
+
var tableSchema = {
|
|
668
|
+
marks: ["bold", "italic", "code", "strike", "link", "underline", "highlight", "superscript", "subscript", "textStyle", "comment"],
|
|
669
|
+
nodes: {
|
|
670
|
+
table: { content: ["table_cell"], atom: true },
|
|
671
|
+
table_cell: { content: [...CELL_BLOCK_TYPES] },
|
|
672
|
+
paragraph: { content: ["text", "mention", "hard_break", "math"], inline: true },
|
|
673
|
+
list: { content: ["list_item"] },
|
|
674
|
+
list_item: { content: ["paragraph", "list"] },
|
|
675
|
+
quote: { content: ["paragraph", "list"] },
|
|
676
|
+
callout: { content: ["paragraph", "list"] },
|
|
677
|
+
code: { content: ["text"], inline: true, marks: [] },
|
|
678
|
+
image: { content: [], atom: true },
|
|
679
|
+
embed: { content: [], atom: true },
|
|
680
|
+
raw: { content: ["text"], inline: true, marks: [] }
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
// src/tree/css.ts
|
|
685
|
+
function safeCssValue(v) {
|
|
686
|
+
let out = "";
|
|
687
|
+
let quote = "";
|
|
688
|
+
let open = -1;
|
|
689
|
+
for (const ch of v) {
|
|
690
|
+
if (ch === ";" || ch === "<" || ch === ">")
|
|
691
|
+
continue;
|
|
692
|
+
if (quote) {
|
|
693
|
+
if (ch === "\\" || ch === "\n" || ch === "\r")
|
|
694
|
+
continue;
|
|
695
|
+
out += ch;
|
|
696
|
+
if (ch === quote)
|
|
697
|
+
quote = "";
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (ch === '"' || ch === "'") {
|
|
701
|
+
quote = ch;
|
|
702
|
+
open = out.length;
|
|
703
|
+
}
|
|
704
|
+
out += ch;
|
|
705
|
+
}
|
|
706
|
+
if (quote)
|
|
707
|
+
out = out.slice(0, open) + out.slice(open + 1);
|
|
708
|
+
return out.trim();
|
|
709
|
+
}
|
|
710
|
+
function highlightColor(v) {
|
|
711
|
+
if (typeof v !== "string")
|
|
712
|
+
return void 0;
|
|
713
|
+
const c = v.trim();
|
|
714
|
+
return c && safeCssValue(c) === c ? c : void 0;
|
|
715
|
+
}
|
|
716
|
+
function styleAttr(style) {
|
|
717
|
+
let esc = "";
|
|
718
|
+
for (const ch of style)
|
|
719
|
+
esc += ch === "&" ? "&" : ch === "<" ? "<" : ch === ">" ? ">" : ch;
|
|
720
|
+
if (!esc.includes('"'))
|
|
721
|
+
return `style="${esc}"`;
|
|
722
|
+
return `style='${esc.replaceAll("'", "'")}'`;
|
|
723
|
+
}
|
|
724
|
+
function asAlign(v) {
|
|
725
|
+
return v === "left" || v === "right" || v === "center" || v === "justify" ? v : void 0;
|
|
726
|
+
}
|
|
727
|
+
var LINE_HEIGHT_UNITS = /* @__PURE__ */ new Set(["", "%", "px", "em", "rem", "ex", "ch", "pt", "pc", "cm", "mm", "in", "vh", "vw", "vmin", "vmax"]);
|
|
728
|
+
var LINE_HEIGHT_WORDS = /* @__PURE__ */ new Set(["normal", "inherit", "initial", "unset", "revert"]);
|
|
729
|
+
function cleanLineHeight(v) {
|
|
730
|
+
const s = safeCssValue(v);
|
|
731
|
+
const lower = s.toLowerCase();
|
|
732
|
+
if (LINE_HEIGHT_WORDS.has(lower))
|
|
733
|
+
return s;
|
|
734
|
+
let i = 0;
|
|
735
|
+
const digit = (k) => k < s.length && s[k] >= "0" && s[k] <= "9";
|
|
736
|
+
while (digit(i))
|
|
737
|
+
i++;
|
|
738
|
+
const intLen = i;
|
|
739
|
+
let fracLen = 0;
|
|
740
|
+
if (s[i] === ".") {
|
|
741
|
+
i++;
|
|
742
|
+
const from = i;
|
|
743
|
+
while (digit(i))
|
|
744
|
+
i++;
|
|
745
|
+
fracLen = i - from;
|
|
746
|
+
if (!fracLen)
|
|
747
|
+
return void 0;
|
|
748
|
+
}
|
|
749
|
+
if (!intLen && !fracLen)
|
|
750
|
+
return void 0;
|
|
751
|
+
return LINE_HEIGHT_UNITS.has(lower.slice(i)) ? s : void 0;
|
|
752
|
+
}
|
|
753
|
+
var TEXT_STYLE_PROPS = [
|
|
754
|
+
{ attr: "color", css: "color" },
|
|
755
|
+
{ attr: "backgroundColor", css: "background-color", alias: "background" },
|
|
756
|
+
{ attr: "fontFamily", css: "font-family" },
|
|
757
|
+
{ attr: "fontSize", css: "font-size" },
|
|
758
|
+
{ attr: "lineHeight", css: "line-height", clean: cleanLineHeight }
|
|
759
|
+
];
|
|
760
|
+
function attrsToStyle(attrs) {
|
|
761
|
+
if (!attrs)
|
|
762
|
+
return "";
|
|
763
|
+
const parts = [];
|
|
764
|
+
for (const p of TEXT_STYLE_PROPS) {
|
|
765
|
+
const raw = attrs[p.attr];
|
|
766
|
+
if (typeof raw !== "string" || !raw)
|
|
767
|
+
continue;
|
|
768
|
+
const v = p.clean ? p.clean(raw) : safeCssValue(raw);
|
|
769
|
+
if (v !== void 0)
|
|
770
|
+
parts.push(`${p.css}:${v}`);
|
|
771
|
+
}
|
|
772
|
+
return parts.join(";");
|
|
773
|
+
}
|
|
774
|
+
function styleToAttrs(style) {
|
|
775
|
+
if (!style)
|
|
776
|
+
return null;
|
|
777
|
+
const out = {};
|
|
778
|
+
for (const part of style.split(";")) {
|
|
779
|
+
const i = part.indexOf(":");
|
|
780
|
+
if (i < 0)
|
|
781
|
+
continue;
|
|
782
|
+
const prop = part.slice(0, i).trim().toLowerCase();
|
|
783
|
+
const val = part.slice(i + 1).trim();
|
|
784
|
+
if (!val)
|
|
785
|
+
continue;
|
|
786
|
+
const p = TEXT_STYLE_PROPS.find((x) => x.css === prop || x.alias === prop);
|
|
787
|
+
if (!p)
|
|
788
|
+
continue;
|
|
789
|
+
const v = p.clean ? p.clean(val) : val;
|
|
790
|
+
if (v !== void 0)
|
|
791
|
+
out[p.attr] = v;
|
|
792
|
+
}
|
|
793
|
+
return Object.keys(out).length ? out : null;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// src/tree/front-matter.ts
|
|
797
|
+
var FRONT_MATTER = "frontmatter";
|
|
798
|
+
function isFrontMatter(n) {
|
|
799
|
+
return !!n && _chunkPCSVBPORcjs.isElement.call(void 0, n) && n.type === "raw" && _optionalChain([n, 'access', _24 => _24.attrs, 'optionalAccess', _25 => _25.mdType]) === FRONT_MATTER;
|
|
800
|
+
}
|
|
801
|
+
function frontMatterEnd(md) {
|
|
802
|
+
if (!md.startsWith("---"))
|
|
803
|
+
return -1;
|
|
804
|
+
let at = md.indexOf("\n");
|
|
805
|
+
if (at < 0 || md.slice(0, at).trimEnd() !== "---")
|
|
806
|
+
return -1;
|
|
807
|
+
let body = false;
|
|
808
|
+
while (at < md.length) {
|
|
809
|
+
const start = at + 1;
|
|
810
|
+
let end = md.indexOf("\n", start);
|
|
811
|
+
if (end < 0)
|
|
812
|
+
end = md.length;
|
|
813
|
+
const line = md.slice(start, end).trimEnd();
|
|
814
|
+
if (line === "---" || line === "...")
|
|
815
|
+
return body ? start + line.length : -1;
|
|
816
|
+
if (line !== "")
|
|
817
|
+
body = true;
|
|
818
|
+
at = end;
|
|
819
|
+
}
|
|
820
|
+
return -1;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// src/md.ts
|
|
824
|
+
var _mdastutilfrommarkdown = require('mdast-util-from-markdown');
|
|
825
|
+
var _mdastutilgfm = require('mdast-util-gfm');
|
|
826
|
+
var _mdastutiltomarkdown = require('mdast-util-to-markdown');
|
|
827
|
+
var _micromarkextensioncjkfriendly = require('micromark-extension-cjk-friendly');
|
|
828
|
+
var _micromarkextensiongfm = require('micromark-extension-gfm');
|
|
829
|
+
|
|
830
|
+
// src/md-chunk.ts
|
|
831
|
+
var DEF = /^[ \t>*+\-\d.)]*\[[^\]\n]+\]:/m;
|
|
832
|
+
var LIST_MARK = /^(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)/;
|
|
833
|
+
var FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
|
|
834
|
+
var HTML_OPEN = [
|
|
835
|
+
[/^ {0,3}<(?:script|pre|style|textarea)(?:[ \t>]|$)/i, /<\/(?:script|pre|style|textarea)>/i],
|
|
836
|
+
[/^ {0,3}<!--/, /-->/],
|
|
837
|
+
[/^ {0,3}<\?/, /\?>/],
|
|
838
|
+
[/^ {0,3}<!\[CDATA\[/, /\]\]>/],
|
|
839
|
+
[/^ {0,3}<![A-Za-z]/, />/]
|
|
840
|
+
];
|
|
841
|
+
function chunkStarts(md, minChunk) {
|
|
842
|
+
if (DEF.test(md))
|
|
843
|
+
return null;
|
|
844
|
+
const starts = [0];
|
|
845
|
+
let pos = 0;
|
|
846
|
+
let blankBefore = false;
|
|
847
|
+
let fence = null;
|
|
848
|
+
let html = null;
|
|
849
|
+
let first = true;
|
|
850
|
+
let sinceCut = 0;
|
|
851
|
+
const n = md.length;
|
|
852
|
+
while (pos < n) {
|
|
853
|
+
let eol = md.indexOf("\n", pos);
|
|
854
|
+
if (eol < 0)
|
|
855
|
+
eol = n;
|
|
856
|
+
const line = md.slice(pos, eol);
|
|
857
|
+
const blank = line.trim() === "";
|
|
858
|
+
if (first) {
|
|
859
|
+
first = false;
|
|
860
|
+
if (line.trimEnd() === "---") {
|
|
861
|
+
const close = md.indexOf("\n---", eol);
|
|
862
|
+
if (close >= 0) {
|
|
863
|
+
const e2 = md.indexOf("\n", close + 1);
|
|
864
|
+
pos = e2 < 0 ? n : e2 + 1;
|
|
865
|
+
blankBefore = false;
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
if (fence) {
|
|
871
|
+
const m = FENCE.exec(line);
|
|
872
|
+
if (m && m[1][0] === fence.ch && m[1].length >= fence.len && m[2].trim() === "")
|
|
873
|
+
fence = null;
|
|
874
|
+
} else if (html) {
|
|
875
|
+
if (html.test(line))
|
|
876
|
+
html = null;
|
|
877
|
+
} else {
|
|
878
|
+
if (blankBefore && !blank && line[0] !== " " && line[0] !== " " && !LIST_MARK.test(line) && sinceCut >= minChunk) {
|
|
879
|
+
starts.push(pos);
|
|
880
|
+
sinceCut = 0;
|
|
881
|
+
}
|
|
882
|
+
const m = FENCE.exec(line);
|
|
883
|
+
if (m && !(m[1][0] === "`" && m[2].includes("`")))
|
|
884
|
+
fence = { ch: m[1][0], len: m[1].length };
|
|
885
|
+
else {
|
|
886
|
+
for (const [open, close] of HTML_OPEN) {
|
|
887
|
+
if (open.test(line)) {
|
|
888
|
+
if (!close.test(line.replace(open, "")))
|
|
889
|
+
html = close;
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
sinceCut += eol + 1 - pos;
|
|
896
|
+
blankBefore = blank;
|
|
897
|
+
pos = eol + 1;
|
|
898
|
+
}
|
|
899
|
+
return starts.length > 1 ? starts : null;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// src/md-math.ts
|
|
903
|
+
var T = (s) => s;
|
|
904
|
+
var DOLLAR = 36;
|
|
905
|
+
var BACKSLASH = 92;
|
|
906
|
+
var isEol = (code) => code !== null && code < -2;
|
|
907
|
+
var isWs = (code) => code === null || code < 0 || /\s/u.test(String.fromCodePoint(code));
|
|
908
|
+
var isDigit = (code) => code !== null && code >= 48 && code <= 57;
|
|
909
|
+
function tokenize(effects, ok, nok) {
|
|
910
|
+
let open = 0;
|
|
911
|
+
let close = 0;
|
|
912
|
+
let last = null;
|
|
913
|
+
let seq;
|
|
914
|
+
const start = (code) => {
|
|
915
|
+
effects.enter(T("mathText"));
|
|
916
|
+
effects.enter(T("mathTextSequence"));
|
|
917
|
+
return opening(code);
|
|
918
|
+
};
|
|
919
|
+
const opening = (code) => {
|
|
920
|
+
if (code === DOLLAR) {
|
|
921
|
+
if (open === 2)
|
|
922
|
+
return nok(code);
|
|
923
|
+
effects.consume(code);
|
|
924
|
+
open++;
|
|
925
|
+
return opening;
|
|
926
|
+
}
|
|
927
|
+
effects.exit(T("mathTextSequence"));
|
|
928
|
+
return open === 1 && isWs(code) ? nok(code) : between(code);
|
|
929
|
+
};
|
|
930
|
+
const between = (code) => {
|
|
931
|
+
if (code === null)
|
|
932
|
+
return nok(code);
|
|
933
|
+
if (isEol(code)) {
|
|
934
|
+
if (open === 1)
|
|
935
|
+
return nok(code);
|
|
936
|
+
effects.enter(T("lineEnding"));
|
|
937
|
+
effects.consume(code);
|
|
938
|
+
effects.exit(T("lineEnding"));
|
|
939
|
+
last = code;
|
|
940
|
+
return between;
|
|
941
|
+
}
|
|
942
|
+
if (code === DOLLAR) {
|
|
943
|
+
seq = effects.enter(T("mathTextSequence"));
|
|
944
|
+
close = 0;
|
|
945
|
+
return closing(code);
|
|
946
|
+
}
|
|
947
|
+
effects.enter(T("mathTextData"));
|
|
948
|
+
return data(code);
|
|
949
|
+
};
|
|
950
|
+
const data = (code) => {
|
|
951
|
+
if (code === null || isEol(code) || code === DOLLAR) {
|
|
952
|
+
effects.exit(T("mathTextData"));
|
|
953
|
+
return between(code);
|
|
954
|
+
}
|
|
955
|
+
effects.consume(code);
|
|
956
|
+
last = code;
|
|
957
|
+
return code === BACKSLASH ? escaped : data;
|
|
958
|
+
};
|
|
959
|
+
const escaped = (code) => {
|
|
960
|
+
if (code === null || isEol(code))
|
|
961
|
+
return data(code);
|
|
962
|
+
effects.consume(code);
|
|
963
|
+
last = code;
|
|
964
|
+
return data;
|
|
965
|
+
};
|
|
966
|
+
const closing = (code) => {
|
|
967
|
+
if (code === DOLLAR && close < open) {
|
|
968
|
+
effects.consume(code);
|
|
969
|
+
close++;
|
|
970
|
+
return closing;
|
|
971
|
+
}
|
|
972
|
+
const done = close === open && (open === 2 || !isWs(last) && !isDigit(code));
|
|
973
|
+
if (done) {
|
|
974
|
+
effects.exit(T("mathTextSequence"));
|
|
975
|
+
effects.exit(T("mathText"));
|
|
976
|
+
return ok(code);
|
|
977
|
+
}
|
|
978
|
+
seq.type = T("mathTextData");
|
|
979
|
+
last = DOLLAR;
|
|
980
|
+
return data(code);
|
|
981
|
+
};
|
|
982
|
+
return start;
|
|
983
|
+
}
|
|
984
|
+
var mathText = {
|
|
985
|
+
name: "mathText",
|
|
986
|
+
tokenize,
|
|
987
|
+
// 一串 $ 只从第一个起读;`\$` 之后可以开始
|
|
988
|
+
previous(code) {
|
|
989
|
+
return code !== DOLLAR || _optionalChain([this, 'access', _26 => _26.events, 'access', _27 => _27[this.events.length - 1], 'optionalAccess', _28 => _28[1], 'access', _29 => _29.type]) === "characterEscape";
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
var dollarMathSyntax = { text: { [DOLLAR]: mathText } };
|
|
993
|
+
var dollarMathFromMarkdown = {
|
|
994
|
+
enter: {
|
|
995
|
+
[T("mathText")](token) {
|
|
996
|
+
this.enter({ type: "inlineMath", value: "" }, token);
|
|
997
|
+
this.buffer();
|
|
998
|
+
}
|
|
999
|
+
},
|
|
1000
|
+
exit: {
|
|
1001
|
+
[T("mathTextData")](token) {
|
|
1002
|
+
this.config.enter.data.call(this, token);
|
|
1003
|
+
this.config.exit.data.call(this, token);
|
|
1004
|
+
},
|
|
1005
|
+
[T("mathText")](token) {
|
|
1006
|
+
let value = this.resume();
|
|
1007
|
+
const node = this.stack[this.stack.length - 1];
|
|
1008
|
+
this.exit(token);
|
|
1009
|
+
const display = this.sliceSerialize(token).startsWith("$$");
|
|
1010
|
+
if (display && value.startsWith("\n"))
|
|
1011
|
+
value = value.slice(1);
|
|
1012
|
+
if (display && value.endsWith("\n"))
|
|
1013
|
+
value = value.slice(0, -1);
|
|
1014
|
+
node.value = value;
|
|
1015
|
+
node.data = { display: display ? "block" : "inline" };
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
function dollarCandidate(latex, display, alone) {
|
|
1020
|
+
if (display === "inline")
|
|
1021
|
+
return `$${latex}$`;
|
|
1022
|
+
return alone ? `$$
|
|
1023
|
+
${latex}
|
|
1024
|
+
$$` : `$$${latex}$$`;
|
|
1025
|
+
}
|
|
1026
|
+
function dollarEscapes(pieces) {
|
|
1027
|
+
const cells = [];
|
|
1028
|
+
pieces.forEach((p, i) => {
|
|
1029
|
+
cells.push({ ch: "", piece: -1, at: 0 });
|
|
1030
|
+
for (let k = 0; k < p.text.length; k++)
|
|
1031
|
+
cells.push({ ch: p.text[k], piece: i, at: k });
|
|
1032
|
+
});
|
|
1033
|
+
cells.push({ ch: "", piece: -1, at: 0 });
|
|
1034
|
+
const out = pieces.map(() => /* @__PURE__ */ new Set());
|
|
1035
|
+
const kind = (j) => cells[j].piece < 0 ? "sep" : pieces[cells[j].piece].kind;
|
|
1036
|
+
const escaped = (j) => cells[j].piece >= 0 && out[cells[j].piece].has(cells[j].at);
|
|
1037
|
+
const escape = (j) => out[cells[j].piece].add(cells[j].at);
|
|
1038
|
+
const ws = (j) => j < 0 || j >= cells.length || kind(j) !== "sep" && /\s/u.test(cells[j].ch);
|
|
1039
|
+
const digit = (j) => j < cells.length && cells[j].ch >= "0" && cells[j].ch <= "9";
|
|
1040
|
+
const real = (j, step) => {
|
|
1041
|
+
while (j >= 0 && j < cells.length && kind(j) === "sep") j += step;
|
|
1042
|
+
return j >= 0 && j < cells.length ? j : -1;
|
|
1043
|
+
};
|
|
1044
|
+
const isDollar = (j) => j >= 0 && cells[j].ch === "$";
|
|
1045
|
+
const closerAfter = (i) => {
|
|
1046
|
+
for (let j = i + 1; j < cells.length; j++) {
|
|
1047
|
+
if (cells[j].ch === "\n")
|
|
1048
|
+
return -1;
|
|
1049
|
+
if (cells[j].ch === "$" && !escaped(j) && !ws(j - 1) && !digit(j + 1))
|
|
1050
|
+
return j;
|
|
1051
|
+
}
|
|
1052
|
+
return -1;
|
|
1053
|
+
};
|
|
1054
|
+
for (let i = 0; i < cells.length; i++) {
|
|
1055
|
+
const c = cells[i];
|
|
1056
|
+
if (kind(i) === "math" && c.at === 0) {
|
|
1057
|
+
while (i + 1 < cells.length && cells[i + 1].piece === c.piece) i++;
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
if (c.ch !== "$" || escaped(i))
|
|
1061
|
+
continue;
|
|
1062
|
+
if (kind(i) === "text" && (isDollar(real(i - 1, -1)) || isDollar(real(i + 1, 1)))) {
|
|
1063
|
+
escape(i);
|
|
1064
|
+
continue;
|
|
1065
|
+
}
|
|
1066
|
+
if (ws(i + 1) || i + 1 === cells.length - 1)
|
|
1067
|
+
continue;
|
|
1068
|
+
for (let q = closerAfter(i); q !== -1; q = closerAfter(i)) {
|
|
1069
|
+
if (kind(i) === "text") {
|
|
1070
|
+
escape(i);
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
if (kind(q) !== "text")
|
|
1074
|
+
break;
|
|
1075
|
+
escape(q);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return out;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
// src/yield.ts
|
|
1082
|
+
var yieldToMain = (() => {
|
|
1083
|
+
if (typeof window === "undefined" || typeof MessageChannel === "undefined")
|
|
1084
|
+
return () => new Promise((r) => setTimeout(r, 0));
|
|
1085
|
+
const { port1, port2 } = new MessageChannel();
|
|
1086
|
+
const waiting = [];
|
|
1087
|
+
port1.onmessage = () => {
|
|
1088
|
+
_optionalChain([waiting, 'access', _30 => _30.shift, 'call', _31 => _31(), 'optionalCall', _32 => _32()]);
|
|
1089
|
+
};
|
|
1090
|
+
return () => new Promise((r) => {
|
|
1091
|
+
waiting.push(r);
|
|
1092
|
+
port2.postMessage(0);
|
|
1093
|
+
});
|
|
1094
|
+
})();
|
|
1095
|
+
|
|
1096
|
+
// src/tree/list-marker.ts
|
|
1097
|
+
var ROMAN = [[1e3, "m"], [900, "cm"], [500, "d"], [400, "cd"], [100, "c"], [90, "xc"], [50, "l"], [40, "xl"], [10, "x"], [9, "ix"], [5, "v"], [4, "iv"], [1, "i"]];
|
|
1098
|
+
function toRoman(n) {
|
|
1099
|
+
let out = "";
|
|
1100
|
+
for (const [v, s] of ROMAN) {
|
|
1101
|
+
for (; n >= v; n -= v) out += s;
|
|
1102
|
+
}
|
|
1103
|
+
return out;
|
|
1104
|
+
}
|
|
1105
|
+
function fromRoman(s) {
|
|
1106
|
+
let n = 0;
|
|
1107
|
+
let rest = s;
|
|
1108
|
+
for (const [v, r] of ROMAN) {
|
|
1109
|
+
while (rest.startsWith(r)) {
|
|
1110
|
+
n += v;
|
|
1111
|
+
rest = rest.slice(r.length);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
return rest === "" && n > 0 && toRoman(n) === s ? n : null;
|
|
1115
|
+
}
|
|
1116
|
+
function toAlpha(n) {
|
|
1117
|
+
return n <= 26 ? String.fromCharCode(96 + n) : String.fromCharCode(96 + Math.ceil(n / 26) - 1) + String.fromCharCode(97 + (n - 1) % 26);
|
|
1118
|
+
}
|
|
1119
|
+
function fromAlpha(s) {
|
|
1120
|
+
if (s.length < 1 || s.length > 2)
|
|
1121
|
+
return null;
|
|
1122
|
+
let n = 0;
|
|
1123
|
+
for (const ch of s) {
|
|
1124
|
+
const d = ch.charCodeAt(0) - 96;
|
|
1125
|
+
if (d < 1 || d > 26)
|
|
1126
|
+
return null;
|
|
1127
|
+
n = n * 26 + d;
|
|
1128
|
+
}
|
|
1129
|
+
return n;
|
|
1130
|
+
}
|
|
1131
|
+
var up = (f) => (n) => f(n).toUpperCase();
|
|
1132
|
+
var lowerOnly = (f) => (s) => s === s.toLowerCase() ? f(s) : null;
|
|
1133
|
+
var upperOnly = (f) => (s) => s === s.toUpperCase() ? f(s.toLowerCase()) : null;
|
|
1134
|
+
var STYLES = [
|
|
1135
|
+
{ type: "i", css: "lower-roman", format: toRoman, parse: lowerOnly(fromRoman) },
|
|
1136
|
+
{ type: "I", css: "upper-roman", format: up(toRoman), parse: upperOnly(fromRoman) },
|
|
1137
|
+
{ type: "a", css: "lower-alpha", format: toAlpha, parse: lowerOnly(fromAlpha) },
|
|
1138
|
+
{ type: "A", css: "upper-alpha", format: up(toAlpha), parse: upperOnly(fromAlpha) }
|
|
1139
|
+
];
|
|
1140
|
+
var LIST_TYPES = ["a", "A", "i", "I"];
|
|
1141
|
+
var LIST_TYPE_CSS = Object.fromEntries(STYLES.map((s) => [s.type, s.css]));
|
|
1142
|
+
function asListType(v) {
|
|
1143
|
+
return typeof v === "string" && LIST_TYPES.includes(v) ? v : void 0;
|
|
1144
|
+
}
|
|
1145
|
+
function detectPlainList(text) {
|
|
1146
|
+
const lines = text.split("\n").map((l) => l.replace(/\r$/, "")).filter((l) => l.trim() !== "");
|
|
1147
|
+
if (!lines.length)
|
|
1148
|
+
return null;
|
|
1149
|
+
const rows = [];
|
|
1150
|
+
for (const line of lines) {
|
|
1151
|
+
const m = /^([a-z]+)([.)]) +(\S.*)$/i.exec(line);
|
|
1152
|
+
if (!m)
|
|
1153
|
+
return null;
|
|
1154
|
+
rows.push({ marker: m[1], delim: m[2], body: m[3].trimEnd() });
|
|
1155
|
+
}
|
|
1156
|
+
if (rows.some((r) => r.delim !== rows[0].delim))
|
|
1157
|
+
return null;
|
|
1158
|
+
for (const style of STYLES) {
|
|
1159
|
+
const ns = rows.map((r) => style.parse(r.marker));
|
|
1160
|
+
if (ns.every((n, i) => n !== null && (i === 0 || n === ns[i - 1] + 1)))
|
|
1161
|
+
return { type: style.type, start: ns[0], items: rows.map((r) => r.body) };
|
|
1162
|
+
}
|
|
1163
|
+
return null;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// src/md.ts
|
|
1167
|
+
var mdExt = { extensions: [_micromarkextensiongfm.gfm.call(void 0, ), _micromarkextensioncjkfriendly.cjkFriendlyExtension.call(void 0, )], mdastExtensions: [_mdastutilgfm.gfmFromMarkdown.call(void 0, )] };
|
|
1168
|
+
var mdExtDollar = { extensions: [_micromarkextensiongfm.gfm.call(void 0, ), _micromarkextensioncjkfriendly.cjkFriendlyExtension.call(void 0, ), dollarMathSyntax], mdastExtensions: [_mdastutilgfm.gfmFromMarkdown.call(void 0, ), dollarMathFromMarkdown] };
|
|
1169
|
+
var dollarMath = false;
|
|
1170
|
+
var parseExt = () => dollarMath ? mdExtDollar : mdExt;
|
|
1171
|
+
function setMathSyntax(syntax) {
|
|
1172
|
+
dollarMath = syntax === "dollar";
|
|
1173
|
+
cleanCache = /* @__PURE__ */ new WeakMap();
|
|
1174
|
+
}
|
|
1175
|
+
function imgHtmlToMark(html) {
|
|
1176
|
+
if (typeof DOMParser === "undefined")
|
|
1177
|
+
return null;
|
|
1178
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
1179
|
+
const img = body.querySelector("img");
|
|
1180
|
+
if (!img || body.querySelectorAll("*").length !== 1)
|
|
1181
|
+
return null;
|
|
1182
|
+
const attrs = { url: _nullishCoalesce(img.getAttribute("src"), () => ( "")) };
|
|
1183
|
+
const title = img.getAttribute("title");
|
|
1184
|
+
if (title != null)
|
|
1185
|
+
attrs.title = title;
|
|
1186
|
+
const w = img.getAttribute("width");
|
|
1187
|
+
const h = img.getAttribute("height");
|
|
1188
|
+
if (w != null && w !== "")
|
|
1189
|
+
attrs.w = Number(w);
|
|
1190
|
+
if (h != null && h !== "")
|
|
1191
|
+
attrs.h = Number(h);
|
|
1192
|
+
const align = imgStyleAlign(img.getAttribute("style"));
|
|
1193
|
+
if (align)
|
|
1194
|
+
attrs.align = align;
|
|
1195
|
+
return { alt: _nullishCoalesce(img.getAttribute("alt"), () => ( "")), attrs };
|
|
1196
|
+
}
|
|
1197
|
+
function mentionFromHtml(openTag) {
|
|
1198
|
+
if (typeof DOMParser === "undefined")
|
|
1199
|
+
return null;
|
|
1200
|
+
const el = new DOMParser().parseFromString(`${openTag}</span>`, "text/html").body.querySelector("span");
|
|
1201
|
+
if (!el || !el.hasAttribute("data-mention"))
|
|
1202
|
+
return null;
|
|
1203
|
+
const id = _nullishCoalesce(el.getAttribute("data-id"), () => ( ""));
|
|
1204
|
+
const label = _nullishCoalesce(el.getAttribute("data-label"), () => ( id));
|
|
1205
|
+
const kind = el.getAttribute("data-kind");
|
|
1206
|
+
const date = el.getAttribute("data-date");
|
|
1207
|
+
const attrs = { id, label };
|
|
1208
|
+
if (kind)
|
|
1209
|
+
attrs.kind = kind;
|
|
1210
|
+
if (date)
|
|
1211
|
+
attrs.date = date;
|
|
1212
|
+
return { type: "mention", attrs, children: [] };
|
|
1213
|
+
}
|
|
1214
|
+
function mathFromHtml(html) {
|
|
1215
|
+
if (typeof DOMParser === "undefined")
|
|
1216
|
+
return null;
|
|
1217
|
+
const src = html.includes("</span>") ? html : `${html}</span>`;
|
|
1218
|
+
const el = new DOMParser().parseFromString(src, "text/html").body.querySelector("span");
|
|
1219
|
+
if (!el || !el.hasAttribute("data-math"))
|
|
1220
|
+
return null;
|
|
1221
|
+
const latex = _nullishCoalesce(el.getAttribute("data-latex"), () => ( ""));
|
|
1222
|
+
const display = el.getAttribute("data-display") === "block" ? "block" : "inline";
|
|
1223
|
+
return { type: "math", attrs: { latex, display }, children: [] };
|
|
1224
|
+
}
|
|
1225
|
+
function embedFromHtml(html) {
|
|
1226
|
+
if (typeof DOMParser === "undefined")
|
|
1227
|
+
return null;
|
|
1228
|
+
const el = new DOMParser().parseFromString(html, "text/html").body.querySelector("div[data-embed]");
|
|
1229
|
+
if (!el)
|
|
1230
|
+
return null;
|
|
1231
|
+
const kind = _nullishCoalesce(el.getAttribute("data-kind"), () => ( ""));
|
|
1232
|
+
const src = _nullishCoalesce(el.getAttribute("data-src"), () => ( ""));
|
|
1233
|
+
if (!kind || !src)
|
|
1234
|
+
return null;
|
|
1235
|
+
const attrs = { kind, src };
|
|
1236
|
+
const title = el.getAttribute("data-title");
|
|
1237
|
+
const name = el.getAttribute("data-name");
|
|
1238
|
+
if (title)
|
|
1239
|
+
attrs.title = title;
|
|
1240
|
+
if (name)
|
|
1241
|
+
attrs.name = name;
|
|
1242
|
+
return { type: "embed", attrs, children: [] };
|
|
1243
|
+
}
|
|
1244
|
+
function imgStyleAlign(style) {
|
|
1245
|
+
if (!style)
|
|
1246
|
+
return "";
|
|
1247
|
+
const decls = style.split(";").map((s) => s.trim());
|
|
1248
|
+
const left = decls.includes("margin-left:auto");
|
|
1249
|
+
const right = decls.includes("margin-right:auto");
|
|
1250
|
+
return left && right ? "center" : left ? "right" : "";
|
|
1251
|
+
}
|
|
1252
|
+
function figureFromHtml(html) {
|
|
1253
|
+
if (typeof DOMParser === "undefined")
|
|
1254
|
+
return null;
|
|
1255
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
1256
|
+
const fig = body.querySelector("figure");
|
|
1257
|
+
const img = _optionalChain([fig, 'optionalAccess', _33 => _33.querySelector, 'call', _34 => _34("img")]);
|
|
1258
|
+
if (!fig || !img)
|
|
1259
|
+
return null;
|
|
1260
|
+
const mark = imgHtmlToMark(img.outerHTML);
|
|
1261
|
+
if (!mark)
|
|
1262
|
+
return null;
|
|
1263
|
+
const attrs = { ...mark.attrs, alt: mark.alt };
|
|
1264
|
+
const cap = _nullishCoalesce(_optionalChain([fig, 'access', _35 => _35.querySelector, 'call', _36 => _36("figcaption"), 'optionalAccess', _37 => _37.textContent]), () => ( ""));
|
|
1265
|
+
if (cap)
|
|
1266
|
+
attrs.caption = cap;
|
|
1267
|
+
const falign = _optionalChain([fig, 'access', _38 => _38.getAttribute, 'call', _39 => _39("style"), 'optionalAccess', _40 => _40.split, 'call', _41 => _41(";"), 'access', _42 => _42.map, 'call', _43 => _43((s) => s.trim()), 'access', _44 => _44.find, 'call', _45 => _45((s) => s.startsWith("text-align:")), 'optionalAccess', _46 => _46.slice, 'call', _47 => _47("text-align:".length), 'access', _48 => _48.trim, 'call', _49 => _49()]);
|
|
1268
|
+
if (falign === "center" || falign === "right")
|
|
1269
|
+
attrs.align = falign;
|
|
1270
|
+
return { type: "image", attrs, children: [] };
|
|
1271
|
+
}
|
|
1272
|
+
var htmlTagName = (v) => {
|
|
1273
|
+
const s = v.trim();
|
|
1274
|
+
if (s[0] !== "<" || s[s.length - 1] !== ">")
|
|
1275
|
+
return null;
|
|
1276
|
+
let i = 1;
|
|
1277
|
+
const close = s[i] === "/";
|
|
1278
|
+
if (close)
|
|
1279
|
+
i++;
|
|
1280
|
+
let name = "";
|
|
1281
|
+
for (; i < s.length; i++) {
|
|
1282
|
+
const c2 = s[i].toLowerCase();
|
|
1283
|
+
if (c2 >= "a" && c2 <= "z")
|
|
1284
|
+
name += c2;
|
|
1285
|
+
else
|
|
1286
|
+
break;
|
|
1287
|
+
}
|
|
1288
|
+
if (!name)
|
|
1289
|
+
return null;
|
|
1290
|
+
if (close)
|
|
1291
|
+
return s.slice(i) === ">" ? { name, close } : null;
|
|
1292
|
+
const c = s[i];
|
|
1293
|
+
if (c === ">" || c === " " || c === " " || c === "\n" || c === "\r")
|
|
1294
|
+
return { name, close: false };
|
|
1295
|
+
return null;
|
|
1296
|
+
};
|
|
1297
|
+
function isHtmlMarkOpen(open, raw) {
|
|
1298
|
+
if (open.close)
|
|
1299
|
+
return false;
|
|
1300
|
+
if (simpleTagMark(open.name))
|
|
1301
|
+
return true;
|
|
1302
|
+
if (open.name === "font" || open.name === "code")
|
|
1303
|
+
return true;
|
|
1304
|
+
if (open.name === "span") {
|
|
1305
|
+
if (raw.includes("data-mention") || raw.includes("data-math"))
|
|
1306
|
+
return false;
|
|
1307
|
+
return raw.includes("style=") || raw.includes("data-mark") || raw.includes("data-comment");
|
|
1308
|
+
}
|
|
1309
|
+
return false;
|
|
1310
|
+
}
|
|
1311
|
+
function stitchInlineHtml(nodes, carry, md) {
|
|
1312
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1313
|
+
const n = nodes[i];
|
|
1314
|
+
if (n.type !== "html")
|
|
1315
|
+
continue;
|
|
1316
|
+
const open = htmlTagName(n.value);
|
|
1317
|
+
if (!open || !isHtmlMarkOpen(open, n.value))
|
|
1318
|
+
continue;
|
|
1319
|
+
let depth = 1, j = i + 1;
|
|
1320
|
+
for (; j < nodes.length; j++) {
|
|
1321
|
+
const t = nodes[j].type === "html" ? htmlTagName(nodes[j].value) : null;
|
|
1322
|
+
if (t && t.name === open.name)
|
|
1323
|
+
depth += t.close ? -1 : 1;
|
|
1324
|
+
if (depth === 0)
|
|
1325
|
+
break;
|
|
1326
|
+
}
|
|
1327
|
+
if (j >= nodes.length)
|
|
1328
|
+
continue;
|
|
1329
|
+
const start = _optionalChain([nodes, 'access', _50 => _50[i], 'access', _51 => _51.position, 'optionalAccess', _52 => _52.start, 'access', _53 => _53.offset]);
|
|
1330
|
+
const end = _optionalChain([nodes, 'access', _54 => _54[j], 'access', _55 => _55.position, 'optionalAccess', _56 => _56.end, 'access', _57 => _57.offset]);
|
|
1331
|
+
if (start === void 0 || end === void 0)
|
|
1332
|
+
continue;
|
|
1333
|
+
const leaves2 = inlineHtmlToLeaves(md.slice(start, end), carry);
|
|
1334
|
+
if (!leaves2)
|
|
1335
|
+
continue;
|
|
1336
|
+
const dropEmpty = (ns) => ns.filter((n2) => !(n2.text === "" && !_optionalChain([n2, 'access', _58 => _58.marks, 'optionalAccess', _59 => _59.length])));
|
|
1337
|
+
const out = [
|
|
1338
|
+
...dropEmpty(i > 0 ? phrasingToText(nodes.slice(0, i), carry, md) : []),
|
|
1339
|
+
...leaves2,
|
|
1340
|
+
...dropEmpty(j + 1 < nodes.length ? phrasingToText(nodes.slice(j + 1), carry, md) : [])
|
|
1341
|
+
];
|
|
1342
|
+
return out.length ? out : [{ text: "" }];
|
|
1343
|
+
}
|
|
1344
|
+
return null;
|
|
1345
|
+
}
|
|
1346
|
+
var nestedBreaks = /* @__PURE__ */ new WeakMap();
|
|
1347
|
+
function breakAttrs(n, md) {
|
|
1348
|
+
const at = n.position;
|
|
1349
|
+
const raw = _optionalChain([at, 'optionalAccess', _60 => _60.start, 'access', _61 => _61.offset]) !== void 0 && at.end.offset !== void 0 ? md.slice(at.start.offset, at.end.offset) : "";
|
|
1350
|
+
const m = /^( {2,}|\\)(\r?\n)?$/.exec(raw);
|
|
1351
|
+
const attrs = {};
|
|
1352
|
+
if (_optionalChain([m, 'optionalAccess', _62 => _62[1]]) !== "\\" && _optionalChain([m, 'optionalAccess', _63 => _63[1]]))
|
|
1353
|
+
attrs.trail = m[1];
|
|
1354
|
+
if (_optionalChain([m, 'optionalAccess', _64 => _64[2]]) === "\r\n")
|
|
1355
|
+
attrs.eol = "\r\n";
|
|
1356
|
+
return Object.keys(attrs).length ? attrs : void 0;
|
|
1357
|
+
}
|
|
1358
|
+
var hardBreak = (attrs) => attrs ? { type: "hard_break", attrs, children: [] } : { type: "hard_break", children: [] };
|
|
1359
|
+
var plusMark = (carry, name) => carry.some((m) => m.name === name) ? carry : carry.concat({ name });
|
|
1360
|
+
function phrasingToText(nodes, carry, md) {
|
|
1361
|
+
const stitched = stitchInlineHtml(nodes, carry, md);
|
|
1362
|
+
if (stitched)
|
|
1363
|
+
return stitched;
|
|
1364
|
+
const out = [];
|
|
1365
|
+
for (const n of nodes) {
|
|
1366
|
+
switch (n.type) {
|
|
1367
|
+
case "text":
|
|
1368
|
+
out.push(carry.length ? { text: n.value, marks: carry } : { text: n.value });
|
|
1369
|
+
break;
|
|
1370
|
+
case "inlineCode":
|
|
1371
|
+
out.push({ text: n.value, marks: carry.concat({ name: "code" }) });
|
|
1372
|
+
break;
|
|
1373
|
+
case "strong":
|
|
1374
|
+
out.push(...phrasingToText(n.children, plusMark(carry, "bold"), md));
|
|
1375
|
+
break;
|
|
1376
|
+
case "emphasis":
|
|
1377
|
+
out.push(...phrasingToText(n.children, plusMark(carry, "italic"), md));
|
|
1378
|
+
break;
|
|
1379
|
+
case "delete":
|
|
1380
|
+
out.push(...phrasingToText(n.children, plusMark(carry, "strike"), md));
|
|
1381
|
+
break;
|
|
1382
|
+
case "link":
|
|
1383
|
+
out.push(...phrasingToText(n.children, carry.concat({
|
|
1384
|
+
name: "link",
|
|
1385
|
+
attrs: n.title == null ? { url: n.url } : { url: n.url, title: n.title }
|
|
1386
|
+
}), md));
|
|
1387
|
+
break;
|
|
1388
|
+
case "break": {
|
|
1389
|
+
const leaf = { text: "\n", marks: carry };
|
|
1390
|
+
nestedBreaks.set(leaf, breakAttrs(n, md));
|
|
1391
|
+
out.push(leaf);
|
|
1392
|
+
break;
|
|
1393
|
+
}
|
|
1394
|
+
case "inlineMath": {
|
|
1395
|
+
const off = n.position;
|
|
1396
|
+
const raw = _optionalChain([off, 'optionalAccess', _65 => _65.start, 'access', _66 => _66.offset]) !== void 0 && off.end.offset !== void 0 ? md.slice(off.start.offset, off.end.offset) : _optionalChain([n, 'access', _67 => _67.data, 'optionalAccess', _68 => _68.display]) === "block" ? `$$${n.value}$$` : `$${n.value}$`;
|
|
1397
|
+
out.push({ text: raw, marks: carry.concat({ name: "html" }) });
|
|
1398
|
+
break;
|
|
1399
|
+
}
|
|
1400
|
+
case "html": {
|
|
1401
|
+
const img = imgHtmlToMark(n.value);
|
|
1402
|
+
if (img) {
|
|
1403
|
+
out.push({ text: img.alt, marks: carry.concat({ name: "image", attrs: img.attrs }) });
|
|
1404
|
+
break;
|
|
1405
|
+
}
|
|
1406
|
+
const parsed = inlineHtmlToLeaves(n.value, carry);
|
|
1407
|
+
if (parsed && parsed.length) {
|
|
1408
|
+
out.push(...parsed);
|
|
1409
|
+
break;
|
|
1410
|
+
}
|
|
1411
|
+
out.push({ text: n.value, marks: carry.concat({ name: "html" }) });
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
case "image":
|
|
1415
|
+
out.push({
|
|
1416
|
+
text: _nullishCoalesce(n.alt, () => ( "")),
|
|
1417
|
+
marks: carry.concat({
|
|
1418
|
+
name: "image",
|
|
1419
|
+
attrs: n.title == null ? { url: n.url } : { url: n.url, title: n.title }
|
|
1420
|
+
})
|
|
1421
|
+
});
|
|
1422
|
+
break;
|
|
1423
|
+
default: {
|
|
1424
|
+
const s = n.value;
|
|
1425
|
+
if (typeof s === "string") {
|
|
1426
|
+
out.push({ text: s, marks: carry });
|
|
1427
|
+
break;
|
|
1428
|
+
}
|
|
1429
|
+
const off = n.position;
|
|
1430
|
+
if (_optionalChain([off, 'optionalAccess', _69 => _69.start, 'access', _70 => _70.offset]) !== void 0 && off.end.offset !== void 0)
|
|
1431
|
+
out.push({ text: md.slice(off.start.offset, off.end.offset), marks: carry.concat({ name: "html" }) });
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
return out.length ? out : [{ text: "" }];
|
|
1436
|
+
}
|
|
1437
|
+
function phrasingToNodes(nodes, md) {
|
|
1438
|
+
const isSpanOpen = (n) => n.type === "html" && n.value.trimStart().toLowerCase().startsWith("<span") && (n.value.includes("data-mention") || n.value.includes("data-math"));
|
|
1439
|
+
const isSpanClose = (n) => n.type === "html" && n.value.trim().toLowerCase() === "</span>";
|
|
1440
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1441
|
+
if (!isSpanOpen(nodes[i]))
|
|
1442
|
+
continue;
|
|
1443
|
+
let j = i + 1;
|
|
1444
|
+
for (; j < nodes.length; j++)
|
|
1445
|
+
if (isSpanClose(nodes[j]))
|
|
1446
|
+
break;
|
|
1447
|
+
if (j >= nodes.length)
|
|
1448
|
+
continue;
|
|
1449
|
+
const open = nodes[i].value;
|
|
1450
|
+
const atom = open.includes("data-math") ? mathFromHtml(open) : mentionFromHtml(open);
|
|
1451
|
+
if (!atom)
|
|
1452
|
+
continue;
|
|
1453
|
+
const dropEmpty = (ns) => ns.filter((n) => !(_chunkPCSVBPORcjs.isText.call(void 0, n) && n.text === "" && !_optionalChain([n, 'access', _71 => _71.marks, 'optionalAccess', _72 => _72.length])));
|
|
1454
|
+
return [
|
|
1455
|
+
...i > 0 ? dropEmpty(phrasingToNodes(nodes.slice(0, i), md)) : [],
|
|
1456
|
+
atom,
|
|
1457
|
+
...j + 1 < nodes.length ? dropEmpty(phrasingToNodes(nodes.slice(j + 1), md)) : []
|
|
1458
|
+
];
|
|
1459
|
+
}
|
|
1460
|
+
return phrasingWithBreaks(nodes, md);
|
|
1461
|
+
}
|
|
1462
|
+
var isBrTag = (v) => /^<br\s*\/?>$/i.test(v.trim());
|
|
1463
|
+
function phrasingWithBreaks(nodes, md) {
|
|
1464
|
+
const out = [];
|
|
1465
|
+
let run = [];
|
|
1466
|
+
const flush = () => {
|
|
1467
|
+
if (!run.length)
|
|
1468
|
+
return;
|
|
1469
|
+
const leaves2 = phrasingToText(run, [], md);
|
|
1470
|
+
for (const l of leaves2) {
|
|
1471
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, l) && l.text === "" && !_optionalChain([l, 'access', _73 => _73.marks, 'optionalAccess', _74 => _74.length]) && out.length)
|
|
1472
|
+
continue;
|
|
1473
|
+
out.push(nestedBreaks.has(l) ? hardBreak(nestedBreaks.get(l)) : l);
|
|
1474
|
+
}
|
|
1475
|
+
run = [];
|
|
1476
|
+
};
|
|
1477
|
+
for (const n of nodes) {
|
|
1478
|
+
if (n.type === "break") {
|
|
1479
|
+
flush();
|
|
1480
|
+
out.push(hardBreak(breakAttrs(n, md)));
|
|
1481
|
+
} else if (n.type === "html" && isBrTag(n.value)) {
|
|
1482
|
+
flush();
|
|
1483
|
+
out.push({ type: "hard_break", attrs: { html: n.value }, children: [] });
|
|
1484
|
+
} else if (n.type === "inlineMath") {
|
|
1485
|
+
flush();
|
|
1486
|
+
out.push({ type: "math", attrs: { latex: n.value, display: _nullishCoalesce(_optionalChain([n, 'access', _75 => _75.data, 'optionalAccess', _76 => _76.display]), () => ( "inline")) }, children: [] });
|
|
1487
|
+
} else if (n.type === "footnoteReference") {
|
|
1488
|
+
flush();
|
|
1489
|
+
out.push({ type: "footnote_ref", attrs: { label: _nullishCoalesce(n.label, () => ( n.identifier)) }, children: [] });
|
|
1490
|
+
} else {
|
|
1491
|
+
run.push(n);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
flush();
|
|
1495
|
+
return out.length ? out : [{ text: "" }];
|
|
1496
|
+
}
|
|
1497
|
+
function emptyTaskBox(first, md) {
|
|
1498
|
+
const t = _optionalChain([first, 'optionalAccess', _77 => _77.type]) === "paragraph" && first.children.length === 1 && first.children[0].type === "text" ? first.children[0].value : "";
|
|
1499
|
+
const at = _optionalChain([first, 'optionalAccess', _78 => _78.position, 'optionalAccess', _79 => _79.start, 'access', _80 => _80.offset]);
|
|
1500
|
+
if (at != null && md[at] !== "[")
|
|
1501
|
+
return null;
|
|
1502
|
+
return t === "[ ]" ? false : t === "[x]" || t === "[X]" ? true : null;
|
|
1503
|
+
}
|
|
1504
|
+
function span(n) {
|
|
1505
|
+
return _optionalChain([n, 'access', _81 => _81.position, 'optionalAccess', _82 => _82.start, 'access', _83 => _83.offset]) !== void 0 && n.position.end.offset !== void 0 ? [n.position.start.offset, n.position.end.offset] : void 0;
|
|
1506
|
+
}
|
|
1507
|
+
function tableToAtom(t, md) {
|
|
1508
|
+
const rows = t.children;
|
|
1509
|
+
const cols = rows.reduce((m, r) => Math.max(m, r.children.length), 0);
|
|
1510
|
+
const children = [];
|
|
1511
|
+
for (const row of rows) {
|
|
1512
|
+
for (let c = 0; c < cols; c++) {
|
|
1513
|
+
const cell = row.children[c];
|
|
1514
|
+
const text = cell ? phrasingToNodes(cell.children, md) : [{ text: "" }];
|
|
1515
|
+
children.push({ type: "table_cell", children: leavesToParas(text) });
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
return { type: "table", attrs: { rows: rows.length, cols, span: span(t), align: _nullishCoalesce(t.align, () => ( void 0)) }, children };
|
|
1519
|
+
}
|
|
1520
|
+
function leavesToParas(leaves2) {
|
|
1521
|
+
const isBreak = (l) => _chunkPCSVBPORcjs.isText.call(void 0, l) ? l.text === "\n" || l.text === "<br>" && !!_optionalChain([l, 'access', _84 => _84.marks, 'optionalAccess', _85 => _85.some, 'call', _86 => _86((m) => m.name === "html")]) : l.type === "hard_break";
|
|
1522
|
+
const paras = [[]];
|
|
1523
|
+
for (const l of leaves2) {
|
|
1524
|
+
if (isBreak(l))
|
|
1525
|
+
paras.push([]);
|
|
1526
|
+
else
|
|
1527
|
+
paras[paras.length - 1].push(l);
|
|
1528
|
+
}
|
|
1529
|
+
return paras.map((text) => ({ type: "paragraph", children: text.length ? text : [{ text: "" }] }));
|
|
1530
|
+
}
|
|
1531
|
+
function simpleTagMark(tag) {
|
|
1532
|
+
switch (tag) {
|
|
1533
|
+
case "strong":
|
|
1534
|
+
case "b":
|
|
1535
|
+
return "bold";
|
|
1536
|
+
case "em":
|
|
1537
|
+
case "i":
|
|
1538
|
+
return "italic";
|
|
1539
|
+
case "del":
|
|
1540
|
+
case "s":
|
|
1541
|
+
return "strike";
|
|
1542
|
+
case "u":
|
|
1543
|
+
return "underline";
|
|
1544
|
+
case "mark":
|
|
1545
|
+
return "highlight";
|
|
1546
|
+
case "sup":
|
|
1547
|
+
return "superscript";
|
|
1548
|
+
case "sub":
|
|
1549
|
+
return "subscript";
|
|
1550
|
+
default:
|
|
1551
|
+
return null;
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
function simpleElementMark(el) {
|
|
1555
|
+
const name = simpleTagMark(el.tagName.toLowerCase());
|
|
1556
|
+
if (!name)
|
|
1557
|
+
return null;
|
|
1558
|
+
const color = name === "highlight" ? highlightColor(el.getAttribute("data-color")) : void 0;
|
|
1559
|
+
return color ? { name, attrs: { color } } : { name };
|
|
1560
|
+
}
|
|
1561
|
+
function withMark(marks, m) {
|
|
1562
|
+
if (m.name !== "textStyle")
|
|
1563
|
+
return marks.concat(m);
|
|
1564
|
+
const i = marks.findIndex((x) => x.name === "textStyle");
|
|
1565
|
+
if (i < 0)
|
|
1566
|
+
return marks.concat(m);
|
|
1567
|
+
const merged = { name: "textStyle", attrs: { ...marks[i].attrs, ...m.attrs } };
|
|
1568
|
+
return marks.map((x, k) => k === i ? merged : x);
|
|
1569
|
+
}
|
|
1570
|
+
function verticalAlignMark(style) {
|
|
1571
|
+
if (!style)
|
|
1572
|
+
return null;
|
|
1573
|
+
for (const part of style.split(";")) {
|
|
1574
|
+
const i = part.indexOf(":");
|
|
1575
|
+
if (i < 0 || part.slice(0, i).trim().toLowerCase() !== "vertical-align")
|
|
1576
|
+
continue;
|
|
1577
|
+
const v = part.slice(i + 1).trim().toLowerCase();
|
|
1578
|
+
return v === "super" ? "superscript" : v === "sub" ? "subscript" : null;
|
|
1579
|
+
}
|
|
1580
|
+
return null;
|
|
1581
|
+
}
|
|
1582
|
+
function spanStyleMarks(el, marks) {
|
|
1583
|
+
const style = el.getAttribute("style");
|
|
1584
|
+
let next = marks;
|
|
1585
|
+
const ts = styleToAttrs(style);
|
|
1586
|
+
if (ts || el.getAttribute("data-mark") === "textStyle")
|
|
1587
|
+
next = withMark(next, { name: "textStyle", attrs: _nullishCoalesce(ts, () => ( {})) });
|
|
1588
|
+
const va = verticalAlignMark(style);
|
|
1589
|
+
if (va && !next.some((m) => m.name === va))
|
|
1590
|
+
next = next.concat({ name: va });
|
|
1591
|
+
return next;
|
|
1592
|
+
}
|
|
1593
|
+
function linkMark(el) {
|
|
1594
|
+
const title = el.getAttribute("title");
|
|
1595
|
+
return { name: "link", attrs: title ? { url: _nullishCoalesce(el.getAttribute("href"), () => ( "")), title } : { url: _nullishCoalesce(el.getAttribute("href"), () => ( "")) } };
|
|
1596
|
+
}
|
|
1597
|
+
function inlineHtmlToLeaves(html, carry) {
|
|
1598
|
+
if (typeof DOMParser === "undefined")
|
|
1599
|
+
return null;
|
|
1600
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
1601
|
+
const out = [];
|
|
1602
|
+
let known = true;
|
|
1603
|
+
const walk = (node, marks) => {
|
|
1604
|
+
if (node.nodeType === 3) {
|
|
1605
|
+
const t = _nullishCoalesce(node.textContent, () => ( ""));
|
|
1606
|
+
if (t)
|
|
1607
|
+
out.push(marks.length ? { text: t, marks } : { text: t });
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1610
|
+
if (node.nodeType !== 1)
|
|
1611
|
+
return;
|
|
1612
|
+
const el = node;
|
|
1613
|
+
const tag = el.tagName.toLowerCase();
|
|
1614
|
+
const simple = simpleElementMark(el);
|
|
1615
|
+
if (simple) {
|
|
1616
|
+
el.childNodes.forEach((ch) => walk(ch, marks.concat(simple)));
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
if (tag === "code") {
|
|
1620
|
+
out.push({ text: _nullishCoalesce(el.textContent, () => ( "")), marks: marks.concat({ name: "code" }) });
|
|
1621
|
+
return;
|
|
1622
|
+
}
|
|
1623
|
+
if (tag === "a") {
|
|
1624
|
+
el.childNodes.forEach((ch) => walk(ch, marks.concat(linkMark(el))));
|
|
1625
|
+
return;
|
|
1626
|
+
}
|
|
1627
|
+
if (tag === "span") {
|
|
1628
|
+
const cid = el.getAttribute("data-comment");
|
|
1629
|
+
if (cid) {
|
|
1630
|
+
el.childNodes.forEach((ch) => walk(ch, marks.concat({ name: "comment", attrs: { id: cid } })));
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
const next = spanStyleMarks(el, marks);
|
|
1634
|
+
el.childNodes.forEach((ch) => walk(ch, next));
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
if (tag === "font") {
|
|
1638
|
+
const attrs = {};
|
|
1639
|
+
const color = el.getAttribute("color");
|
|
1640
|
+
const face = el.getAttribute("face");
|
|
1641
|
+
const size = el.getAttribute("size");
|
|
1642
|
+
if (color)
|
|
1643
|
+
attrs.color = color;
|
|
1644
|
+
if (face)
|
|
1645
|
+
attrs.fontFamily = face;
|
|
1646
|
+
if (size)
|
|
1647
|
+
attrs.fontSize = size;
|
|
1648
|
+
if (Object.keys(attrs).length) {
|
|
1649
|
+
el.childNodes.forEach((ch) => walk(ch, withMark(marks, { name: "textStyle", attrs })));
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
el.childNodes.forEach((ch) => walk(ch, marks));
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
known = false;
|
|
1656
|
+
};
|
|
1657
|
+
body.childNodes.forEach((ch) => walk(ch, carry));
|
|
1658
|
+
return known ? out : null;
|
|
1659
|
+
}
|
|
1660
|
+
function domInlineToNodes(root) {
|
|
1661
|
+
const out = [];
|
|
1662
|
+
const walk = (node, marks) => {
|
|
1663
|
+
if (node.nodeType === 3) {
|
|
1664
|
+
const t = _nullishCoalesce(node.textContent, () => ( ""));
|
|
1665
|
+
if (t)
|
|
1666
|
+
out.push(marks.length ? { text: t, marks } : { text: t });
|
|
1667
|
+
return;
|
|
1668
|
+
}
|
|
1669
|
+
if (node.nodeType !== 1)
|
|
1670
|
+
return;
|
|
1671
|
+
const el = node;
|
|
1672
|
+
const tag = el.tagName.toLowerCase();
|
|
1673
|
+
if (tag === "br") {
|
|
1674
|
+
out.push({ type: "hard_break", children: [] });
|
|
1675
|
+
return;
|
|
1676
|
+
}
|
|
1677
|
+
const add = (m) => marks.concat(m);
|
|
1678
|
+
let next = marks;
|
|
1679
|
+
const simple = simpleElementMark(el);
|
|
1680
|
+
if (simple)
|
|
1681
|
+
next = add(simple);
|
|
1682
|
+
else if (tag === "code") {
|
|
1683
|
+
out.push({ text: _nullishCoalesce(el.textContent, () => ( "")), marks: add({ name: "code" }) });
|
|
1684
|
+
return;
|
|
1685
|
+
} else if (tag === "a")
|
|
1686
|
+
next = add(linkMark(el));
|
|
1687
|
+
else if (tag === "img") {
|
|
1688
|
+
const attrs = { url: _nullishCoalesce(el.getAttribute("src"), () => ( "")) };
|
|
1689
|
+
const w = el.getAttribute("width");
|
|
1690
|
+
const h = el.getAttribute("height");
|
|
1691
|
+
if (w != null && w !== "") attrs.w = Number(w);
|
|
1692
|
+
if (h != null && h !== "") attrs.h = Number(h);
|
|
1693
|
+
out.push({ text: _nullishCoalesce(el.getAttribute("alt"), () => ( "")), marks: add({ name: "image", attrs }) });
|
|
1694
|
+
return;
|
|
1695
|
+
} else if (inlineAtomFromDom(el)) {
|
|
1696
|
+
out.push(inlineAtomFromDom(el));
|
|
1697
|
+
return;
|
|
1698
|
+
} else if (tag === "span") {
|
|
1699
|
+
const cid = el.getAttribute("data-comment");
|
|
1700
|
+
if (cid)
|
|
1701
|
+
next = add({ name: "comment", attrs: { id: cid } });
|
|
1702
|
+
else
|
|
1703
|
+
next = spanStyleMarks(el, marks);
|
|
1704
|
+
} else if (tag === "font") {
|
|
1705
|
+
const attrs = {};
|
|
1706
|
+
const color = el.getAttribute("color");
|
|
1707
|
+
const face = el.getAttribute("face");
|
|
1708
|
+
if (color) attrs.color = color;
|
|
1709
|
+
if (face) attrs.fontFamily = face;
|
|
1710
|
+
if (Object.keys(attrs).length)
|
|
1711
|
+
next = withMark(marks, { name: "textStyle", attrs });
|
|
1712
|
+
}
|
|
1713
|
+
el.childNodes.forEach((ch) => walk(ch, next));
|
|
1714
|
+
};
|
|
1715
|
+
root.childNodes.forEach((ch) => walk(ch, []));
|
|
1716
|
+
return out.length ? out : [{ text: "" }];
|
|
1717
|
+
}
|
|
1718
|
+
function inlineAtomFromDom(el) {
|
|
1719
|
+
if (el.tagName.toLowerCase() !== "span")
|
|
1720
|
+
return null;
|
|
1721
|
+
if (el.hasAttribute("data-math")) {
|
|
1722
|
+
const latex = _nullishCoalesce(_nullishCoalesce(el.getAttribute("data-latex"), () => ( el.textContent)), () => ( ""));
|
|
1723
|
+
const display = el.getAttribute("data-display") === "block" ? "block" : "inline";
|
|
1724
|
+
return { type: "math", attrs: { latex, display }, children: [] };
|
|
1725
|
+
}
|
|
1726
|
+
if (el.hasAttribute("data-mention")) {
|
|
1727
|
+
const id = _nullishCoalesce(el.getAttribute("data-id"), () => ( ""));
|
|
1728
|
+
const label = _nullishCoalesce(el.getAttribute("data-label"), () => ( id));
|
|
1729
|
+
const attrs = { id, label };
|
|
1730
|
+
const kind = el.getAttribute("data-kind");
|
|
1731
|
+
const date = el.getAttribute("data-date");
|
|
1732
|
+
if (kind) attrs.kind = kind;
|
|
1733
|
+
if (date) attrs.date = date;
|
|
1734
|
+
return { type: "mention", attrs, children: [] };
|
|
1735
|
+
}
|
|
1736
|
+
return null;
|
|
1737
|
+
}
|
|
1738
|
+
function inlineDomToLeaves(root, base = [], wrapper = false) {
|
|
1739
|
+
const out = [];
|
|
1740
|
+
const walk = (node, marks) => {
|
|
1741
|
+
if (node.nodeType === 3) {
|
|
1742
|
+
const t = _nullishCoalesce(node.textContent, () => ( ""));
|
|
1743
|
+
if (t)
|
|
1744
|
+
out.push(marks.length ? { text: t, marks } : { text: t });
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
if (node.nodeType !== 1)
|
|
1748
|
+
return;
|
|
1749
|
+
const el = node;
|
|
1750
|
+
const tag = el.tagName.toLowerCase();
|
|
1751
|
+
if (tag === "br") {
|
|
1752
|
+
out.push({ type: "hard_break", children: [] });
|
|
1753
|
+
return;
|
|
1754
|
+
}
|
|
1755
|
+
const atom = inlineAtomFromDom(el);
|
|
1756
|
+
if (atom) {
|
|
1757
|
+
out.push(atom);
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
const add = (m) => marks.concat(m);
|
|
1761
|
+
let next = marks;
|
|
1762
|
+
const simple = simpleElementMark(el);
|
|
1763
|
+
if (simple)
|
|
1764
|
+
next = add(simple);
|
|
1765
|
+
else if (tag === "code") {
|
|
1766
|
+
out.push({ text: _nullishCoalesce(el.textContent, () => ( "")), marks: add({ name: "code" }) });
|
|
1767
|
+
return;
|
|
1768
|
+
} else if (tag === "a")
|
|
1769
|
+
next = add(linkMark(el));
|
|
1770
|
+
else if (tag === "img") {
|
|
1771
|
+
const attrs = { url: _nullishCoalesce(el.getAttribute("src"), () => ( "")) };
|
|
1772
|
+
const w = el.getAttribute("width");
|
|
1773
|
+
const h = el.getAttribute("height");
|
|
1774
|
+
if (w != null && w !== "") attrs.w = Number(w);
|
|
1775
|
+
if (h != null && h !== "") attrs.h = Number(h);
|
|
1776
|
+
out.push({ text: _nullishCoalesce(el.getAttribute("alt"), () => ( "")), marks: add({ name: "image", attrs }) });
|
|
1777
|
+
return;
|
|
1778
|
+
} else if (tag === "ul" || tag === "ol" || tag === "li" || tag === "p")
|
|
1779
|
+
return;
|
|
1780
|
+
el.childNodes.forEach((ch) => walk(ch, next));
|
|
1781
|
+
};
|
|
1782
|
+
if (root.nodeType === 1 && !wrapper)
|
|
1783
|
+
root.childNodes.forEach((ch) => walk(ch, base));
|
|
1784
|
+
else
|
|
1785
|
+
walk(root, base);
|
|
1786
|
+
return out.length ? out : [{ text: "" }];
|
|
1787
|
+
}
|
|
1788
|
+
function listDomToNode(el) {
|
|
1789
|
+
const ordered = el.tagName.toLowerCase() === "ol";
|
|
1790
|
+
const startAttr = el.getAttribute("start");
|
|
1791
|
+
const attrs = { ordered };
|
|
1792
|
+
if (ordered && startAttr != null && startAttr !== "" && Number(startAttr) !== 1)
|
|
1793
|
+
attrs.start = Number(startAttr);
|
|
1794
|
+
const listType = ordered ? asListType(el.getAttribute("type")) : void 0;
|
|
1795
|
+
if (listType)
|
|
1796
|
+
attrs.type = listType;
|
|
1797
|
+
const items = [];
|
|
1798
|
+
for (const child of Array.from(el.children)) {
|
|
1799
|
+
if (child.tagName.toLowerCase() !== "li")
|
|
1800
|
+
continue;
|
|
1801
|
+
if (Array.from(child.children).some((el2) => /^(blockquote|pre|div|img|hr|figure)$/i.test(el2.tagName))) {
|
|
1802
|
+
const li = child.cloneNode(true);
|
|
1803
|
+
li.querySelectorAll(":scope > label").forEach((l) => l.querySelector('input[type="checkbox"]') && l.remove());
|
|
1804
|
+
const checked2 = child.getAttribute("data-checked");
|
|
1805
|
+
const kids = cellDomToParas(li);
|
|
1806
|
+
items.push({ type: "list_item", ...checked2 != null ? { attrs: { checked: checked2 === "true" } } : {}, children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
1807
|
+
continue;
|
|
1808
|
+
}
|
|
1809
|
+
const itemKids = [];
|
|
1810
|
+
let paraLeaves = [];
|
|
1811
|
+
const flushPara = () => {
|
|
1812
|
+
if (!paraLeaves.length)
|
|
1813
|
+
return;
|
|
1814
|
+
itemKids.push({ type: "paragraph", children: paraLeaves });
|
|
1815
|
+
paraLeaves = [];
|
|
1816
|
+
};
|
|
1817
|
+
for (const n of Array.from(child.childNodes)) {
|
|
1818
|
+
if (n.nodeType === 1) {
|
|
1819
|
+
const tag = n.tagName.toLowerCase();
|
|
1820
|
+
if (tag === "label" && n.querySelector('input[type="checkbox"]'))
|
|
1821
|
+
continue;
|
|
1822
|
+
if (tag === "ul" || tag === "ol") {
|
|
1823
|
+
flushPara();
|
|
1824
|
+
itemKids.push(listDomToNode(n));
|
|
1825
|
+
continue;
|
|
1826
|
+
}
|
|
1827
|
+
if (tag === "p") {
|
|
1828
|
+
flushPara();
|
|
1829
|
+
itemKids.push({ type: "paragraph", children: inlineDomToLeaves(n) });
|
|
1830
|
+
continue;
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
paraLeaves.push(...inlineDomToLeaves(n, [], true));
|
|
1834
|
+
}
|
|
1835
|
+
flushPara();
|
|
1836
|
+
if (!itemKids.length)
|
|
1837
|
+
itemKids.push({ type: "paragraph", children: [{ text: "" }] });
|
|
1838
|
+
const checked = child.getAttribute("data-checked");
|
|
1839
|
+
const itemAttrs = checked != null ? { checked: checked === "true" } : void 0;
|
|
1840
|
+
items.push({ type: "list_item", ...itemAttrs ? { attrs: itemAttrs } : {}, children: itemKids });
|
|
1841
|
+
}
|
|
1842
|
+
if (!items.length)
|
|
1843
|
+
items.push({ type: "list_item", children: [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
1844
|
+
return { type: "list", attrs, children: items };
|
|
1845
|
+
}
|
|
1846
|
+
function cellDomToParas(cell) {
|
|
1847
|
+
const hasStruct = Array.from(cell.children).some((el) => {
|
|
1848
|
+
const t = el.tagName.toLowerCase();
|
|
1849
|
+
return t === "ul" || t === "ol" || t === "p" || t === "blockquote" || t === "pre" || t === "div" || t === "img" || t === "hr" || t === "figure";
|
|
1850
|
+
});
|
|
1851
|
+
if (hasStruct) {
|
|
1852
|
+
const out = [];
|
|
1853
|
+
for (const n of Array.from(cell.childNodes)) {
|
|
1854
|
+
if (n.nodeType === 1) {
|
|
1855
|
+
const el = n;
|
|
1856
|
+
const tag = el.tagName.toLowerCase();
|
|
1857
|
+
if (tag === "ul" || tag === "ol") {
|
|
1858
|
+
out.push(listDomToNode(el));
|
|
1859
|
+
continue;
|
|
1860
|
+
}
|
|
1861
|
+
if (tag === "p") {
|
|
1862
|
+
out.push({ type: "paragraph", children: inlineDomToLeaves(el) });
|
|
1863
|
+
continue;
|
|
1864
|
+
}
|
|
1865
|
+
if (tag === "blockquote") {
|
|
1866
|
+
const kids = cellDomToParas(el);
|
|
1867
|
+
out.push({ type: "quote", children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
1868
|
+
continue;
|
|
1869
|
+
}
|
|
1870
|
+
if (tag === "div" && el.hasAttribute("data-callout")) {
|
|
1871
|
+
const variant = el.getAttribute("data-variant") || "info";
|
|
1872
|
+
const kids = cellDomToParas(el);
|
|
1873
|
+
out.push({ type: "quote", attrs: { variant }, children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
1874
|
+
continue;
|
|
1875
|
+
}
|
|
1876
|
+
if (tag === "div" && el.hasAttribute("data-embed")) {
|
|
1877
|
+
const emb = embedFromHtml(el.outerHTML);
|
|
1878
|
+
if (emb)
|
|
1879
|
+
out.push(emb);
|
|
1880
|
+
continue;
|
|
1881
|
+
}
|
|
1882
|
+
if (tag === "pre") {
|
|
1883
|
+
const code = el.querySelector("code");
|
|
1884
|
+
const text = _nullishCoalesce((_nullishCoalesce(code, () => ( el))).textContent, () => ( ""));
|
|
1885
|
+
out.push({ type: "code", children: [{ text }] });
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
if (tag === "img") {
|
|
1889
|
+
const attrs = { url: _nullishCoalesce(el.getAttribute("src"), () => ( "")), alt: _nullishCoalesce(el.getAttribute("alt"), () => ( "")) };
|
|
1890
|
+
const w = el.getAttribute("width");
|
|
1891
|
+
const h = el.getAttribute("height");
|
|
1892
|
+
if (w != null && w !== "") attrs.w = Number(w);
|
|
1893
|
+
if (h != null && h !== "") attrs.h = Number(h);
|
|
1894
|
+
out.push({ type: "image", attrs, children: [] });
|
|
1895
|
+
continue;
|
|
1896
|
+
}
|
|
1897
|
+
if (tag === "hr") {
|
|
1898
|
+
out.push({ type: "raw", attrs: { mdType: "thematicBreak" }, children: [{ text: "" }] });
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
if (n.nodeType === 3 && !(_nullishCoalesce(n.textContent, () => ( ""))).trim())
|
|
1903
|
+
continue;
|
|
1904
|
+
if (n.nodeType === 1 || n.nodeType === 3 && (_nullishCoalesce(n.textContent, () => ( "")))) {
|
|
1905
|
+
const leaves2 = inlineDomToLeaves(n);
|
|
1906
|
+
if (leaves2.some((l) => !_chunkPCSVBPORcjs.isText.call(void 0, l) || l.text))
|
|
1907
|
+
out.push({ type: "paragraph", children: leaves2 });
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
return out.length ? out : [{ type: "paragraph", children: [{ text: "" }] }];
|
|
1911
|
+
}
|
|
1912
|
+
const paras = [[]];
|
|
1913
|
+
const walk = (node, marks) => {
|
|
1914
|
+
if (node.nodeType === 3) {
|
|
1915
|
+
const t = _nullishCoalesce(node.textContent, () => ( ""));
|
|
1916
|
+
if (t)
|
|
1917
|
+
paras[paras.length - 1].push(marks.length ? { text: t, marks } : { text: t });
|
|
1918
|
+
return;
|
|
1919
|
+
}
|
|
1920
|
+
if (node.nodeType !== 1)
|
|
1921
|
+
return;
|
|
1922
|
+
const el = node;
|
|
1923
|
+
const tag = el.tagName.toLowerCase();
|
|
1924
|
+
if (tag === "br") {
|
|
1925
|
+
paras.push([]);
|
|
1926
|
+
return;
|
|
1927
|
+
}
|
|
1928
|
+
const add = (m) => marks.concat(m);
|
|
1929
|
+
let next = marks;
|
|
1930
|
+
const simple = simpleElementMark(el);
|
|
1931
|
+
if (simple)
|
|
1932
|
+
next = add(simple);
|
|
1933
|
+
else if (tag === "code") {
|
|
1934
|
+
paras[paras.length - 1].push({ text: _nullishCoalesce(el.textContent, () => ( "")), marks: add({ name: "code" }) });
|
|
1935
|
+
return;
|
|
1936
|
+
} else if (tag === "a")
|
|
1937
|
+
next = add(linkMark(el));
|
|
1938
|
+
else if (tag === "img") {
|
|
1939
|
+
const attrs = { url: _nullishCoalesce(el.getAttribute("src"), () => ( "")) };
|
|
1940
|
+
const w = el.getAttribute("width");
|
|
1941
|
+
const h = el.getAttribute("height");
|
|
1942
|
+
if (w != null && w !== "") attrs.w = Number(w);
|
|
1943
|
+
if (h != null && h !== "") attrs.h = Number(h);
|
|
1944
|
+
paras[paras.length - 1].push({ text: _nullishCoalesce(el.getAttribute("alt"), () => ( "")), marks: add({ name: "image", attrs }) });
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
el.childNodes.forEach((ch) => walk(ch, next));
|
|
1948
|
+
};
|
|
1949
|
+
cell.childNodes.forEach((ch) => walk(ch, []));
|
|
1950
|
+
return paras.map((text) => ({ type: "paragraph", children: text.length ? text : [{ text: "" }] }));
|
|
1951
|
+
}
|
|
1952
|
+
function parseCellStyle(style) {
|
|
1953
|
+
const out = {};
|
|
1954
|
+
for (const decl of style.split(";")) {
|
|
1955
|
+
const i = decl.indexOf(":");
|
|
1956
|
+
if (i < 0)
|
|
1957
|
+
continue;
|
|
1958
|
+
const prop = decl.slice(0, i).trim();
|
|
1959
|
+
const val = decl.slice(i + 1).trim();
|
|
1960
|
+
if (prop === "background" || prop === "background-color")
|
|
1961
|
+
out.bg = val;
|
|
1962
|
+
else if (prop === "border-color")
|
|
1963
|
+
out.border = val;
|
|
1964
|
+
}
|
|
1965
|
+
return out;
|
|
1966
|
+
}
|
|
1967
|
+
function pxOf(v) {
|
|
1968
|
+
if (!v || v.includes("%"))
|
|
1969
|
+
return void 0;
|
|
1970
|
+
const n = Number.parseFloat(v);
|
|
1971
|
+
return Number.isFinite(n) && n > 0 ? Math.round(n) : void 0;
|
|
1972
|
+
}
|
|
1973
|
+
function styleWidthPx(style) {
|
|
1974
|
+
for (const part of (_nullishCoalesce(style, () => ( ""))).split(";")) {
|
|
1975
|
+
const i = part.indexOf(":");
|
|
1976
|
+
if (i >= 0 && part.slice(0, i).trim().toLowerCase() === "width")
|
|
1977
|
+
return pxOf(part.slice(i + 1).trim());
|
|
1978
|
+
}
|
|
1979
|
+
return void 0;
|
|
1980
|
+
}
|
|
1981
|
+
function htmlTableToAtom(html, s) {
|
|
1982
|
+
if (typeof DOMParser === "undefined")
|
|
1983
|
+
return null;
|
|
1984
|
+
const root = new DOMParser().parseFromString(html, "text/html").body;
|
|
1985
|
+
collapseWhitespace(root);
|
|
1986
|
+
const table = root.querySelector("table");
|
|
1987
|
+
if (!table)
|
|
1988
|
+
return null;
|
|
1989
|
+
const trs = Array.from(table.querySelectorAll("tr"));
|
|
1990
|
+
const cellsByRow = trs.map((tr) => Array.from(tr.children).filter((el) => el.tagName === "TD" || el.tagName === "TH"));
|
|
1991
|
+
const owner = /* @__PURE__ */ new Set();
|
|
1992
|
+
const placed = /* @__PURE__ */ new Map();
|
|
1993
|
+
const widths = [];
|
|
1994
|
+
let rows = 0;
|
|
1995
|
+
let cols = 0;
|
|
1996
|
+
const cs = (c) => Math.max(1, Number(_nullishCoalesce(c.getAttribute("colspan"), () => ( 1))) || 1);
|
|
1997
|
+
const rs = (c) => Math.max(1, Number(_nullishCoalesce(c.getAttribute("rowspan"), () => ( 1))) || 1);
|
|
1998
|
+
cellsByRow.forEach((row, r) => {
|
|
1999
|
+
let c = 0;
|
|
2000
|
+
for (const cell of row) {
|
|
2001
|
+
while (owner.has(`${r},${c}`)) c++;
|
|
2002
|
+
const colspan = cs(cell);
|
|
2003
|
+
const rowspan = rs(cell);
|
|
2004
|
+
for (let dr = 0; dr < rowspan; dr++) for (let dc = 0; dc < colspan; dc++) owner.add(`${r + dr},${c + dc}`);
|
|
2005
|
+
const style = parseCellStyle(_nullishCoalesce(cell.getAttribute("style"), () => ( "")));
|
|
2006
|
+
const attrs2 = {};
|
|
2007
|
+
if (colspan > 1) attrs2.colspan = colspan;
|
|
2008
|
+
if (rowspan > 1) attrs2.rowspan = rowspan;
|
|
2009
|
+
if (style.bg) attrs2.bg = style.bg;
|
|
2010
|
+
if (style.border) attrs2.border = style.border;
|
|
2011
|
+
placed.set(`${r},${c}`, { cell, node: { type: "table_cell", ...Object.keys(attrs2).length ? { attrs: attrs2 } : {}, children: cellDomToParas(cell) } });
|
|
2012
|
+
const cw = (_nullishCoalesce(cell.getAttribute("colwidth"), () => ( ""))).split(",");
|
|
2013
|
+
for (let k = 0; k < Math.min(cw.length, colspan); k++) {
|
|
2014
|
+
const w = pxOf(cw[k]);
|
|
2015
|
+
if (w !== void 0) widths[c + k] = w;
|
|
2016
|
+
}
|
|
2017
|
+
rows = Math.max(rows, r + rowspan);
|
|
2018
|
+
cols = Math.max(cols, c + colspan);
|
|
2019
|
+
c += colspan;
|
|
2020
|
+
}
|
|
2021
|
+
});
|
|
2022
|
+
let colIdx = 0;
|
|
2023
|
+
for (const group of Array.from(table.children).filter((el) => el.tagName === "COLGROUP")) {
|
|
2024
|
+
for (const col of Array.from(group.children).filter((el) => el.tagName === "COL")) {
|
|
2025
|
+
const span2 = Math.max(1, Number(_nullishCoalesce(col.getAttribute("span"), () => ( 1))) || 1);
|
|
2026
|
+
const w = _nullishCoalesce(pxOf(col.getAttribute("width")), () => ( styleWidthPx(col.getAttribute("style"))));
|
|
2027
|
+
for (let k = 0; k < span2; k++, colIdx++)
|
|
2028
|
+
if (w !== void 0 && widths[colIdx] === void 0) widths[colIdx] = w;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
const children = [];
|
|
2032
|
+
for (let r = 0; r < rows; r++) {
|
|
2033
|
+
for (let c = 0; c < cols; c++) {
|
|
2034
|
+
const hit = placed.get(`${r},${c}`);
|
|
2035
|
+
if (hit)
|
|
2036
|
+
children.push(hit.node);
|
|
2037
|
+
else if (!owner.has(`${r},${c}`))
|
|
2038
|
+
children.push({ type: "table_cell", children: [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
const attrs = s ? { rows, cols, span: s } : { rows, cols };
|
|
2042
|
+
if (widths.some((w) => w !== void 0))
|
|
2043
|
+
attrs.colWidths = Array.from({ length: cols }, (_, c) => _nullishCoalesce(widths[c], () => ( DEFAULT_COL_WIDTH)));
|
|
2044
|
+
const fr = Number(_nullishCoalesce(table.getAttribute("data-freeze-rows"), () => ( 0)));
|
|
2045
|
+
const fc = Number(_nullishCoalesce(table.getAttribute("data-freeze-cols"), () => ( 0)));
|
|
2046
|
+
if (fr > 0) attrs.freezeRows = fr;
|
|
2047
|
+
if (fc > 0) attrs.freezeCols = fc;
|
|
2048
|
+
const col0 = Array.from({ length: Math.max(0, rows - 1) }, (_, i) => placed.get(`${i + 1},0`)).filter(Boolean);
|
|
2049
|
+
if (col0.length > 0 && col0.every((p) => p.cell.tagName === "TH")) attrs.headerCol = true;
|
|
2050
|
+
return { type: "table", attrs, children };
|
|
2051
|
+
}
|
|
2052
|
+
function paragraphToBlocks(children, md, s) {
|
|
2053
|
+
if (children.length === 1 && children[0].type === "text" && children[0].value === "\xA0")
|
|
2054
|
+
return [{ type: "paragraph", ...s ? { attrs: { span: s } } : {}, children: [{ text: "" }] }];
|
|
2055
|
+
const imgAttrs = (n) => {
|
|
2056
|
+
if (n.type === "image")
|
|
2057
|
+
return n.title == null ? { url: n.url, alt: _nullishCoalesce(n.alt, () => ( "")) } : { url: n.url, alt: _nullishCoalesce(n.alt, () => ( "")), title: n.title };
|
|
2058
|
+
if (n.type === "html" && n.value.trimStart().toLowerCase().startsWith("<img")) {
|
|
2059
|
+
const img = imgHtmlToMark(n.value);
|
|
2060
|
+
if (img)
|
|
2061
|
+
return { ...img.attrs, alt: img.alt };
|
|
2062
|
+
}
|
|
2063
|
+
return null;
|
|
2064
|
+
};
|
|
2065
|
+
const hasImg = children.some((c) => imgAttrs(c));
|
|
2066
|
+
if (!hasImg) {
|
|
2067
|
+
const para = { type: "paragraph", children: phrasingToNodes(children, md) };
|
|
2068
|
+
return [s ? { ...para, attrs: { span: s } } : para];
|
|
2069
|
+
}
|
|
2070
|
+
const out = [];
|
|
2071
|
+
let run = [];
|
|
2072
|
+
const trimmed = (nodes) => {
|
|
2073
|
+
const r = nodes.slice();
|
|
2074
|
+
const head = r[0];
|
|
2075
|
+
if (_optionalChain([head, 'optionalAccess', _87 => _87.type]) === "text")
|
|
2076
|
+
r[0] = { ...head, value: head.value.trimStart() };
|
|
2077
|
+
const tail = r[r.length - 1];
|
|
2078
|
+
if (_optionalChain([tail, 'optionalAccess', _88 => _88.type]) === "text")
|
|
2079
|
+
r[r.length - 1] = { ...tail, value: tail.value.trimEnd() };
|
|
2080
|
+
return r.filter((n) => n.type !== "text" || n.value !== "");
|
|
2081
|
+
};
|
|
2082
|
+
const flush = () => {
|
|
2083
|
+
const kept = trimmed(run);
|
|
2084
|
+
run = [];
|
|
2085
|
+
if (kept.length)
|
|
2086
|
+
out.push({ type: "paragraph", children: phrasingToNodes(kept, md) });
|
|
2087
|
+
};
|
|
2088
|
+
for (const c of children) {
|
|
2089
|
+
const attrs = imgAttrs(c);
|
|
2090
|
+
if (attrs) {
|
|
2091
|
+
flush();
|
|
2092
|
+
out.push({ type: "image", attrs, children: [] });
|
|
2093
|
+
} else {
|
|
2094
|
+
run.push(c);
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
flush();
|
|
2098
|
+
if (s && out.length)
|
|
2099
|
+
out[0] = { ...out[0], attrs: { ...out[0].attrs, span: s } };
|
|
2100
|
+
return out;
|
|
2101
|
+
}
|
|
2102
|
+
function alignBlockFromHtml(html) {
|
|
2103
|
+
if (typeof DOMParser === "undefined")
|
|
2104
|
+
return null;
|
|
2105
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
2106
|
+
if (body.children.length !== 1)
|
|
2107
|
+
return null;
|
|
2108
|
+
const el = body.children[0];
|
|
2109
|
+
const tag = el.tagName.toLowerCase();
|
|
2110
|
+
const isP = tag === "p";
|
|
2111
|
+
const isH = tag.length === 2 && tag[0] === "h" && tag[1] >= "1" && tag[1] <= "6";
|
|
2112
|
+
if (!isP && !isH)
|
|
2113
|
+
return null;
|
|
2114
|
+
const align = asAlign(_optionalChain([el, 'access', _89 => _89.getAttribute, 'call', _90 => _90("style"), 'optionalAccess', _91 => _91.split, 'call', _92 => _92(";"), 'access', _93 => _93.map, 'call', _94 => _94((s) => s.trim()), 'access', _95 => _95.find, 'call', _96 => _96((s) => s.startsWith("text-align:")), 'optionalAccess', _97 => _97.slice, 'call', _98 => _98("text-align:".length), 'access', _99 => _99.trim, 'call', _100 => _100()]));
|
|
2115
|
+
if (!align)
|
|
2116
|
+
return null;
|
|
2117
|
+
const leaves2 = inlineHtmlToLeaves(el.innerHTML, []);
|
|
2118
|
+
if (!leaves2)
|
|
2119
|
+
return null;
|
|
2120
|
+
const children = leaves2.length ? leaves2 : [{ text: "" }];
|
|
2121
|
+
return isH ? { type: "heading", attrs: { depth: Number(tag[1]), align }, children } : { type: "paragraph", attrs: { align }, children };
|
|
2122
|
+
}
|
|
2123
|
+
var childBlocks = (kids, md) => drain(stitchG(kids, md));
|
|
2124
|
+
function blockToNode(b, md) {
|
|
2125
|
+
const s = span(b);
|
|
2126
|
+
const withSpan2 = (n) => s ? { ...n, attrs: { ...n.attrs, span: s } } : n;
|
|
2127
|
+
switch (b.type) {
|
|
2128
|
+
case "heading":
|
|
2129
|
+
return withSpan2({ type: "heading", attrs: { depth: b.depth }, children: phrasingToNodes(b.children, md) });
|
|
2130
|
+
case "paragraph":
|
|
2131
|
+
return paragraphToBlocks(b.children, md, s);
|
|
2132
|
+
case "blockquote": {
|
|
2133
|
+
const kids = childBlocks(b.children, md);
|
|
2134
|
+
return withSpan2({ type: "quote", children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
2135
|
+
}
|
|
2136
|
+
case "list":
|
|
2137
|
+
return withSpan2({
|
|
2138
|
+
type: "list",
|
|
2139
|
+
attrs: { ordered: !!b.ordered, ...b.start == null ? {} : { start: b.start }, ...b.spread ? { spread: true } : {} },
|
|
2140
|
+
children: childBlocks(b.children, md)
|
|
2141
|
+
});
|
|
2142
|
+
case "listItem": {
|
|
2143
|
+
const kids = childBlocks(b.children, md);
|
|
2144
|
+
const box = b.checked == null ? emptyTaskBox(b.children[0], md) : null;
|
|
2145
|
+
if (box != null && kids[1] && kids[1].type !== "paragraph")
|
|
2146
|
+
kids.shift();
|
|
2147
|
+
else if (box != null)
|
|
2148
|
+
kids[0] = { type: "paragraph", children: [{ text: "" }] };
|
|
2149
|
+
const checked = _nullishCoalesce(box, () => ( b.checked));
|
|
2150
|
+
return withSpan2({
|
|
2151
|
+
type: "list_item",
|
|
2152
|
+
...checked == null ? {} : { attrs: { checked } },
|
|
2153
|
+
children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }]
|
|
2154
|
+
});
|
|
2155
|
+
}
|
|
2156
|
+
case "footnoteDefinition": {
|
|
2157
|
+
const kids = childBlocks(b.children, md);
|
|
2158
|
+
return withSpan2({
|
|
2159
|
+
type: "quote",
|
|
2160
|
+
attrs: { footnote: _nullishCoalesce(b.label, () => ( b.identifier)) },
|
|
2161
|
+
children: kids.length ? kids : [{ type: "paragraph", children: [{ text: "" }] }]
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
case "code":
|
|
2165
|
+
return withSpan2({
|
|
2166
|
+
type: "code",
|
|
2167
|
+
...b.lang == null && b.meta == null ? {} : { attrs: { ...b.lang == null ? {} : { lang: b.lang }, ...b.meta == null ? {} : { meta: b.meta } } },
|
|
2168
|
+
children: [{ text: b.value }]
|
|
2169
|
+
});
|
|
2170
|
+
case "table":
|
|
2171
|
+
return tableToAtom(b, md);
|
|
2172
|
+
case "html": {
|
|
2173
|
+
const trimmed = b.value.trimStart().toLowerCase();
|
|
2174
|
+
if (trimmed.startsWith("<table")) {
|
|
2175
|
+
const atom = htmlTableToAtom(b.value, s);
|
|
2176
|
+
if (atom)
|
|
2177
|
+
return atom;
|
|
2178
|
+
}
|
|
2179
|
+
if (trimmed.startsWith("<figure")) {
|
|
2180
|
+
const fig = figureFromHtml(b.value);
|
|
2181
|
+
if (fig)
|
|
2182
|
+
return withSpan2(fig);
|
|
2183
|
+
}
|
|
2184
|
+
if (trimmed.startsWith("<img")) {
|
|
2185
|
+
const img = imgHtmlToMark(b.value);
|
|
2186
|
+
if (img)
|
|
2187
|
+
return withSpan2({ type: "image", attrs: { ...img.attrs, alt: img.alt }, children: [] });
|
|
2188
|
+
}
|
|
2189
|
+
if (trimmed.startsWith("<ol")) {
|
|
2190
|
+
const list = typedListFromHtml(b.value);
|
|
2191
|
+
if (list)
|
|
2192
|
+
return withSpan2(list);
|
|
2193
|
+
}
|
|
2194
|
+
if (trimmed.startsWith("<div") && b.value.includes("data-embed")) {
|
|
2195
|
+
const emb = embedFromHtml(b.value);
|
|
2196
|
+
if (emb)
|
|
2197
|
+
return withSpan2(emb);
|
|
2198
|
+
}
|
|
2199
|
+
if (trimmed.startsWith("<span") && b.value.includes("data-math")) {
|
|
2200
|
+
const m = mathFromHtml(b.value);
|
|
2201
|
+
if (m)
|
|
2202
|
+
return withSpan2({ type: "paragraph", children: [m] });
|
|
2203
|
+
}
|
|
2204
|
+
if (trimmed.startsWith("<p") || trimmed.length >= 2 && trimmed[0] === "<" && trimmed[1] === "h") {
|
|
2205
|
+
const aligned = alignBlockFromHtml(b.value);
|
|
2206
|
+
if (aligned)
|
|
2207
|
+
return withSpan2(aligned);
|
|
2208
|
+
}
|
|
2209
|
+
return withSpan2({ type: "raw", attrs: { mdType: b.type }, children: [{ text: b.value }] });
|
|
2210
|
+
}
|
|
2211
|
+
default:
|
|
2212
|
+
return withSpan2({ type: "raw", attrs: { mdType: b.type, ...b.type === "definition" ? { label: _nullishCoalesce(b.label, () => ( b.identifier)), url: b.url, ...b.title != null ? { title: b.title } : {} } : {} }, children: [{ text: "value" in b && typeof b.value === "string" ? b.value : "" }] });
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
var CHUNK_BYTES = 16 * 1024;
|
|
2216
|
+
var CHUNK_MIN = 4 * 1024;
|
|
2217
|
+
function shiftPositions(node, dOff, dLine) {
|
|
2218
|
+
const p = node.position;
|
|
2219
|
+
if (p) {
|
|
2220
|
+
if (p.start.offset !== void 0)
|
|
2221
|
+
p.start.offset += dOff;
|
|
2222
|
+
if (p.end.offset !== void 0)
|
|
2223
|
+
p.end.offset += dOff;
|
|
2224
|
+
p.start.line += dLine;
|
|
2225
|
+
p.end.line += dLine;
|
|
2226
|
+
}
|
|
2227
|
+
if (node.children)
|
|
2228
|
+
for (const c of node.children)
|
|
2229
|
+
shiftPositions(c, dOff, dLine);
|
|
2230
|
+
}
|
|
2231
|
+
var STITCH = { phase: "stitch" };
|
|
2232
|
+
function* parseBlocksG(md, chunkBytes) {
|
|
2233
|
+
const fm = frontMatterEnd(md);
|
|
2234
|
+
if (fm < 0)
|
|
2235
|
+
return yield* parseBodyG(md, chunkBytes, 0, 0, []);
|
|
2236
|
+
let lines = 0;
|
|
2237
|
+
for (let at = md.indexOf("\n"); at >= 0 && at < fm; at = md.indexOf("\n", at + 1))
|
|
2238
|
+
lines++;
|
|
2239
|
+
const head = { type: FRONT_MATTER, value: md.slice(0, fm), position: { start: { line: 1, column: 1, offset: 0 }, end: { line: lines + 1, column: 1, offset: fm } } };
|
|
2240
|
+
return yield* parseBodyG(md.slice(fm), chunkBytes, fm, lines, [head]);
|
|
2241
|
+
}
|
|
2242
|
+
function* parseBodyG(md, chunkBytes, dOff, dLine, out) {
|
|
2243
|
+
const starts = md.length >= chunkBytes ? chunkStarts(md, Math.min(CHUNK_MIN, chunkBytes)) : null;
|
|
2244
|
+
if (!starts) {
|
|
2245
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, md, parseExt()).children;
|
|
2246
|
+
if (dOff || dLine)
|
|
2247
|
+
for (const k of kids)
|
|
2248
|
+
shiftPositions(k, dOff, dLine);
|
|
2249
|
+
out.push(...kids);
|
|
2250
|
+
return out;
|
|
2251
|
+
}
|
|
2252
|
+
let line = 0;
|
|
2253
|
+
for (let i = 0; i < starts.length; i++) {
|
|
2254
|
+
const text = md.slice(starts[i], _nullishCoalesce(starts[i + 1], () => ( md.length)));
|
|
2255
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, text, parseExt()).children;
|
|
2256
|
+
for (const k of kids)
|
|
2257
|
+
shiftPositions(k, starts[i] + dOff, line + dLine);
|
|
2258
|
+
out.push(...kids);
|
|
2259
|
+
for (let at = text.indexOf("\n"); at >= 0; at = text.indexOf("\n", at + 1))
|
|
2260
|
+
line++;
|
|
2261
|
+
yield { phase: "parse", blocks: out };
|
|
2262
|
+
}
|
|
2263
|
+
return out;
|
|
2264
|
+
}
|
|
2265
|
+
function drain(g) {
|
|
2266
|
+
for (; ; ) {
|
|
2267
|
+
const r = g.next();
|
|
2268
|
+
if (r.done)
|
|
2269
|
+
return r.value;
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
function* docG(md, chunkBytes) {
|
|
2273
|
+
const blocks = yield* parseBlocksG(md, chunkBytes);
|
|
2274
|
+
const kids = yield* stitchG(blocks, md, true);
|
|
2275
|
+
return { type: "doc", children: kids.map((b, i) => ({ ...b, id: `b${i}` })) };
|
|
2276
|
+
}
|
|
2277
|
+
async function mdToDocAsync(md, opts = {}) {
|
|
2278
|
+
const { chunkBytes = CHUNK_BYTES, budgetMs = 6, headBlocks = 200, onHead, signal } = opts;
|
|
2279
|
+
const aborted = () => Object.assign(new Error("mdToDocAsync aborted"), { name: "AbortError" });
|
|
2280
|
+
const g = docG(md, chunkBytes);
|
|
2281
|
+
let headSent = !onHead;
|
|
2282
|
+
let sliceStart = performance.now();
|
|
2283
|
+
for (; ; ) {
|
|
2284
|
+
if (_optionalChain([signal, 'optionalAccess', _101 => _101.aborted]))
|
|
2285
|
+
throw aborted();
|
|
2286
|
+
const r = g.next();
|
|
2287
|
+
if (r.done)
|
|
2288
|
+
return r.value;
|
|
2289
|
+
if (!headSent && r.value.phase === "parse" && r.value.blocks.length >= headBlocks) {
|
|
2290
|
+
headSent = true;
|
|
2291
|
+
onHead({ type: "doc", children: drain(stitchG(r.value.blocks.slice(), md, true)).map((b, i) => ({ ...b, id: `b${i}` })) });
|
|
2292
|
+
}
|
|
2293
|
+
if (performance.now() - sliceStart >= budgetMs) {
|
|
2294
|
+
await yieldToMain();
|
|
2295
|
+
sliceStart = performance.now();
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
function mdToDoc(md, chunkBytes = CHUNK_BYTES) {
|
|
2300
|
+
return drain(docG(md, chunkBytes));
|
|
2301
|
+
}
|
|
2302
|
+
function langFromClass(cls) {
|
|
2303
|
+
if (!cls)
|
|
2304
|
+
return void 0;
|
|
2305
|
+
for (const part of cls.split(" ")) {
|
|
2306
|
+
if (part.startsWith("language-") && part.length > 9)
|
|
2307
|
+
return part.slice(9);
|
|
2308
|
+
if (part.startsWith("lang-") && part.length > 5)
|
|
2309
|
+
return part.slice(5);
|
|
2310
|
+
}
|
|
2311
|
+
return void 0;
|
|
2312
|
+
}
|
|
2313
|
+
function styleAlign(el) {
|
|
2314
|
+
const align = _optionalChain([el, 'access', _102 => _102.getAttribute, 'call', _103 => _103("style"), 'optionalAccess', _104 => _104.split, 'call', _105 => _105(";"), 'access', _106 => _106.map, 'call', _107 => _107((s) => s.trim()), 'access', _108 => _108.find, 'call', _109 => _109((s) => s.startsWith("text-align:")), 'optionalAccess', _110 => _110.slice, 'call', _111 => _111("text-align:".length), 'access', _112 => _112.trim, 'call', _113 => _113()]);
|
|
2315
|
+
return align === "center" || align === "right" || align === "justify" ? align : void 0;
|
|
2316
|
+
}
|
|
2317
|
+
function elevateInlineImages(block) {
|
|
2318
|
+
const out = [];
|
|
2319
|
+
let buf = [];
|
|
2320
|
+
const flush = () => {
|
|
2321
|
+
if (!buf.length)
|
|
2322
|
+
return;
|
|
2323
|
+
out.push({ ...block, children: buf });
|
|
2324
|
+
buf = [];
|
|
2325
|
+
};
|
|
2326
|
+
for (const c of block.children) {
|
|
2327
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, c)) {
|
|
2328
|
+
const im = _optionalChain([c, 'access', _114 => _114.marks, 'optionalAccess', _115 => _115.find, 'call', _116 => _116((m) => m.name === "image")]);
|
|
2329
|
+
if (im) {
|
|
2330
|
+
flush();
|
|
2331
|
+
out.push({ type: "image", attrs: { ...im.attrs, alt: c.text }, children: [] });
|
|
2332
|
+
continue;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
buf.push(c);
|
|
2336
|
+
}
|
|
2337
|
+
flush();
|
|
2338
|
+
return out.length ? out : [{ ...block, children: [{ text: "" }] }];
|
|
2339
|
+
}
|
|
2340
|
+
var BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
2341
|
+
"address",
|
|
2342
|
+
"article",
|
|
2343
|
+
"aside",
|
|
2344
|
+
"blockquote",
|
|
2345
|
+
"body",
|
|
2346
|
+
"caption",
|
|
2347
|
+
"center",
|
|
2348
|
+
"dd",
|
|
2349
|
+
"details",
|
|
2350
|
+
"div",
|
|
2351
|
+
"dl",
|
|
2352
|
+
"dt",
|
|
2353
|
+
"fieldset",
|
|
2354
|
+
"figcaption",
|
|
2355
|
+
"figure",
|
|
2356
|
+
"footer",
|
|
2357
|
+
"form",
|
|
2358
|
+
"h1",
|
|
2359
|
+
"h2",
|
|
2360
|
+
"h3",
|
|
2361
|
+
"h4",
|
|
2362
|
+
"h5",
|
|
2363
|
+
"h6",
|
|
2364
|
+
"header",
|
|
2365
|
+
"hr",
|
|
2366
|
+
"li",
|
|
2367
|
+
"main",
|
|
2368
|
+
"nav",
|
|
2369
|
+
"ol",
|
|
2370
|
+
"p",
|
|
2371
|
+
"pre",
|
|
2372
|
+
"section",
|
|
2373
|
+
"summary",
|
|
2374
|
+
"table",
|
|
2375
|
+
"tbody",
|
|
2376
|
+
"td",
|
|
2377
|
+
"tfoot",
|
|
2378
|
+
"th",
|
|
2379
|
+
"thead",
|
|
2380
|
+
"tr",
|
|
2381
|
+
"ul",
|
|
2382
|
+
"colgroup",
|
|
2383
|
+
"col"
|
|
2384
|
+
]);
|
|
2385
|
+
var SKIP_TAGS = /* @__PURE__ */ new Set(["script", "style", "head", "title", "meta", "link", "template"]);
|
|
2386
|
+
var isHtmlSpace = (ch) => ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "\f";
|
|
2387
|
+
function keepsWhitespace(el) {
|
|
2388
|
+
const tag = el.tagName.toLowerCase();
|
|
2389
|
+
if (tag === "pre" || tag === "textarea")
|
|
2390
|
+
return true;
|
|
2391
|
+
const ws = _nullishCoalesce(_optionalChain([el, 'access', _117 => _117.style, 'optionalAccess', _118 => _118.whiteSpace]), () => ( ""));
|
|
2392
|
+
return ws.startsWith("pre") || ws === "break-spaces";
|
|
2393
|
+
}
|
|
2394
|
+
function collapseWhitespace(root) {
|
|
2395
|
+
let last = null;
|
|
2396
|
+
let space = true;
|
|
2397
|
+
const endLine = () => {
|
|
2398
|
+
if (last && last.data.endsWith(" ")) {
|
|
2399
|
+
last.data = last.data.slice(0, -1);
|
|
2400
|
+
if (!last.data)
|
|
2401
|
+
last.remove();
|
|
2402
|
+
}
|
|
2403
|
+
last = null;
|
|
2404
|
+
space = true;
|
|
2405
|
+
};
|
|
2406
|
+
const walk = (node) => {
|
|
2407
|
+
if (node.nodeType === 3) {
|
|
2408
|
+
const t = node;
|
|
2409
|
+
let out = "";
|
|
2410
|
+
for (const ch of t.data) {
|
|
2411
|
+
if (!isHtmlSpace(ch)) {
|
|
2412
|
+
out += ch;
|
|
2413
|
+
space = false;
|
|
2414
|
+
} else if (!space) {
|
|
2415
|
+
out += " ";
|
|
2416
|
+
space = true;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
if (!out) {
|
|
2420
|
+
t.remove();
|
|
2421
|
+
return;
|
|
2422
|
+
}
|
|
2423
|
+
t.data = out;
|
|
2424
|
+
last = t;
|
|
2425
|
+
return;
|
|
2426
|
+
}
|
|
2427
|
+
if (node.nodeType !== 1)
|
|
2428
|
+
return;
|
|
2429
|
+
const el = node;
|
|
2430
|
+
const tag = el.tagName.toLowerCase();
|
|
2431
|
+
if (SKIP_TAGS.has(tag))
|
|
2432
|
+
return;
|
|
2433
|
+
if (tag === "br") {
|
|
2434
|
+
endLine();
|
|
2435
|
+
return;
|
|
2436
|
+
}
|
|
2437
|
+
const block = BLOCK_TAGS.has(tag);
|
|
2438
|
+
if (block)
|
|
2439
|
+
endLine();
|
|
2440
|
+
if (keepsWhitespace(el)) {
|
|
2441
|
+
last = null;
|
|
2442
|
+
space = block;
|
|
2443
|
+
return;
|
|
2444
|
+
}
|
|
2445
|
+
if (tag === "img" || tag === "input") {
|
|
2446
|
+
last = null;
|
|
2447
|
+
space = false;
|
|
2448
|
+
return;
|
|
2449
|
+
}
|
|
2450
|
+
for (const ch of Array.from(el.childNodes))
|
|
2451
|
+
walk(ch);
|
|
2452
|
+
if (block)
|
|
2453
|
+
endLine();
|
|
2454
|
+
};
|
|
2455
|
+
for (const ch of Array.from(root.childNodes))
|
|
2456
|
+
walk(ch);
|
|
2457
|
+
endLine();
|
|
2458
|
+
}
|
|
2459
|
+
function isBlockishTag(tag) {
|
|
2460
|
+
return tag === "p" || tag === "div" || tag === "section" || tag === "article" || tag === "main" || tag === "ul" || tag === "ol" || tag === "li" || tag === "blockquote" || tag === "pre" || tag === "table" || tag === "hr" || tag === "figure" || tag === "img" || tag === "h1" || tag === "h2" || tag === "h3" || tag === "h4" || tag === "h5" || tag === "h6";
|
|
2461
|
+
}
|
|
2462
|
+
var SPECIAL_TAGS = /* @__PURE__ */ new Set(["br", "figure", "div", "body", "section", "article", "main"]);
|
|
2463
|
+
function isInlineRunNode(n) {
|
|
2464
|
+
if (n.nodeType === 3)
|
|
2465
|
+
return true;
|
|
2466
|
+
if (n.nodeType !== 1)
|
|
2467
|
+
return false;
|
|
2468
|
+
const el = n;
|
|
2469
|
+
const tag = el.tagName.toLowerCase();
|
|
2470
|
+
if (isBlockishTag(tag) || SPECIAL_TAGS.has(tag))
|
|
2471
|
+
return false;
|
|
2472
|
+
return !Array.from(el.children).some((c) => isBlockishTag(c.tagName.toLowerCase()));
|
|
2473
|
+
}
|
|
2474
|
+
function groupInlineRuns(nodes) {
|
|
2475
|
+
const out = [];
|
|
2476
|
+
let run = [];
|
|
2477
|
+
const hasContent = (n) => (_nullishCoalesce(n.textContent, () => ( ""))).trim() !== "" || n.nodeType === 1 && !!n.querySelector("br,[data-math],[data-mention]");
|
|
2478
|
+
const flush = () => {
|
|
2479
|
+
if ((run.length > 1 || run.length === 1 && run[0].nodeType === 1) && run.some(hasContent)) {
|
|
2480
|
+
const p = run[0].ownerDocument.createElement("p");
|
|
2481
|
+
for (const n of run)
|
|
2482
|
+
p.append(n);
|
|
2483
|
+
out.push(p);
|
|
2484
|
+
} else {
|
|
2485
|
+
out.push(...run);
|
|
2486
|
+
}
|
|
2487
|
+
run = [];
|
|
2488
|
+
};
|
|
2489
|
+
for (const n of nodes) {
|
|
2490
|
+
if (n.nodeType === 1 && SKIP_TAGS.has(n.tagName.toLowerCase())) {
|
|
2491
|
+
out.push(n);
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
if (isInlineRunNode(n))
|
|
2495
|
+
run.push(n);
|
|
2496
|
+
else {
|
|
2497
|
+
flush();
|
|
2498
|
+
out.push(n);
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
flush();
|
|
2502
|
+
return out;
|
|
2503
|
+
}
|
|
2504
|
+
function domNodesToBlocks(rawNodes) {
|
|
2505
|
+
const out = [];
|
|
2506
|
+
const nodes = groupInlineRuns(rawNodes);
|
|
2507
|
+
for (const n of nodes) {
|
|
2508
|
+
if (n.nodeType === 3) {
|
|
2509
|
+
const t = (_nullishCoalesce(n.textContent, () => ( ""))).split("\xA0").join(" ");
|
|
2510
|
+
if (!t.trim())
|
|
2511
|
+
continue;
|
|
2512
|
+
out.push({ type: "paragraph", children: [{ text: t }] });
|
|
2513
|
+
continue;
|
|
2514
|
+
}
|
|
2515
|
+
if (n.nodeType !== 1)
|
|
2516
|
+
continue;
|
|
2517
|
+
const el = n;
|
|
2518
|
+
const tag = el.tagName.toLowerCase();
|
|
2519
|
+
if (tag === "meta" || tag === "style" || tag === "script" || tag === "link" || tag === "title" || tag === "head")
|
|
2520
|
+
continue;
|
|
2521
|
+
if (tag === "br") {
|
|
2522
|
+
out.push({ type: "paragraph", children: [{ text: "" }] });
|
|
2523
|
+
continue;
|
|
2524
|
+
}
|
|
2525
|
+
if (tag === "p") {
|
|
2526
|
+
const align = styleAlign(el);
|
|
2527
|
+
const kids = domInlineToNodes(el);
|
|
2528
|
+
const b = align ? { type: "paragraph", attrs: { align }, children: kids } : { type: "paragraph", children: kids };
|
|
2529
|
+
out.push(...elevateInlineImages(b));
|
|
2530
|
+
continue;
|
|
2531
|
+
}
|
|
2532
|
+
if (tag.length === 2 && tag[0] === "h" && tag[1] >= "1" && tag[1] <= "6") {
|
|
2533
|
+
const attrs = { depth: Number(tag[1]) };
|
|
2534
|
+
const align = styleAlign(el);
|
|
2535
|
+
if (align)
|
|
2536
|
+
attrs.align = align;
|
|
2537
|
+
out.push(...elevateInlineImages({ type: "heading", attrs, children: domInlineToNodes(el) }));
|
|
2538
|
+
continue;
|
|
2539
|
+
}
|
|
2540
|
+
if (tag === "ul" || tag === "ol") {
|
|
2541
|
+
const items = Array.from(el.children).filter((c) => c.tagName.toLowerCase() === "li");
|
|
2542
|
+
const children = items.map((li) => {
|
|
2543
|
+
const isBoxLabel = (n2) => n2.nodeType === 1 && n2.tagName.toLowerCase() === "label" && !!n2.querySelector('input[type="checkbox"]');
|
|
2544
|
+
const inner = domNodesToBlocks(Array.from(li.childNodes).filter((n2) => !isBoxLabel(n2)));
|
|
2545
|
+
const body = inner.length ? inner : cellDomToParas(li);
|
|
2546
|
+
return { type: "list_item", children: body.length ? body : [{ type: "paragraph", children: [{ text: "" }] }] };
|
|
2547
|
+
});
|
|
2548
|
+
const startAttr = el.getAttribute("start");
|
|
2549
|
+
const start = tag === "ol" && startAttr ? Number(startAttr) : NaN;
|
|
2550
|
+
const listType = tag === "ol" ? asListType(el.getAttribute("type")) : void 0;
|
|
2551
|
+
out.push({ type: "list", attrs: { ordered: tag === "ol", ...Number.isInteger(start) && start !== 1 ? { start } : {}, ...listType ? { type: listType } : {} }, children });
|
|
2552
|
+
continue;
|
|
2553
|
+
}
|
|
2554
|
+
if (tag === "blockquote") {
|
|
2555
|
+
const inner = domNodesToBlocks(Array.from(el.childNodes));
|
|
2556
|
+
out.push({ type: "quote", children: inner.length ? inner : [{ type: "paragraph", children: [{ text: "" }] }] });
|
|
2557
|
+
continue;
|
|
2558
|
+
}
|
|
2559
|
+
if (tag === "pre") {
|
|
2560
|
+
const codeEl = _nullishCoalesce(el.querySelector("code"), () => ( el));
|
|
2561
|
+
const lang = langFromClass(codeEl.getAttribute("class"));
|
|
2562
|
+
const text = _nullishCoalesce(codeEl.textContent, () => ( ""));
|
|
2563
|
+
out.push({ type: "code", attrs: lang ? { lang } : {}, children: [{ text }] });
|
|
2564
|
+
continue;
|
|
2565
|
+
}
|
|
2566
|
+
if (tag === "table") {
|
|
2567
|
+
const atom = htmlTableToAtom(el.outerHTML, void 0);
|
|
2568
|
+
if (atom)
|
|
2569
|
+
out.push(atom);
|
|
2570
|
+
continue;
|
|
2571
|
+
}
|
|
2572
|
+
if (tag === "hr") {
|
|
2573
|
+
out.push({ type: "raw", attrs: { mdType: "thematicBreak" }, children: [{ text: "" }] });
|
|
2574
|
+
continue;
|
|
2575
|
+
}
|
|
2576
|
+
if (tag === "figure") {
|
|
2577
|
+
const fig = figureFromHtml(el.outerHTML);
|
|
2578
|
+
if (fig)
|
|
2579
|
+
out.push(fig);
|
|
2580
|
+
continue;
|
|
2581
|
+
}
|
|
2582
|
+
if (tag === "img") {
|
|
2583
|
+
const mark = imgHtmlToMark(el.outerHTML);
|
|
2584
|
+
if (mark)
|
|
2585
|
+
out.push({ type: "image", attrs: { ...mark.attrs, alt: mark.alt }, children: [] });
|
|
2586
|
+
continue;
|
|
2587
|
+
}
|
|
2588
|
+
if (tag === "div" && el.hasAttribute("data-embed")) {
|
|
2589
|
+
const emb = embedFromHtml(el.outerHTML);
|
|
2590
|
+
if (emb)
|
|
2591
|
+
out.push(emb);
|
|
2592
|
+
continue;
|
|
2593
|
+
}
|
|
2594
|
+
if (tag === "span" && el.hasAttribute("data-math")) {
|
|
2595
|
+
const latex = _nullishCoalesce(_nullishCoalesce(el.getAttribute("data-latex"), () => ( el.textContent)), () => ( ""));
|
|
2596
|
+
const display = el.getAttribute("data-display") === "block" ? "block" : "inline";
|
|
2597
|
+
out.push({ type: "paragraph", children: [{ type: "math", attrs: { latex, display }, children: [] }] });
|
|
2598
|
+
continue;
|
|
2599
|
+
}
|
|
2600
|
+
if (tag === "div" || tag === "section" || tag === "article" || tag === "main" || tag === "body" || Array.from(el.children).some((c) => isBlockishTag(c.tagName.toLowerCase()))) {
|
|
2601
|
+
out.push(...domNodesToBlocks(Array.from(el.childNodes)));
|
|
2602
|
+
continue;
|
|
2603
|
+
}
|
|
2604
|
+
const paras = cellDomToParas(el);
|
|
2605
|
+
for (const para of paras) {
|
|
2606
|
+
if (para.children.length === 1 && _chunkPCSVBPORcjs.isText.call(void 0, para.children[0]) && !para.children[0].text.trim() && !para.children[0].marks)
|
|
2607
|
+
continue;
|
|
2608
|
+
out.push(...elevateInlineImages(para));
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
return out;
|
|
2612
|
+
}
|
|
2613
|
+
function htmlToBlocks(html) {
|
|
2614
|
+
if (typeof DOMParser === "undefined" || !html.trim())
|
|
2615
|
+
return null;
|
|
2616
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
2617
|
+
collapseWhitespace(body);
|
|
2618
|
+
const blocks = domNodesToBlocks(Array.from(body.childNodes));
|
|
2619
|
+
return blocks.length ? blocks : null;
|
|
2620
|
+
}
|
|
2621
|
+
function isRichHtmlBlocks(blocks) {
|
|
2622
|
+
if (blocks.length !== 1)
|
|
2623
|
+
return true;
|
|
2624
|
+
const b = blocks[0];
|
|
2625
|
+
if (b.type !== "paragraph" && b.type !== "heading")
|
|
2626
|
+
return true;
|
|
2627
|
+
return b.children.some((c) => !_chunkPCSVBPORcjs.isText.call(void 0, c) || (_nullishCoalesce(_optionalChain([c, 'access', _119 => _119.marks, 'optionalAccess', _120 => _120.length]), () => ( 0))) > 0 || c.text.includes("\n"));
|
|
2628
|
+
}
|
|
2629
|
+
function isPlainTransportHtml(blocks) {
|
|
2630
|
+
if (!blocks.length)
|
|
2631
|
+
return false;
|
|
2632
|
+
for (const b of blocks) {
|
|
2633
|
+
if (b.type === "code")
|
|
2634
|
+
continue;
|
|
2635
|
+
if (b.type === "raw")
|
|
2636
|
+
continue;
|
|
2637
|
+
if (b.type !== "paragraph")
|
|
2638
|
+
return false;
|
|
2639
|
+
if (paraHasRichInline(b))
|
|
2640
|
+
return false;
|
|
2641
|
+
}
|
|
2642
|
+
return true;
|
|
2643
|
+
}
|
|
2644
|
+
function paraHasRichInline(b) {
|
|
2645
|
+
for (const c of b.children) {
|
|
2646
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, c)) {
|
|
2647
|
+
if ((_nullishCoalesce(_optionalChain([c, 'access', _121 => _121.marks, 'optionalAccess', _122 => _122.length]), () => ( 0))) > 0)
|
|
2648
|
+
return true;
|
|
2649
|
+
continue;
|
|
2650
|
+
}
|
|
2651
|
+
if (!_chunkPCSVBPORcjs.isElement.call(void 0, c))
|
|
2652
|
+
return true;
|
|
2653
|
+
if (c.type === "hard_break")
|
|
2654
|
+
continue;
|
|
2655
|
+
return true;
|
|
2656
|
+
}
|
|
2657
|
+
return false;
|
|
2658
|
+
}
|
|
2659
|
+
function mdStructureBeatsTransport(plain) {
|
|
2660
|
+
const blocks = mdToDoc(plain).children.filter((c) => _chunkPCSVBPORcjs.isElement.call(void 0, c));
|
|
2661
|
+
if (!blocks.length)
|
|
2662
|
+
return false;
|
|
2663
|
+
for (const b of blocks) {
|
|
2664
|
+
if (b.type === "heading" || b.type === "list" || b.type === "blockquote" || b.type === "table" || b.type === "code" || b.type === "image" || b.type === "embed")
|
|
2665
|
+
return true;
|
|
2666
|
+
if (b.type === "paragraph" && paraHasRichInline(b))
|
|
2667
|
+
return true;
|
|
2668
|
+
if (b.type === "raw" && _optionalChain([b, 'access', _123 => _123.attrs, 'optionalAccess', _124 => _124.mdType]) === "thematicBreak")
|
|
2669
|
+
return true;
|
|
2670
|
+
}
|
|
2671
|
+
return false;
|
|
2672
|
+
}
|
|
2673
|
+
function preferMarkdownPaste(plain, htmlBlocks) {
|
|
2674
|
+
return !!plain.trim() && isPlainTransportHtml(htmlBlocks) && mdStructureBeatsTransport(plain);
|
|
2675
|
+
}
|
|
2676
|
+
var htmlOnlyMarks = /* @__PURE__ */ new Set(["underline", "highlight", "superscript", "subscript", "textStyle", "comment"]);
|
|
2677
|
+
function textToPhrasing(leaf) {
|
|
2678
|
+
if (_optionalChain([leaf, 'access', _125 => _125.marks, 'optionalAccess', _126 => _126.some, 'call', _127 => _127((m) => htmlOnlyMarks.has(m.name))]))
|
|
2679
|
+
return { type: "html", value: leafToHtml(leaf) };
|
|
2680
|
+
let node = { type: "text", value: leaf.text };
|
|
2681
|
+
for (const m of _nullishCoalesce(leaf.marks, () => ( []))) {
|
|
2682
|
+
switch (m.name) {
|
|
2683
|
+
case "bold":
|
|
2684
|
+
node = { type: "strong", children: [node] };
|
|
2685
|
+
break;
|
|
2686
|
+
case "italic":
|
|
2687
|
+
node = { type: "emphasis", children: [node] };
|
|
2688
|
+
break;
|
|
2689
|
+
case "strike":
|
|
2690
|
+
node = { type: "delete", children: [node] };
|
|
2691
|
+
break;
|
|
2692
|
+
case "code":
|
|
2693
|
+
node = { type: "inlineCode", value: leaf.text };
|
|
2694
|
+
break;
|
|
2695
|
+
case "html":
|
|
2696
|
+
node = { type: "html", value: leaf.text };
|
|
2697
|
+
break;
|
|
2698
|
+
case "image":
|
|
2699
|
+
node = _optionalChain([m, 'access', _128 => _128.attrs, 'optionalAccess', _129 => _129.w]) != null || _optionalChain([m, 'access', _130 => _130.attrs, 'optionalAccess', _131 => _131.h]) != null ? { type: "html", value: imgHtml(String(_nullishCoalesce(_optionalChain([m, 'access', _132 => _132.attrs, 'optionalAccess', _133 => _133.url]), () => ( ""))), leaf.text, m.attrs) } : { type: "image", url: String(_nullishCoalesce(_optionalChain([m, 'access', _134 => _134.attrs, 'optionalAccess', _135 => _135.url]), () => ( ""))), title: _nullishCoalesce(_optionalChain([m, 'access', _136 => _136.attrs, 'optionalAccess', _137 => _137.title]), () => ( null)), alt: leaf.text };
|
|
2700
|
+
break;
|
|
2701
|
+
case "link":
|
|
2702
|
+
node = { type: "link", url: String(_nullishCoalesce(_optionalChain([m, 'access', _138 => _138.attrs, 'optionalAccess', _139 => _139.url]), () => ( ""))), title: _nullishCoalesce(_optionalChain([m, 'access', _140 => _140.attrs, 'optionalAccess', _141 => _141.title]), () => ( null)), children: [node] };
|
|
2703
|
+
break;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
return node;
|
|
2707
|
+
}
|
|
2708
|
+
var leaves = (n) => n.children.filter(_chunkPCSVBPORcjs.isText);
|
|
2709
|
+
function inlineHtml(n) {
|
|
2710
|
+
return n.children.map((c) => {
|
|
2711
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, c))
|
|
2712
|
+
return leafToHtml(c);
|
|
2713
|
+
if (c.type === "hard_break")
|
|
2714
|
+
return "<br>";
|
|
2715
|
+
if (c.type === "math")
|
|
2716
|
+
return mathHtml(c);
|
|
2717
|
+
if (c.type === "mention")
|
|
2718
|
+
return mentionHtml(c);
|
|
2719
|
+
return "";
|
|
2720
|
+
}).join("");
|
|
2721
|
+
}
|
|
2722
|
+
function mentionHtml(n) {
|
|
2723
|
+
const id = String(_nullishCoalesce(_optionalChain([n, 'access', _142 => _142.attrs, 'optionalAccess', _143 => _143.id]), () => ( "")));
|
|
2724
|
+
const label = typeof _optionalChain([n, 'access', _144 => _144.attrs, 'optionalAccess', _145 => _145.label]) === "string" ? n.attrs.label : id;
|
|
2725
|
+
const kind = typeof _optionalChain([n, 'access', _146 => _146.attrs, 'optionalAccess', _147 => _147.kind]) === "string" && n.attrs.kind !== "person" ? ` data-kind="${escHtml(n.attrs.kind)}"` : "";
|
|
2726
|
+
const date = typeof _optionalChain([n, 'access', _148 => _148.attrs, 'optionalAccess', _149 => _149.date]) === "string" ? ` data-date="${escHtml(n.attrs.date)}"` : "";
|
|
2727
|
+
const glyph = _optionalChain([n, 'access', _150 => _150.attrs, 'optionalAccess', _151 => _151.kind]) === "date" ? "" : "@";
|
|
2728
|
+
return `<span data-mention data-id="${escHtml(id)}" data-label="${escHtml(label)}"${kind}${date}>${glyph}${escHtml(label)}</span>`;
|
|
2729
|
+
}
|
|
2730
|
+
function mathHtml(n) {
|
|
2731
|
+
const latex = String(_nullishCoalesce(_optionalChain([n, 'access', _152 => _152.attrs, 'optionalAccess', _153 => _153.latex]), () => ( "")));
|
|
2732
|
+
const display = _optionalChain([n, 'access', _154 => _154.attrs, 'optionalAccess', _155 => _155.display]) === "block" ? "block" : "inline";
|
|
2733
|
+
const wrap = display === "block" ? "$$" : "$";
|
|
2734
|
+
const esc = escHtml(latex).split("\n").join(" ");
|
|
2735
|
+
return `<span data-math data-latex="${esc}" data-display="${display}">${wrap}${esc}${wrap}</span>`;
|
|
2736
|
+
}
|
|
2737
|
+
function footnoteRefToMdast(n) {
|
|
2738
|
+
const label = String(_nullishCoalesce(_optionalChain([n, 'access', _156 => _156.attrs, 'optionalAccess', _157 => _157.label]), () => ( "")));
|
|
2739
|
+
return { type: "footnoteReference", identifier: _chunkPCSVBPORcjs.footnoteKey.call(void 0, label), label };
|
|
2740
|
+
}
|
|
2741
|
+
function embedHtml(n) {
|
|
2742
|
+
const kind = String(_nullishCoalesce(_optionalChain([n, 'access', _158 => _158.attrs, 'optionalAccess', _159 => _159.kind]), () => ( "")));
|
|
2743
|
+
const src = String(_nullishCoalesce(_optionalChain([n, 'access', _160 => _160.attrs, 'optionalAccess', _161 => _161.src]), () => ( "")));
|
|
2744
|
+
const title = typeof _optionalChain([n, 'access', _162 => _162.attrs, 'optionalAccess', _163 => _163.title]) === "string" ? ` data-title="${escHtml(n.attrs.title)}"` : "";
|
|
2745
|
+
const name = typeof _optionalChain([n, 'access', _164 => _164.attrs, 'optionalAccess', _165 => _165.name]) === "string" ? ` data-name="${escHtml(n.attrs.name)}"` : "";
|
|
2746
|
+
return `<div data-embed data-kind="${escHtml(kind)}" data-src="${escHtml(src)}"${title}${name}></div>`;
|
|
2747
|
+
}
|
|
2748
|
+
var TREE_MARKS = ["link", "bold", "italic", "strike"];
|
|
2749
|
+
var EMPH_MARKS = /* @__PURE__ */ new Set(["bold", "italic", "strike"]);
|
|
2750
|
+
var markKey = (m) => m.name + JSON.stringify(_nullishCoalesce(m.attrs, () => ( null)));
|
|
2751
|
+
var isEmptyLeaf = (c) => _chunkPCSVBPORcjs.isText.call(void 0, c) && c.text === "" && !_optionalChain([c, 'access', _166 => _166.marks, 'optionalAccess', _167 => _167.some, 'call', _168 => _168((m) => m.name === "image")]);
|
|
2752
|
+
var afterText = (c) => !!c && _chunkPCSVBPORcjs.isText.call(void 0, c) && /\S$/.test(c.text);
|
|
2753
|
+
var isTailBreak = (c) => _chunkPCSVBPORcjs.isElement.call(void 0, c) && c.type === "hard_break";
|
|
2754
|
+
var liveChildren = (children) => children.filter((c) => !isEmptyLeaf(c));
|
|
2755
|
+
function breakSource(br, next, oneLine, afterText2 = false) {
|
|
2756
|
+
if (typeof _optionalChain([br, 'access', _169 => _169.attrs, 'optionalAccess', _170 => _170.html]) === "string")
|
|
2757
|
+
return { type: "html", value: br.attrs.html };
|
|
2758
|
+
if (oneLine || !next || !_chunkPCSVBPORcjs.isText.call(void 0, next) || next.text.startsWith("\n"))
|
|
2759
|
+
return { type: "html", value: "<br>" };
|
|
2760
|
+
const eol = _optionalChain([br, 'access', _171 => _171.attrs, 'optionalAccess', _172 => _172.eol]) === "\r\n" ? "\r\n" : "\n";
|
|
2761
|
+
if (typeof _optionalChain([br, 'access', _173 => _173.attrs, 'optionalAccess', _174 => _174.trail]) === "string" && afterText2)
|
|
2762
|
+
return { type: "html", value: `${br.attrs.trail}${eol}` };
|
|
2763
|
+
return eol === "\n" ? { type: "break" } : { type: "html", value: `\\${eol}` };
|
|
2764
|
+
}
|
|
2765
|
+
function mathSource(n, next, alone) {
|
|
2766
|
+
if (!dollarMath)
|
|
2767
|
+
return mathHtml(n);
|
|
2768
|
+
const latex = String(_nullishCoalesce(_optionalChain([n, 'access', _175 => _175.attrs, 'optionalAccess', _176 => _176.latex]), () => ( "")));
|
|
2769
|
+
const display = _optionalChain([n, 'access', _177 => _177.attrs, 'optionalAccess', _178 => _178.display]) === "block" ? "block" : "inline";
|
|
2770
|
+
const first = next == null ? "" : _chunkPCSVBPORcjs.isText.call(void 0, next) ? next.text.charAt(0) : next.type === "math" ? "$" : "";
|
|
2771
|
+
if (first === "$" || display === "inline" && first >= "0" && first <= "9")
|
|
2772
|
+
return mathHtml(n);
|
|
2773
|
+
const src = dollarCandidate(latex, display, alone);
|
|
2774
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, src, mdExtDollar).children;
|
|
2775
|
+
const p = kids.length === 1 && kids[0].type === "paragraph" ? kids[0].children : [];
|
|
2776
|
+
const back = p.length === 1 && p[0].type === "inlineMath" ? p[0] : null;
|
|
2777
|
+
return back && back.value === latex && (_nullishCoalesce(_optionalChain([back, 'access', _179 => _179.data, 'optionalAccess', _180 => _180.display]), () => ( "inline"))) === display ? src : mathHtml(n);
|
|
2778
|
+
}
|
|
2779
|
+
function pieceOf(c, math) {
|
|
2780
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, c)) {
|
|
2781
|
+
const plain = !_optionalChain([c, 'access', _181 => _181.marks, 'optionalAccess', _182 => _182.some, 'call', _183 => _183((m) => !TREE_MARKS.includes(m.name))]);
|
|
2782
|
+
const image = _optionalChain([c, 'access', _184 => _184.marks, 'optionalAccess', _185 => _185.some, 'call', _186 => _186((m) => m.name === "image")]);
|
|
2783
|
+
return plain ? { kind: "text", text: c.text } : { kind: "fixed", text: image ? "" : c.text };
|
|
2784
|
+
}
|
|
2785
|
+
if (c.type === "math")
|
|
2786
|
+
return { kind: "math", text: math };
|
|
2787
|
+
if (c.type === "mention")
|
|
2788
|
+
return { kind: "fixed", text: mentionHtml(c) };
|
|
2789
|
+
return { kind: "fixed", text: c.type === "hard_break" ? "\n" : "" };
|
|
2790
|
+
}
|
|
2791
|
+
function withEscapes(text, at) {
|
|
2792
|
+
const out = [];
|
|
2793
|
+
let from = 0;
|
|
2794
|
+
for (const i of [...at].sort((a, b) => a - b)) {
|
|
2795
|
+
if (i > from)
|
|
2796
|
+
out.push({ type: "text", value: text.slice(from, i) });
|
|
2797
|
+
out.push({ type: "html", value: "\\$" });
|
|
2798
|
+
from = i + 1;
|
|
2799
|
+
}
|
|
2800
|
+
if (from < text.length)
|
|
2801
|
+
out.push({ type: "text", value: text.slice(from) });
|
|
2802
|
+
return out;
|
|
2803
|
+
}
|
|
2804
|
+
var escInert = (s) => escHtml(s).replace(/[*_~`[\]\\$!#|]/g, (ch) => `&#x${ch.charCodeAt(0).toString(16).toUpperCase()};`);
|
|
2805
|
+
function runItems(allChildren, inParagraph, oneLine = false, tagEmph = false) {
|
|
2806
|
+
const kids = liveChildren(allChildren);
|
|
2807
|
+
if (kids.every(isTailBreak))
|
|
2808
|
+
return [];
|
|
2809
|
+
const math = kids.map((c, i) => _chunkPCSVBPORcjs.isElement.call(void 0, c) && c.type === "math" ? mathSource(c, kids[i + 1], inParagraph && kids.length === 1) : "");
|
|
2810
|
+
const escapes = dollarMath ? dollarEscapes(kids.map((c, i) => pieceOf(c, math[i]))) : [];
|
|
2811
|
+
const out = [];
|
|
2812
|
+
const breaks = [];
|
|
2813
|
+
const leadSoft = kids[0] && _chunkPCSVBPORcjs.isElement.call(void 0, kids[0]) && kids[0].type === "hard_break" && typeof _optionalChain([kids, 'access', _187 => _187[0], 'access', _188 => _188.attrs, 'optionalAccess', _189 => _189.html]) !== "string" && !oneLine && !tagEmph && !!kids[1] && _chunkPCSVBPORcjs.isText.call(void 0, kids[1]) && !_optionalChain([kids, 'access', _190 => _190[1], 'access', _191 => _191.marks, 'optionalAccess', _192 => _192.length]) && kids[1].text.startsWith("\n");
|
|
2814
|
+
kids.forEach((c0, i) => {
|
|
2815
|
+
const c = leadSoft && i === 1 && _chunkPCSVBPORcjs.isText.call(void 0, c0) ? { ...c0, text: c0.text.slice(1) } : c0;
|
|
2816
|
+
if (leadSoft && i === 0) {
|
|
2817
|
+
out.push({ inner: { type: "html", value: "\\\n " }, tree: [] });
|
|
2818
|
+
return;
|
|
2819
|
+
}
|
|
2820
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, c)) {
|
|
2821
|
+
if (_optionalChain([c, 'access', _193 => _193.marks, 'optionalAccess', _194 => _194.some, 'call', _195 => _195((m) => htmlOnlyMarks.has(m.name))])) {
|
|
2822
|
+
out.push({ inner: textToPhrasing(c), tree: [] });
|
|
2823
|
+
return;
|
|
2824
|
+
}
|
|
2825
|
+
if (tagEmph && _optionalChain([c, 'access', _196 => _196.marks, 'optionalAccess', _197 => _197.some, 'call', _198 => _198((m) => EMPH_MARKS.has(m.name))]) && !c.marks.some((m) => m.name === "html" || m.name === "image")) {
|
|
2826
|
+
out.push({ inner: { type: "html", value: leafHtmlWith({ text: c.text, marks: c.marks.filter((m) => m.name !== "link") }, escInert) }, tree: c.marks.filter((m) => m.name === "link") });
|
|
2827
|
+
return;
|
|
2828
|
+
}
|
|
2829
|
+
const tree = (_nullishCoalesce(c.marks, () => ( []))).filter((m) => TREE_MARKS.includes(m.name));
|
|
2830
|
+
const rest = (_nullishCoalesce(c.marks, () => ( []))).filter((m) => !TREE_MARKS.includes(m.name));
|
|
2831
|
+
if (_optionalChain([escapes, 'access', _199 => _199[i], 'optionalAccess', _200 => _200.size]))
|
|
2832
|
+
out.push(...withEscapes(c.text, escapes[i]).map((inner) => ({ inner, tree })));
|
|
2833
|
+
else
|
|
2834
|
+
out.push({ inner: textToPhrasing({ text: c.text, marks: rest }), tree });
|
|
2835
|
+
return;
|
|
2836
|
+
}
|
|
2837
|
+
if (_chunkPCSVBPORcjs.isInlineAtom.call(void 0, c)) {
|
|
2838
|
+
const inner = c.type === "hard_break" ? breakSource(c, kids[i + 1], oneLine || tagEmph, afterText(kids[i - 1])) : c.type === "footnote_ref" ? footnoteRefToMdast(c) : { type: "html", value: c.type === "math" ? math[i] : mentionHtml(c) };
|
|
2839
|
+
out.push({ inner, tree: [] });
|
|
2840
|
+
if (inner.type === "break" || inner.type === "html" && inner.value.endsWith("\n"))
|
|
2841
|
+
breaks.push(out.length - 1);
|
|
2842
|
+
}
|
|
2843
|
+
});
|
|
2844
|
+
for (const k of breaks) {
|
|
2845
|
+
const next = _nullishCoalesce(_optionalChain([out, 'access', _201 => _201[k + 1], 'optionalAccess', _202 => _202.tree]), () => ( []));
|
|
2846
|
+
out[k].tree = (_nullishCoalesce(_optionalChain([out, 'access', _203 => _203[k - 1], 'optionalAccess', _204 => _204.tree]), () => ( []))).filter((m) => next.some((t) => markKey(t) === markKey(m)));
|
|
2847
|
+
}
|
|
2848
|
+
return keepNewlineBeforeHtml(out);
|
|
2849
|
+
}
|
|
2850
|
+
function staysInline(html) {
|
|
2851
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, `x
|
|
2852
|
+
${html}`, parseExt()).children;
|
|
2853
|
+
return kids.length === 1 && kids[0].type === "paragraph";
|
|
2854
|
+
}
|
|
2855
|
+
function keepNewlineBeforeHtml(items) {
|
|
2856
|
+
const out = [];
|
|
2857
|
+
for (const it of items) {
|
|
2858
|
+
const prev = out[out.length - 1];
|
|
2859
|
+
const nx = it.inner;
|
|
2860
|
+
const same = prev && prev.tree.length === it.tree.length && prev.tree.every((m, i) => markKey(m) === markKey(it.tree[i]));
|
|
2861
|
+
const tail = !prev ? "" : prev.inner.type === "break" ? "\\\n" : prev.inner.type === "html" && /^( *|\\)\r?\n$/.test(prev.inner.value) ? prev.inner.value : "";
|
|
2862
|
+
if (nx.type === "html" && prev && same && !nx.value.startsWith("<")) {
|
|
2863
|
+
if (tail) {
|
|
2864
|
+
out[out.length - 1] = { ...it, inner: { type: "html", value: tail + nx.value } };
|
|
2865
|
+
continue;
|
|
2866
|
+
}
|
|
2867
|
+
const pt = prev.inner.type === "text" ? prev.inner.value : "";
|
|
2868
|
+
const nl = _optionalChain([/\n+$/, 'access', _205 => _205.exec, 'call', _206 => _206(pt), 'optionalAccess', _207 => _207[0]]);
|
|
2869
|
+
if (nl) {
|
|
2870
|
+
prev.inner = { type: "text", value: pt.slice(0, -nl.length) };
|
|
2871
|
+
out.push({ ...it, inner: { type: "html", value: nl + nx.value } });
|
|
2872
|
+
continue;
|
|
2873
|
+
}
|
|
2874
|
+
}
|
|
2875
|
+
if (nx.type === "html" && prev && tail)
|
|
2876
|
+
prev.inner = { type: "html", value: "<br>" };
|
|
2877
|
+
if (nx.type === "html" && prev && same && !tail && nx.value.startsWith("<") && prev.inner.type === "text") {
|
|
2878
|
+
const pt = prev.inner.value;
|
|
2879
|
+
const nl = _optionalChain([/\n+$/, 'access', _208 => _208.exec, 'call', _209 => _209(pt), 'optionalAccess', _210 => _210[0]]);
|
|
2880
|
+
if (nl && staysInline(nx.value)) {
|
|
2881
|
+
const rest = pt.slice(0, -nl.length);
|
|
2882
|
+
const ws = _nullishCoalesce(_optionalChain([/[ \t]+$/, 'access', _211 => _211.exec, 'call', _212 => _212(rest), 'optionalAccess', _213 => _213[0]]), () => ( ""));
|
|
2883
|
+
prev.inner = { type: "text", value: rest.slice(0, rest.length - ws.length) };
|
|
2884
|
+
out.push({ ...it, inner: { type: "html", value: [...ws].map((c) => c === " " ? " " : "	").join("") + nl + nx.value } });
|
|
2885
|
+
continue;
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
out.push(it);
|
|
2889
|
+
}
|
|
2890
|
+
return out;
|
|
2891
|
+
}
|
|
2892
|
+
function markNode(m, children, reopen) {
|
|
2893
|
+
const base = m.name === "bold" ? { type: "strong", children } : m.name === "italic" ? { type: "emphasis", children } : m.name === "strike" ? { type: "delete", children } : { type: "link", url: String(_nullishCoalesce(_optionalChain([m, 'access', _214 => _214.attrs, 'optionalAccess', _215 => _215.url]), () => ( ""))), title: _nullishCoalesce(_optionalChain([m, 'access', _216 => _216.attrs, 'optionalAccess', _217 => _217.title]), () => ( null)), children };
|
|
2894
|
+
if (reopen)
|
|
2895
|
+
base.data = { reopen: true };
|
|
2896
|
+
return base;
|
|
2897
|
+
}
|
|
2898
|
+
function closeFrame(m, rawKids, reopen) {
|
|
2899
|
+
if (!EMPH_MARKS.has(m.name))
|
|
2900
|
+
return [markNode(m, mergeText(rawKids), reopen)];
|
|
2901
|
+
const kids = mergeText(rawKids);
|
|
2902
|
+
let lead = "";
|
|
2903
|
+
let trail = "";
|
|
2904
|
+
while (kids.length && kids[0].type === "text" && /^\s/.test(kids[0].value)) {
|
|
2905
|
+
const t = kids[0];
|
|
2906
|
+
const rest = t.value.replace(/^\s+/, "");
|
|
2907
|
+
lead += t.value.slice(0, t.value.length - rest.length);
|
|
2908
|
+
if (rest) kids[0] = { type: "text", value: rest };
|
|
2909
|
+
else kids.shift();
|
|
2910
|
+
}
|
|
2911
|
+
while (kids.length && kids[kids.length - 1].type === "text" && /\s$/.test(kids[kids.length - 1].value)) {
|
|
2912
|
+
const t = kids[kids.length - 1];
|
|
2913
|
+
const rest = t.value.replace(/\s+$/, "");
|
|
2914
|
+
trail = t.value.slice(rest.length) + trail;
|
|
2915
|
+
if (rest) kids[kids.length - 1] = { type: "text", value: rest };
|
|
2916
|
+
else kids.pop();
|
|
2917
|
+
}
|
|
2918
|
+
const out = [];
|
|
2919
|
+
if (lead) out.push({ type: "text", value: lead });
|
|
2920
|
+
if (kids.length) out.push(markNode(m, kids, reopen));
|
|
2921
|
+
if (trail) out.push({ type: "text", value: trail });
|
|
2922
|
+
return out;
|
|
2923
|
+
}
|
|
2924
|
+
function phrasingChildren(n, cell = false, tagEmph = false) {
|
|
2925
|
+
const items = runItems(n.children, n.type === "paragraph" && !cell, n.type === "heading" || cell, tagEmph);
|
|
2926
|
+
const root = [];
|
|
2927
|
+
const stack = [];
|
|
2928
|
+
const kidsOf = () => stack.length ? stack[stack.length - 1].kids : root;
|
|
2929
|
+
const closeTo = (depth) => {
|
|
2930
|
+
while (stack.length > depth) {
|
|
2931
|
+
const f = stack.pop();
|
|
2932
|
+
kidsOf().push(...closeFrame(f.mark, f.kids, f.reopen));
|
|
2933
|
+
}
|
|
2934
|
+
};
|
|
2935
|
+
let closedNow = /* @__PURE__ */ new Set();
|
|
2936
|
+
items.forEach((it, k) => {
|
|
2937
|
+
const want = new Map(it.tree.map((m) => [markKey(m), m]));
|
|
2938
|
+
let keep = 0;
|
|
2939
|
+
while (keep < stack.length && want.has(markKey(stack[keep].mark))) keep++;
|
|
2940
|
+
closedNow = new Set(stack.slice(keep).map((f) => markKey(f.mark)));
|
|
2941
|
+
closeTo(keep);
|
|
2942
|
+
const have = new Set(stack.map((f) => markKey(f.mark)));
|
|
2943
|
+
const runLen = (m) => {
|
|
2944
|
+
let len = 0;
|
|
2945
|
+
while (k + len < items.length && items[k + len].tree.some((t) => markKey(t) === markKey(m))) len++;
|
|
2946
|
+
return len;
|
|
2947
|
+
};
|
|
2948
|
+
const opening = it.tree.filter((m) => !have.has(markKey(m))).map((m, i) => ({ m, i, len: runLen(m) })).sort((a, b) => b.len - a.len || TREE_MARKS.indexOf(a.m.name) - TREE_MARKS.indexOf(b.m.name) || a.i - b.i);
|
|
2949
|
+
for (const { m } of opening)
|
|
2950
|
+
stack.push({ mark: m, kids: [], reopen: closedNow.has(markKey(m)) && m.name !== "link" && m.name !== "strike" });
|
|
2951
|
+
kidsOf().push(it.inner);
|
|
2952
|
+
});
|
|
2953
|
+
closeTo(0);
|
|
2954
|
+
return mergeText(root);
|
|
2955
|
+
}
|
|
2956
|
+
function mergeText(ns) {
|
|
2957
|
+
const out = [];
|
|
2958
|
+
for (const n of ns) {
|
|
2959
|
+
const p = out[out.length - 1];
|
|
2960
|
+
if (p && p.type === "text" && n.type === "text")
|
|
2961
|
+
out[out.length - 1] = { type: "text", value: p.value + n.value };
|
|
2962
|
+
else if (p && p.type === "inlineCode" && n.type === "inlineCode")
|
|
2963
|
+
out[out.length - 1] = { type: "inlineCode", value: p.value + n.value };
|
|
2964
|
+
else out.push(n);
|
|
2965
|
+
}
|
|
2966
|
+
return out;
|
|
2967
|
+
}
|
|
2968
|
+
var CELL_BR = { type: "html", value: "<br>" };
|
|
2969
|
+
function cellPhrasing(cell) {
|
|
2970
|
+
const paras = cell.children.filter(_chunkPCSVBPORcjs.isElement);
|
|
2971
|
+
const src = paras.length ? paras : [cell];
|
|
2972
|
+
const out = [];
|
|
2973
|
+
src.forEach((p, i) => {
|
|
2974
|
+
if (i > 0)
|
|
2975
|
+
out.push(CELL_BR);
|
|
2976
|
+
const kids = p.children.map((c) => _chunkPCSVBPORcjs.isText.call(void 0, c) && c.text === "\n" && !_optionalChain([c, 'access', _218 => _218.marks, 'optionalAccess', _219 => _219.length]) ? { type: "hard_break", children: [] } : c);
|
|
2977
|
+
out.push(...phrasingChildren({ ...p, children: kids }, true));
|
|
2978
|
+
});
|
|
2979
|
+
return out;
|
|
2980
|
+
}
|
|
2981
|
+
function tableToMdast(n) {
|
|
2982
|
+
const cols = Number(_nullishCoalesce(_optionalChain([n, 'access', _220 => _220.attrs, 'optionalAccess', _221 => _221.cols]), () => ( 1)));
|
|
2983
|
+
const align = Array.isArray(_optionalChain([n, 'access', _222 => _222.attrs, 'optionalAccess', _223 => _223.align])) ? n.attrs.align : void 0;
|
|
2984
|
+
const cells = n.children.filter(_chunkPCSVBPORcjs.isElement);
|
|
2985
|
+
const rows = [];
|
|
2986
|
+
for (let r = 0; r * cols < cells.length; r++) {
|
|
2987
|
+
const rowCells = cells.slice(r * cols, (r + 1) * cols).map((cell) => ({
|
|
2988
|
+
type: "tableCell",
|
|
2989
|
+
children: cellPhrasing(cell)
|
|
2990
|
+
}));
|
|
2991
|
+
rows.push({ type: "tableRow", children: rowCells });
|
|
2992
|
+
}
|
|
2993
|
+
return { type: "table", align, children: rows };
|
|
2994
|
+
}
|
|
2995
|
+
function escHtml(s) {
|
|
2996
|
+
let out = "";
|
|
2997
|
+
for (const ch of s) out += ch === "&" ? "&" : ch === "<" ? "<" : ch === ">" ? ">" : ch;
|
|
2998
|
+
return out;
|
|
2999
|
+
}
|
|
3000
|
+
function imgHtml(url, alt, attrs, align = "") {
|
|
3001
|
+
const w = _optionalChain([attrs, 'optionalAccess', _224 => _224.w]);
|
|
3002
|
+
const h = _optionalChain([attrs, 'optionalAccess', _225 => _225.h]);
|
|
3003
|
+
const title = _optionalChain([attrs, 'optionalAccess', _226 => _226.title]);
|
|
3004
|
+
const style = align === "center" ? "display:block;margin-left:auto;margin-right:auto" : align === "right" ? "display:block;margin-left:auto" : "";
|
|
3005
|
+
return `<img src="${escHtml(url)}" alt="${escHtml(alt)}"` + (title != null ? ` title="${escHtml(String(title))}"` : "") + (w != null ? ` width="${Number(w)}"` : "") + (h != null ? ` height="${Number(h)}"` : "") + (style ? ` style="${style}"` : "") + ">";
|
|
3006
|
+
}
|
|
3007
|
+
function figureHtml(url, alt, attrs, caption, align = "") {
|
|
3008
|
+
const fstyle = align === "center" ? ' style="text-align:center"' : align === "right" ? ' style="text-align:right"' : "";
|
|
3009
|
+
return `<figure${fstyle}>${imgHtml(url, alt, attrs)}<figcaption>${escHtml(caption)}</figcaption></figure>`;
|
|
3010
|
+
}
|
|
3011
|
+
function leafToHtml(leaf) {
|
|
3012
|
+
return leafHtmlWith(leaf, escHtml);
|
|
3013
|
+
}
|
|
3014
|
+
function leafHtmlWith(leaf, esc) {
|
|
3015
|
+
let s = _optionalChain([leaf, 'access', _227 => _227.marks, 'optionalAccess', _228 => _228.some, 'call', _229 => _229((m) => m.name === "code")]) ? `<code>${esc(leaf.text)}</code>` : esc(leaf.text);
|
|
3016
|
+
for (const m of _nullishCoalesce(leaf.marks, () => ( []))) {
|
|
3017
|
+
switch (m.name) {
|
|
3018
|
+
case "bold":
|
|
3019
|
+
s = `<strong>${s}</strong>`;
|
|
3020
|
+
break;
|
|
3021
|
+
case "italic":
|
|
3022
|
+
s = `<em>${s}</em>`;
|
|
3023
|
+
break;
|
|
3024
|
+
case "strike":
|
|
3025
|
+
s = `<del>${s}</del>`;
|
|
3026
|
+
break;
|
|
3027
|
+
case "underline":
|
|
3028
|
+
s = `<u>${s}</u>`;
|
|
3029
|
+
break;
|
|
3030
|
+
case "highlight":
|
|
3031
|
+
s = highlightColor(_optionalChain([m, 'access', _230 => _230.attrs, 'optionalAccess', _231 => _231.color])) ? `<mark data-color="${escHtml(highlightColor(_optionalChain([m, 'access', _232 => _232.attrs, 'optionalAccess', _233 => _233.color])))}">${s}</mark>` : `<mark>${s}</mark>`;
|
|
3032
|
+
break;
|
|
3033
|
+
case "superscript":
|
|
3034
|
+
s = `<sup>${s}</sup>`;
|
|
3035
|
+
break;
|
|
3036
|
+
case "subscript":
|
|
3037
|
+
s = `<sub>${s}</sub>`;
|
|
3038
|
+
break;
|
|
3039
|
+
case "textStyle": {
|
|
3040
|
+
const st = attrsToStyle(m.attrs);
|
|
3041
|
+
s = st ? `<span data-mark="textStyle" ${styleAttr(st)}>${s}</span>` : s;
|
|
3042
|
+
break;
|
|
3043
|
+
}
|
|
3044
|
+
case "comment": {
|
|
3045
|
+
const cid = escHtml(String(_nullishCoalesce(_optionalChain([m, 'access', _234 => _234.attrs, 'optionalAccess', _235 => _235.id]), () => ( ""))));
|
|
3046
|
+
s = `<span data-comment="${cid}">${s}</span>`;
|
|
3047
|
+
break;
|
|
3048
|
+
}
|
|
3049
|
+
case "html":
|
|
3050
|
+
s = leaf.text;
|
|
3051
|
+
break;
|
|
3052
|
+
// 逐字 HTML 叶:不转义
|
|
3053
|
+
case "link":
|
|
3054
|
+
s = `<a href="${escHtml(String(_nullishCoalesce(_optionalChain([m, 'access', _236 => _236.attrs, 'optionalAccess', _237 => _237.url]), () => ( ""))))}">${s}</a>`;
|
|
3055
|
+
break;
|
|
3056
|
+
case "image":
|
|
3057
|
+
s = imgHtml(String(_nullishCoalesce(_optionalChain([m, 'access', _238 => _238.attrs, 'optionalAccess', _239 => _239.url]), () => ( ""))), leaf.text, m.attrs);
|
|
3058
|
+
break;
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
return s;
|
|
3062
|
+
}
|
|
3063
|
+
var LIST_HTML_TAGS = /* @__PURE__ */ new Set(["ol", "ul", "li", "p", "br", "strong", "b", "em", "i", "del", "s", "strike", "u", "code", "a", "mark", "sup", "sub", "span", "img", "label", "input"]);
|
|
3064
|
+
function typedListFromHtml(html) {
|
|
3065
|
+
if (typeof DOMParser === "undefined")
|
|
3066
|
+
return null;
|
|
3067
|
+
const body = new DOMParser().parseFromString(html, "text/html").body;
|
|
3068
|
+
const el = body.children[0];
|
|
3069
|
+
if (body.children.length !== 1 || el.tagName.toLowerCase() !== "ol" || !asListType(el.getAttribute("type")))
|
|
3070
|
+
return null;
|
|
3071
|
+
if (!Array.from(body.childNodes).every((n) => n === el || n.nodeType === 3 && !(_nullishCoalesce(n.textContent, () => ( ""))).trim()))
|
|
3072
|
+
return null;
|
|
3073
|
+
if (!Array.from(el.querySelectorAll("*")).every((e) => LIST_HTML_TAGS.has(e.tagName.toLowerCase())))
|
|
3074
|
+
return null;
|
|
3075
|
+
return listDomToNode(el);
|
|
3076
|
+
}
|
|
3077
|
+
function listShape(n) {
|
|
3078
|
+
const attrs = { ...n.attrs };
|
|
3079
|
+
delete attrs.spread;
|
|
3080
|
+
if (attrs.start === 1)
|
|
3081
|
+
delete attrs.start;
|
|
3082
|
+
const kids = [];
|
|
3083
|
+
let run;
|
|
3084
|
+
for (const c of n.children) {
|
|
3085
|
+
if (!_chunkPCSVBPORcjs.isText.call(void 0, c)) {
|
|
3086
|
+
run = void 0;
|
|
3087
|
+
kids.push(listShape(c));
|
|
3088
|
+
continue;
|
|
3089
|
+
}
|
|
3090
|
+
if (run && JSON.stringify(_nullishCoalesce(run.marks, () => ( []))) === JSON.stringify(_nullishCoalesce(c.marks, () => ( []))))
|
|
3091
|
+
run.text += c.text;
|
|
3092
|
+
else
|
|
3093
|
+
kids.push(run = { ...c });
|
|
3094
|
+
}
|
|
3095
|
+
return { type: n.type, attrs, children: kids };
|
|
3096
|
+
}
|
|
3097
|
+
function typedListToHtml(list) {
|
|
3098
|
+
if (!_optionalChain([list, 'access', _240 => _240.attrs, 'optionalAccess', _241 => _241.ordered]) || !asListType(list.attrs.type))
|
|
3099
|
+
return null;
|
|
3100
|
+
const html = listToCellHtml(list);
|
|
3101
|
+
const back = typedListFromHtml(html);
|
|
3102
|
+
return back && eq(listShape(back), listShape(list)) ? html : null;
|
|
3103
|
+
}
|
|
3104
|
+
function listToCellHtml(list) {
|
|
3105
|
+
const tag = _optionalChain([list, 'access', _242 => _242.attrs, 'optionalAccess', _243 => _243.ordered]) ? "ol" : "ul";
|
|
3106
|
+
const start = _optionalChain([list, 'access', _244 => _244.attrs, 'optionalAccess', _245 => _245.ordered]) && list.attrs.start != null && Number(list.attrs.start) !== 1 ? ` start="${Number(list.attrs.start)}"` : "";
|
|
3107
|
+
const listType = _optionalChain([list, 'access', _246 => _246.attrs, 'optionalAccess', _247 => _247.ordered]) ? asListType(list.attrs.type) : void 0;
|
|
3108
|
+
const typeAttr = listType ? ` type="${listType}"` : "";
|
|
3109
|
+
const items = list.children.filter(_chunkPCSVBPORcjs.isElement).map((item) => {
|
|
3110
|
+
const kids = item.children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3111
|
+
const bare = kids.every((ch, i) => ch.type === "list" || i === 0 && ch.type === "paragraph");
|
|
3112
|
+
const body = kids.map((ch) => bare && ch.type === "paragraph" ? inlineHtml(ch) : blockToCellHtml(ch)).join("");
|
|
3113
|
+
const checked = _optionalChain([item, 'access', _248 => _248.attrs, 'optionalAccess', _249 => _249.checked]);
|
|
3114
|
+
const task = checked != null ? ` data-checked="${checked ? "true" : "false"}"` : "";
|
|
3115
|
+
return `<li${task}>${body}</li>`;
|
|
3116
|
+
}).join("");
|
|
3117
|
+
return `<${tag}${typeAttr}${start}>${items}</${tag}>`;
|
|
3118
|
+
}
|
|
3119
|
+
function blockToCellHtml(n) {
|
|
3120
|
+
switch (n.type) {
|
|
3121
|
+
case "list":
|
|
3122
|
+
return listToCellHtml(n);
|
|
3123
|
+
case "paragraph":
|
|
3124
|
+
return `<p>${inlineHtml(n)}</p>`;
|
|
3125
|
+
case "quote": {
|
|
3126
|
+
if (_chunkPCSVBPORcjs.isCallout.call(void 0, n)) {
|
|
3127
|
+
const variant = typeof _optionalChain([n, 'access', _250 => _250.attrs, 'optionalAccess', _251 => _251.variant]) === "string" ? n.attrs.variant : "info";
|
|
3128
|
+
const inner = n.children.filter(_chunkPCSVBPORcjs.isElement).map(blockToCellHtml).join("");
|
|
3129
|
+
return `<div data-callout data-variant="${escHtml(variant)}">${inner}</div>`;
|
|
3130
|
+
}
|
|
3131
|
+
return `<blockquote>${n.children.filter(_chunkPCSVBPORcjs.isElement).map(blockToCellHtml).join("")}</blockquote>`;
|
|
3132
|
+
}
|
|
3133
|
+
case "code": {
|
|
3134
|
+
const text = leaves(n).map((l) => l.text).join("");
|
|
3135
|
+
return `<pre><code>${escHtml(text)}</code></pre>`;
|
|
3136
|
+
}
|
|
3137
|
+
case "image":
|
|
3138
|
+
return imgHtml(String(_nullishCoalesce(_optionalChain([n, 'access', _252 => _252.attrs, 'optionalAccess', _253 => _253.url]), () => ( ""))), String(_nullishCoalesce(_optionalChain([n, 'access', _254 => _254.attrs, 'optionalAccess', _255 => _255.alt]), () => ( ""))), n.attrs);
|
|
3139
|
+
case "embed":
|
|
3140
|
+
return embedHtml(n);
|
|
3141
|
+
case "raw":
|
|
3142
|
+
if (_optionalChain([n, 'access', _256 => _256.attrs, 'optionalAccess', _257 => _257.mdType]) === "thematicBreak")
|
|
3143
|
+
return "<hr>";
|
|
3144
|
+
return leaves(n).map((l) => l.text).join("");
|
|
3145
|
+
default:
|
|
3146
|
+
return inlineHtml(n);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
function cellInnerHtml(cell) {
|
|
3150
|
+
const kids = cell.children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3151
|
+
if (!kids.length)
|
|
3152
|
+
return leaves(cell).map(leafToHtml).join("");
|
|
3153
|
+
const struct = kids.some((k) => k.type !== "paragraph");
|
|
3154
|
+
if (!struct)
|
|
3155
|
+
return kids.map(inlineHtml).join("<br>");
|
|
3156
|
+
return kids.map(blockToCellHtml).join("");
|
|
3157
|
+
}
|
|
3158
|
+
function tableHasExtras(n) {
|
|
3159
|
+
if (Number(_nullishCoalesce(_optionalChain([n, 'access', _258 => _258.attrs, 'optionalAccess', _259 => _259.freezeRows]), () => ( 0))) > 0 || Number(_nullishCoalesce(_optionalChain([n, 'access', _260 => _260.attrs, 'optionalAccess', _261 => _261.freezeCols]), () => ( 0))) > 0 || _optionalChain([n, 'access', _262 => _262.attrs, 'optionalAccess', _263 => _263.headerCol]) === true)
|
|
3160
|
+
return true;
|
|
3161
|
+
return n.children.filter(_chunkPCSVBPORcjs.isElement).some((c) => colspanOf(c) > 1 || rowspanOf(c) > 1 || _optionalChain([c, 'access', _264 => _264.attrs, 'optionalAccess', _265 => _265.bg]) != null || _optionalChain([c, 'access', _266 => _266.attrs, 'optionalAccess', _267 => _267.border]) != null || c.children.some((ch) => !_chunkPCSVBPORcjs.isText.call(void 0, ch) && ch.type !== "paragraph"));
|
|
3162
|
+
}
|
|
3163
|
+
function keepsSpaces(cell) {
|
|
3164
|
+
const lines = [];
|
|
3165
|
+
const walk = (n) => {
|
|
3166
|
+
if (n.children.some(_chunkPCSVBPORcjs.isText))
|
|
3167
|
+
lines.push(...n.children.map((c) => _chunkPCSVBPORcjs.isText.call(void 0, c) ? c.text : "\0").join("").split("\0"));
|
|
3168
|
+
n.children.forEach((c) => !_chunkPCSVBPORcjs.isText.call(void 0, c) && walk(c));
|
|
3169
|
+
};
|
|
3170
|
+
walk(cell);
|
|
3171
|
+
return lines.some((t) => t !== t.replace(/\s+/g, " ").trim());
|
|
3172
|
+
}
|
|
3173
|
+
function tableToHtml(n) {
|
|
3174
|
+
const grid = buildGrid(n);
|
|
3175
|
+
const align = Array.isArray(_optionalChain([n, 'access', _268 => _268.attrs, 'optionalAccess', _269 => _269.align])) ? n.attrs.align : null;
|
|
3176
|
+
const freezeRows = Number(_nullishCoalesce(_optionalChain([n, 'access', _270 => _270.attrs, 'optionalAccess', _271 => _271.freezeRows]), () => ( 0)));
|
|
3177
|
+
const freezeCols = Number(_nullishCoalesce(_optionalChain([n, 'access', _272 => _272.attrs, 'optionalAccess', _273 => _273.freezeCols]), () => ( 0)));
|
|
3178
|
+
const headerCol = _optionalChain([n, 'access', _274 => _274.attrs, 'optionalAccess', _275 => _275.headerCol]) === true;
|
|
3179
|
+
const rowCells = /* @__PURE__ */ new Map();
|
|
3180
|
+
for (const idx of grid.anchors) {
|
|
3181
|
+
const cell = n.children[idx];
|
|
3182
|
+
const m = grid.cellOf(idx);
|
|
3183
|
+
const a = _optionalChain([align, 'optionalAccess', _276 => _276[m.c]]);
|
|
3184
|
+
const bg = _optionalChain([cell, 'access', _277 => _277.attrs, 'optionalAccess', _278 => _278.bg]);
|
|
3185
|
+
const border = _optionalChain([cell, 'access', _279 => _279.attrs, 'optionalAccess', _280 => _280.border]);
|
|
3186
|
+
const styles = [a && `text-align:${a}`, bg && `background:${bg}`, border && `border-color:${border}`, keepsSpaces(cell) && "white-space:pre-wrap"].filter(Boolean).join(";");
|
|
3187
|
+
const attrs = [
|
|
3188
|
+
m.colspan > 1 && ` colspan="${m.colspan}"`,
|
|
3189
|
+
m.rowspan > 1 && ` rowspan="${m.rowspan}"`,
|
|
3190
|
+
styles && ` style="${styles}"`
|
|
3191
|
+
].filter(Boolean).join("");
|
|
3192
|
+
const tag = m.r === 0 || headerCol && m.c === 0 ? "th" : "td";
|
|
3193
|
+
const html = `<${tag}${attrs}>${cellInnerHtml(cell)}</${tag}>`;
|
|
3194
|
+
const arr = _nullishCoalesce(rowCells.get(m.r), () => ( []));
|
|
3195
|
+
arr.push(html);
|
|
3196
|
+
rowCells.set(m.r, arr);
|
|
3197
|
+
}
|
|
3198
|
+
const trs = Array.from(rowCells.keys()).sort((a, b) => a - b).map((r) => `<tr>${rowCells.get(r).join("")}</tr>`);
|
|
3199
|
+
const tableAttrs = [freezeRows > 0 && ` data-freeze-rows="${freezeRows}"`, freezeCols > 0 && ` data-freeze-cols="${freezeCols}"`].filter(Boolean).join("");
|
|
3200
|
+
return `<table${tableAttrs}>${trs.join("")}</table>`.replace(/\n(?=[ \t]*\n)/g, " ");
|
|
3201
|
+
}
|
|
3202
|
+
function trimLeadBreak(n) {
|
|
3203
|
+
const i = n.children.findIndex((c2) => !isEmptyLeaf(c2));
|
|
3204
|
+
const c = n.children[i];
|
|
3205
|
+
if (n.type !== "paragraph" || !c || !_chunkPCSVBPORcjs.isText.call(void 0, c) || !c.text.startsWith("\n"))
|
|
3206
|
+
return n;
|
|
3207
|
+
const children = [...n.children];
|
|
3208
|
+
children[i] = { ...c, text: c.text.replace(/^\n+/, "") };
|
|
3209
|
+
return { ...n, children };
|
|
3210
|
+
}
|
|
3211
|
+
function trimEdgeBreaks(n) {
|
|
3212
|
+
const kids = [...n.children];
|
|
3213
|
+
const first = kids.findIndex((c) => !isEmptyLeaf(c));
|
|
3214
|
+
const c0 = kids[first];
|
|
3215
|
+
if (c0 && _chunkPCSVBPORcjs.isText.call(void 0, c0))
|
|
3216
|
+
kids[first] = { ...c0, text: c0.text.replace(/^\n+/, "") };
|
|
3217
|
+
let last = kids.length - 1;
|
|
3218
|
+
while (last >= 0 && isEmptyLeaf(kids[last]))
|
|
3219
|
+
last--;
|
|
3220
|
+
const c1 = kids[last];
|
|
3221
|
+
if (c1 && _chunkPCSVBPORcjs.isText.call(void 0, c1))
|
|
3222
|
+
kids[last] = { ...c1, text: c1.text.replace(/\n+$/, "") };
|
|
3223
|
+
return { ...n, children: kids };
|
|
3224
|
+
}
|
|
3225
|
+
var isEmptyParagraph = (n) => n.type === "paragraph" && n.children.every((c) => _chunkPCSVBPORcjs.isText.call(void 0, c) && c.text === "" || isTailBreak(c));
|
|
3226
|
+
var emptyPara = (n) => n.type === "paragraph" && (_nullishCoalesce(n.children, () => ( []))).every((c) => c.type === "text" && !c.value);
|
|
3227
|
+
function withOption(state, key, value, on, run) {
|
|
3228
|
+
if (!on)
|
|
3229
|
+
return run();
|
|
3230
|
+
const prev = state.options[key];
|
|
3231
|
+
state.options[key] = value;
|
|
3232
|
+
try {
|
|
3233
|
+
return run();
|
|
3234
|
+
} finally {
|
|
3235
|
+
state.options[key] = prev;
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
function pipeAfterOddBackslashes(text) {
|
|
3239
|
+
let run = 0;
|
|
3240
|
+
for (const ch of text) {
|
|
3241
|
+
if (ch === "\\")
|
|
3242
|
+
run++;
|
|
3243
|
+
else {
|
|
3244
|
+
if (ch === "|" && run % 2 === 1)
|
|
3245
|
+
return true;
|
|
3246
|
+
run = 0;
|
|
3247
|
+
}
|
|
3248
|
+
}
|
|
3249
|
+
return false;
|
|
3250
|
+
}
|
|
3251
|
+
function inlineCodeKeepingBreaks(node, parent, state) {
|
|
3252
|
+
const plain = _mdastutiltomarkdown.defaultHandlers.inlineCode(node, parent, state);
|
|
3253
|
+
if (!String(_nullishCoalesce(node.value, () => ( ""))).includes("\n"))
|
|
3254
|
+
return plain;
|
|
3255
|
+
const unsafe = state.unsafe;
|
|
3256
|
+
state.unsafe = unsafe.filter((u) => !u.atBreak);
|
|
3257
|
+
let kept;
|
|
3258
|
+
try {
|
|
3259
|
+
kept = _mdastutiltomarkdown.defaultHandlers.inlineCode(node, parent, state);
|
|
3260
|
+
} finally {
|
|
3261
|
+
state.unsafe = unsafe;
|
|
3262
|
+
}
|
|
3263
|
+
if (kept === plain)
|
|
3264
|
+
return plain;
|
|
3265
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, kept, parseExt()).children;
|
|
3266
|
+
const p = kids.length === 1 && kids[0].type === "paragraph" ? kids[0].children : [];
|
|
3267
|
+
return p.length === 1 && p[0].type === "inlineCode" && p[0].value === node.value ? kept : plain;
|
|
3268
|
+
}
|
|
3269
|
+
var inlineCodeInCell = Object.assign(
|
|
3270
|
+
(node, parent, state, info) => {
|
|
3271
|
+
if (!state.stack.includes("tableCell"))
|
|
3272
|
+
return inlineCodeKeepingBreaks(node, parent, state);
|
|
3273
|
+
if (pipeAfterOddBackslashes(node.value)) {
|
|
3274
|
+
let out = "";
|
|
3275
|
+
for (const ch of node.value)
|
|
3276
|
+
out += ch === "&" ? "&" : ch === "<" ? "<" : ch === ">" ? ">" : ch === "\\" ? "\" : ch === "|" ? "|" : ch;
|
|
3277
|
+
return `<code>${out}</code>`;
|
|
3278
|
+
}
|
|
3279
|
+
return _mdastutiltomarkdown.defaultHandlers.inlineCode(node, parent, state).replaceAll("|", "\\|");
|
|
3280
|
+
},
|
|
3281
|
+
{ peek: () => "`" }
|
|
3282
|
+
);
|
|
3283
|
+
function attentionHandler(kind) {
|
|
3284
|
+
const base = _mdastutiltomarkdown.defaultHandlers[kind];
|
|
3285
|
+
const handler = (node, parent, state, info) => {
|
|
3286
|
+
const out = withOption(state, kind, "_", _optionalChain([node, 'access', _281 => _281.data, 'optionalAccess', _282 => _282.reopen]), () => base(node, parent, state, info));
|
|
3287
|
+
keepNeighbours(kind, out, info, state);
|
|
3288
|
+
return out;
|
|
3289
|
+
};
|
|
3290
|
+
const peek = (node, parent, state) => _optionalChain([node, 'access', _283 => _283.data, 'optionalAccess', _284 => _284.reopen]) ? "_" : base.peek(node, parent, state);
|
|
3291
|
+
const attention = base.attention && ((node, state) => withOption(state, kind, "_", _optionalChain([node, 'access', _285 => _285.data, 'optionalAccess', _286 => _286.reopen]), () => base.attention(node, state)));
|
|
3292
|
+
return Object.assign(handler, { peek }, attention ? { attention } : {});
|
|
3293
|
+
}
|
|
3294
|
+
function keepNeighbours(kind, out, info, state) {
|
|
3295
|
+
const sides = state.attentionEncodeSurroundingInfo;
|
|
3296
|
+
if (!_optionalChain([sides, 'optionalAccess', _287 => _287.before]) && !_optionalChain([sides, 'optionalAccess', _288 => _288.after]))
|
|
3297
|
+
return;
|
|
3298
|
+
const before = sides.before ? String(_nullishCoalesce(info.before, () => ( ""))).slice(-1) : "";
|
|
3299
|
+
const after = sides.after ? String(_nullishCoalesce(info.after, () => ( ""))).charAt(0) : "";
|
|
3300
|
+
const kids = _mdastutilfrommarkdown.fromMarkdown.call(void 0, before + out + after, parseExt()).children;
|
|
3301
|
+
const p = kids.length === 1 && kids[0].type === "paragraph" ? kids[0].children : [];
|
|
3302
|
+
const want = [before && "text", kind, after && "text"].filter(Boolean);
|
|
3303
|
+
if (p.length === want.length && p.every((c, i) => c.type === want[i]))
|
|
3304
|
+
state.attentionEncodeSurroundingInfo = void 0;
|
|
3305
|
+
}
|
|
3306
|
+
function ruleAfterParagraph(node, parent, state) {
|
|
3307
|
+
const kids = _nullishCoalesce(_optionalChain([parent, 'optionalAccess', _289 => _289.children]), () => ( []));
|
|
3308
|
+
const prev = kids[kids.indexOf(node) - 1];
|
|
3309
|
+
const marker = _optionalChain([node, 'access', _290 => _290.data, 'optionalAccess', _291 => _291.marker]);
|
|
3310
|
+
if (_optionalChain([prev, 'optionalAccess', _292 => _292.type]) === "paragraph" && (typeof marker !== "string" || marker[0] === "-"))
|
|
3311
|
+
return "***";
|
|
3312
|
+
return typeof marker === "string" ? marker : _mdastutiltomarkdown.defaultHandlers.thematicBreak(node, parent, state);
|
|
3313
|
+
}
|
|
3314
|
+
var CLOSED_BLOCKS = /* @__PURE__ */ new Set(["heading", "code", "thematicBreak"]);
|
|
3315
|
+
var interruptsParagraph = (b) => b.type === "blockquote" || b.type === "code" || b.type === "heading" || b.type === "thematicBreak" || b.type === "list" && (!b.ordered || (_nullishCoalesce(b.start, () => ( 1))) === 1);
|
|
3316
|
+
function tightItemJoin(left, right, parent) {
|
|
3317
|
+
if (parent.type !== "listItem" || CLOSED_BLOCKS.has(left.type) || left.type === "paragraph" && interruptsParagraph(right))
|
|
3318
|
+
return void 0;
|
|
3319
|
+
return 1;
|
|
3320
|
+
}
|
|
3321
|
+
var MD_OUT = {
|
|
3322
|
+
bullet: "-",
|
|
3323
|
+
rule: "-",
|
|
3324
|
+
handlers: {
|
|
3325
|
+
inlineCode: inlineCodeInCell,
|
|
3326
|
+
emphasis: attentionHandler("emphasis"),
|
|
3327
|
+
strong: attentionHandler("strong"),
|
|
3328
|
+
thematicBreak: ruleAfterParagraph
|
|
3329
|
+
},
|
|
3330
|
+
extensions: [_mdastutilgfm.gfmToMarkdown.call(void 0, )],
|
|
3331
|
+
join: [tightItemJoin, (left, _right, parent) => parent.type === "listItem" && emptyPara(left) ? 0 : void 0]
|
|
3332
|
+
};
|
|
3333
|
+
var MD_OUT_DOLLAR = { ...MD_OUT, unsafe: [{ character: "$", inConstruct: "label", notInConstruct: ["link"] }] };
|
|
3334
|
+
var mdOut = () => dollarMath ? MD_OUT_DOLLAR : MD_OUT;
|
|
3335
|
+
var endsInHtml = (n) => !_chunkPCSVBPORcjs.isText.call(void 0, n) && (n.type === "raw" ? _optionalChain([n, 'access', _293 => _293.attrs, 'optionalAccess', _294 => _294.mdType]) === "html" : n.children.length > 0 && endsInHtml(n.children[n.children.length - 1]));
|
|
3336
|
+
function blockMd(n, opts) {
|
|
3337
|
+
return blocksMd([n], opts);
|
|
3338
|
+
}
|
|
3339
|
+
function blocksMd(ns, opts) {
|
|
3340
|
+
const t = _mdastutiltomarkdown.toMarkdown.call(void 0, { type: "root", children: ns.map(nodeToMdast) }, opts).replace(/\n+$/, "");
|
|
3341
|
+
return ns.length && endsInHtml(ns[ns.length - 1]) ? t : t.trimEnd();
|
|
3342
|
+
}
|
|
3343
|
+
function containerToHtml(openAttrs, children) {
|
|
3344
|
+
const kids = children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3345
|
+
const inner = blocksMd(kids, mdOut());
|
|
3346
|
+
return `<div ${openAttrs}>
|
|
3347
|
+
|
|
3348
|
+
${inner}
|
|
3349
|
+
|
|
3350
|
+
</div>`;
|
|
3351
|
+
}
|
|
3352
|
+
function calloutToHtml(n) {
|
|
3353
|
+
const variant = typeof _optionalChain([n, 'access', _295 => _295.attrs, 'optionalAccess', _296 => _296.variant]) === "string" ? n.attrs.variant : "info";
|
|
3354
|
+
return containerToHtml(`data-callout data-variant="${escHtml(variant)}"`, n.children);
|
|
3355
|
+
}
|
|
3356
|
+
function containerOpenKind(b) {
|
|
3357
|
+
if (b.type !== "html" || !b.value.trimStart().toLowerCase().startsWith("<div"))
|
|
3358
|
+
return null;
|
|
3359
|
+
if (b.value.includes("data-callout"))
|
|
3360
|
+
return "callout";
|
|
3361
|
+
return null;
|
|
3362
|
+
}
|
|
3363
|
+
function isDivOpen(b) {
|
|
3364
|
+
return b.type === "html" && b.value.trimStart().toLowerCase().startsWith("<div");
|
|
3365
|
+
}
|
|
3366
|
+
function isDivClose(b) {
|
|
3367
|
+
return b.type === "html" && b.value.trim().toLowerCase() === "</div>";
|
|
3368
|
+
}
|
|
3369
|
+
function readOpenAttr(openTag, sel, attr, fallback) {
|
|
3370
|
+
if (typeof DOMParser === "undefined")
|
|
3371
|
+
return fallback;
|
|
3372
|
+
const div = new DOMParser().parseFromString(`${openTag}</div>`, "text/html").querySelector(sel);
|
|
3373
|
+
return _nullishCoalesce(_optionalChain([div, 'optionalAccess', _297 => _297.getAttribute, 'call', _298 => _298(attr)]), () => ( fallback));
|
|
3374
|
+
}
|
|
3375
|
+
var blankPara = () => ({ type: "paragraph", children: [{ text: "" }] });
|
|
3376
|
+
function gapParas(gap) {
|
|
3377
|
+
if (gap.trim() !== "")
|
|
3378
|
+
return 0;
|
|
3379
|
+
return Math.max(0, Math.floor(((_nullishCoalesce(gap.match(/\n/g), () => ( []))).length - 2) / 2));
|
|
3380
|
+
}
|
|
3381
|
+
function* stitchG(blocks, md, top = false) {
|
|
3382
|
+
const out = [];
|
|
3383
|
+
let prevEnd;
|
|
3384
|
+
const push = (b, nodes) => {
|
|
3385
|
+
const start = _optionalChain([b, 'access', _299 => _299.position, 'optionalAccess', _300 => _300.start, 'access', _301 => _301.offset]);
|
|
3386
|
+
const prev = out[out.length - 1];
|
|
3387
|
+
if (top && prevEnd !== void 0 && start !== void 0 && prev && nodes.length && !isOpaqueBlock(prev) && !isOpaqueBlock(nodes[0]))
|
|
3388
|
+
for (let k = gapParas(md.slice(prevEnd, start)); k > 0; k--)
|
|
3389
|
+
out.push(blankPara());
|
|
3390
|
+
out.push(...nodes);
|
|
3391
|
+
};
|
|
3392
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
3393
|
+
if (top && (i & 31) === 31)
|
|
3394
|
+
yield STITCH;
|
|
3395
|
+
const b = blocks[i];
|
|
3396
|
+
const kind = containerOpenKind(b);
|
|
3397
|
+
if (!kind) {
|
|
3398
|
+
push(b, [blockToNode(b, md)].flat());
|
|
3399
|
+
prevEnd = _optionalChain([b, 'access', _302 => _302.position, 'optionalAccess', _303 => _303.end, 'access', _304 => _304.offset]);
|
|
3400
|
+
continue;
|
|
3401
|
+
}
|
|
3402
|
+
let depth = 1;
|
|
3403
|
+
let j = i + 1;
|
|
3404
|
+
for (; j < blocks.length; j++) {
|
|
3405
|
+
if (isDivClose(blocks[j]))
|
|
3406
|
+
depth--;
|
|
3407
|
+
else if (isDivOpen(blocks[j]))
|
|
3408
|
+
depth++;
|
|
3409
|
+
if (depth === 0)
|
|
3410
|
+
break;
|
|
3411
|
+
}
|
|
3412
|
+
if (j >= blocks.length) {
|
|
3413
|
+
push(b, [blockToNode(b, md)].flat());
|
|
3414
|
+
prevEnd = _optionalChain([b, 'access', _305 => _305.position, 'optionalAccess', _306 => _306.end, 'access', _307 => _307.offset]);
|
|
3415
|
+
continue;
|
|
3416
|
+
}
|
|
3417
|
+
const inner = drain(stitchG(blocks.slice(i + 1, j), md));
|
|
3418
|
+
const s = span(b);
|
|
3419
|
+
const tag = b.value;
|
|
3420
|
+
const children = inner.length ? inner : [{ type: "paragraph", children: [{ text: "" }] }];
|
|
3421
|
+
push(b, [{ type: "quote", attrs: { variant: readOpenAttr(tag, "div[data-callout]", "data-variant", "info"), span: s }, children }]);
|
|
3422
|
+
prevEnd = _optionalChain([blocks, 'access', _308 => _308[j], 'access', _309 => _309.position, 'optionalAccess', _310 => _310.end, 'access', _311 => _311.offset]);
|
|
3423
|
+
i = j;
|
|
3424
|
+
}
|
|
3425
|
+
return out;
|
|
3426
|
+
}
|
|
3427
|
+
function emphSig(kids) {
|
|
3428
|
+
return liveChildren(kids).map((c) => {
|
|
3429
|
+
if (!_chunkPCSVBPORcjs.isText.call(void 0, c))
|
|
3430
|
+
return c.type === "footnote_ref" ? `[^${String(_nullishCoalesce(_optionalChain([c, 'access', _312 => _312.attrs, 'optionalAccess', _313 => _313.label]), () => ( "")))}]` : `\0${c.type}:${String(_nullishCoalesce(_nullishCoalesce(_optionalChain([c, 'access', _314 => _314.attrs, 'optionalAccess', _315 => _315.latex]), () => ( _optionalChain([c, 'access', _316 => _316.attrs, 'optionalAccess', _317 => _317.label]))), () => ( "")))}\0`;
|
|
3431
|
+
const e = (_nullishCoalesce(c.marks, () => ( []))).filter((m) => EMPH_MARKS.has(m.name)).map((m) => m.name[0]).sort().join("");
|
|
3432
|
+
return [...c.text].map((ch) => /\s/.test(ch) || !e ? ch : `${ch}${e}`).join("");
|
|
3433
|
+
}).join("").trim();
|
|
3434
|
+
}
|
|
3435
|
+
function emphSafe(n, wrap) {
|
|
3436
|
+
const plain = wrap(phrasingChildren(n));
|
|
3437
|
+
if (!n.children.some((c) => _chunkPCSVBPORcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _318 => _318.marks, 'optionalAccess', _319 => _319.some, 'call', _320 => _320((m) => EMPH_MARKS.has(m.name))])))
|
|
3438
|
+
return plain;
|
|
3439
|
+
const want = emphSig(n.children);
|
|
3440
|
+
const reads = (b) => {
|
|
3441
|
+
const back = mdToDoc(_mdastutiltomarkdown.toMarkdown.call(void 0, { type: "root", children: [b] }, mdOut())).children;
|
|
3442
|
+
const first = back[0];
|
|
3443
|
+
return back.length === 1 && !!first && !_chunkPCSVBPORcjs.isText.call(void 0, first) && first.type === n.type && emphSig(first.children) === want;
|
|
3444
|
+
};
|
|
3445
|
+
if (reads(plain))
|
|
3446
|
+
return plain;
|
|
3447
|
+
const tagged = wrap(phrasingChildren(n, false, true));
|
|
3448
|
+
return reads(tagged) ? tagged : plain;
|
|
3449
|
+
}
|
|
3450
|
+
function nodeToMdast(n) {
|
|
3451
|
+
switch (n.type) {
|
|
3452
|
+
case "heading": {
|
|
3453
|
+
const depth = _nullishCoalesce(_optionalChain([n, 'access', _321 => _321.attrs, 'optionalAccess', _322 => _322.depth]), () => ( 1));
|
|
3454
|
+
const align = asAlign(_optionalChain([n, 'access', _323 => _323.attrs, 'optionalAccess', _324 => _324.align]));
|
|
3455
|
+
if (align && align !== "left")
|
|
3456
|
+
return { type: "html", value: `<h${depth} style="text-align:${align}">${leaves(n).filter((l) => !isEmptyLeaf(l)).map(leafToHtml).join("")}</h${depth}>` };
|
|
3457
|
+
return emphSafe(trimEdgeBreaks(n), (children) => ({ type: "heading", depth, children }));
|
|
3458
|
+
}
|
|
3459
|
+
case "paragraph": {
|
|
3460
|
+
const align = asAlign(_optionalChain([n, 'access', _325 => _325.attrs, 'optionalAccess', _326 => _326.align]));
|
|
3461
|
+
if (align && align !== "left")
|
|
3462
|
+
return { type: "html", value: `<p style="text-align:${align}">${leaves(n).filter((l) => !isEmptyLeaf(l)).map(leafToHtml).join("")}</p>` };
|
|
3463
|
+
return emphSafe(n, (children) => ({ type: "paragraph", children }));
|
|
3464
|
+
}
|
|
3465
|
+
case "quote":
|
|
3466
|
+
if (_chunkPCSVBPORcjs.isFootnoteDef.call(void 0, n)) {
|
|
3467
|
+
const label = String(_nullishCoalesce(_optionalChain([n, 'access', _327 => _327.attrs, 'optionalAccess', _328 => _328.footnote]), () => ( "")));
|
|
3468
|
+
return { type: "footnoteDefinition", identifier: _chunkPCSVBPORcjs.footnoteKey.call(void 0, label), label, children: n.children.filter(_chunkPCSVBPORcjs.isElement).filter((c) => !isEmptyParagraph(c)).map(nodeToMdast) };
|
|
3469
|
+
}
|
|
3470
|
+
return _chunkPCSVBPORcjs.isCallout.call(void 0, n) ? { type: "html", value: calloutToHtml(n) } : { type: "blockquote", children: n.children.filter(_chunkPCSVBPORcjs.isElement).map(nodeToMdast) };
|
|
3471
|
+
// spread:读进来是松散列表(项间有空行)就写回松散,否则紧凑。不写 spread,mdast-util-to-markdown 会把每个紧凑列表写成松散的。
|
|
3472
|
+
case "list": {
|
|
3473
|
+
const html = typedListToHtml(n);
|
|
3474
|
+
if (html)
|
|
3475
|
+
return { type: "html", value: html };
|
|
3476
|
+
const spread = !!_optionalChain([n, 'access', _329 => _329.attrs, 'optionalAccess', _330 => _330.spread]);
|
|
3477
|
+
const needsBlank = (i) => i.children.some((c, k) => k > 0 && _chunkPCSVBPORcjs.isElement.call(void 0, i.children[k - 1]) && i.children[k - 1].type === "paragraph" && startsEmptyItem(c));
|
|
3478
|
+
return { type: "list", ordered: !!_optionalChain([n, 'access', _331 => _331.attrs, 'optionalAccess', _332 => _332.ordered]), start: _nullishCoalesce(_optionalChain([n, 'access', _333 => _333.attrs, 'optionalAccess', _334 => _334.start]), () => ( null)), spread, children: n.children.filter(_chunkPCSVBPORcjs.isElement).map((i) => {
|
|
3479
|
+
const li = nodeToMdast(i);
|
|
3480
|
+
return { ...li, spread: spread || needsBlank(i) || !!li.spread };
|
|
3481
|
+
}) };
|
|
3482
|
+
}
|
|
3483
|
+
case "list_item": {
|
|
3484
|
+
const kids = n.children.filter(_chunkPCSVBPORcjs.isElement).map((c, i) => i === 0 ? trimLeadBreak(c) : c);
|
|
3485
|
+
const checked = _nullishCoalesce(_optionalChain([n, 'access', _335 => _335.attrs, 'optionalAccess', _336 => _336.checked]), () => ( null));
|
|
3486
|
+
if (checked !== null && (!kids[0] || kids[0].type !== "paragraph" || isEmptyParagraph(kids[0]))) {
|
|
3487
|
+
const rest = kids[0] && isEmptyParagraph(kids[0]) ? kids.slice(1) : kids;
|
|
3488
|
+
const box = { type: "paragraph", children: [{ type: "html", value: checked ? "[x]" : "[ ]" }] };
|
|
3489
|
+
return { type: "listItem", checked: null, spread: rest.length > 0, children: [box, ...rest.map(nodeToMdast)] };
|
|
3490
|
+
}
|
|
3491
|
+
return { type: "listItem", checked, children: kids.length === 1 && isEmptyParagraph(kids[0]) ? [] : kids.map(nodeToMdast) };
|
|
3492
|
+
}
|
|
3493
|
+
// 代码块只有一个叶,但编辑器的哪条路径漏了合并,多出来的叶也是屏幕上的字:全写,不能只写第一个(生产里没有不变量闸门,只写第一个就是静默丢字)
|
|
3494
|
+
case "code":
|
|
3495
|
+
return { type: "code", lang: _nullishCoalesce(_optionalChain([n, 'access', _337 => _337.attrs, 'optionalAccess', _338 => _338.lang]), () => ( null)), meta: _nullishCoalesce(_optionalChain([n, 'access', _339 => _339.attrs, 'optionalAccess', _340 => _340.meta]), () => ( null)), value: leaves(n).map((l) => l.text).join("") };
|
|
3496
|
+
case "table":
|
|
3497
|
+
return tableHasExtras(n) ? { type: "html", value: tableToHtml(n) } : tableToMdast(n);
|
|
3498
|
+
case "image": {
|
|
3499
|
+
const a = _nullishCoalesce(n.attrs, () => ( {}));
|
|
3500
|
+
const url = String(_nullishCoalesce(a.url, () => ( "")));
|
|
3501
|
+
const alt = String(_nullishCoalesce(a.alt, () => ( "")));
|
|
3502
|
+
const caption = typeof a.caption === "string" ? a.caption : "";
|
|
3503
|
+
const align = a.align === "center" || a.align === "right" ? a.align : "";
|
|
3504
|
+
if (caption)
|
|
3505
|
+
return { type: "html", value: figureHtml(url, alt, a, caption, align) };
|
|
3506
|
+
if (a.w != null || a.h != null || align)
|
|
3507
|
+
return { type: "html", value: imgHtml(url, alt, a, align) };
|
|
3508
|
+
return { type: "paragraph", children: [{ type: "image", url, title: _nullishCoalesce(a.title, () => ( null)), alt }] };
|
|
3509
|
+
}
|
|
3510
|
+
case "embed":
|
|
3511
|
+
return { type: "html", value: embedHtml(n) };
|
|
3512
|
+
case "raw": {
|
|
3513
|
+
if (_optionalChain([n, 'access', _341 => _341.attrs, 'optionalAccess', _342 => _342.mdType]) === "thematicBreak")
|
|
3514
|
+
return { type: "thematicBreak", data: { marker: n.attrs.marker } };
|
|
3515
|
+
if (_optionalChain([n, 'access', _343 => _343.attrs, 'optionalAccess', _344 => _344.mdType]) === "definition" && typeof n.attrs.url === "string" && typeof n.attrs.label === "string")
|
|
3516
|
+
return { type: "definition", identifier: normLabel(n.attrs.label), label: n.attrs.label, url: n.attrs.url, title: typeof n.attrs.title === "string" ? n.attrs.title : null };
|
|
3517
|
+
const raw = _nullishCoalesce(_optionalChain([leaves, 'call', _345 => _345(n), 'access', _346 => _346[0], 'optionalAccess', _347 => _347.text]), () => ( ""));
|
|
3518
|
+
return { type: "html", value: raw };
|
|
3519
|
+
}
|
|
3520
|
+
default:
|
|
3521
|
+
return { type: "paragraph", children: leaves(n).map(textToPhrasing) };
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
function startsEmptyItem(n) {
|
|
3525
|
+
if (!n || !_chunkPCSVBPORcjs.isElement.call(void 0, n))
|
|
3526
|
+
return false;
|
|
3527
|
+
if (n.type === "list")
|
|
3528
|
+
return opensEmpty(n.children[0]);
|
|
3529
|
+
return n.type === "quote" && !_chunkPCSVBPORcjs.isCallout.call(void 0, n) && !_chunkPCSVBPORcjs.isFootnoteDef.call(void 0, n) && startsEmptyItem(n.children[0]);
|
|
3530
|
+
}
|
|
3531
|
+
function opensEmpty(i) {
|
|
3532
|
+
const first = i && _chunkPCSVBPORcjs.isElement.call(void 0, i) ? i.children[0] : void 0;
|
|
3533
|
+
return !!first && _chunkPCSVBPORcjs.isElement.call(void 0, first) && first.type === "paragraph" && first.children.every((t) => _chunkPCSVBPORcjs.isText.call(void 0, t) && t.text === "");
|
|
3534
|
+
}
|
|
3535
|
+
function eq(a, b) {
|
|
3536
|
+
if (a === b)
|
|
3537
|
+
return true;
|
|
3538
|
+
if (typeof a !== "object" || typeof b !== "object" || !a || !b)
|
|
3539
|
+
return false;
|
|
3540
|
+
if (Array.isArray(a) || Array.isArray(b))
|
|
3541
|
+
return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((x, i) => eq(x, b[i]));
|
|
3542
|
+
const ig = (k) => k === "span" || k === "id";
|
|
3543
|
+
const empty = (v) => v === void 0 || typeof v === "object" && v !== null && !Array.isArray(v) && Object.entries(v).every(([k, x]) => ig(k) || x === void 0);
|
|
3544
|
+
const ak = Object.keys(a).filter((k) => !ig(k) && !(k === "attrs" ? empty : (v) => v === void 0)(a[k]));
|
|
3545
|
+
const bk = Object.keys(b).filter((k) => !ig(k) && !(k === "attrs" ? empty : (v) => v === void 0)(b[k]));
|
|
3546
|
+
return ak.length === bk.length && ak.every((k) => eq(a[k], b[k]));
|
|
3547
|
+
}
|
|
3548
|
+
var cleanCache = /* @__PURE__ */ new WeakMap();
|
|
3549
|
+
var docLinkLabels = [];
|
|
3550
|
+
var normLabel = (l) => l.replace(/\s+/g, " ").trim().toLowerCase();
|
|
3551
|
+
function linkLabels(n, out = []) {
|
|
3552
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, n))
|
|
3553
|
+
return out;
|
|
3554
|
+
if (n.type === "raw" && _optionalChain([n, 'access', _348 => _348.attrs, 'optionalAccess', _349 => _349.mdType]) === "definition" && typeof n.attrs.label === "string")
|
|
3555
|
+
out.push(n.attrs.label);
|
|
3556
|
+
n.children.forEach((c) => linkLabels(c, out));
|
|
3557
|
+
return out;
|
|
3558
|
+
}
|
|
3559
|
+
function clean(block, srcMd) {
|
|
3560
|
+
const hit = cleanCache.get(block);
|
|
3561
|
+
const labels = docLinkLabels.join("\n");
|
|
3562
|
+
if (hit && hit.src === srcMd && hit.labels === labels)
|
|
3563
|
+
return hit.clean;
|
|
3564
|
+
const verdict = cleanUncached(block, srcMd);
|
|
3565
|
+
cleanCache.set(block, { src: srcMd, labels, clean: verdict });
|
|
3566
|
+
return verdict;
|
|
3567
|
+
}
|
|
3568
|
+
function footnoteLabels(n) {
|
|
3569
|
+
const out = /* @__PURE__ */ new Set();
|
|
3570
|
+
const walk = (x) => {
|
|
3571
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, x))
|
|
3572
|
+
return;
|
|
3573
|
+
if (x.type === "footnote_ref")
|
|
3574
|
+
out.add(String(_nullishCoalesce(_optionalChain([x, 'access', _350 => _350.attrs, 'optionalAccess', _351 => _351.label]), () => ( ""))));
|
|
3575
|
+
x.children.forEach(walk);
|
|
3576
|
+
};
|
|
3577
|
+
walk(n);
|
|
3578
|
+
return [...out];
|
|
3579
|
+
}
|
|
3580
|
+
function cleanUncached(block, srcMd) {
|
|
3581
|
+
const s = _optionalChain([block, 'access', _352 => _352.attrs, 'optionalAccess', _353 => _353.span]);
|
|
3582
|
+
if (!s)
|
|
3583
|
+
return false;
|
|
3584
|
+
return readsAs(srcMd.slice(lineLead(srcMd, s[0]), s[1]), block);
|
|
3585
|
+
}
|
|
3586
|
+
function lineLead(srcMd, at) {
|
|
3587
|
+
let i = at;
|
|
3588
|
+
while (i > 0 && (srcMd[i - 1] === " " || srcMd[i - 1] === " "))
|
|
3589
|
+
i--;
|
|
3590
|
+
return i === 0 || srcMd[i - 1] === "\n" ? i : at;
|
|
3591
|
+
}
|
|
3592
|
+
function readsAs(slice, block) {
|
|
3593
|
+
const flat = normLabel(slice);
|
|
3594
|
+
const defs = [...footnoteLabels(block).map((l) => `[^${l}]: x`), ...docLinkLabels.filter((l) => flat.includes(normLabel(l))).map((l) => `[${l}]: u`)];
|
|
3595
|
+
const reparsed = defs.length ? mdToDoc(slice + defs.map((d) => `
|
|
3596
|
+
|
|
3597
|
+
${d}`).join("")).children.filter((c) => !_chunkPCSVBPORcjs.isElement.call(void 0, c) || (_nullishCoalesce(_optionalChain([c, 'access', _354 => _354.attrs, 'optionalAccess', _355 => _355.span, 'optionalAccess', _356 => _356[0]]), () => ( 0))) < slice.length) : mdToDoc(slice).children;
|
|
3598
|
+
if (reparsed.length !== 1)
|
|
3599
|
+
return false;
|
|
3600
|
+
const only = reparsed[0];
|
|
3601
|
+
return !_chunkPCSVBPORcjs.isText.call(void 0, only) && eq(block, only);
|
|
3602
|
+
}
|
|
3603
|
+
var spanOf = (n) => _optionalChain([n, 'access', _357 => _357.attrs, 'optionalAccess', _358 => _358.span]);
|
|
3604
|
+
var origKey = (type, s) => `${type}@${s[0]},${s[1]}`;
|
|
3605
|
+
var LIST_MARKER = /^([-+*]|\d{1,9}[.)])( {1,4})(\[[ xX]\] +)?$/;
|
|
3606
|
+
var markerOf = (text) => {
|
|
3607
|
+
const m = /^ *(?:([-*+])|\d{1,9}([.)]))/.exec(text);
|
|
3608
|
+
return m ? _nullishCoalesce(_nullishCoalesce(m[1], () => ( m[2])), () => ( null)) : null;
|
|
3609
|
+
};
|
|
3610
|
+
function prefixLen(line, chain, lineAt) {
|
|
3611
|
+
let at = 0;
|
|
3612
|
+
for (const a of chain) {
|
|
3613
|
+
if (a.quote) {
|
|
3614
|
+
const m = /^ {0,3}> ?/.exec(line.slice(at));
|
|
3615
|
+
if (!m)
|
|
3616
|
+
return at;
|
|
3617
|
+
at += m[0].length;
|
|
3618
|
+
continue;
|
|
3619
|
+
}
|
|
3620
|
+
if (lineAt === a.line) {
|
|
3621
|
+
at += a.width;
|
|
3622
|
+
continue;
|
|
3623
|
+
}
|
|
3624
|
+
const sp = /^ */.exec(line.slice(at, at + a.width))[0].length;
|
|
3625
|
+
at += sp;
|
|
3626
|
+
if (sp < a.width && at < line.length)
|
|
3627
|
+
return at;
|
|
3628
|
+
}
|
|
3629
|
+
return at;
|
|
3630
|
+
}
|
|
3631
|
+
function indexOrig(n, chain, src, base, out) {
|
|
3632
|
+
const s = spanOf(n);
|
|
3633
|
+
if (!s)
|
|
3634
|
+
return;
|
|
3635
|
+
const o = { node: n, chain, src };
|
|
3636
|
+
out.set(origKey(n.type, [s[0] + base, s[1] + base]), o);
|
|
3637
|
+
out.set(origKey("", [s[0] + base, s[1] + base]), o);
|
|
3638
|
+
out.set(n, o);
|
|
3639
|
+
if (n.type === "quote" && !_chunkPCSVBPORcjs.isCallout.call(void 0, n) && !_chunkPCSVBPORcjs.isFootnoteDef.call(void 0, n)) {
|
|
3640
|
+
const after = src[s[0] + 1];
|
|
3641
|
+
o.space = after === void 0 || after === " " || after === "\n" ? " " : "";
|
|
3642
|
+
const inner = [...chain, { quote: true, width: 0, line: -1 }];
|
|
3643
|
+
for (const c of n.children) {
|
|
3644
|
+
if (_chunkPCSVBPORcjs.isElement.call(void 0, c))
|
|
3645
|
+
indexOrig(c, inner, src, base, out);
|
|
3646
|
+
}
|
|
3647
|
+
return;
|
|
3648
|
+
}
|
|
3649
|
+
if (n.type !== "list")
|
|
3650
|
+
return;
|
|
3651
|
+
for (const it of n.children) {
|
|
3652
|
+
const is = _chunkPCSVBPORcjs.isElement.call(void 0, it) ? spanOf(it) : void 0;
|
|
3653
|
+
if (!is || !_chunkPCSVBPORcjs.isElement.call(void 0, it))
|
|
3654
|
+
continue;
|
|
3655
|
+
const io = { node: it, chain, src };
|
|
3656
|
+
out.set(origKey(it.type, [is[0] + base, is[1] + base]), io);
|
|
3657
|
+
out.set(origKey("", [is[0] + base, is[1] + base]), io);
|
|
3658
|
+
out.set(it, io);
|
|
3659
|
+
const first = it.children.find(_chunkPCSVBPORcjs.isElement);
|
|
3660
|
+
const fs = first && spanOf(first);
|
|
3661
|
+
const ls = src.lastIndexOf("\n", is[0] - 1) + 1;
|
|
3662
|
+
const p = ls + prefixLen(src.slice(ls, is[0]), chain, ls);
|
|
3663
|
+
const lead = src.slice(p, is[0]);
|
|
3664
|
+
const marker = fs ? src.slice(is[0], fs[0]) : "";
|
|
3665
|
+
if (!fs || !/^ {0,3}$/.test(lead) || !LIST_MARKER.test(marker))
|
|
3666
|
+
continue;
|
|
3667
|
+
io.lead = lead;
|
|
3668
|
+
io.marker = marker;
|
|
3669
|
+
const inner = [...chain, { quote: false, width: lead.length + marker.length, line: ls }];
|
|
3670
|
+
for (const c of it.children) {
|
|
3671
|
+
if (_chunkPCSVBPORcjs.isElement.call(void 0, c))
|
|
3672
|
+
indexOrig(c, inner, src, base, out);
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
function dedent(o) {
|
|
3677
|
+
const s = spanOf(o.node);
|
|
3678
|
+
const lines = o.src.slice(s[0], s[1]).split("\n");
|
|
3679
|
+
for (let i = 1; i < lines.length; i++) {
|
|
3680
|
+
const cut = prefixLen(lines[i], o.chain, -1);
|
|
3681
|
+
if (lines[i].slice(0, cut + 1).includes(" "))
|
|
3682
|
+
return null;
|
|
3683
|
+
lines[i] = lines[i].slice(cut);
|
|
3684
|
+
}
|
|
3685
|
+
return lines.join("\n");
|
|
3686
|
+
}
|
|
3687
|
+
function origOf(n, ctx) {
|
|
3688
|
+
const s = spanOf(n);
|
|
3689
|
+
return s && ctx.orig.get(origKey(n.type, s));
|
|
3690
|
+
}
|
|
3691
|
+
function emitNode(n, ctx) {
|
|
3692
|
+
const o = origOf(n, ctx);
|
|
3693
|
+
if (o && n !== ctx.root && eq(n, o.node)) {
|
|
3694
|
+
const t = dedent(o);
|
|
3695
|
+
if (t != null)
|
|
3696
|
+
return t;
|
|
3697
|
+
}
|
|
3698
|
+
if (n.type === "list" && !typedListToHtml(n))
|
|
3699
|
+
return emitList(n, o, ctx);
|
|
3700
|
+
if (n.type === "quote" && !_chunkPCSVBPORcjs.isCallout.call(void 0, n) && !_chunkPCSVBPORcjs.isFootnoteDef.call(void 0, n))
|
|
3701
|
+
return emitQuote(n, o, ctx);
|
|
3702
|
+
return emitLeaf(n, o);
|
|
3703
|
+
}
|
|
3704
|
+
function siblingGap(a, b, parent, ctx, fallback) {
|
|
3705
|
+
const at = (n) => _nullishCoalesce(origOf(n, ctx), () => ( (spanOf(n) ? ctx.orig.get(origKey("", spanOf(n))) : void 0)));
|
|
3706
|
+
const oa = at(a);
|
|
3707
|
+
const ob = at(b);
|
|
3708
|
+
const kids = parent ? parent.node.children : [];
|
|
3709
|
+
const ia = oa ? kids.indexOf(oa.node) : -1;
|
|
3710
|
+
if (ia < 0 || !ob || kids.indexOf(ob.node) !== ia + 1)
|
|
3711
|
+
return "\n".repeat(fallback + 1);
|
|
3712
|
+
const between = oa.src.slice(spanOf(oa.node)[1], spanOf(ob.node)[0]);
|
|
3713
|
+
return "\n".repeat(Math.max(1, between.split("\n").length - 1));
|
|
3714
|
+
}
|
|
3715
|
+
function emitKids(kids, parent, ctx, fallback) {
|
|
3716
|
+
const gap = (l, r) => {
|
|
3717
|
+
const g = siblingGap(l, r, parent, ctx, fallback(l, r));
|
|
3718
|
+
return g === "\n" && l.type === "paragraph" && startsEmptyItem(r) ? "\n\n" : g;
|
|
3719
|
+
};
|
|
3720
|
+
return kids.map((k, i) => (i ? gap(kids[i - 1], k) : "") + emitNode(k, ctx)).join("");
|
|
3721
|
+
}
|
|
3722
|
+
function emitQuote(n, o, ctx) {
|
|
3723
|
+
const sp = _nullishCoalesce(_optionalChain([o, 'optionalAccess', _359 => _359.space]), () => ( " "));
|
|
3724
|
+
const body = emitKids(n.children.filter(_chunkPCSVBPORcjs.isElement), o, ctx, () => 1);
|
|
3725
|
+
return body.split("\n").map((l) => l === "" ? ">" : `>${sp}${l}`).join("\n");
|
|
3726
|
+
}
|
|
3727
|
+
function emitList(n, o, ctx) {
|
|
3728
|
+
const items = n.children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3729
|
+
const origs = items.map((it) => origOf(it, ctx));
|
|
3730
|
+
const origItems = o ? o.node.children.filter(_chunkPCSVBPORcjs.isElement) : [];
|
|
3731
|
+
const same = !!o && items.length === origItems.length && origs.every((io, i) => _optionalChain([io, 'optionalAccess', _360 => _360.node]) === origItems[i]);
|
|
3732
|
+
const ordered = !!_optionalChain([n, 'access', _361 => _361.attrs, 'optionalAccess', _362 => _362.ordered]);
|
|
3733
|
+
const start = typeof _optionalChain([n, 'access', _363 => _363.attrs, 'optionalAccess', _364 => _364.start]) === "number" ? n.attrs.start : 1;
|
|
3734
|
+
const model = _nullishCoalesce(origs.find((io) => _optionalChain([io, 'optionalAccess', _365 => _365.marker]) && origItems.includes(io.node)), () => ( origs.find((io) => _optionalChain([io, 'optionalAccess', _366 => _366.marker]))));
|
|
3735
|
+
const mm = model ? LIST_MARKER.exec(model.marker) : null;
|
|
3736
|
+
const isNum = (m) => !!m && /\d/.test(m[1]);
|
|
3737
|
+
let sym = ordered ? isNum(mm) ? mm[1].slice(-1) : "." : mm && !isNum(mm) ? mm[1] : "-";
|
|
3738
|
+
if (n === ctx.root && ctx.avoid.includes(sym))
|
|
3739
|
+
sym = _nullishCoalesce((ordered ? [".", ")"] : ["-", "*", "+"]).find((m) => !ctx.avoid.includes(m)), () => ( sym));
|
|
3740
|
+
const nums = origItems.map((it) => Number.parseInt(_nullishCoalesce(_optionalChain([ctx, 'access', _367 => _367.orig, 'access', _368 => _368.get, 'call', _369 => _369(it), 'optionalAccess', _370 => _370.marker]), () => ( "")), 10));
|
|
3741
|
+
const constNum = nums.length > 1 && nums.every((x) => x === nums[0]);
|
|
3742
|
+
const loose = !!_optionalChain([n, 'access', _371 => _371.attrs, 'optionalAccess', _372 => _372.spread]);
|
|
3743
|
+
return items.map((it, i) => {
|
|
3744
|
+
const io = origs[i];
|
|
3745
|
+
const m = _optionalChain([io, 'optionalAccess', _373 => _373.marker]) ? LIST_MARKER.exec(io.marker) : null;
|
|
3746
|
+
const lead = _nullishCoalesce(_nullishCoalesce(_optionalChain([io, 'optionalAccess', _374 => _374.lead]), () => ( _optionalChain([model, 'optionalAccess', _375 => _375.lead]))), () => ( ""));
|
|
3747
|
+
const gap = _nullishCoalesce(_nullishCoalesce(_optionalChain([m, 'optionalAccess', _376 => _376[2]]), () => ( _optionalChain([mm, 'optionalAccess', _377 => _377[2]]))), () => ( " "));
|
|
3748
|
+
const head = !ordered ? sym : `${same && isNum(m) ? m[1].slice(0, -1) : constNum ? start : start + i}${sym}`;
|
|
3749
|
+
const checked = _optionalChain([it, 'access', _378 => _378.attrs, 'optionalAccess', _379 => _379.checked]);
|
|
3750
|
+
const box = checked == null ? "" : _optionalChain([m, 'optionalAccess', _380 => _380[3]]) && m[3][1] !== " " === checked ? m[3] : checked ? "[x] " : "[ ] ";
|
|
3751
|
+
const marker = lead + head + gap + box;
|
|
3752
|
+
const body = emitKids(it.children.filter(_chunkPCSVBPORcjs.isElement), io, ctx, (l, r) => loose || l.type === "list" || l.type === "paragraph" && r.type === "paragraph" ? 1 : 0);
|
|
3753
|
+
const pad = " ".repeat(marker.length);
|
|
3754
|
+
const lines = body.split("\n");
|
|
3755
|
+
const text = (lines[0] === "" ? marker.trimEnd() : marker + lines[0]) + lines.slice(1).map((l) => `
|
|
3756
|
+
${l === "" ? "" : pad + l}`).join("");
|
|
3757
|
+
return (i ? siblingGap(items[i - 1], it, o, ctx, loose ? 1 : 0) : "") + text;
|
|
3758
|
+
}).join("");
|
|
3759
|
+
}
|
|
3760
|
+
function emitLeaf(n, o) {
|
|
3761
|
+
const opts = {};
|
|
3762
|
+
const was = o && dedent(o);
|
|
3763
|
+
if (was) {
|
|
3764
|
+
if (n.type === "heading" && !/^ {0,3}#/.test(was))
|
|
3765
|
+
opts.setext = true;
|
|
3766
|
+
if (n.type === "code" && /^ {0,3}~/.test(was))
|
|
3767
|
+
opts.fence = "~";
|
|
3768
|
+
if (n.type === "paragraph" || n.type === "heading")
|
|
3769
|
+
Object.assign(opts, attentionChars(was));
|
|
3770
|
+
}
|
|
3771
|
+
if (was && n.type === "table" && o.node.type === "table" && !tableHasExtras(n))
|
|
3772
|
+
return emitTable(n, o.node, was);
|
|
3773
|
+
const t = blockMd(n, { ...mdOut(), ...opts });
|
|
3774
|
+
return n.type === "paragraph" || n.type === "heading" ? fewerEscapes(t, n) : t;
|
|
3775
|
+
}
|
|
3776
|
+
function emitTable(n, orig, was) {
|
|
3777
|
+
const cols = Number(_nullishCoalesce(_optionalChain([n, 'access', _381 => _381.attrs, 'optionalAccess', _382 => _382.cols]), () => ( 1)));
|
|
3778
|
+
const fresh = _mdastutiltomarkdown.toMarkdown.call(void 0, { type: "root", children: [nodeToMdast(n)] }, { ...mdOut(), extensions: [_mdastutilgfm.gfmToMarkdown.call(void 0, { tablePipeAlign: false })] }).trimEnd().split("\n");
|
|
3779
|
+
const lines = was.split("\n");
|
|
3780
|
+
if (cols !== Number(_nullishCoalesce(_optionalChain([orig, 'access', _383 => _383.attrs, 'optionalAccess', _384 => _384.cols]), () => ( 1))) || lines.length !== Math.ceil(orig.children.length / cols) + 1)
|
|
3781
|
+
return fresh.join("\n");
|
|
3782
|
+
const row = (t, r) => t.children.slice(r * cols, (r + 1) * cols);
|
|
3783
|
+
const sameAlign = JSON.stringify(_nullishCoalesce(_optionalChain([n, 'access', _385 => _385.attrs, 'optionalAccess', _386 => _386.align]), () => ( null))) === JSON.stringify(_nullishCoalesce(_optionalChain([orig, 'access', _387 => _387.attrs, 'optionalAccess', _388 => _388.align]), () => ( null)));
|
|
3784
|
+
return fresh.map((line, i) => {
|
|
3785
|
+
if (i === 1)
|
|
3786
|
+
return sameAlign ? lines[1] : line;
|
|
3787
|
+
const r = i === 0 ? 0 : i - 1;
|
|
3788
|
+
if (r >= lines.length - 1)
|
|
3789
|
+
return line;
|
|
3790
|
+
const a = row(n, r);
|
|
3791
|
+
const b = row(orig, r);
|
|
3792
|
+
if (eq(a, b))
|
|
3793
|
+
return lines[i];
|
|
3794
|
+
const was2 = pipeCells(lines[i]);
|
|
3795
|
+
const now = pipeCells(line);
|
|
3796
|
+
if (!was2 || !now || was2.cells.length !== cols || now.cells.length !== cols)
|
|
3797
|
+
return line;
|
|
3798
|
+
return was2.lead + was2.cells.map((c, j) => {
|
|
3799
|
+
if (eq(a[j], b[j]))
|
|
3800
|
+
return c;
|
|
3801
|
+
const body = now.cells[j].trim();
|
|
3802
|
+
const pad = c.length - 1 - body.length;
|
|
3803
|
+
return ` ${body}${" ".repeat(Math.max(1, pad))}`;
|
|
3804
|
+
}).join("|") + was2.tail;
|
|
3805
|
+
}).join("\n");
|
|
3806
|
+
}
|
|
3807
|
+
function pipeCells(line) {
|
|
3808
|
+
const at = [];
|
|
3809
|
+
for (let i = 0; i < line.length; i++) {
|
|
3810
|
+
if (line[i] === "\\")
|
|
3811
|
+
i++;
|
|
3812
|
+
else if (line[i] === "|")
|
|
3813
|
+
at.push(i);
|
|
3814
|
+
}
|
|
3815
|
+
if (at.length < 2 || line.slice(0, at[0]).trim() !== "" || line.slice(at[at.length - 1] + 1).trim() !== "")
|
|
3816
|
+
return null;
|
|
3817
|
+
return { lead: line.slice(0, at[0] + 1), cells: at.slice(1).map((e, k) => line.slice(at[k] + 1, e)), tail: line.slice(at[at.length - 1]) };
|
|
3818
|
+
}
|
|
3819
|
+
function attentionChars(src) {
|
|
3820
|
+
const out = {};
|
|
3821
|
+
const walk = (x) => {
|
|
3822
|
+
if ((x.type === "emphasis" || x.type === "strong") && !out[x.type] && x.position) {
|
|
3823
|
+
const ch = src[x.position.start.offset];
|
|
3824
|
+
if (ch === "_" || ch === "*")
|
|
3825
|
+
out[x.type] = ch;
|
|
3826
|
+
}
|
|
3827
|
+
_optionalChain([x, 'access', _389 => _389.children, 'optionalAccess', _390 => _390.forEach, 'call', _391 => _391(walk)]);
|
|
3828
|
+
};
|
|
3829
|
+
walk(_mdastutilfrommarkdown.fromMarkdown.call(void 0, src, parseExt()));
|
|
3830
|
+
return out;
|
|
3831
|
+
}
|
|
3832
|
+
var ESCAPED = /\\([!-/:-@[-`{-~])/g;
|
|
3833
|
+
function fewerEscapes(t, n) {
|
|
3834
|
+
if (!t.includes("\\"))
|
|
3835
|
+
return t;
|
|
3836
|
+
const none = t.replace(ESCAPED, "$1");
|
|
3837
|
+
if (none !== t && readsAs(none, n))
|
|
3838
|
+
return none;
|
|
3839
|
+
let tries = 0;
|
|
3840
|
+
for (let i = t.indexOf("\\"); i >= 0 && tries < 64; i = t.indexOf("\\", i + 1)) {
|
|
3841
|
+
if (!/[!-/:-@[-`{-~]/.test(_nullishCoalesce(t[i + 1], () => ( ""))))
|
|
3842
|
+
continue;
|
|
3843
|
+
tries++;
|
|
3844
|
+
const cand = t.slice(0, i) + t.slice(i + 1);
|
|
3845
|
+
if (readsAs(cand, n))
|
|
3846
|
+
t = cand;
|
|
3847
|
+
}
|
|
3848
|
+
return t;
|
|
3849
|
+
}
|
|
3850
|
+
function imageGroup(blocks, idx, srcMd) {
|
|
3851
|
+
const first = blocks[idx];
|
|
3852
|
+
const s = _chunkPCSVBPORcjs.isElement.call(void 0, first) ? activeSpan(first, srcMd) : void 0;
|
|
3853
|
+
if (!s || !_chunkPCSVBPORcjs.isElement.call(void 0, first))
|
|
3854
|
+
return null;
|
|
3855
|
+
const slice = srcMd.slice(s[0], s[1]);
|
|
3856
|
+
const root = _mdastutilfrommarkdown.fromMarkdown.call(void 0, slice, parseExt()).children;
|
|
3857
|
+
if (root.length !== 1 || root[0].type !== "paragraph")
|
|
3858
|
+
return null;
|
|
3859
|
+
const was = mdToDoc(slice).children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3860
|
+
if (was.length < 2 || !was.some((b) => b.type === "image"))
|
|
3861
|
+
return null;
|
|
3862
|
+
const group = blocks.slice(idx, idx + was.length).filter(_chunkPCSVBPORcjs.isElement);
|
|
3863
|
+
if (group.length !== was.length || group.some((b, i) => b.type !== was[i].type || i > 0 && activeSpan(b, srcMd)))
|
|
3864
|
+
return null;
|
|
3865
|
+
if (group.every((b, i) => eq(b, was[i])))
|
|
3866
|
+
return { n: group.length, text: slice, clean: true };
|
|
3867
|
+
const text = group.map((b) => _mdastutiltomarkdown.toMarkdown.call(void 0, { type: "root", children: [nodeToMdast(b)] }, mdOut()).trimEnd()).join(" ");
|
|
3868
|
+
const back = mdToDoc(text).children.filter(_chunkPCSVBPORcjs.isElement);
|
|
3869
|
+
return back.length === group.length && back.every((b, i) => eq(b, group[i])) ? { n: group.length, text, clean: false } : null;
|
|
3870
|
+
}
|
|
3871
|
+
function saveBlock(block, srcMd, s, avoid) {
|
|
3872
|
+
if (!s)
|
|
3873
|
+
return null;
|
|
3874
|
+
const ctx = { orig: /* @__PURE__ */ new Map(), root: block, avoid };
|
|
3875
|
+
const from = lineLead(srcMd, s[0]);
|
|
3876
|
+
const slice = srcMd.slice(from, s[1]);
|
|
3877
|
+
const kids = mdToDoc(slice).children;
|
|
3878
|
+
if (kids.length !== 1 || !_chunkPCSVBPORcjs.isElement.call(void 0, kids[0]) || kids[0].type !== block.type)
|
|
3879
|
+
return null;
|
|
3880
|
+
indexOrig(kids[0], [], slice, from, ctx.orig);
|
|
3881
|
+
const t = emitNode(block, ctx);
|
|
3882
|
+
return readsAs(t, block) ? t : null;
|
|
3883
|
+
}
|
|
3884
|
+
var isOpaqueBlock = (n) => !_chunkPCSVBPORcjs.isText.call(void 0, n) && (n.type === "table" || n.type === "image" || n.type === "embed" || n.type === "raw" && _optionalChain([n, 'access', _392 => _392.attrs, 'optionalAccess', _393 => _393.mdType]) === "thematicBreak");
|
|
3885
|
+
var isSynthGapPara = (n) => !_chunkPCSVBPORcjs.isText.call(void 0, n) && n.type === "paragraph" && !_optionalChain([n, 'access', _394 => _394.attrs, 'optionalAccess', _395 => _395.span]) && n.children.length === 1 && _chunkPCSVBPORcjs.isText.call(void 0, n.children[0]) && n.children[0].text === "";
|
|
3886
|
+
function trimSynthGapPara(children) {
|
|
3887
|
+
return children.filter((n, i) => {
|
|
3888
|
+
if (!isSynthGapPara(n))
|
|
3889
|
+
return true;
|
|
3890
|
+
const prev = children[i - 1];
|
|
3891
|
+
const next = children[i + 1];
|
|
3892
|
+
return !(prev && isOpaqueBlock(prev) || next && isOpaqueBlock(next));
|
|
3893
|
+
});
|
|
3894
|
+
}
|
|
3895
|
+
function activeSpan(block, srcMd) {
|
|
3896
|
+
if (srcMd === "")
|
|
3897
|
+
return void 0;
|
|
3898
|
+
const s = _optionalChain([block, 'access', _396 => _396.attrs, 'optionalAccess', _397 => _397.span]);
|
|
3899
|
+
if (!s || s.length < 2)
|
|
3900
|
+
return void 0;
|
|
3901
|
+
const lo = s[0];
|
|
3902
|
+
const hi = s[1];
|
|
3903
|
+
if (typeof lo !== "number" || typeof hi !== "number")
|
|
3904
|
+
return void 0;
|
|
3905
|
+
if (lo < 0 || hi < lo || hi > srcMd.length)
|
|
3906
|
+
return void 0;
|
|
3907
|
+
return [lo, hi];
|
|
3908
|
+
}
|
|
3909
|
+
var canonGap = (m) => m === 0 ? "\n\n" : `
|
|
3910
|
+
|
|
3911
|
+
|
|
3912
|
+
|
|
3913
|
+
${" \n\n".repeat(m - 1)}`;
|
|
3914
|
+
var isBlankPara = (n) => n.type === "paragraph" && !_optionalChain([n, 'access', _398 => _398.attrs, 'optionalAccess', _399 => _399.span]) && n.children.every((c) => isEmptyLeaf(c) || isTailBreak(c));
|
|
3915
|
+
function srcMarker(block, srcMd) {
|
|
3916
|
+
const s = activeSpan(block, srcMd);
|
|
3917
|
+
const m = s && /^[ \t]*(?:([-*+])|\d{1,9}([.)]))/.exec(srcMd.slice(s[0], s[0] + 16));
|
|
3918
|
+
return m ? _nullishCoalesce(_nullishCoalesce(m[1], () => ( m[2])), () => ( null)) : null;
|
|
3919
|
+
}
|
|
3920
|
+
var sameRead = (a, b) => eq(mdToDoc(a).children, mdToDoc(b).children);
|
|
3921
|
+
function docToMd(doc, srcMd) {
|
|
3922
|
+
const outer = docLinkLabels;
|
|
3923
|
+
docLinkLabels = linkLabels(doc);
|
|
3924
|
+
try {
|
|
3925
|
+
return docToMdIn(doc, srcMd);
|
|
3926
|
+
} finally {
|
|
3927
|
+
docLinkLabels = outer;
|
|
3928
|
+
}
|
|
3929
|
+
}
|
|
3930
|
+
function docToMdIn(doc, srcMd) {
|
|
3931
|
+
let out = "";
|
|
3932
|
+
let cursor = 0;
|
|
3933
|
+
let blanks = 0;
|
|
3934
|
+
let prev;
|
|
3935
|
+
const blocks = trimSynthGapPara(doc.children);
|
|
3936
|
+
let last = "";
|
|
3937
|
+
let lastClean = false;
|
|
3938
|
+
let lastMarker = null;
|
|
3939
|
+
let skipTo = -1;
|
|
3940
|
+
blocks.forEach((block, idx) => {
|
|
3941
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, block) || idx < skipTo)
|
|
3942
|
+
return;
|
|
3943
|
+
const s = activeSpan(block, srcMd);
|
|
3944
|
+
if (!s && isBlankPara(block)) {
|
|
3945
|
+
blanks++;
|
|
3946
|
+
return;
|
|
3947
|
+
}
|
|
3948
|
+
const group = s && (block.type === "paragraph" || block.type === "image") ? imageGroup(blocks, idx, srcMd) : null;
|
|
3949
|
+
if (group)
|
|
3950
|
+
skipTo = idx + group.n;
|
|
3951
|
+
const joins = (b) => b.type === "list" && _optionalChain([prev, 'optionalAccess', _400 => _400.type]) === "list" && _optionalChain([prev, 'access', _401 => _401.attrs, 'optionalAccess', _402 => _402.ordered]) === _optionalChain([b, 'access', _403 => _403.attrs, 'optionalAccess', _404 => _404.ordered]) && lastMarker !== null && srcMarker(b, srcMd) === lastMarker;
|
|
3952
|
+
const isClean = group ? group.clean : !!s && clean(block, srcMd) && !joins(block);
|
|
3953
|
+
let marker = isClean ? srcMarker(block, srcMd) : null;
|
|
3954
|
+
let body;
|
|
3955
|
+
if (group) {
|
|
3956
|
+
body = group.text;
|
|
3957
|
+
} else if (isClean) {
|
|
3958
|
+
body = srcMd.slice(s[0], s[1]);
|
|
3959
|
+
} else {
|
|
3960
|
+
const opts = { ...mdOut() };
|
|
3961
|
+
let avoid = [];
|
|
3962
|
+
if (block.type === "list") {
|
|
3963
|
+
const next = blocks[idx + 1];
|
|
3964
|
+
avoid = [
|
|
3965
|
+
_optionalChain([prev, 'optionalAccess', _405 => _405.type]) === "list" && _optionalChain([prev, 'access', _406 => _406.attrs, 'optionalAccess', _407 => _407.ordered]) === _optionalChain([block, 'access', _408 => _408.attrs, 'optionalAccess', _409 => _409.ordered]) ? lastMarker : null,
|
|
3966
|
+
next && !_chunkPCSVBPORcjs.isText.call(void 0, next) && next.type === "list" && _optionalChain([next, 'access', _410 => _410.attrs, 'optionalAccess', _411 => _411.ordered]) === _optionalChain([block, 'access', _412 => _412.attrs, 'optionalAccess', _413 => _413.ordered]) && clean(next, srcMd) ? srcMarker(next, srcMd) : null
|
|
3967
|
+
];
|
|
3968
|
+
marker = _nullishCoalesce((_optionalChain([block, 'access', _414 => _414.attrs, 'optionalAccess', _415 => _415.ordered]) ? [".", ")"] : ["-", "*", "+"]).find((m) => !avoid.includes(m)), () => ( null));
|
|
3969
|
+
if (marker && marker !== "-" && marker !== ".")
|
|
3970
|
+
Object.assign(opts, _optionalChain([block, 'access', _416 => _416.attrs, 'optionalAccess', _417 => _417.ordered]) ? { bulletOrdered: marker } : { bullet: marker, bulletOther: marker === "*" ? "+" : "*" });
|
|
3971
|
+
}
|
|
3972
|
+
const kept = saveBlock(block, srcMd, s, avoid);
|
|
3973
|
+
if (kept != null && block.type === "list")
|
|
3974
|
+
marker = markerOf(kept);
|
|
3975
|
+
body = _nullishCoalesce(kept, () => ( blockMd(block, opts)));
|
|
3976
|
+
}
|
|
3977
|
+
const plain = !prev || isOpaqueBlock(prev) || isOpaqueBlock(block);
|
|
3978
|
+
if (s) {
|
|
3979
|
+
const raw = s[0] >= cursor ? srcMd.slice(cursor, s[0]) : null;
|
|
3980
|
+
let gap = raw === null ? canonGap(blanks) : gapOf(raw, out === "");
|
|
3981
|
+
if (!isClean && (block.type === "list" || block.type === "code" || block.type === "raw" || /^[ \t]/.test(body)))
|
|
3982
|
+
gap = gap.replace(/[ \t]+$/, "");
|
|
3983
|
+
const lead = gap.slice(gap.replace(/[ \t]+$/, "").length);
|
|
3984
|
+
if (lead && !(lastClean && isClean && raw === gap) && !readsAs(lead + body, block) && readsAs(body, block))
|
|
3985
|
+
gap = gap.slice(0, -lead.length);
|
|
3986
|
+
if (out !== "" && !plain && blanks !== gapParas(gap))
|
|
3987
|
+
gap = canonGap(blanks);
|
|
3988
|
+
if (out !== "" && !gap.includes("\n\n") && !(lastClean && isClean && raw === gap) && (gap === "" || !sameRead(last + gap + body, `${last}
|
|
3989
|
+
|
|
3990
|
+
${body}`)))
|
|
3991
|
+
gap = "\n\n";
|
|
3992
|
+
if (isClean && _optionalChain([prev, 'optionalAccess', _418 => _418.type]) === "list" && !(lastClean && raw === gap) && /^(?: {4}|\t)/.test(body))
|
|
3993
|
+
body = _mdastutiltomarkdown.toMarkdown.call(void 0, { type: "root", children: [nodeToMdast(block)] }, mdOut()).trimEnd();
|
|
3994
|
+
out += gap;
|
|
3995
|
+
} else if (out && !plain && blanks > 0) {
|
|
3996
|
+
out = out.replace(/\n+$/, "") + canonGap(blanks);
|
|
3997
|
+
} else if (out && !out.endsWith("\n\n")) {
|
|
3998
|
+
out += out.endsWith("\n") ? "\n" : "\n\n";
|
|
3999
|
+
}
|
|
4000
|
+
out += body;
|
|
4001
|
+
if (s)
|
|
4002
|
+
cursor = Math.max(cursor, s[1]);
|
|
4003
|
+
blanks = 0;
|
|
4004
|
+
prev = block;
|
|
4005
|
+
last = body;
|
|
4006
|
+
lastClean = isClean;
|
|
4007
|
+
lastMarker = block.type === "list" ? marker : null;
|
|
4008
|
+
});
|
|
4009
|
+
if (cursor < srcMd.length)
|
|
4010
|
+
out += gapOf(srcMd.slice(cursor), false);
|
|
4011
|
+
if (blanks > 0 && srcMd === "" && out.trim() !== "" && !out.endsWith("\n\n"))
|
|
4012
|
+
out += out.endsWith("\n") ? "\n" : "\n\n";
|
|
4013
|
+
if (!isFrontMatter(doc.children[0]) && frontMatterEnd(out) >= 0)
|
|
4014
|
+
out = `***${out.slice(3)}`;
|
|
4015
|
+
return out;
|
|
4016
|
+
}
|
|
4017
|
+
function gapOf(gap, atStart) {
|
|
4018
|
+
const blank = gap.slice(gap.trimEnd().length);
|
|
4019
|
+
if (blank === gap)
|
|
4020
|
+
return gap;
|
|
4021
|
+
return atStart ? "" : blank;
|
|
4022
|
+
}
|
|
4023
|
+
|
|
4024
|
+
// src/tree/ai.ts
|
|
4025
|
+
function pathOfId(doc, id) {
|
|
4026
|
+
function walk(node, prefix) {
|
|
4027
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
4028
|
+
return null;
|
|
4029
|
+
if (node.id === id)
|
|
4030
|
+
return prefix;
|
|
4031
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
4032
|
+
const r = walk(node.children[i], prefix.concat(i));
|
|
4033
|
+
if (r)
|
|
4034
|
+
return r;
|
|
4035
|
+
}
|
|
4036
|
+
return null;
|
|
4037
|
+
}
|
|
4038
|
+
return walk(doc, []);
|
|
4039
|
+
}
|
|
4040
|
+
function buildIdIndex(doc) {
|
|
4041
|
+
const index = /* @__PURE__ */ new Map();
|
|
4042
|
+
function walk(node, prefix) {
|
|
4043
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
4044
|
+
return;
|
|
4045
|
+
if (node.id !== void 0 && !index.has(node.id))
|
|
4046
|
+
index.set(node.id, prefix);
|
|
4047
|
+
for (let i = 0; i < node.children.length; i++)
|
|
4048
|
+
walk(node.children[i], prefix.concat(i));
|
|
4049
|
+
}
|
|
4050
|
+
walk(doc, []);
|
|
4051
|
+
return index;
|
|
4052
|
+
}
|
|
4053
|
+
function leafPathAndLen(doc, blockPath) {
|
|
4054
|
+
const block = blockPath.reduce((n, i2) => n.children[i2], doc);
|
|
4055
|
+
if (!_chunkPCSVBPORcjs.isElement.call(void 0, block))
|
|
4056
|
+
throw new Error("\u5757\u4E0D\u662F\u5143\u7D20");
|
|
4057
|
+
const i = block.children.findIndex(_chunkPCSVBPORcjs.isText);
|
|
4058
|
+
if (i === -1)
|
|
4059
|
+
return null;
|
|
4060
|
+
const leaf = block.children[i];
|
|
4061
|
+
return { path: blockPath.concat(i), len: _chunkPCSVBPORcjs.isText.call(void 0, leaf) ? leaf.text.length : 0 };
|
|
4062
|
+
}
|
|
4063
|
+
function resolve(doc, id, index) {
|
|
4064
|
+
return index ? _nullishCoalesce(index.get(id), () => ( null)) : pathOfId(doc, id);
|
|
4065
|
+
}
|
|
4066
|
+
function collectIds(node, out) {
|
|
4067
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
4068
|
+
return;
|
|
4069
|
+
if (node.id !== void 0)
|
|
4070
|
+
out.push(node.id);
|
|
4071
|
+
for (const c of node.children)
|
|
4072
|
+
collectIds(c, out);
|
|
4073
|
+
}
|
|
4074
|
+
function assertFreshIds(existing, inserted) {
|
|
4075
|
+
const ids = [];
|
|
4076
|
+
for (const n of inserted)
|
|
4077
|
+
collectIds(n, ids);
|
|
4078
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4079
|
+
for (const id of ids) {
|
|
4080
|
+
if (existing.has(id))
|
|
4081
|
+
throw new Error(`duplicate id: ${id}`);
|
|
4082
|
+
if (seen.has(id))
|
|
4083
|
+
throw new Error(`duplicate id within insertion: ${id}`);
|
|
4084
|
+
seen.add(id);
|
|
4085
|
+
}
|
|
4086
|
+
}
|
|
4087
|
+
function appendText(doc, blockId, text, index) {
|
|
4088
|
+
const bp = resolve(doc, blockId, index);
|
|
4089
|
+
if (!bp)
|
|
4090
|
+
throw new Error(`block not found: ${blockId}`);
|
|
4091
|
+
const at = leafPathAndLen(doc, bp);
|
|
4092
|
+
if (!at)
|
|
4093
|
+
return { kind: "spliceChildren", path: bp, at: 0, remove: 0, insert: [{ text }] };
|
|
4094
|
+
return { kind: "insertText", path: at.path, offset: at.len, text };
|
|
4095
|
+
}
|
|
4096
|
+
function badTable(doc) {
|
|
4097
|
+
for (const c of doc.children) {
|
|
4098
|
+
if (!_chunkPCSVBPORcjs.isText.call(void 0, c) && c.type === "table") {
|
|
4099
|
+
const errs = checkTable(c);
|
|
4100
|
+
if (errs.length > 0)
|
|
4101
|
+
return errs[0];
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
return null;
|
|
4105
|
+
}
|
|
4106
|
+
function rewriteNode(doc, blockId, node, index, schema) {
|
|
4107
|
+
const bp = resolve(doc, blockId, index);
|
|
4108
|
+
if (!bp)
|
|
4109
|
+
throw new Error(`block not found: ${blockId}`);
|
|
4110
|
+
const existing = new Set(buildIdIndex(doc).keys());
|
|
4111
|
+
const replaced = [];
|
|
4112
|
+
collectIds(bp.reduce((n, i) => n.children[i], doc), replaced);
|
|
4113
|
+
for (const id of replaced)
|
|
4114
|
+
existing.delete(id);
|
|
4115
|
+
assertFreshIds(existing, [{ ...node, id: blockId }]);
|
|
4116
|
+
const step = { kind: "replaceNode", path: bp, node: { ...node, id: blockId } };
|
|
4117
|
+
if (schema) {
|
|
4118
|
+
const applied = _chunkHXABDVLZcjs.apply.call(void 0, doc, step);
|
|
4119
|
+
const v = validate(schema, applied);
|
|
4120
|
+
if (v !== true)
|
|
4121
|
+
throw new Error(`schema violation: ${v}`);
|
|
4122
|
+
const t = badTable(applied);
|
|
4123
|
+
if (t)
|
|
4124
|
+
throw new Error(`table invariant: ${t}`);
|
|
4125
|
+
}
|
|
4126
|
+
return step;
|
|
4127
|
+
}
|
|
4128
|
+
function insertAfter(doc, afterId, nodes, index, schema) {
|
|
4129
|
+
const bp = resolve(doc, afterId, index);
|
|
4130
|
+
if (!bp || bp.length === 0)
|
|
4131
|
+
throw new Error(`block not found or is root: ${afterId}`);
|
|
4132
|
+
for (const n of nodes) {
|
|
4133
|
+
if (n.id === void 0)
|
|
4134
|
+
throw new Error("insertAfter: node missing id");
|
|
4135
|
+
}
|
|
4136
|
+
assertFreshIds(new Set(buildIdIndex(doc).keys()), nodes);
|
|
4137
|
+
const parentPath = bp.slice(0, -1);
|
|
4138
|
+
const idx = bp[bp.length - 1];
|
|
4139
|
+
const step = { kind: "spliceChildren", path: parentPath, at: idx + 1, remove: 0, insert: nodes };
|
|
4140
|
+
if (schema) {
|
|
4141
|
+
const applied = _chunkHXABDVLZcjs.applyAll.call(void 0, doc, [step]);
|
|
4142
|
+
const v = validate(schema, applied);
|
|
4143
|
+
if (v !== true)
|
|
4144
|
+
throw new Error(`schema violation: ${v}`);
|
|
4145
|
+
const t = badTable(applied);
|
|
4146
|
+
if (t)
|
|
4147
|
+
throw new Error(`table invariant: ${t}`);
|
|
4148
|
+
}
|
|
4149
|
+
return step;
|
|
4150
|
+
}
|
|
4151
|
+
function idBlocks(doc) {
|
|
4152
|
+
const out = [];
|
|
4153
|
+
function walk(node) {
|
|
4154
|
+
if (_chunkPCSVBPORcjs.isText.call(void 0, node))
|
|
4155
|
+
return;
|
|
4156
|
+
if (node.id)
|
|
4157
|
+
out.push(node);
|
|
4158
|
+
node.children.forEach(walk);
|
|
4159
|
+
}
|
|
4160
|
+
walk(doc);
|
|
4161
|
+
return out;
|
|
4162
|
+
}
|
|
4163
|
+
function diffById(before, after) {
|
|
4164
|
+
const beforeMap = new Map(idBlocks(before).map((b) => [b.id, b]));
|
|
4165
|
+
const afterBlocks = idBlocks(after);
|
|
4166
|
+
const afterIds = new Set(afterBlocks.map((b) => b.id));
|
|
4167
|
+
const out = [];
|
|
4168
|
+
for (const b of afterBlocks) {
|
|
4169
|
+
const prev = beforeMap.get(b.id);
|
|
4170
|
+
if (!prev)
|
|
4171
|
+
out.push({ type: "added", id: b.id, after: b });
|
|
4172
|
+
else if (sameNode(prev, b))
|
|
4173
|
+
out.push({ type: "same", id: b.id });
|
|
4174
|
+
else
|
|
4175
|
+
out.push({ type: "changed", id: b.id, before: prev, after: b });
|
|
4176
|
+
}
|
|
4177
|
+
for (const [id, b] of beforeMap) {
|
|
4178
|
+
if (!afterIds.has(id))
|
|
4179
|
+
out.push({ type: "removed", id, before: b });
|
|
4180
|
+
}
|
|
4181
|
+
return out;
|
|
4182
|
+
}
|
|
4183
|
+
function sameNode(a, b) {
|
|
4184
|
+
return a.id === b.id && a.type === b.type && deepEqual(a.attrs, b.attrs) && deepEqual(a.children, b.children);
|
|
4185
|
+
}
|
|
4186
|
+
function deepEqual(a, b) {
|
|
4187
|
+
if (a === b)
|
|
4188
|
+
return true;
|
|
4189
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
4190
|
+
return false;
|
|
4191
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
4192
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length)
|
|
4193
|
+
return false;
|
|
4194
|
+
return a.every((x, i) => deepEqual(x, b[i]));
|
|
4195
|
+
}
|
|
4196
|
+
const ak = Object.keys(a);
|
|
4197
|
+
const bk = Object.keys(b);
|
|
4198
|
+
if (ak.length !== bk.length)
|
|
4199
|
+
return false;
|
|
4200
|
+
return ak.every((k) => deepEqual(a[k], b[k]));
|
|
4201
|
+
}
|
|
4202
|
+
|
|
4203
|
+
|
|
4204
|
+
|
|
4205
|
+
|
|
4206
|
+
|
|
4207
|
+
|
|
4208
|
+
|
|
4209
|
+
|
|
4210
|
+
|
|
4211
|
+
|
|
4212
|
+
|
|
4213
|
+
|
|
4214
|
+
|
|
4215
|
+
|
|
4216
|
+
|
|
4217
|
+
|
|
4218
|
+
|
|
4219
|
+
|
|
4220
|
+
|
|
4221
|
+
|
|
4222
|
+
|
|
4223
|
+
|
|
4224
|
+
|
|
4225
|
+
|
|
4226
|
+
|
|
4227
|
+
|
|
4228
|
+
|
|
4229
|
+
|
|
4230
|
+
|
|
4231
|
+
|
|
4232
|
+
|
|
4233
|
+
|
|
4234
|
+
|
|
4235
|
+
|
|
4236
|
+
|
|
4237
|
+
|
|
4238
|
+
|
|
4239
|
+
|
|
4240
|
+
|
|
4241
|
+
|
|
4242
|
+
|
|
4243
|
+
|
|
4244
|
+
|
|
4245
|
+
|
|
4246
|
+
|
|
4247
|
+
|
|
4248
|
+
|
|
4249
|
+
|
|
4250
|
+
|
|
4251
|
+
|
|
4252
|
+
|
|
4253
|
+
|
|
4254
|
+
|
|
4255
|
+
|
|
4256
|
+
|
|
4257
|
+
|
|
4258
|
+
|
|
4259
|
+
|
|
4260
|
+
|
|
4261
|
+
|
|
4262
|
+
|
|
4263
|
+
|
|
4264
|
+
|
|
4265
|
+
|
|
4266
|
+
|
|
4267
|
+
|
|
4268
|
+
|
|
4269
|
+
|
|
4270
|
+
|
|
4271
|
+
|
|
4272
|
+
|
|
4273
|
+
|
|
4274
|
+
|
|
4275
|
+
|
|
4276
|
+
|
|
4277
|
+
|
|
4278
|
+
|
|
4279
|
+
exports.checkSchema = checkSchema; exports.validate = validate; exports.validateAll = validateAll; exports.basicSchema = basicSchema; exports.CELL_EXCLUDED_FROM_BODY = CELL_EXCLUDED_FROM_BODY; exports.bodyBlockTypes = bodyBlockTypes; exports.cellBlockTypes = cellBlockTypes; exports.isBodyBlockType = isBodyBlockType; exports.isCellBlockTypeInSpace = isCellBlockTypeInSpace; exports.tableShape = tableShape; exports.colspanOf = colspanOf; exports.rowspanOf = rowspanOf; exports.buildGrid = buildGrid; exports.cellCaret = cellCaret; exports.caretCell = caretCell; exports.DEFAULT_COL_WIDTH = DEFAULT_COL_WIDTH; exports.makeTable = makeTable; exports.cellText = cellText; exports.nextCell = nextCell; exports.isLastCell = isLastCell; exports.arrowCell = arrowCell; exports.insertRow = insertRow; exports.deleteRow = deleteRow; exports.insertCol = insertCol; exports.deleteCol = deleteCol; exports.hasSpan = hasSpan; exports.moveRow = moveRow; exports.moveCol = moveCol; exports.cellRectTable = cellRectTable; exports.tableColWidths = tableColWidths; exports.resolvedColWidths = resolvedColWidths; exports.setColWidths = setColWidths; exports.setAlign = setAlign; exports.mergeCells = mergeCells; exports.splitCell = splitCell; exports.setCellStyle = setCellStyle; exports.clearCells = clearCells; exports.setFreeze = setFreeze; exports.setHeaderCol = setHeaderCol; exports.deleteTableBlock = deleteTableBlock; exports.tableAt = tableAt; exports.checkTable = checkTable; exports.CELL_BLOCK_TYPES = CELL_BLOCK_TYPES; exports.isCellBlockType = isCellBlockType; exports.tableSchema = tableSchema; exports.yieldToMain = yieldToMain; exports.safeCssValue = safeCssValue; exports.highlightColor = highlightColor; exports.asAlign = asAlign; exports.TEXT_STYLE_PROPS = TEXT_STYLE_PROPS; exports.attrsToStyle = attrsToStyle; exports.FRONT_MATTER = FRONT_MATTER; exports.isFrontMatter = isFrontMatter; exports.frontMatterEnd = frontMatterEnd; exports.asListType = asListType; exports.detectPlainList = detectPlainList; exports.setMathSyntax = setMathSyntax; exports.mdToDocAsync = mdToDocAsync; exports.mdToDoc = mdToDoc; exports.htmlToBlocks = htmlToBlocks; exports.isRichHtmlBlocks = isRichHtmlBlocks; exports.isPlainTransportHtml = isPlainTransportHtml; exports.mdStructureBeatsTransport = mdStructureBeatsTransport; exports.preferMarkdownPaste = preferMarkdownPaste; exports.embedHtml = embedHtml; exports.tableHasExtras = tableHasExtras; exports.tableToHtml = tableToHtml; exports.calloutToHtml = calloutToHtml; exports.docToMd = docToMd; exports.pathOfId = pathOfId; exports.buildIdIndex = buildIdIndex; exports.appendText = appendText; exports.rewriteNode = rewriteNode; exports.insertAfter = insertAfter; exports.diffById = diffById;
|