@sovereignfs/ui 0.107.3 → 0.110.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/CHANGELOG.md +73 -0
- package/README.md +5 -1
- package/dist/CompareView.module.css +203 -0
- package/dist/MarkdownEditor.module.css +422 -0
- package/dist/MergeView.module.css +178 -0
- package/dist/SourceEditor.module.css +206 -0
- package/dist/chunk-JWLJDBL4.js +5841 -0
- package/dist/index.d.ts +134 -9
- package/dist/index.js +2151 -6409
- package/dist/markdown-editor.d.ts +83 -0
- package/dist/markdown-editor.js +2838 -0
- package/dist/markdown.d.ts +44 -0
- package/dist/markdown.js +1877 -0
- package/dist/text.d.ts +68 -0
- package/dist/text.js +268 -0
- package/dist/tokens/semantic.css +14 -0
- package/package.json +84 -1
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,1877 @@
|
|
|
1
|
+
// src/text/diff.ts
|
|
2
|
+
var DEFAULT_MAX_EDITS = 4e3;
|
|
3
|
+
function intern(a, b) {
|
|
4
|
+
const ids = /* @__PURE__ */ new Map();
|
|
5
|
+
const encode = (items) => {
|
|
6
|
+
const out = new Int32Array(items.length);
|
|
7
|
+
for (let i = 0; i < items.length; i++) {
|
|
8
|
+
const item = items[i];
|
|
9
|
+
let id = ids.get(item);
|
|
10
|
+
if (id === void 0) {
|
|
11
|
+
id = ids.size;
|
|
12
|
+
ids.set(item, id);
|
|
13
|
+
}
|
|
14
|
+
out[i] = id;
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
};
|
|
18
|
+
return [encode(a), encode(b)];
|
|
19
|
+
}
|
|
20
|
+
function middleSnake(a, b, aLo, aHi, bLo, bHi, maxEdits) {
|
|
21
|
+
const n = aHi - aLo;
|
|
22
|
+
const m = bHi - bLo;
|
|
23
|
+
const delta = n - m;
|
|
24
|
+
const odd = (delta & 1) !== 0;
|
|
25
|
+
const max = Math.min(Math.ceil((n + m) / 2), Math.ceil(maxEdits / 2));
|
|
26
|
+
const offset = max + 1;
|
|
27
|
+
const forward = new Int32Array(2 * max + 3);
|
|
28
|
+
const backward = new Int32Array(2 * max + 3);
|
|
29
|
+
for (let d = 0; d <= max; d++) {
|
|
30
|
+
for (let k = -d; k <= d; k += 2) {
|
|
31
|
+
const down = k === -d || k !== d && (forward[offset + k - 1] ?? 0) < (forward[offset + k + 1] ?? 0);
|
|
32
|
+
let x = down ? forward[offset + k + 1] ?? 0 : (forward[offset + k - 1] ?? 0) + 1;
|
|
33
|
+
let y = x - k;
|
|
34
|
+
const x0 = x;
|
|
35
|
+
const y0 = y;
|
|
36
|
+
while (x < n && y < m && a[aLo + x] === b[bLo + y]) {
|
|
37
|
+
x++;
|
|
38
|
+
y++;
|
|
39
|
+
}
|
|
40
|
+
forward[offset + k] = x;
|
|
41
|
+
const other = delta - k;
|
|
42
|
+
if (odd && other >= -(d - 1) && other <= d - 1 && x + (backward[offset + other] ?? 0) >= n) {
|
|
43
|
+
return { x: x0, y: y0, u: x, v: y };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (let k = -d; k <= d; k += 2) {
|
|
47
|
+
const down = k === -d || k !== d && (backward[offset + k - 1] ?? 0) < (backward[offset + k + 1] ?? 0);
|
|
48
|
+
let x = down ? backward[offset + k + 1] ?? 0 : (backward[offset + k - 1] ?? 0) + 1;
|
|
49
|
+
let y = x - k;
|
|
50
|
+
const x0 = x;
|
|
51
|
+
const y0 = y;
|
|
52
|
+
while (x < n && y < m && a[aHi - 1 - x] === b[bHi - 1 - y]) {
|
|
53
|
+
x++;
|
|
54
|
+
y++;
|
|
55
|
+
}
|
|
56
|
+
backward[offset + k] = x;
|
|
57
|
+
const other = delta - k;
|
|
58
|
+
if (!odd && other >= -d && other <= d && x + (forward[offset + other] ?? 0) >= n) {
|
|
59
|
+
return { x: n - x, y: m - y, u: n - x0, v: m - y0 };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
function diffSequences(a, b, { maxEdits = DEFAULT_MAX_EDITS } = {}) {
|
|
66
|
+
const [left, right] = intern(a, b);
|
|
67
|
+
const runs2 = [];
|
|
68
|
+
const push = (equal, aStart, aEnd, bStart, bEnd) => {
|
|
69
|
+
if (aStart === aEnd && bStart === bEnd) return;
|
|
70
|
+
const last = runs2[runs2.length - 1];
|
|
71
|
+
if (last && last.equal === equal) {
|
|
72
|
+
runs2[runs2.length - 1] = { ...last, aEnd, bEnd };
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
runs2.push({ equal, aStart, aEnd, bStart, bEnd });
|
|
76
|
+
};
|
|
77
|
+
const jobs = [{ aLo: 0, aHi: left.length, bLo: 0, bHi: right.length }];
|
|
78
|
+
while (jobs.length > 0) {
|
|
79
|
+
const job = jobs.pop();
|
|
80
|
+
let { aLo, aHi, bLo, bHi } = job;
|
|
81
|
+
if (job.equal) {
|
|
82
|
+
push(true, aLo, aHi, bLo, bHi);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
let head = 0;
|
|
86
|
+
while (aLo + head < aHi && bLo + head < bHi && left[aLo + head] === right[bLo + head]) head++;
|
|
87
|
+
push(true, aLo, aLo + head, bLo, bLo + head);
|
|
88
|
+
aLo += head;
|
|
89
|
+
bLo += head;
|
|
90
|
+
let tail = 0;
|
|
91
|
+
while (aHi - tail > aLo && bHi - tail > bLo && left[aHi - 1 - tail] === right[bHi - 1 - tail]) {
|
|
92
|
+
tail++;
|
|
93
|
+
}
|
|
94
|
+
aHi -= tail;
|
|
95
|
+
bHi -= tail;
|
|
96
|
+
const after = { equal: true, aLo: aHi, aHi: aHi + tail, bLo: bHi, bHi: bHi + tail };
|
|
97
|
+
const snake = aLo < aHi && bLo < bHi ? middleSnake(left, right, aLo, aHi, bLo, bHi, maxEdits) : void 0;
|
|
98
|
+
if (!snake) {
|
|
99
|
+
push(false, aLo, aHi, bLo, bHi);
|
|
100
|
+
jobs.push(after);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
jobs.push(
|
|
104
|
+
after,
|
|
105
|
+
{ aLo: aLo + snake.u, aHi, bLo: bLo + snake.v, bHi },
|
|
106
|
+
{
|
|
107
|
+
equal: true,
|
|
108
|
+
aLo: aLo + snake.x,
|
|
109
|
+
aHi: aLo + snake.u,
|
|
110
|
+
bLo: bLo + snake.y,
|
|
111
|
+
bHi: bLo + snake.v
|
|
112
|
+
},
|
|
113
|
+
{ aLo, aHi: aLo + snake.x, bLo, bHi: bLo + snake.y }
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return runs2;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/text/lines.ts
|
|
120
|
+
function splitLines(text2) {
|
|
121
|
+
const lines = [];
|
|
122
|
+
let start = 0;
|
|
123
|
+
for (let i = 0; i < text2.length; i++) {
|
|
124
|
+
const code = text2.charCodeAt(i);
|
|
125
|
+
if (code !== 10 && code !== 13) continue;
|
|
126
|
+
const crlf = code === 13 && text2.charCodeAt(i + 1) === 10;
|
|
127
|
+
const ending = crlf ? "\r\n" : code === 13 ? "\r" : "\n";
|
|
128
|
+
lines.push({ text: text2.slice(start, i), ending });
|
|
129
|
+
if (crlf) i++;
|
|
130
|
+
start = i + 1;
|
|
131
|
+
}
|
|
132
|
+
if (start < text2.length) lines.push({ text: text2.slice(start), ending: "" });
|
|
133
|
+
return lines;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/markdown/schema.ts
|
|
137
|
+
import { Schema } from "prosemirror-model";
|
|
138
|
+
var attrs = (defaults) => Object.fromEntries(Object.entries(defaults).map(([name, value]) => [name, { default: value }]));
|
|
139
|
+
var place = { gap: null, indent: "" };
|
|
140
|
+
var SAFE_SCHEMES = /* @__PURE__ */ new Set(["http", "https", "mailto", "tel"]);
|
|
141
|
+
function schemeOf(address) {
|
|
142
|
+
const read = Array.from(address).filter((char) => char > " ").join("");
|
|
143
|
+
return /^([a-zA-Z][a-zA-Z0-9+.-]*):/.exec(read)?.[1]?.toLowerCase();
|
|
144
|
+
}
|
|
145
|
+
function isSafeAddress(address) {
|
|
146
|
+
const scheme = schemeOf(address);
|
|
147
|
+
return scheme === void 0 || SAFE_SCHEMES.has(scheme);
|
|
148
|
+
}
|
|
149
|
+
var RUNNING_SCHEMES = /* @__PURE__ */ new Set(["javascript", "vbscript", "data"]);
|
|
150
|
+
var ALIGNMENTS = /* @__PURE__ */ new Set(["left", "center", "right"]);
|
|
151
|
+
var cell = (tag, role) => ({
|
|
152
|
+
content: "inline*",
|
|
153
|
+
// `colspan`, `rowspan` and `colwidth` are the table commands' own; Markdown has one of each.
|
|
154
|
+
attrs: attrs({ align: null, pad: null, colspan: 1, rowspan: 1, colwidth: null }),
|
|
155
|
+
tableRole: role,
|
|
156
|
+
isolating: true,
|
|
157
|
+
parseDOM: [
|
|
158
|
+
{
|
|
159
|
+
tag,
|
|
160
|
+
getAttrs: (dom) => {
|
|
161
|
+
const align2 = dom.style.textAlign || dom.getAttribute("data-align") || "";
|
|
162
|
+
return { align: ALIGNMENTS.has(align2) ? align2 : null };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
],
|
|
166
|
+
toDOM: (node) => [
|
|
167
|
+
tag,
|
|
168
|
+
node.attrs.align ? { "data-align": String(node.attrs.align) } : {},
|
|
169
|
+
0
|
|
170
|
+
]
|
|
171
|
+
});
|
|
172
|
+
var schema = new Schema({
|
|
173
|
+
nodes: {
|
|
174
|
+
// What lay before the first block and after the last.
|
|
175
|
+
doc: { content: "block+", attrs: attrs({ lead: "", tail: "\n" }) },
|
|
176
|
+
paragraph: {
|
|
177
|
+
group: "block",
|
|
178
|
+
content: "inline*",
|
|
179
|
+
attrs: attrs(place),
|
|
180
|
+
parseDOM: [{ tag: "p" }],
|
|
181
|
+
toDOM: () => ["p", 0]
|
|
182
|
+
},
|
|
183
|
+
heading: {
|
|
184
|
+
group: "block",
|
|
185
|
+
content: "inline*",
|
|
186
|
+
defining: true,
|
|
187
|
+
attrs: attrs({ ...place, level: 1, underline: null, hashGap: " ", closing: "" }),
|
|
188
|
+
parseDOM: [1, 2, 3, 4, 5, 6].map((level) => ({ tag: `h${level}`, attrs: { level } })),
|
|
189
|
+
toDOM: (node) => [`h${String(node.attrs.level)}`, 0]
|
|
190
|
+
},
|
|
191
|
+
horizontal_rule: {
|
|
192
|
+
group: "block",
|
|
193
|
+
attrs: attrs({ ...place, markup: null }),
|
|
194
|
+
parseDOM: [{ tag: "hr" }],
|
|
195
|
+
toDOM: () => ["hr"]
|
|
196
|
+
},
|
|
197
|
+
code_block: {
|
|
198
|
+
group: "block",
|
|
199
|
+
content: "text*",
|
|
200
|
+
marks: "",
|
|
201
|
+
code: true,
|
|
202
|
+
defining: true,
|
|
203
|
+
// `indented` is the form with four spaces and no fence.
|
|
204
|
+
attrs: attrs({ ...place, indented: false, fence: null, info: "", close: null, open: false }),
|
|
205
|
+
parseDOM: [
|
|
206
|
+
{
|
|
207
|
+
tag: "pre",
|
|
208
|
+
preserveWhitespace: "full",
|
|
209
|
+
getAttrs: (dom) => ({ info: dom.getAttribute("data-info") ?? "" })
|
|
210
|
+
}
|
|
211
|
+
],
|
|
212
|
+
toDOM: (node) => [
|
|
213
|
+
"pre",
|
|
214
|
+
node.attrs.info ? { "data-info": String(node.attrs.info) } : {},
|
|
215
|
+
["code", 0]
|
|
216
|
+
]
|
|
217
|
+
},
|
|
218
|
+
blockquote: {
|
|
219
|
+
group: "block",
|
|
220
|
+
content: "block+",
|
|
221
|
+
defining: true,
|
|
222
|
+
// `bare` writes `>text` without a space; `blank` is how an empty quoted line is written.
|
|
223
|
+
attrs: attrs({ ...place, bare: false, blank: ">" }),
|
|
224
|
+
parseDOM: [{ tag: "blockquote" }],
|
|
225
|
+
toDOM: () => ["blockquote", 0]
|
|
226
|
+
},
|
|
227
|
+
bullet_list: {
|
|
228
|
+
group: "block",
|
|
229
|
+
content: "list_item+",
|
|
230
|
+
attrs: attrs({ ...place, marker: "-", tight: true }),
|
|
231
|
+
parseDOM: [{ tag: "ul" }],
|
|
232
|
+
toDOM: (node) => ["ul", node.attrs.tight ? { "data-tight": "true" } : {}, 0]
|
|
233
|
+
},
|
|
234
|
+
ordered_list: {
|
|
235
|
+
group: "block",
|
|
236
|
+
content: "list_item+",
|
|
237
|
+
// `numbering`: each item one more than the last (`up`), or every item the first number (`one`).
|
|
238
|
+
attrs: attrs({ ...place, start: 1, delimiter: ".", numbering: "up", tight: true }),
|
|
239
|
+
parseDOM: [
|
|
240
|
+
{
|
|
241
|
+
tag: "ol",
|
|
242
|
+
getAttrs: (dom) => ({ start: Number(dom.getAttribute("start") ?? 1) || 1 })
|
|
243
|
+
}
|
|
244
|
+
],
|
|
245
|
+
toDOM: (node) => [
|
|
246
|
+
"ol",
|
|
247
|
+
{
|
|
248
|
+
...node.attrs.start === 1 ? {} : { start: String(node.attrs.start) },
|
|
249
|
+
...node.attrs.tight ? { "data-tight": "true" } : {}
|
|
250
|
+
},
|
|
251
|
+
0
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
list_item: {
|
|
255
|
+
content: "block+",
|
|
256
|
+
defining: true,
|
|
257
|
+
attrs: attrs({ gap: null, checked: null, spacing: " ", check: null, checkSpacing: " " }),
|
|
258
|
+
parseDOM: [
|
|
259
|
+
{
|
|
260
|
+
tag: "li",
|
|
261
|
+
getAttrs: (dom) => {
|
|
262
|
+
const checked = dom.getAttribute("data-checked");
|
|
263
|
+
return { checked: checked === null ? null : checked === "true" };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
],
|
|
267
|
+
toDOM: (node) => [
|
|
268
|
+
"li",
|
|
269
|
+
node.attrs.checked === null ? {} : { "data-checked": String(node.attrs.checked) },
|
|
270
|
+
0
|
|
271
|
+
]
|
|
272
|
+
},
|
|
273
|
+
table: {
|
|
274
|
+
group: "block",
|
|
275
|
+
content: "table_row+",
|
|
276
|
+
tableRole: "table",
|
|
277
|
+
isolating: true,
|
|
278
|
+
// The delimiter row as written, whether rows begin and end with a pipe, and whether columns were lined up.
|
|
279
|
+
attrs: attrs({ ...place, delimiter: null, leading: true, trailing: true, aligned: false }),
|
|
280
|
+
parseDOM: [{ tag: "table" }],
|
|
281
|
+
toDOM: () => ["table", ["tbody", 0]]
|
|
282
|
+
},
|
|
283
|
+
table_row: {
|
|
284
|
+
content: "(table_cell | table_header)*",
|
|
285
|
+
attrs: attrs({ after: "" }),
|
|
286
|
+
tableRole: "row",
|
|
287
|
+
parseDOM: [{ tag: "tr" }],
|
|
288
|
+
toDOM: () => ["tr", 0]
|
|
289
|
+
},
|
|
290
|
+
table_header: cell("th", "header_cell"),
|
|
291
|
+
table_cell: cell("td", "cell"),
|
|
292
|
+
// What the editor does not model (raw HTML, definitions, footnotes): kept as written, and shown.
|
|
293
|
+
raw_block: {
|
|
294
|
+
group: "block",
|
|
295
|
+
atom: true,
|
|
296
|
+
attrs: attrs({ ...place, raw: "", kind: "html" }),
|
|
297
|
+
toDOM: (node) => ["pre", { "data-raw": String(node.attrs.kind) }, String(node.attrs.raw)]
|
|
298
|
+
},
|
|
299
|
+
text: { group: "inline" },
|
|
300
|
+
image: {
|
|
301
|
+
group: "inline",
|
|
302
|
+
inline: true,
|
|
303
|
+
draggable: true,
|
|
304
|
+
attrs: attrs({
|
|
305
|
+
src: "",
|
|
306
|
+
alt: "",
|
|
307
|
+
title: null,
|
|
308
|
+
tail: null,
|
|
309
|
+
label: null,
|
|
310
|
+
wasSrc: null,
|
|
311
|
+
wasTitle: null,
|
|
312
|
+
wasAlt: null
|
|
313
|
+
}),
|
|
314
|
+
parseDOM: [
|
|
315
|
+
{
|
|
316
|
+
tag: "img",
|
|
317
|
+
getAttrs: (dom) => {
|
|
318
|
+
const src = dom.getAttribute("data-src") ?? dom.getAttribute("src");
|
|
319
|
+
if (!src) return false;
|
|
320
|
+
return { src, alt: dom.getAttribute("alt") ?? "", title: dom.getAttribute("title") };
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
],
|
|
324
|
+
// Never `src`: the address is the text's, and only the app says what may be loaded for it.
|
|
325
|
+
toDOM: (node) => [
|
|
326
|
+
"img",
|
|
327
|
+
{
|
|
328
|
+
"data-src": String(node.attrs.src),
|
|
329
|
+
alt: String(node.attrs.alt),
|
|
330
|
+
...node.attrs.title ? { title: String(node.attrs.title) } : {}
|
|
331
|
+
}
|
|
332
|
+
]
|
|
333
|
+
},
|
|
334
|
+
hard_break: {
|
|
335
|
+
group: "inline",
|
|
336
|
+
inline: true,
|
|
337
|
+
selectable: false,
|
|
338
|
+
attrs: attrs({ markup: null }),
|
|
339
|
+
parseDOM: [{ tag: "br" }],
|
|
340
|
+
toDOM: () => ["br"]
|
|
341
|
+
},
|
|
342
|
+
// A line break in the source that reads as a space.
|
|
343
|
+
soft_break: {
|
|
344
|
+
group: "inline",
|
|
345
|
+
inline: true,
|
|
346
|
+
selectable: false,
|
|
347
|
+
attrs: attrs({ raw: "\n" }),
|
|
348
|
+
parseDOM: [{ tag: "span[data-soft-break]" }],
|
|
349
|
+
toDOM: () => ["span", { "data-soft-break": "" }, " "]
|
|
350
|
+
},
|
|
351
|
+
raw_inline: {
|
|
352
|
+
group: "inline",
|
|
353
|
+
inline: true,
|
|
354
|
+
atom: true,
|
|
355
|
+
attrs: attrs({ raw: "", kind: "html" }),
|
|
356
|
+
toDOM: (node) => ["code", { "data-raw": String(node.attrs.kind) }, String(node.attrs.raw)]
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
marks: {
|
|
360
|
+
link: {
|
|
361
|
+
inclusive: false,
|
|
362
|
+
// `form`: `[text](address)`, `<address>` or an address written bare.
|
|
363
|
+
attrs: attrs({
|
|
364
|
+
href: "",
|
|
365
|
+
title: null,
|
|
366
|
+
form: "inline",
|
|
367
|
+
tail: null,
|
|
368
|
+
wasHref: null,
|
|
369
|
+
wasTitle: null,
|
|
370
|
+
depth: 0
|
|
371
|
+
}),
|
|
372
|
+
parseDOM: [
|
|
373
|
+
{
|
|
374
|
+
tag: "a[href], a[data-href]",
|
|
375
|
+
getAttrs: (dom) => {
|
|
376
|
+
const href = dom.getAttribute("data-href") ?? dom.getAttribute("href") ?? "";
|
|
377
|
+
if (RUNNING_SCHEMES.has(schemeOf(href) ?? "")) return false;
|
|
378
|
+
return { href, title: dom.getAttribute("title") };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
],
|
|
382
|
+
toDOM: (mark) => {
|
|
383
|
+
const href = String(mark.attrs.href);
|
|
384
|
+
return [
|
|
385
|
+
"a",
|
|
386
|
+
{
|
|
387
|
+
...isSafeAddress(href) ? { href } : {},
|
|
388
|
+
"data-href": href,
|
|
389
|
+
...mark.attrs.title ? { title: String(mark.attrs.title) } : {},
|
|
390
|
+
rel: "noopener noreferrer"
|
|
391
|
+
},
|
|
392
|
+
0
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
// `depth` is how far inside other marks the source put it, so that they are written in the same order.
|
|
397
|
+
em: {
|
|
398
|
+
attrs: attrs({ marker: null, depth: 0 }),
|
|
399
|
+
parseDOM: [{ tag: "em" }, { tag: "i" }, { style: "font-style=italic" }],
|
|
400
|
+
toDOM: () => ["em", 0]
|
|
401
|
+
},
|
|
402
|
+
strong: {
|
|
403
|
+
attrs: attrs({ marker: null, depth: 0 }),
|
|
404
|
+
parseDOM: [
|
|
405
|
+
{ tag: "strong" },
|
|
406
|
+
{ tag: "b", getAttrs: (dom) => dom.style.fontWeight === "normal" ? false : null },
|
|
407
|
+
{
|
|
408
|
+
style: "font-weight",
|
|
409
|
+
getAttrs: (value) => /^(bold|[6-9]00)/.test(value) ? null : false
|
|
410
|
+
}
|
|
411
|
+
],
|
|
412
|
+
toDOM: () => ["strong", 0]
|
|
413
|
+
},
|
|
414
|
+
strike: {
|
|
415
|
+
attrs: attrs({ marker: null, depth: 0 }),
|
|
416
|
+
parseDOM: [{ tag: "s" }, { tag: "del" }, { style: "text-decoration=line-through" }],
|
|
417
|
+
toDOM: () => ["s", 0]
|
|
418
|
+
},
|
|
419
|
+
code: {
|
|
420
|
+
attrs: attrs({ ticks: null, padded: false }),
|
|
421
|
+
parseDOM: [{ tag: "code" }],
|
|
422
|
+
toDOM: () => ["code", 0]
|
|
423
|
+
},
|
|
424
|
+
// A character the source spelled another way (`\*`, `&`): written back so while it is unchanged.
|
|
425
|
+
source: {
|
|
426
|
+
inclusive: false,
|
|
427
|
+
attrs: attrs({ raw: "", value: "" }),
|
|
428
|
+
toDOM: () => ["span", 0]
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// src/markdown/tree.ts
|
|
434
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
435
|
+
import { gfmFromMarkdown } from "mdast-util-gfm";
|
|
436
|
+
import { gfm } from "micromark-extension-gfm";
|
|
437
|
+
function readTree(source) {
|
|
438
|
+
return fromMarkdown(source, {
|
|
439
|
+
extensions: [gfm()],
|
|
440
|
+
mdastExtensions: [gfmFromMarkdown()]
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/markdown/parse.ts
|
|
445
|
+
var startOf = (node) => node.position?.start.offset ?? 0;
|
|
446
|
+
var endOf = (node) => node.position?.end.offset ?? 0;
|
|
447
|
+
function sourceLines(context, from, to) {
|
|
448
|
+
return context.source.slice(from, to).split("\n").map((line, index) => index === 0 ? line : context.strip(line));
|
|
449
|
+
}
|
|
450
|
+
var linesOf = (context, node) => sourceLines(context, startOf(node), endOf(node));
|
|
451
|
+
function blockEnd(context, node) {
|
|
452
|
+
if (node.type === "list" || node.type === "listItem" || node.type === "blockquote") {
|
|
453
|
+
const last = node.children?.[node.children.length - 1];
|
|
454
|
+
if (last) return blockEnd(context, last);
|
|
455
|
+
}
|
|
456
|
+
if (node.type === "code") return endOf(node) - (context.source[endOf(node) - 1] === "\n" ? 1 : 0);
|
|
457
|
+
const own2 = context.source.slice(startOf(node), endOf(node));
|
|
458
|
+
const empty = node.type === "listItem" && !node.children?.length;
|
|
459
|
+
return startOf(node) + (empty ? own2.replace(/\n+$/, "") : own2.trimEnd()).length;
|
|
460
|
+
}
|
|
461
|
+
var ESCAPABLE = /[!-/:-@[-`{-~]/;
|
|
462
|
+
var REFERENCE = /&(?:#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6}|[A-Za-z][A-Za-z0-9]{1,31});/y;
|
|
463
|
+
var decoded = /* @__PURE__ */ new Map();
|
|
464
|
+
function decode(reference) {
|
|
465
|
+
let value = decoded.get(reference);
|
|
466
|
+
if (value === void 0) {
|
|
467
|
+
value = readTree(reference).children?.[0]?.children?.[0]?.value ?? reference;
|
|
468
|
+
if (decoded.size > 500) decoded.clear();
|
|
469
|
+
decoded.set(reference, value);
|
|
470
|
+
}
|
|
471
|
+
return value;
|
|
472
|
+
}
|
|
473
|
+
function align(value, raw) {
|
|
474
|
+
const pieces = [];
|
|
475
|
+
let plain2 = "";
|
|
476
|
+
const push = (piece) => {
|
|
477
|
+
if (plain2) pieces.push({ kind: "text", value: plain2 });
|
|
478
|
+
plain2 = "";
|
|
479
|
+
pieces.push(piece);
|
|
480
|
+
};
|
|
481
|
+
let i = 0;
|
|
482
|
+
for (let j = 0; j < value.length; ) {
|
|
483
|
+
const char = value[j];
|
|
484
|
+
if (char === "\n") {
|
|
485
|
+
let end = i;
|
|
486
|
+
while (raw[end] === " " || raw[end] === " ") end++;
|
|
487
|
+
if (raw[end] !== "\n") return void 0;
|
|
488
|
+
end++;
|
|
489
|
+
while (raw[end] === " " || raw[end] === " ") end++;
|
|
490
|
+
push({ kind: "break", raw: raw.slice(i, end) });
|
|
491
|
+
i = end;
|
|
492
|
+
j++;
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (raw[i] === "\\" && raw[i + 1] === char && ESCAPABLE.test(char)) {
|
|
496
|
+
push({ kind: "text", value: char, raw: raw.slice(i, i + 2) });
|
|
497
|
+
i += 2;
|
|
498
|
+
j++;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
if (raw[i] === "&") {
|
|
502
|
+
REFERENCE.lastIndex = i;
|
|
503
|
+
const reference = REFERENCE.exec(raw)?.[0];
|
|
504
|
+
if (reference) {
|
|
505
|
+
const stands = decode(reference);
|
|
506
|
+
if (stands !== reference && value.startsWith(stands, j)) {
|
|
507
|
+
push({ kind: "text", value: stands, raw: reference });
|
|
508
|
+
i += reference.length;
|
|
509
|
+
j += stands.length;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (raw[i] !== char) return void 0;
|
|
515
|
+
plain2 += char;
|
|
516
|
+
i++;
|
|
517
|
+
j++;
|
|
518
|
+
}
|
|
519
|
+
if (plain2) pieces.push({ kind: "text", value: plain2 });
|
|
520
|
+
return pieces;
|
|
521
|
+
}
|
|
522
|
+
function textNodes(context, node, marks) {
|
|
523
|
+
const value = node.value ?? "";
|
|
524
|
+
const aligned = node.position ? align(value, linesOf(context, node).join("\n")) : void 0;
|
|
525
|
+
const pieces = aligned ?? value.split("\n").flatMap((text2, index) => [
|
|
526
|
+
...index ? [{ kind: "break", raw: "\n" }] : [],
|
|
527
|
+
{ kind: "text", value: text2 }
|
|
528
|
+
]);
|
|
529
|
+
const out = [];
|
|
530
|
+
for (const piece of pieces) {
|
|
531
|
+
if (piece.kind === "break") {
|
|
532
|
+
out.push(schema.node("soft_break", { raw: piece.raw }, void 0, marks));
|
|
533
|
+
} else if (piece.value) {
|
|
534
|
+
const own2 = piece.raw ? schema.mark("source", { raw: piece.raw, value: piece.value }).addToSet(marks) : marks;
|
|
535
|
+
out.push(schema.text(piece.value, own2));
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return out;
|
|
539
|
+
}
|
|
540
|
+
var MARKS = {
|
|
541
|
+
emphasis: "em",
|
|
542
|
+
strong: "strong",
|
|
543
|
+
delete: "strike"
|
|
544
|
+
};
|
|
545
|
+
function inline(context, nodes, marks = [], depth = 0) {
|
|
546
|
+
return nodes.flatMap((node) => {
|
|
547
|
+
const raw = () => linesOf(context, node).join("\n");
|
|
548
|
+
const kept2 = (kind2) => {
|
|
549
|
+
context.unsupported.push({ reason: kind2, line: node.position?.start.line ?? 1 });
|
|
550
|
+
return [schema.node("raw_inline", { raw: raw(), kind: kind2 }, void 0, marks)];
|
|
551
|
+
};
|
|
552
|
+
const mark = MARKS[node.type];
|
|
553
|
+
if (mark) {
|
|
554
|
+
const written2 = raw();
|
|
555
|
+
const marker = mark === "em" ? written2.slice(0, 1) : mark === "strong" ? written2.slice(0, 2) : written2.startsWith("~~") ? "~~" : "~";
|
|
556
|
+
const set = schema.mark(mark, { marker, depth }).addToSet(marks);
|
|
557
|
+
return inline(context, node.children ?? [], set, depth + 1);
|
|
558
|
+
}
|
|
559
|
+
switch (node.type) {
|
|
560
|
+
case "text":
|
|
561
|
+
return textNodes(context, node, marks);
|
|
562
|
+
case "inlineCode": {
|
|
563
|
+
const written2 = raw();
|
|
564
|
+
let ticks = 0;
|
|
565
|
+
while (written2[ticks] === "`") ticks++;
|
|
566
|
+
const inside = written2.slice(ticks, written2.length - ticks);
|
|
567
|
+
const value = node.value ?? "";
|
|
568
|
+
const padded = inside.length === value.length + 2 && inside === ` ${value} `;
|
|
569
|
+
if (!value) return kept2("unsupported");
|
|
570
|
+
const set = schema.mark("code", { ticks, padded }).addToSet(marks);
|
|
571
|
+
return [schema.text(value, set)];
|
|
572
|
+
}
|
|
573
|
+
case "link": {
|
|
574
|
+
const written2 = raw();
|
|
575
|
+
const form = written2.startsWith("<") ? "autolink" : written2.startsWith("[") ? "inline" : "literal";
|
|
576
|
+
const children2 = node.children ?? [];
|
|
577
|
+
const last = children2[children2.length - 1];
|
|
578
|
+
const tail = form !== "inline" ? null : sourceLines(context, last ? endOf(last) : startOf(node) + 1, endOf(node)).join("\n");
|
|
579
|
+
const title = node.title ?? null;
|
|
580
|
+
const link = schema.mark("link", {
|
|
581
|
+
href: node.url ?? "",
|
|
582
|
+
title,
|
|
583
|
+
form,
|
|
584
|
+
tail,
|
|
585
|
+
wasHref: node.url ?? "",
|
|
586
|
+
wasTitle: title,
|
|
587
|
+
depth
|
|
588
|
+
});
|
|
589
|
+
if (children2.length === 0) return kept2("unsupported");
|
|
590
|
+
return inline(context, children2, link.addToSet(marks), depth + 1);
|
|
591
|
+
}
|
|
592
|
+
case "image": {
|
|
593
|
+
const written2 = raw();
|
|
594
|
+
const split = written2.lastIndexOf("](");
|
|
595
|
+
const title = node.title ?? null;
|
|
596
|
+
const alt = node.alt ?? "";
|
|
597
|
+
const src = node.url ?? "";
|
|
598
|
+
return [
|
|
599
|
+
schema.node(
|
|
600
|
+
"image",
|
|
601
|
+
{
|
|
602
|
+
src,
|
|
603
|
+
alt,
|
|
604
|
+
title,
|
|
605
|
+
label: split > 1 ? written2.slice(2, split) : null,
|
|
606
|
+
tail: split > 1 ? written2.slice(split) : null,
|
|
607
|
+
wasSrc: src,
|
|
608
|
+
wasTitle: title,
|
|
609
|
+
wasAlt: alt
|
|
610
|
+
},
|
|
611
|
+
void 0,
|
|
612
|
+
marks
|
|
613
|
+
)
|
|
614
|
+
];
|
|
615
|
+
}
|
|
616
|
+
case "break": {
|
|
617
|
+
const markup = raw().split("\n")[0] ?? "";
|
|
618
|
+
return [schema.node("hard_break", { markup }, void 0, marks)];
|
|
619
|
+
}
|
|
620
|
+
case "html":
|
|
621
|
+
return kept2("html");
|
|
622
|
+
case "footnoteReference":
|
|
623
|
+
return kept2("footnote");
|
|
624
|
+
case "linkReference":
|
|
625
|
+
case "imageReference":
|
|
626
|
+
return kept2("reference");
|
|
627
|
+
default:
|
|
628
|
+
return kept2("unsupported");
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
function splitRow(line) {
|
|
633
|
+
const parts = [];
|
|
634
|
+
let part = "";
|
|
635
|
+
for (let i = 0; i < line.length; i++) {
|
|
636
|
+
const char = line[i];
|
|
637
|
+
if (char === "\\") {
|
|
638
|
+
part += char + (line[i + 1] ?? "");
|
|
639
|
+
i++;
|
|
640
|
+
} else if (char === "|") {
|
|
641
|
+
parts.push(part);
|
|
642
|
+
part = "";
|
|
643
|
+
} else part += char;
|
|
644
|
+
}
|
|
645
|
+
parts.push(part);
|
|
646
|
+
const leading = parts.length > 1 && parts[0].trim() === "";
|
|
647
|
+
const trailing = parts.length > 1 && parts[parts.length - 1].trim() === "";
|
|
648
|
+
return {
|
|
649
|
+
cells: parts.slice(leading ? 1 : 0, trailing ? -1 : parts.length),
|
|
650
|
+
leading,
|
|
651
|
+
trailing
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
function padding(cell2) {
|
|
655
|
+
if (cell2.trim() === "") return [cell2, ""];
|
|
656
|
+
return [cell2.slice(0, cell2.length - cell2.trimStart().length), cell2.slice(cell2.trimEnd().length)];
|
|
657
|
+
}
|
|
658
|
+
function cellPadding(row, column) {
|
|
659
|
+
const [left, right] = padding(row?.cells[column] ?? " ");
|
|
660
|
+
const open = !row?.trailing && column === (row?.cells.length ?? 0) - 1;
|
|
661
|
+
return [left, open ? "" : right];
|
|
662
|
+
}
|
|
663
|
+
var rowEnd = (line = "") => line.slice(line.trimEnd().length);
|
|
664
|
+
function table(context, node, place2) {
|
|
665
|
+
const lines = linesOf(context, node);
|
|
666
|
+
const delimiter = lines.length > 2 ? lines[1] ?? "" : (lines[1] ?? "").trimEnd();
|
|
667
|
+
const header = splitRow(lines[0] ?? "");
|
|
668
|
+
const written2 = [header, ...lines.slice(2).map(splitRow)];
|
|
669
|
+
const rows = node.children ?? [];
|
|
670
|
+
const widths = [splitRow(delimiter).cells, ...written2.map((row) => row.cells)];
|
|
671
|
+
const columns = header.cells.length;
|
|
672
|
+
const aligned = rows.length > 1 && Array.from(
|
|
673
|
+
{ length: columns },
|
|
674
|
+
(_, column) => widths.every((cells) => cells[column]?.length === widths[0]?.[column]?.length)
|
|
675
|
+
).every(Boolean);
|
|
676
|
+
if (rows.some((row) => (row.children?.length ?? 0) !== columns)) {
|
|
677
|
+
context.unsupported.push({ reason: "unsupported", line: node.position?.start.line ?? 1 });
|
|
678
|
+
}
|
|
679
|
+
return schema.node(
|
|
680
|
+
"table",
|
|
681
|
+
{ ...place2, delimiter, leading: header.leading, trailing: header.trailing, aligned },
|
|
682
|
+
rows.map(
|
|
683
|
+
(row, index) => schema.node(
|
|
684
|
+
"table_row",
|
|
685
|
+
// Space at the row's end, unless it is the table's last line, whose end lies between blocks.
|
|
686
|
+
{ after: index === rows.length - 1 && index > 0 ? "" : rowEnd(lines[index && index + 1]) },
|
|
687
|
+
(row.children ?? []).map(
|
|
688
|
+
(cell2, column) => schema.node(
|
|
689
|
+
index === 0 ? "table_header" : "table_cell",
|
|
690
|
+
{
|
|
691
|
+
align: node.align?.[column] ?? null,
|
|
692
|
+
pad: cellPadding(written2[index], column)
|
|
693
|
+
},
|
|
694
|
+
inline(context, cell2.children ?? [])
|
|
695
|
+
)
|
|
696
|
+
)
|
|
697
|
+
)
|
|
698
|
+
)
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
var FENCE = /^(`{3,}|~{3,})(.*)$/;
|
|
702
|
+
var CLOSE = /^ {0,3}(`{3,}|~{3,})[ \t]*$/;
|
|
703
|
+
var MARKER = /^([-*+]|\d{1,9}[.)])([ \t]*)/;
|
|
704
|
+
function list(context, node, place2) {
|
|
705
|
+
const items = node.children ?? [];
|
|
706
|
+
const markers = items.map((item) => MARKER.exec(linesOf(context, item)[0] ?? ""));
|
|
707
|
+
const first = markers[0]?.[1] ?? (node.ordered ? "1." : "-");
|
|
708
|
+
const built = [];
|
|
709
|
+
items.forEach((item, index) => {
|
|
710
|
+
const lines = linesOf(context, item);
|
|
711
|
+
const marker = markers[index]?.[1] ?? first;
|
|
712
|
+
const spacing = markers[index]?.[2] ?? " ";
|
|
713
|
+
const rest = (lines[0] ?? "").slice(marker.length + spacing.length);
|
|
714
|
+
const task = typeof item.checked === "boolean" ? /^\[([ xX])\]([ \t]*)/.exec(rest) : null;
|
|
715
|
+
const column = marker.length + (spacing.length >= 1 && spacing.length <= 4 && rest ? spacing.length : 1);
|
|
716
|
+
const indent = " ".repeat(column);
|
|
717
|
+
const inner = {
|
|
718
|
+
...context,
|
|
719
|
+
strip: (line) => {
|
|
720
|
+
const outer = context.strip(line);
|
|
721
|
+
return outer.startsWith(indent) ? outer.slice(column) : outer.trimStart();
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
const previous = items[index - 1];
|
|
725
|
+
const gap = previous ? context.source.slice(blockEnd(context, previous), startOf(item)).split("\n").length - 1 : null;
|
|
726
|
+
const children2 = blocks(inner, item.children ?? []);
|
|
727
|
+
built.push(
|
|
728
|
+
schema.node(
|
|
729
|
+
"list_item",
|
|
730
|
+
{
|
|
731
|
+
gap,
|
|
732
|
+
spacing,
|
|
733
|
+
checked: typeof item.checked === "boolean" ? item.checked : null,
|
|
734
|
+
check: task?.[1] ?? null,
|
|
735
|
+
checkSpacing: task?.[2] ?? " "
|
|
736
|
+
},
|
|
737
|
+
children2.length > 0 ? children2 : [schema.node("paragraph")]
|
|
738
|
+
)
|
|
739
|
+
);
|
|
740
|
+
});
|
|
741
|
+
const tight = !node.spread && !items.some((item) => item.spread);
|
|
742
|
+
if (!node.ordered) {
|
|
743
|
+
return schema.node("bullet_list", { ...place2, marker: first, tight }, built);
|
|
744
|
+
}
|
|
745
|
+
const numbers = markers.map((found) => Number.parseInt(found?.[1] ?? "", 10));
|
|
746
|
+
const numbering = numbers.length > 1 && numbers.every((n) => n === numbers[0]) ? "one" : "up";
|
|
747
|
+
return schema.node(
|
|
748
|
+
"ordered_list",
|
|
749
|
+
{ ...place2, start: node.start ?? 1, delimiter: first.slice(-1), numbering, tight },
|
|
750
|
+
built
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
function quote(context, node, place2) {
|
|
754
|
+
const lines = linesOf(context, node);
|
|
755
|
+
const bare2 = lines.some((line) => /^ {0,3}>(?![ \t]|$)/.test(line));
|
|
756
|
+
const empty = lines.find((line) => /^ {0,3}>[ \t]*$/.test(line));
|
|
757
|
+
const inner = {
|
|
758
|
+
...context,
|
|
759
|
+
strip: (line) => context.strip(line).replace(/^ {0,3}>[ \t]?/, "")
|
|
760
|
+
};
|
|
761
|
+
const children2 = blocks(inner, node.children ?? []);
|
|
762
|
+
return schema.node(
|
|
763
|
+
"blockquote",
|
|
764
|
+
{ ...place2, bare: bare2, blank: empty?.trimStart() ?? ">" },
|
|
765
|
+
children2.length > 0 ? children2 : [schema.node("paragraph")]
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
var SET_IN_WHOLE = /* @__PURE__ */ new Set([
|
|
769
|
+
"list",
|
|
770
|
+
"code",
|
|
771
|
+
"blockquote",
|
|
772
|
+
"table",
|
|
773
|
+
"html",
|
|
774
|
+
"definition",
|
|
775
|
+
"footnoteDefinition"
|
|
776
|
+
]);
|
|
777
|
+
function block(outside, node, place2) {
|
|
778
|
+
const { indent } = place2;
|
|
779
|
+
const context = indent && SET_IN_WHOLE.has(node.type) ? {
|
|
780
|
+
...outside,
|
|
781
|
+
strip: (line) => {
|
|
782
|
+
const rest = outside.strip(line);
|
|
783
|
+
return rest.startsWith(indent) ? rest.slice(indent.length) : rest;
|
|
784
|
+
}
|
|
785
|
+
} : outside;
|
|
786
|
+
const lines = () => linesOf(context, node);
|
|
787
|
+
switch (node.type) {
|
|
788
|
+
case "paragraph":
|
|
789
|
+
return schema.node("paragraph", place2, inline(context, node.children ?? []));
|
|
790
|
+
case "heading": {
|
|
791
|
+
const written2 = lines();
|
|
792
|
+
const first = written2[0] ?? "";
|
|
793
|
+
const level = node.depth ?? 1;
|
|
794
|
+
const children2 = node.children ?? [];
|
|
795
|
+
const content2 = inline(context, children2);
|
|
796
|
+
const hashes = /^#{1,6}/.exec(first)?.[0];
|
|
797
|
+
if (!hashes) {
|
|
798
|
+
const underline = written2[written2.length - 1]?.trimEnd() ?? null;
|
|
799
|
+
return schema.node("heading", { ...place2, level, underline }, content2);
|
|
800
|
+
}
|
|
801
|
+
const after = first.slice(hashes.length);
|
|
802
|
+
const hashGap = after.slice(0, after.length - after.trimStart().length);
|
|
803
|
+
const last = children2[children2.length - 1];
|
|
804
|
+
const closing = (last ? context.source.slice(endOf(last), endOf(node)) : after.slice(hashGap.length)).trimEnd();
|
|
805
|
+
return schema.node("heading", { ...place2, level, hashGap, closing }, content2);
|
|
806
|
+
}
|
|
807
|
+
case "thematicBreak":
|
|
808
|
+
return schema.node("horizontal_rule", { ...place2, markup: lines()[0]?.trimEnd() ?? null });
|
|
809
|
+
case "code": {
|
|
810
|
+
const written2 = lines();
|
|
811
|
+
const text2 = node.value ? [schema.text(node.value)] : [];
|
|
812
|
+
const fence = FENCE.exec(written2[0] ?? "");
|
|
813
|
+
if (!fence) return schema.node("code_block", { ...place2, indented: true }, text2);
|
|
814
|
+
const marks = fence[1];
|
|
815
|
+
const end = written2.length > 1 ? CLOSE.exec(written2[written2.length - 1] ?? "")?.[1] : null;
|
|
816
|
+
const closed = !!end && end[0] === marks[0] && end.length >= marks.length;
|
|
817
|
+
return schema.node(
|
|
818
|
+
"code_block",
|
|
819
|
+
{
|
|
820
|
+
...place2,
|
|
821
|
+
fence: marks,
|
|
822
|
+
info: fence[2] ?? "",
|
|
823
|
+
close: closed ? written2[written2.length - 1] ?? null : null,
|
|
824
|
+
open: !closed
|
|
825
|
+
},
|
|
826
|
+
text2
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
case "blockquote":
|
|
830
|
+
return quote(context, node, place2);
|
|
831
|
+
case "list":
|
|
832
|
+
return list(context, node, place2);
|
|
833
|
+
case "table":
|
|
834
|
+
return table(context, node, place2);
|
|
835
|
+
default: {
|
|
836
|
+
const kind2 = node.type === "html" ? "html" : node.type === "definition" ? "reference" : node.type === "footnoteDefinition" ? "footnote" : "unsupported";
|
|
837
|
+
context.unsupported.push({ reason: kind2, line: node.position?.start.line ?? 1 });
|
|
838
|
+
return schema.node("raw_block", { ...place2, raw: lines().join("\n"), kind: kind2 });
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
function blocks(context, nodes) {
|
|
843
|
+
return nodes.map((node, index) => {
|
|
844
|
+
const previous = nodes[index - 1];
|
|
845
|
+
if (!previous) return block(context, node, { gap: null, indent: "" });
|
|
846
|
+
const between = context.source.slice(blockEnd(context, previous), startOf(node));
|
|
847
|
+
const lineStart = context.source.lastIndexOf("\n", startOf(node) - 1) + 1;
|
|
848
|
+
const before = context.strip(context.source.slice(lineStart, startOf(node)));
|
|
849
|
+
return block(context, node, {
|
|
850
|
+
gap: between.split("\n").length - 1,
|
|
851
|
+
indent: before.trim() === "" ? before : ""
|
|
852
|
+
});
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
var asRead = /* @__PURE__ */ new WeakSet();
|
|
856
|
+
function parseMarkdown(source) {
|
|
857
|
+
const context = { source, strip: (line) => line, unsupported: [] };
|
|
858
|
+
const nodes = readTree(source).children ?? [];
|
|
859
|
+
let lead = source;
|
|
860
|
+
let end = 0;
|
|
861
|
+
const built = nodes.map((node, index) => {
|
|
862
|
+
const lineStart = source.lastIndexOf("\n", startOf(node) - 1) + 1;
|
|
863
|
+
if (index === 0) lead = source.slice(0, lineStart);
|
|
864
|
+
const gap = index === 0 ? null : source.slice(end, lineStart);
|
|
865
|
+
end = blockEnd(context, node);
|
|
866
|
+
return block(context, node, { gap, indent: source.slice(lineStart, startOf(node)) });
|
|
867
|
+
});
|
|
868
|
+
for (const node of built) asRead.add(node);
|
|
869
|
+
const doc = built.length > 0 ? schema.node("doc", { lead, tail: source.slice(end) }, built) : (
|
|
870
|
+
// A text with nothing in it: what is written into it ends with a line break.
|
|
871
|
+
schema.node("doc", { lead: source, tail: "\n" }, [schema.node("paragraph")])
|
|
872
|
+
);
|
|
873
|
+
return { doc, unsupported: context.unsupported };
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// src/markdown/serialize.ts
|
|
877
|
+
import { defaultHandlers, toMarkdown } from "mdast-util-to-markdown";
|
|
878
|
+
import { gfmToMarkdown } from "mdast-util-gfm";
|
|
879
|
+
|
|
880
|
+
// src/markdown/meaning.ts
|
|
881
|
+
var SAID = {
|
|
882
|
+
heading: ["level"],
|
|
883
|
+
ordered_list: ["start"],
|
|
884
|
+
list_item: ["checked"],
|
|
885
|
+
image: ["src", "alt", "title"],
|
|
886
|
+
raw_block: ["raw"],
|
|
887
|
+
raw_inline: ["raw"]
|
|
888
|
+
};
|
|
889
|
+
function standsFor(label, href) {
|
|
890
|
+
return [label, `mailto:${label}`, `http://${label}`].includes(href);
|
|
891
|
+
}
|
|
892
|
+
function marksOf(marks, space, implicit) {
|
|
893
|
+
const said = [];
|
|
894
|
+
for (const mark of marks) {
|
|
895
|
+
const name = mark.type.name;
|
|
896
|
+
if (name === "source" || implicit && mark.eq(implicit)) continue;
|
|
897
|
+
if (space && (name === "em" || name === "strong" || name === "strike")) continue;
|
|
898
|
+
said.push(
|
|
899
|
+
name === "link" ? `link ${JSON.stringify([mark.attrs.href, mark.attrs.title || null])}` : name
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
return said.sort().join(",");
|
|
903
|
+
}
|
|
904
|
+
function runs(node) {
|
|
905
|
+
const out = [];
|
|
906
|
+
const oneLine = node.type.name !== "paragraph";
|
|
907
|
+
const text2 = (value, marks, implicit) => {
|
|
908
|
+
for (const part of value.split(/(\s+)/)) {
|
|
909
|
+
if (!part) continue;
|
|
910
|
+
const space = /^\s/.test(part);
|
|
911
|
+
const own2 = marksOf(marks, space, implicit);
|
|
912
|
+
const code = marks.some((mark) => mark.type.name === "code");
|
|
913
|
+
const said = space && !code && part.includes("\n") ? "\n" : part;
|
|
914
|
+
const last2 = out[out.length - 1];
|
|
915
|
+
if (last2 && "text" in last2 && last2.marks === own2) last2.text += said;
|
|
916
|
+
else out.push({ text: said, marks: own2 });
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
node.forEach((child, _, index) => {
|
|
920
|
+
if (child.isText) {
|
|
921
|
+
const link = child.marks.find((mark) => mark.type.name === "link");
|
|
922
|
+
const alone = link?.attrs.form === "literal" && standsFor(child.text ?? "", String(link.attrs.href)) && !(index > 0 && link.isInSet(node.child(index - 1).marks)) && !(index + 1 < node.childCount && link.isInSet(node.child(index + 1).marks));
|
|
923
|
+
text2(child.text ?? "", child.marks, alone ? link : void 0);
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
const marks = child.marks.filter((mark) => mark.type.name !== "code");
|
|
927
|
+
const isBreak2 = child.type.name === "soft_break" || child.type.name === "hard_break";
|
|
928
|
+
if (isBreak2 && oneLine) text2(" ", marks);
|
|
929
|
+
else if (child.type.name === "soft_break") text2("\n", marks);
|
|
930
|
+
else {
|
|
931
|
+
out.push({
|
|
932
|
+
node: meaning(child),
|
|
933
|
+
marks: marksOf(marks, child.type.name === "hard_break")
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
for (const run2 of out) {
|
|
938
|
+
if ("text" in run2) run2.text = run2.text.replace(/[^\S\n]*\n\s*/g, "\n");
|
|
939
|
+
}
|
|
940
|
+
while (out.length > 0) {
|
|
941
|
+
const end = out[out.length - 1];
|
|
942
|
+
const idle = "text" in end ? end.text.trim() === "" : end.node.type === "hard_break";
|
|
943
|
+
if (!idle) break;
|
|
944
|
+
out.pop();
|
|
945
|
+
}
|
|
946
|
+
const first = out[0];
|
|
947
|
+
if (first && "text" in first) first.text = first.text.replace(/^\s+/, "");
|
|
948
|
+
const last = out[out.length - 1];
|
|
949
|
+
if (last && "text" in last) last.text = last.text.replace(/\s+$/, "");
|
|
950
|
+
return out.filter((run2) => !("text" in run2) || run2.text !== "");
|
|
951
|
+
}
|
|
952
|
+
var isEmpty = (node) => node.type.name === "paragraph" && runs(node).length === 0;
|
|
953
|
+
function saying(node) {
|
|
954
|
+
const blocks2 = Array.from({ length: node.childCount }, (_, i) => node.child(i)).filter(
|
|
955
|
+
(child) => !isEmpty(child)
|
|
956
|
+
);
|
|
957
|
+
const out = [];
|
|
958
|
+
for (const block2 of blocks2) {
|
|
959
|
+
let shown = block2;
|
|
960
|
+
if (isList(block2) && out[out.length - 1]?.type.name === "paragraph") {
|
|
961
|
+
let from = 0;
|
|
962
|
+
while (from < block2.childCount && saysNothing(block2.child(from))) from++;
|
|
963
|
+
if (from === block2.childCount) shown = void 0;
|
|
964
|
+
else if (from > 0) {
|
|
965
|
+
shown = block2.copy(block2.content.cut(offsetOf(block2, from)));
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
if (shown) out.push(shown);
|
|
969
|
+
}
|
|
970
|
+
return out;
|
|
971
|
+
}
|
|
972
|
+
var isList = (node) => node.type.name === "bullet_list" || node.type.name === "ordered_list";
|
|
973
|
+
var saysNothing = (item) => Array.from({ length: item.childCount }, (_, i) => item.child(i)).every(isEmpty);
|
|
974
|
+
var offsetOf = (node, index) => {
|
|
975
|
+
let offset = 0;
|
|
976
|
+
for (let i = 0; i < index; i++) offset += node.child(i).nodeSize;
|
|
977
|
+
return offset;
|
|
978
|
+
};
|
|
979
|
+
function runTogether(first, second) {
|
|
980
|
+
if (second === "paragraph") return ["paragraph", "list", "quote", "table"].includes(first);
|
|
981
|
+
if (second === "table") return ["paragraph", "list", "quote", "table"].includes(first);
|
|
982
|
+
if (second === "rule") return first === "paragraph";
|
|
983
|
+
return second === "quote" && first === "quote";
|
|
984
|
+
}
|
|
985
|
+
var KINDS = {
|
|
986
|
+
paragraph: "paragraph",
|
|
987
|
+
bullet_list: "list",
|
|
988
|
+
ordered_list: "list",
|
|
989
|
+
blockquote: "quote",
|
|
990
|
+
table: "table",
|
|
991
|
+
horizontal_rule: "rule",
|
|
992
|
+
heading: "heading",
|
|
993
|
+
code_block: "code"
|
|
994
|
+
};
|
|
995
|
+
var kind = (node) => KINDS[node.type.name] ?? "other";
|
|
996
|
+
function isLoose(list2) {
|
|
997
|
+
const items = Array.from({ length: list2.childCount }, (_, i) => saying(list2.child(i)));
|
|
998
|
+
const parted = items.some(
|
|
999
|
+
(blocks2) => blocks2.some((block2, i) => i > 0 && runTogether(kind(blocks2[i - 1]), kind(block2)))
|
|
1000
|
+
);
|
|
1001
|
+
if (parted) return true;
|
|
1002
|
+
if (items.length === 1 && (items[0]?.length ?? 0) < 2) return false;
|
|
1003
|
+
return !list2.attrs.tight;
|
|
1004
|
+
}
|
|
1005
|
+
function meaning(node, header = false) {
|
|
1006
|
+
const name = node.type.name;
|
|
1007
|
+
const said = { type: name === "table_header" ? "table_cell" : name };
|
|
1008
|
+
for (const attribute of SAID[name] ?? []) said[attribute] = node.attrs[attribute] || null;
|
|
1009
|
+
if (name === "list_item" && saying(node)[0]?.type.name !== "paragraph") said.checked = null;
|
|
1010
|
+
else if (name === "list_item") said.checked = node.attrs.checked;
|
|
1011
|
+
if (node.type.spec.tableRole === "cell" || node.type.spec.tableRole === "header_cell") {
|
|
1012
|
+
said.align = header ? node.attrs.align || null : null;
|
|
1013
|
+
}
|
|
1014
|
+
if (name === "bullet_list" || name === "ordered_list") said.loose = isLoose(node);
|
|
1015
|
+
if (name === "code_block") {
|
|
1016
|
+
said.info = String(node.attrs.info).trim();
|
|
1017
|
+
said.text = node.textContent;
|
|
1018
|
+
} else if (node.inlineContent) {
|
|
1019
|
+
said.content = runs(node);
|
|
1020
|
+
} else if (node.childCount > 0) {
|
|
1021
|
+
said.content = saying(node).map(
|
|
1022
|
+
(child, index) => meaning(child, name === "table" ? index === 0 : header)
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
if (name === "table") rectangle(said.content);
|
|
1026
|
+
return said;
|
|
1027
|
+
}
|
|
1028
|
+
function rectangle(rows) {
|
|
1029
|
+
const columns = Math.max(0, ...rows.map((row) => row.content?.length ?? 0));
|
|
1030
|
+
for (const row of rows) {
|
|
1031
|
+
const cells = row.content ??= [];
|
|
1032
|
+
while (cells.length < columns) {
|
|
1033
|
+
cells.push({ type: "table_cell", align: null, content: [] });
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
var sameMeaning = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
1038
|
+
|
|
1039
|
+
// src/markdown/serialize.ts
|
|
1040
|
+
var OPEN = "";
|
|
1041
|
+
var SHUT = "";
|
|
1042
|
+
var SHOWN = /^(?!\\)[\p{P}\p{S}]$/u;
|
|
1043
|
+
function kept(value) {
|
|
1044
|
+
const characters = Array.from(value);
|
|
1045
|
+
const first = characters.length > 1 && SHOWN.test(characters[0]) ? 1 : 0;
|
|
1046
|
+
const last = characters.length > first + 1 && SHOWN.test(characters[characters.length - 1]) ? 1 : 0;
|
|
1047
|
+
const end = characters.length - last;
|
|
1048
|
+
return [
|
|
1049
|
+
...characters.slice(0, first),
|
|
1050
|
+
OPEN,
|
|
1051
|
+
...characters.slice(first, end),
|
|
1052
|
+
SHUT,
|
|
1053
|
+
...characters.slice(end)
|
|
1054
|
+
].join("");
|
|
1055
|
+
}
|
|
1056
|
+
var bare = (value) => value.replaceAll(OPEN, "").replaceAll(SHUT, "");
|
|
1057
|
+
var isBreak = (node) => node.type === "break" || node.type === "svSoft";
|
|
1058
|
+
function emphasis(type, sv, children2) {
|
|
1059
|
+
const inside = [...children2];
|
|
1060
|
+
const before = [];
|
|
1061
|
+
const after = [];
|
|
1062
|
+
for (let first = inside[0]; first; first = inside[0]) {
|
|
1063
|
+
const space = first.type === "text" ? /^\s+/.exec(first.value ?? "")?.[0] ?? "" : "";
|
|
1064
|
+
if (isBreak(first)) before.push(inside.shift());
|
|
1065
|
+
else if (space) {
|
|
1066
|
+
before.push({ type: "text", value: space });
|
|
1067
|
+
const rest = first.value.slice(space.length);
|
|
1068
|
+
if (rest) inside[0] = { type: "text", value: rest };
|
|
1069
|
+
else inside.shift();
|
|
1070
|
+
} else break;
|
|
1071
|
+
}
|
|
1072
|
+
for (let last = inside[inside.length - 1]; last; last = inside[inside.length - 1]) {
|
|
1073
|
+
const value = last.type === "text" ? last.value ?? "" : "";
|
|
1074
|
+
const rest = value.trimEnd();
|
|
1075
|
+
if (isBreak(last)) after.unshift(inside.pop());
|
|
1076
|
+
else if (rest.length < value.length) {
|
|
1077
|
+
after.unshift({ type: "text", value: value.slice(rest.length) });
|
|
1078
|
+
if (rest) inside[inside.length - 1] = { type: "text", value: rest };
|
|
1079
|
+
else inside.pop();
|
|
1080
|
+
} else break;
|
|
1081
|
+
}
|
|
1082
|
+
return [...before, ...inside.length > 0 ? [{ type, sv, children: inside }] : [], ...after];
|
|
1083
|
+
}
|
|
1084
|
+
var MARK_NODES = {
|
|
1085
|
+
em: "emphasis",
|
|
1086
|
+
strong: "strong",
|
|
1087
|
+
strike: "delete"
|
|
1088
|
+
};
|
|
1089
|
+
var RANK = ["link", "strong", "em", "strike"];
|
|
1090
|
+
var own = (mark) => mark.type.name !== "source" && mark.type.name !== "code";
|
|
1091
|
+
function leaf(node, flat) {
|
|
1092
|
+
const { attrs: attrs2 } = node;
|
|
1093
|
+
switch (node.type.name) {
|
|
1094
|
+
case "soft_break":
|
|
1095
|
+
return flat ? { type: "text", value: " " } : { type: "svSoft", value: String(attrs2.raw) };
|
|
1096
|
+
case "hard_break":
|
|
1097
|
+
return { type: "break", sv: attrs2 };
|
|
1098
|
+
case "image":
|
|
1099
|
+
return {
|
|
1100
|
+
type: "image",
|
|
1101
|
+
url: String(attrs2.src),
|
|
1102
|
+
alt: String(attrs2.alt),
|
|
1103
|
+
title: attrs2.title,
|
|
1104
|
+
sv: attrs2
|
|
1105
|
+
};
|
|
1106
|
+
default:
|
|
1107
|
+
return { type: "svRaw", value: String(attrs2.raw) };
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
function phrasing(items, applied, flat) {
|
|
1111
|
+
const address = applied.some((mark) => mark.type.name === "link" && mark.attrs.form !== "inline");
|
|
1112
|
+
const out = [];
|
|
1113
|
+
const pending = (node) => node.marks.filter((mark) => own(mark) && !applied.some((other) => other.eq(mark)));
|
|
1114
|
+
for (let i = 0; i < items.length; ) {
|
|
1115
|
+
const item = items[i];
|
|
1116
|
+
const marks = pending(item);
|
|
1117
|
+
if (marks.length > 0) {
|
|
1118
|
+
let best = marks[0];
|
|
1119
|
+
let length = 0;
|
|
1120
|
+
for (const mark of marks) {
|
|
1121
|
+
let end = i;
|
|
1122
|
+
while (end < items.length && mark.isInSet(items[end].marks)) end++;
|
|
1123
|
+
const better = end - i > length || end - i === length && (Number(mark.attrs.depth) - Number(best.attrs.depth) || RANK.indexOf(mark.type.name) - RANK.indexOf(best.type.name)) < 0;
|
|
1124
|
+
if (better) {
|
|
1125
|
+
best = mark;
|
|
1126
|
+
length = end - i;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
const children2 = phrasing(items.slice(i, i + length), [...applied, best], flat);
|
|
1130
|
+
if (best.type.name === "link") {
|
|
1131
|
+
out.push({
|
|
1132
|
+
type: "link",
|
|
1133
|
+
url: String(best.attrs.href),
|
|
1134
|
+
title: best.attrs.title,
|
|
1135
|
+
sv: best.attrs,
|
|
1136
|
+
children: children2
|
|
1137
|
+
});
|
|
1138
|
+
} else {
|
|
1139
|
+
out.push(...emphasis(MARK_NODES[best.type.name], best.attrs, children2));
|
|
1140
|
+
}
|
|
1141
|
+
i += length;
|
|
1142
|
+
continue;
|
|
1143
|
+
}
|
|
1144
|
+
const code = item.marks.find((mark) => mark.type.name === "code");
|
|
1145
|
+
if (code && item.isText) {
|
|
1146
|
+
let value2 = "";
|
|
1147
|
+
for (; i < items.length; i++) {
|
|
1148
|
+
const next = items[i];
|
|
1149
|
+
if (!next.isText || !code.isInSet(next.marks) || pending(next).length > 0) break;
|
|
1150
|
+
value2 += next.text;
|
|
1151
|
+
}
|
|
1152
|
+
out.push({ type: "inlineCode", value: value2, sv: code.attrs });
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
i++;
|
|
1156
|
+
if (!item.isText) {
|
|
1157
|
+
out.push(leaf(item, flat));
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
const text2 = item.text ?? "";
|
|
1161
|
+
const source = item.marks.find((mark) => mark.type.name === "source");
|
|
1162
|
+
const value = String(source?.attrs.value ?? "");
|
|
1163
|
+
const times = value ? text2.length / value.length : 0;
|
|
1164
|
+
if (!address && source && Number.isInteger(times) && value.repeat(times) === text2) {
|
|
1165
|
+
out.push({ type: "svRaw", value: String(source.attrs.raw).repeat(times) });
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
const last = out[out.length - 1];
|
|
1169
|
+
if (last?.type === "text") last.value += text2;
|
|
1170
|
+
else out.push({ type: "text", value: text2 });
|
|
1171
|
+
}
|
|
1172
|
+
return out;
|
|
1173
|
+
}
|
|
1174
|
+
var children = (node) => Array.from({ length: node.childCount }, (_, i) => node.child(i));
|
|
1175
|
+
var isCode = (node) => node.marks.some((mark) => mark.type.name === "code");
|
|
1176
|
+
function content(node) {
|
|
1177
|
+
const items = children(node);
|
|
1178
|
+
const idle = (item) => !!item && (item.type.name === "hard_break" || item.type.name === "soft_break" || item.isText && !isCode(item) && item.text?.trim() === "");
|
|
1179
|
+
while (idle(items[items.length - 1])) items.pop();
|
|
1180
|
+
const leads = (item) => idle(item) && (item?.type.name !== "hard_break" || node.type.name !== "paragraph");
|
|
1181
|
+
while (leads(items[0])) items.shift();
|
|
1182
|
+
const trim = (at, how) => {
|
|
1183
|
+
const item = items[at];
|
|
1184
|
+
if (!item?.isText || isCode(item)) return;
|
|
1185
|
+
const text2 = how(item.text ?? "");
|
|
1186
|
+
if (text2 !== item.text) items[at] = item.type.schema.text(text2, item.marks);
|
|
1187
|
+
};
|
|
1188
|
+
trim(0, (text2) => text2.replace(/^[ \t\n]+/, ""));
|
|
1189
|
+
trim(items.length - 1, (text2) => text2.replace(/[ \t\n]+$/, ""));
|
|
1190
|
+
return items;
|
|
1191
|
+
}
|
|
1192
|
+
var isList2 = (node) => node.type.name === "bullet_list" || node.type.name === "ordered_list";
|
|
1193
|
+
function markerOf(list2, before) {
|
|
1194
|
+
const marker = String(
|
|
1195
|
+
list2.type.name === "bullet_list" ? list2.attrs.marker : list2.attrs.delimiter
|
|
1196
|
+
);
|
|
1197
|
+
if (!before || before.node.type !== list2.type || before.marker !== marker) return marker;
|
|
1198
|
+
if (list2.type.name === "ordered_list") return marker === "." ? ")" : ".";
|
|
1199
|
+
return marker === "-" ? "*" : "-";
|
|
1200
|
+
}
|
|
1201
|
+
function spaced(items, loose) {
|
|
1202
|
+
const gaps = (node) => [node.sv?.gap, ...(node.children ?? []).map((c) => c.sv?.gap)];
|
|
1203
|
+
const parted = items.some((item) => gaps(item).some((gap) => typeof gap === "number" && gap > 1));
|
|
1204
|
+
if (loose === parted) return items;
|
|
1205
|
+
const set = (node, gap) => ({ ...node, sv: { ...node.sv, gap } });
|
|
1206
|
+
return items.map((item, index) => ({
|
|
1207
|
+
...index > 0 ? set(item, loose ? 2 : 1) : item,
|
|
1208
|
+
children: item.children?.map((block2, at) => at > 0 ? set(block2, loose ? 2 : 1) : block2)
|
|
1209
|
+
}));
|
|
1210
|
+
}
|
|
1211
|
+
function blocksOf(node) {
|
|
1212
|
+
const full = saying(node);
|
|
1213
|
+
const shown = full.length > 0 ? full : children(node).slice(0, 1);
|
|
1214
|
+
let list2;
|
|
1215
|
+
return shown.map((child, index) => {
|
|
1216
|
+
const marker = isList2(child) ? markerOf(child, list2) : void 0;
|
|
1217
|
+
list2 = marker ? { node: child, marker } : void 0;
|
|
1218
|
+
const previous = shown[index - 1];
|
|
1219
|
+
return convert(child, {
|
|
1220
|
+
afterList: !!previous && isList2(previous),
|
|
1221
|
+
last: index === shown.length - 1,
|
|
1222
|
+
marker
|
|
1223
|
+
});
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
function convert(node, setting) {
|
|
1227
|
+
const { attrs: attrs2 } = node;
|
|
1228
|
+
const place2 = { gap: attrs2.gap, indent: attrs2.indent };
|
|
1229
|
+
switch (node.type.name) {
|
|
1230
|
+
case "paragraph":
|
|
1231
|
+
return { type: "paragraph", sv: place2, children: phrasing(content(node), [], false) };
|
|
1232
|
+
case "heading": {
|
|
1233
|
+
const level = Number(attrs2.level);
|
|
1234
|
+
const underline = String(attrs2.underline ?? "").trim();
|
|
1235
|
+
const setext = level <= 2 && underline.startsWith(level === 1 ? "=" : "-") && node.content.size > 0;
|
|
1236
|
+
return {
|
|
1237
|
+
type: "heading",
|
|
1238
|
+
depth: level,
|
|
1239
|
+
sv: { ...attrs2, setext },
|
|
1240
|
+
// A heading with `#` is one line.
|
|
1241
|
+
children: phrasing(content(node), [], !setext)
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
case "horizontal_rule":
|
|
1245
|
+
return { type: "thematicBreak", sv: attrs2 };
|
|
1246
|
+
case "code_block":
|
|
1247
|
+
return { type: "code", value: node.textContent, sv: { ...attrs2, ...setting } };
|
|
1248
|
+
case "blockquote":
|
|
1249
|
+
return { type: "blockquote", sv: attrs2, children: blocksOf(node) };
|
|
1250
|
+
case "bullet_list":
|
|
1251
|
+
case "ordered_list": {
|
|
1252
|
+
const ordered = node.type.name === "ordered_list";
|
|
1253
|
+
const marker = setting.marker ?? markerOf(node, void 0);
|
|
1254
|
+
const loose = isLoose(node);
|
|
1255
|
+
const items = children(node).map((item, index) => {
|
|
1256
|
+
const number = attrs2.numbering === "one" ? 0 : index;
|
|
1257
|
+
const mark = ordered ? `${Number(attrs2.start) + number}${marker}` : marker;
|
|
1258
|
+
const blocks2 = blocksOf(item);
|
|
1259
|
+
const first = blocks2[0];
|
|
1260
|
+
if (first?.type === "thematicBreak") {
|
|
1261
|
+
const written2 = String(first.sv?.markup ?? "---").trim();
|
|
1262
|
+
if (written2.startsWith(marker)) {
|
|
1263
|
+
blocks2[0] = { ...first, sv: { ...first.sv, markup: marker === "*" ? "___" : "***" } };
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
return {
|
|
1267
|
+
type: "listItem",
|
|
1268
|
+
// A box to tick begins a paragraph that says something.
|
|
1269
|
+
checked: first?.type === "paragraph" && first.children?.length ? item.attrs.checked : null,
|
|
1270
|
+
spread: loose,
|
|
1271
|
+
sv: { ...item.attrs, mark },
|
|
1272
|
+
children: blocks2
|
|
1273
|
+
};
|
|
1274
|
+
});
|
|
1275
|
+
return {
|
|
1276
|
+
type: "list",
|
|
1277
|
+
ordered,
|
|
1278
|
+
start: ordered ? Number(attrs2.start) : null,
|
|
1279
|
+
spread: loose,
|
|
1280
|
+
sv: attrs2,
|
|
1281
|
+
children: spaced(items, loose)
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
case "table": {
|
|
1285
|
+
const columns = Math.max(0, ...children(node).map((row) => row.childCount));
|
|
1286
|
+
return {
|
|
1287
|
+
type: "table",
|
|
1288
|
+
align: Array.from(
|
|
1289
|
+
{ length: columns },
|
|
1290
|
+
(_, i) => node.firstChild?.maybeChild(i)?.attrs.align ?? null
|
|
1291
|
+
),
|
|
1292
|
+
sv: attrs2,
|
|
1293
|
+
children: children(node).map((row) => ({
|
|
1294
|
+
type: "tableRow",
|
|
1295
|
+
sv: row.attrs,
|
|
1296
|
+
children: Array.from({ length: columns }, (_, i) => {
|
|
1297
|
+
const cell2 = row.maybeChild(i);
|
|
1298
|
+
return {
|
|
1299
|
+
type: "tableCell",
|
|
1300
|
+
sv: cell2?.attrs,
|
|
1301
|
+
children: cell2 ? phrasing(content(cell2), [], true) : []
|
|
1302
|
+
};
|
|
1303
|
+
})
|
|
1304
|
+
}))
|
|
1305
|
+
};
|
|
1306
|
+
}
|
|
1307
|
+
default:
|
|
1308
|
+
return { type: "svRawBlock", value: String(attrs2.raw), sv: attrs2 };
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
var asWritten = true;
|
|
1312
|
+
var handle = (run2, extra = {}) => Object.assign(run2, extra);
|
|
1313
|
+
var text = (value) => typeof value === "string" ? value : "";
|
|
1314
|
+
var usual = (type, node, parent, state, info) => defaultHandlers[type]?.(node, parent, state, info) ?? "";
|
|
1315
|
+
function setIn(node, value, whole) {
|
|
1316
|
+
const indent = text(node.sv?.indent);
|
|
1317
|
+
if (!indent) return value;
|
|
1318
|
+
if (!whole) return kept(indent) + value;
|
|
1319
|
+
return value.split("\n").map((line) => line ? indent + line : line).join("\n");
|
|
1320
|
+
}
|
|
1321
|
+
function attention(type) {
|
|
1322
|
+
const marks = (node) => {
|
|
1323
|
+
const marker = text(node.sv?.marker);
|
|
1324
|
+
if (type === "delete") {
|
|
1325
|
+
return {
|
|
1326
|
+
construct: "strikethrough",
|
|
1327
|
+
markers: ["~"],
|
|
1328
|
+
sizes: marker === "~" ? [1, 2] : [2, 1]
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
const first = marker.startsWith("_") ? "_" : "*";
|
|
1332
|
+
const other = first === "*" ? "_" : "*";
|
|
1333
|
+
return {
|
|
1334
|
+
construct: type,
|
|
1335
|
+
// The serialiser changes marker where two meet; a source that wrote them so is followed while it reads the same.
|
|
1336
|
+
markers: asWritten && marker ? [first] : [first, other],
|
|
1337
|
+
sizes: [type === "strong" ? 2 : 1]
|
|
1338
|
+
};
|
|
1339
|
+
};
|
|
1340
|
+
return handle((node, parent, state, info) => usual(type, node, parent, state, info), {
|
|
1341
|
+
attention: marks,
|
|
1342
|
+
peek: (node) => marks(node).markers[0]
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
function destination(state, before, url, title) {
|
|
1346
|
+
let value = "](";
|
|
1347
|
+
const angled = !url || url.startsWith("<") || Array.from(url).some((c) => c <= " " || c === "\x7F");
|
|
1348
|
+
const exit = state.enter(angled ? "destinationLiteral" : "destinationRaw");
|
|
1349
|
+
if (angled && (url || title)) {
|
|
1350
|
+
value += `<${state.safe(url, { before: `${before}](<`, after: ">" })}>`;
|
|
1351
|
+
} else if (url) {
|
|
1352
|
+
value += state.safe(url, { before: `${before}](`, after: title ? " " : ")" });
|
|
1353
|
+
}
|
|
1354
|
+
exit();
|
|
1355
|
+
if (title) {
|
|
1356
|
+
const done = state.enter("titleQuote");
|
|
1357
|
+
value += ` "${state.safe(title, { before: `${value} "`, after: '"' })}"`;
|
|
1358
|
+
done();
|
|
1359
|
+
}
|
|
1360
|
+
return `${value})`;
|
|
1361
|
+
}
|
|
1362
|
+
var inCell = (state, value) => state.stack.includes("tableCell") ? value.replace(/(?<!\\)\|/g, "\\|") : value;
|
|
1363
|
+
var handlers = {
|
|
1364
|
+
svRaw: handle((node) => kept(text(node.value)), {
|
|
1365
|
+
peek: (node) => text(node.value).slice(0, 1)
|
|
1366
|
+
}),
|
|
1367
|
+
// A line break and the space around it: the break itself stays in sight, so that what follows it is escaped as a line's start.
|
|
1368
|
+
svSoft: handle(
|
|
1369
|
+
(node) => {
|
|
1370
|
+
const raw = text(node.value);
|
|
1371
|
+
const at = raw.indexOf("\n");
|
|
1372
|
+
const before = raw.slice(0, at);
|
|
1373
|
+
return `${before ? kept(before) : ""}
|
|
1374
|
+
${raw.slice(at + 1)}`;
|
|
1375
|
+
},
|
|
1376
|
+
{ peek: () => "\n" }
|
|
1377
|
+
),
|
|
1378
|
+
svRawBlock: handle((node) => setIn(node, kept(text(node.value)), true)),
|
|
1379
|
+
emphasis: attention("emphasis"),
|
|
1380
|
+
strong: attention("strong"),
|
|
1381
|
+
delete: attention("delete"),
|
|
1382
|
+
inlineCode: handle(
|
|
1383
|
+
(node, parent, state, info) => {
|
|
1384
|
+
const value = node.value ?? "";
|
|
1385
|
+
const ticks = Number(node.sv?.ticks ?? 0);
|
|
1386
|
+
const run2 = "`".repeat(ticks);
|
|
1387
|
+
const runs2 = value.match(/`+/g) ?? [];
|
|
1388
|
+
const fits = ticks > 0 && !value.includes("\n") && !runs2.includes(run2) && (node.sv?.padded === true || !/^`|`$|^ .* $/.test(value));
|
|
1389
|
+
const written2 = fits ? node.sv?.padded ? `${run2} ${value} ${run2}` : run2 + value + run2 : usual("inlineCode", node, parent, state, info);
|
|
1390
|
+
return kept(inCell(state, written2));
|
|
1391
|
+
},
|
|
1392
|
+
{ peek: () => "`" }
|
|
1393
|
+
),
|
|
1394
|
+
link: handle(
|
|
1395
|
+
(node, _parent, state) => {
|
|
1396
|
+
const sv = node.sv ?? {};
|
|
1397
|
+
const url = node.url ?? "";
|
|
1398
|
+
const only = node.children?.length === 1 ? node.children[0] : void 0;
|
|
1399
|
+
const label = only?.type === "text" ? only.value ?? "" : void 0;
|
|
1400
|
+
if (label !== void 0 && !node.title && !/[\s<>]/.test(label) && standsFor(label, url)) {
|
|
1401
|
+
if (sv.form === "literal") return kept(label);
|
|
1402
|
+
if (sv.form === "autolink") return kept(`<${label}>`);
|
|
1403
|
+
}
|
|
1404
|
+
const exit = state.enter("link");
|
|
1405
|
+
const inside = state.enter("label");
|
|
1406
|
+
const written2 = state.containerPhrasing(node, { before: "[", after: "](" });
|
|
1407
|
+
inside();
|
|
1408
|
+
const same = url === sv.wasHref && (node.title ?? null) === sv.wasTitle;
|
|
1409
|
+
const tail = same && typeof sv.tail === "string" ? kept(sv.tail) : destination(state, `[${written2}`, url, node.title);
|
|
1410
|
+
exit();
|
|
1411
|
+
return `[${written2}${tail}`;
|
|
1412
|
+
},
|
|
1413
|
+
{ peek: () => "[" }
|
|
1414
|
+
),
|
|
1415
|
+
image: handle(
|
|
1416
|
+
(node, _parent, state) => {
|
|
1417
|
+
const sv = node.sv ?? {};
|
|
1418
|
+
const url = node.url ?? "";
|
|
1419
|
+
const alt = node.alt ?? "";
|
|
1420
|
+
const exit = state.enter("image");
|
|
1421
|
+
const inside = state.enter("label");
|
|
1422
|
+
const label = alt === sv.wasAlt && typeof sv.label === "string" ? kept(sv.label) : state.safe(alt, { before: "![", after: "]" });
|
|
1423
|
+
inside();
|
|
1424
|
+
const same = url === sv.wasSrc && (node.title ?? null) === sv.wasTitle;
|
|
1425
|
+
const tail = same && typeof sv.tail === "string" ? kept(sv.tail) : destination(state, `![${label}`, url, node.title);
|
|
1426
|
+
exit();
|
|
1427
|
+
return `![${label}${tail}`;
|
|
1428
|
+
},
|
|
1429
|
+
{ peek: () => "!" }
|
|
1430
|
+
),
|
|
1431
|
+
break: handle(
|
|
1432
|
+
(node, _parent, state) => {
|
|
1433
|
+
if (state.stack.includes("tableCell") || state.stack.includes("headingAtx")) return " ";
|
|
1434
|
+
return `${kept(text(node.sv?.markup) || "\\")}
|
|
1435
|
+
`;
|
|
1436
|
+
},
|
|
1437
|
+
{ peek: () => "\\" }
|
|
1438
|
+
),
|
|
1439
|
+
paragraph: handle(
|
|
1440
|
+
(node, parent, state, info) => setIn(node, usual("paragraph", node, parent, state, info), false)
|
|
1441
|
+
),
|
|
1442
|
+
heading: handle((node, _parent, state) => {
|
|
1443
|
+
const sv = node.sv ?? {};
|
|
1444
|
+
const exit = state.enter(sv.setext ? "headingSetext" : "headingAtx");
|
|
1445
|
+
const inside = state.enter("phrasing");
|
|
1446
|
+
let value = state.containerPhrasing(node, {
|
|
1447
|
+
before: sv.setext ? "\n" : "# ",
|
|
1448
|
+
after: "\n"
|
|
1449
|
+
});
|
|
1450
|
+
inside();
|
|
1451
|
+
exit();
|
|
1452
|
+
if (sv.setext) return setIn(node, `${value}
|
|
1453
|
+
${kept(text(sv.underline))}`, false);
|
|
1454
|
+
const hashes = "#".repeat(node.depth ?? 1);
|
|
1455
|
+
const closing = text(sv.closing);
|
|
1456
|
+
if (/^[ \t]/.test(value)) value = ` ${value.slice(1)}`;
|
|
1457
|
+
const gap = text(sv.hashGap) || (value || closing ? " " : "");
|
|
1458
|
+
return setIn(
|
|
1459
|
+
node,
|
|
1460
|
+
`${hashes}${value || closing ? gap : ""}${value}${closing ? kept(closing) : ""}`,
|
|
1461
|
+
false
|
|
1462
|
+
);
|
|
1463
|
+
}),
|
|
1464
|
+
thematicBreak: handle((node) => setIn(node, kept(text(node.sv?.markup) || "---"), false)),
|
|
1465
|
+
code: handle((node) => {
|
|
1466
|
+
const sv = node.sv ?? {};
|
|
1467
|
+
const value = node.value ?? "";
|
|
1468
|
+
if (sv.indented && !sv.afterList && value.trim() && !/^[ \t]*\n|\n[ \t]*$/.test(value)) {
|
|
1469
|
+
const lines = value.split("\n").map((line) => line ? ` ${line}` : line);
|
|
1470
|
+
return setIn(node, kept(lines.join("\n")), true);
|
|
1471
|
+
}
|
|
1472
|
+
const fence = text(sv.fence) || "```";
|
|
1473
|
+
const info = text(sv.info);
|
|
1474
|
+
const char = fence.startsWith("`") && info.includes("`") ? "~" : fence[0];
|
|
1475
|
+
const inside = value.split("\n").map((line) => /^[ \t]*([`~]+)/.exec(line)?.[1] ?? "").filter((run3) => run3.startsWith(char)).map((run3) => run3.length);
|
|
1476
|
+
const written2 = fence.startsWith(char) ? fence.length : 3;
|
|
1477
|
+
const run2 = char.repeat(Math.max(3, written2, ...inside.map((length) => length + 1)));
|
|
1478
|
+
const open = sv.open === true && sv.last === true;
|
|
1479
|
+
const close = run2 === fence && typeof sv.close === "string" ? sv.close : run2;
|
|
1480
|
+
const body = value ? `${value}
|
|
1481
|
+
` : "";
|
|
1482
|
+
const whole = `${run2}${info}
|
|
1483
|
+
${body}${open ? "" : close}`;
|
|
1484
|
+
return setIn(node, kept(open ? whole.replace(/\n$/, "") : whole), true);
|
|
1485
|
+
}),
|
|
1486
|
+
blockquote: handle((node, _parent, state, info) => {
|
|
1487
|
+
const exit = state.enter("blockquote");
|
|
1488
|
+
const tracker = state.createTracker(info);
|
|
1489
|
+
tracker.move("> ");
|
|
1490
|
+
tracker.shift(2);
|
|
1491
|
+
const sv = node.sv ?? {};
|
|
1492
|
+
const value = state.indentLines(
|
|
1493
|
+
state.containerFlow(node, tracker.current()),
|
|
1494
|
+
(line, _index, blank) => blank ? text(sv.blank) || ">" : `${sv.bare ? ">" : "> "}${line}`
|
|
1495
|
+
);
|
|
1496
|
+
exit();
|
|
1497
|
+
return setIn(node, value, true);
|
|
1498
|
+
}),
|
|
1499
|
+
list: handle((node, _parent, state, info) => {
|
|
1500
|
+
const exit = state.enter("list");
|
|
1501
|
+
let out = "";
|
|
1502
|
+
(node.children ?? []).forEach((item, index) => {
|
|
1503
|
+
if (index > 0) {
|
|
1504
|
+
const gap = item.sv?.gap;
|
|
1505
|
+
out += "\n".repeat(typeof gap === "number" ? Math.max(1, gap) : node.spread ? 2 : 1);
|
|
1506
|
+
}
|
|
1507
|
+
out += state.handle(item, node, state, info);
|
|
1508
|
+
});
|
|
1509
|
+
exit();
|
|
1510
|
+
return setIn(node, out, true);
|
|
1511
|
+
}),
|
|
1512
|
+
listItem: handle((node, _parent, state, info) => {
|
|
1513
|
+
const sv = node.sv ?? {};
|
|
1514
|
+
const mark = text(sv.mark) || "-";
|
|
1515
|
+
const spacing = text(sv.spacing);
|
|
1516
|
+
const exit = state.enter("listItem");
|
|
1517
|
+
const tracker = state.createTracker(info);
|
|
1518
|
+
tracker.shift(mark.length + 1);
|
|
1519
|
+
const content2 = state.containerFlow(node, tracker.current());
|
|
1520
|
+
exit();
|
|
1521
|
+
const box = typeof node.checked === "boolean" ? `[${node.checked ? sv.check === "X" ? "X" : "x" : " "}]${content2 ? text(sv.checkSpacing) || " " : ""}` : "";
|
|
1522
|
+
const body = box + content2;
|
|
1523
|
+
if (!body) return mark + spacing;
|
|
1524
|
+
const width = spacing.length >= 1 && spacing.length <= 4 ? spacing.length : 1;
|
|
1525
|
+
const indent = " ".repeat(mark.length + width);
|
|
1526
|
+
return state.indentLines(
|
|
1527
|
+
body,
|
|
1528
|
+
(line, index, blank) => index === 0 ? mark + " ".repeat(width) + line : blank ? "" : indent + line
|
|
1529
|
+
);
|
|
1530
|
+
}),
|
|
1531
|
+
table: handle((node, _parent, state) => {
|
|
1532
|
+
const sv = node.sv ?? {};
|
|
1533
|
+
const done = state.enter("table");
|
|
1534
|
+
const rows = (node.children ?? []).map(
|
|
1535
|
+
(row) => (row.children ?? []).map((cell2) => {
|
|
1536
|
+
const exit = state.enter("tableCell");
|
|
1537
|
+
const inside = state.enter("phrasing");
|
|
1538
|
+
const value = state.containerPhrasing(cell2, { before: "|", after: "|" });
|
|
1539
|
+
inside();
|
|
1540
|
+
exit();
|
|
1541
|
+
const pad = cell2.sv?.pad;
|
|
1542
|
+
const [left, right] = !pad || value && !pad[1] && pad[0].length > 1 ? [" ", " "] : pad;
|
|
1543
|
+
return { value, left, right, length: bare(value).length };
|
|
1544
|
+
})
|
|
1545
|
+
);
|
|
1546
|
+
done();
|
|
1547
|
+
const align2 = node.align ?? [];
|
|
1548
|
+
const written2 = splitDelimiter(text(sv.delimiter));
|
|
1549
|
+
const fits = written2.length === align2.length && written2.every((cell2, i) => alignmentOf(cell2) === (align2[i] ?? null));
|
|
1550
|
+
const widths = align2.map((_, column) => {
|
|
1551
|
+
const sizes = rows.map((row) => {
|
|
1552
|
+
const cell2 = row[column];
|
|
1553
|
+
return cell2 ? cell2.left.length + cell2.length + cell2.right.length : 0;
|
|
1554
|
+
});
|
|
1555
|
+
if (fits) sizes.push(written2[column].length);
|
|
1556
|
+
return { even: new Set(sizes).size === 1, needed: 0 };
|
|
1557
|
+
});
|
|
1558
|
+
const leading = sv.leading !== false || align2.length === 1 || rows.some((row) => !row[0]?.value);
|
|
1559
|
+
const trailing = sv.trailing !== false || align2.length === 1 || rows.some((row) => !row.at(-1)?.value);
|
|
1560
|
+
const realign = sv.aligned === true && sv.leading !== false && sv.trailing !== false && (!fits || widths.some((width) => !width.even));
|
|
1561
|
+
if (realign) {
|
|
1562
|
+
widths.forEach((width, column) => {
|
|
1563
|
+
width.needed = Math.max(3, ...rows.map((row) => row[column]?.length ?? 0)) + 2;
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
const edge = (line) => {
|
|
1567
|
+
const begun = leading ? `|${line}` : line.trimStart();
|
|
1568
|
+
return trailing ? `${begun}|` : begun.trimEnd();
|
|
1569
|
+
};
|
|
1570
|
+
const ends = (node.children ?? []).map(
|
|
1571
|
+
(row, index) => index === 0 || index < rows.length - 1 ? text(row.sv?.after) : ""
|
|
1572
|
+
);
|
|
1573
|
+
const lines = rows.map(
|
|
1574
|
+
(row, index) => edge(
|
|
1575
|
+
row.map((cell2, column) => {
|
|
1576
|
+
const width = widths[column]?.needed;
|
|
1577
|
+
if (!realign || !width) return cell2.left + cell2.value + cell2.right;
|
|
1578
|
+
const free = width - 2 - cell2.length;
|
|
1579
|
+
const side = align2[column];
|
|
1580
|
+
const before = side === "right" ? free : side === "center" ? Math.floor(free / 2) : 0;
|
|
1581
|
+
return ` ${" ".repeat(before)}${cell2.value}${" ".repeat(free - before)} `;
|
|
1582
|
+
}).join("|")
|
|
1583
|
+
).concat(ends[index] ?? "")
|
|
1584
|
+
);
|
|
1585
|
+
const delimiter = fits && !realign ? text(sv.delimiter) : edge(
|
|
1586
|
+
align2.map((side, column) => {
|
|
1587
|
+
const width = (realign ? widths[column]?.needed ?? 5 : 5) - 2;
|
|
1588
|
+
const start = side === "left" || side === "center" ? ":" : "";
|
|
1589
|
+
const end = side === "right" || side === "center" ? ":" : "";
|
|
1590
|
+
return ` ${start}${"-".repeat(width - start.length - end.length)}${end} `;
|
|
1591
|
+
}).join("|")
|
|
1592
|
+
);
|
|
1593
|
+
lines.splice(1, 0, delimiter);
|
|
1594
|
+
return setIn(node, lines.join("\n"), true);
|
|
1595
|
+
})
|
|
1596
|
+
};
|
|
1597
|
+
function splitDelimiter(row) {
|
|
1598
|
+
if (!row.trim()) return [];
|
|
1599
|
+
const parts = row.split("|");
|
|
1600
|
+
if (parts.length > 1 && parts[0]?.trim() === "") parts.shift();
|
|
1601
|
+
if (parts.length > 1 && parts[parts.length - 1]?.trim() === "") parts.pop();
|
|
1602
|
+
return parts;
|
|
1603
|
+
}
|
|
1604
|
+
function alignmentOf(cell2) {
|
|
1605
|
+
const value = cell2.trim();
|
|
1606
|
+
const start = value.startsWith(":");
|
|
1607
|
+
const end = value.endsWith(":");
|
|
1608
|
+
return start && end ? "center" : start ? "left" : end ? "right" : null;
|
|
1609
|
+
}
|
|
1610
|
+
var KINDS2 = {
|
|
1611
|
+
paragraph: "paragraph",
|
|
1612
|
+
list: "list",
|
|
1613
|
+
blockquote: "quote",
|
|
1614
|
+
table: "table",
|
|
1615
|
+
thematicBreak: "rule",
|
|
1616
|
+
heading: "heading",
|
|
1617
|
+
code: "code"
|
|
1618
|
+
};
|
|
1619
|
+
var options = {
|
|
1620
|
+
extensions: [gfmToMarkdown()],
|
|
1621
|
+
handlers,
|
|
1622
|
+
join: [
|
|
1623
|
+
(left, right) => {
|
|
1624
|
+
const gap = right.sv?.gap;
|
|
1625
|
+
if (typeof gap === "number" && gap > 1) return gap - 1;
|
|
1626
|
+
if (runTogether(KINDS2[left.type] ?? "other", KINDS2[right.type] ?? "other")) return 1;
|
|
1627
|
+
if (typeof gap === "number") return 0;
|
|
1628
|
+
const begins = !right.ordered || right.start === 1;
|
|
1629
|
+
return left.type === "list" && right.type === "list" && begins ? 0 : void 0;
|
|
1630
|
+
}
|
|
1631
|
+
]
|
|
1632
|
+
};
|
|
1633
|
+
function marksKept(value) {
|
|
1634
|
+
let out = "";
|
|
1635
|
+
let held = false;
|
|
1636
|
+
const referred = (char) => /[!-/:-@[-`{-~]/.test(char) ? char : `&#x${(char.codePointAt(0) ?? 0).toString(16).toUpperCase()};`;
|
|
1637
|
+
for (let i = 0; i < value.length; i++) {
|
|
1638
|
+
const char = value[i];
|
|
1639
|
+
if (char === OPEN) held = true;
|
|
1640
|
+
else if (char === SHUT) held = false;
|
|
1641
|
+
else if (!held && refersTo(value, i, OPEN)) {
|
|
1642
|
+
const next = String.fromCodePoint(value.codePointAt(i + 5) ?? 0);
|
|
1643
|
+
out += OPEN + referred(next);
|
|
1644
|
+
held = true;
|
|
1645
|
+
i += 4 + next.length;
|
|
1646
|
+
continue;
|
|
1647
|
+
} else if (held && refersTo(value, i, SHUT) && !shutFollows(value, i + 5)) {
|
|
1648
|
+
const last = Array.from(out).pop() ?? "";
|
|
1649
|
+
out = out.slice(0, out.length - last.length) + referred(last) + SHUT;
|
|
1650
|
+
held = false;
|
|
1651
|
+
i += 4;
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
out += char;
|
|
1655
|
+
}
|
|
1656
|
+
return out;
|
|
1657
|
+
}
|
|
1658
|
+
function refersTo(value, at, mark) {
|
|
1659
|
+
if (!value.startsWith(`&#x${mark.charCodeAt(0)};`, at)) return false;
|
|
1660
|
+
let backslashes = 0;
|
|
1661
|
+
while (value[at - 1 - backslashes] === "\\") backslashes++;
|
|
1662
|
+
return backslashes % 2 === 0;
|
|
1663
|
+
}
|
|
1664
|
+
function shutFollows(value, from) {
|
|
1665
|
+
for (let i = from; i < value.length; i++) {
|
|
1666
|
+
if (value[i] === SHUT) return true;
|
|
1667
|
+
if (value[i] === OPEN || refersTo(value, i, OPEN)) return false;
|
|
1668
|
+
}
|
|
1669
|
+
return false;
|
|
1670
|
+
}
|
|
1671
|
+
var run = (tree) => marksKept(
|
|
1672
|
+
toMarkdown(
|
|
1673
|
+
{ type: "root", children: [tree] },
|
|
1674
|
+
options
|
|
1675
|
+
).replace(/\n$/, "")
|
|
1676
|
+
);
|
|
1677
|
+
function plain(tree) {
|
|
1678
|
+
const keep = tree.type === "listItem" ? { mark: tree.sv?.mark, spacing: " " } : void 0;
|
|
1679
|
+
const { sv: _sv, ...rest } = tree;
|
|
1680
|
+
const raw = tree.type === "svRawBlock";
|
|
1681
|
+
return {
|
|
1682
|
+
...rest,
|
|
1683
|
+
...raw || tree.type === "code" || tree.type === "heading" ? { sv: unstyled(tree) } : {},
|
|
1684
|
+
...keep ? { sv: keep } : {},
|
|
1685
|
+
...tree.type === "svSoft" ? { type: "text", value: "\n" } : {},
|
|
1686
|
+
...tree.children ? { children: tree.children.map(plain) } : {}
|
|
1687
|
+
};
|
|
1688
|
+
}
|
|
1689
|
+
function unstyled({ type, sv = {} }) {
|
|
1690
|
+
return type === "code" ? { info: sv.info, last: sv.last, afterList: sv.afterList } : {};
|
|
1691
|
+
}
|
|
1692
|
+
var has = (node, type) => {
|
|
1693
|
+
if (node.type.name === type) return true;
|
|
1694
|
+
return children(node).some((child) => has(child, type));
|
|
1695
|
+
};
|
|
1696
|
+
function fewestEscapes(written2, holds) {
|
|
1697
|
+
const escapes = [];
|
|
1698
|
+
let held = false;
|
|
1699
|
+
for (let i = 0; i < written2.length; i++) {
|
|
1700
|
+
const char = written2[i];
|
|
1701
|
+
if (char === OPEN) held = true;
|
|
1702
|
+
else if (char === SHUT) held = false;
|
|
1703
|
+
else if (!held && char === "\\" && /[!-/:-@[-`{-~]/.test(written2[i + 1] ?? "")) {
|
|
1704
|
+
escapes.push(i);
|
|
1705
|
+
i++;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
const without = (value2, at) => at.reduceRight((rest, i) => rest.slice(0, i) + rest.slice(i + 1), value2);
|
|
1709
|
+
if (escapes.length === 0) return bare(written2);
|
|
1710
|
+
const none = bare(without(written2, escapes));
|
|
1711
|
+
if (holds(none)) return none;
|
|
1712
|
+
let value = written2;
|
|
1713
|
+
for (let i = escapes.length - 1; i >= 0; i--) {
|
|
1714
|
+
const candidate = without(value, [escapes[i]]);
|
|
1715
|
+
if (holds(bare(candidate))) value = candidate;
|
|
1716
|
+
}
|
|
1717
|
+
return bare(value);
|
|
1718
|
+
}
|
|
1719
|
+
function write(node, setting) {
|
|
1720
|
+
const first = writeOnce(node, setting);
|
|
1721
|
+
if (asRead.has(node)) return first.value;
|
|
1722
|
+
const read = parseMarkdown(first.value).doc;
|
|
1723
|
+
if (!first.styled || read.childCount !== 1) return first.value;
|
|
1724
|
+
const again = writeOnce(read.firstChild, setting);
|
|
1725
|
+
return again.styled && first.holds(again.value) ? again.value : first.value;
|
|
1726
|
+
}
|
|
1727
|
+
function writeOnce(node, setting) {
|
|
1728
|
+
const tree = convert(node, setting);
|
|
1729
|
+
const attempt = (form, strict) => {
|
|
1730
|
+
asWritten = strict;
|
|
1731
|
+
const value = run(form);
|
|
1732
|
+
asWritten = true;
|
|
1733
|
+
return value;
|
|
1734
|
+
};
|
|
1735
|
+
const styled = attempt(tree, true);
|
|
1736
|
+
const target = meaning(node);
|
|
1737
|
+
const followed = !setting.last && !asRead.has(node);
|
|
1738
|
+
const holds = (value) => {
|
|
1739
|
+
const read = parseMarkdown(`${value}
|
|
1740
|
+
${followed ? "\n> x\n" : ""}`).doc;
|
|
1741
|
+
return read.childCount === (followed ? 2 : 1) && sameMeaning(meaning(read.firstChild), target);
|
|
1742
|
+
};
|
|
1743
|
+
const done = (value, as = true) => ({ value, styled: as, holds });
|
|
1744
|
+
if (has(node, "raw_block") || has(node, "raw_inline")) return done(bare(styled), false);
|
|
1745
|
+
if (holds(bare(styled))) return done(fewestEscapes(styled, holds));
|
|
1746
|
+
const other = attempt(tree, false);
|
|
1747
|
+
if (holds(bare(other))) return done(fewestEscapes(other, holds));
|
|
1748
|
+
const simple = attempt(plain(tree), false);
|
|
1749
|
+
return done(holds(bare(simple)) ? fewestEscapes(simple, holds) : bare(simple));
|
|
1750
|
+
}
|
|
1751
|
+
var written = /* @__PURE__ */ new WeakMap();
|
|
1752
|
+
function blockText(node, setting) {
|
|
1753
|
+
const key = `${setting.afterList ? 1 : 0}${setting.last ? 1 : 0}${setting.marker ?? ""}`;
|
|
1754
|
+
let forms = written.get(node);
|
|
1755
|
+
if (!forms) written.set(node, forms = /* @__PURE__ */ new Map());
|
|
1756
|
+
let value = forms.get(key);
|
|
1757
|
+
if (value === void 0) forms.set(key, value = write(node, setting));
|
|
1758
|
+
return value;
|
|
1759
|
+
}
|
|
1760
|
+
function apart(first, gap, second) {
|
|
1761
|
+
const read = parseMarkdown(first[1] + gap + second[1]).doc;
|
|
1762
|
+
return read.childCount === 2 && sameMeaning(meaning(read.child(0)), meaning(first[0])) && sameMeaning(meaning(read.child(1)), meaning(second[0]));
|
|
1763
|
+
}
|
|
1764
|
+
var BLANK_LINE = /\n[ \t]*\n/;
|
|
1765
|
+
var joined = /* @__PURE__ */ new WeakMap();
|
|
1766
|
+
function serializeMarkdown(doc) {
|
|
1767
|
+
const blocks2 = children(doc).filter((node) => !isEmpty(node));
|
|
1768
|
+
let out = "";
|
|
1769
|
+
let before;
|
|
1770
|
+
let list2;
|
|
1771
|
+
blocks2.forEach((node, index) => {
|
|
1772
|
+
const marker = isList2(node) ? markerOf(node, list2) : void 0;
|
|
1773
|
+
const value = blockText(node, {
|
|
1774
|
+
afterList: !!before && isList2(before.node),
|
|
1775
|
+
last: index === blocks2.length - 1,
|
|
1776
|
+
marker
|
|
1777
|
+
});
|
|
1778
|
+
if (before) {
|
|
1779
|
+
let gap = typeof node.attrs.gap === "string" ? node.attrs.gap : "\n\n";
|
|
1780
|
+
if (!gap.includes("\n")) gap = "\n\n";
|
|
1781
|
+
if (!BLANK_LINE.test(gap)) {
|
|
1782
|
+
let known = joined.get(before.node);
|
|
1783
|
+
if (!known) joined.set(before.node, known = /* @__PURE__ */ new WeakMap());
|
|
1784
|
+
let holds = known.get(node);
|
|
1785
|
+
if (holds === void 0) {
|
|
1786
|
+
holds = apart([before.node, before.text], gap, [node, value]);
|
|
1787
|
+
known.set(node, holds);
|
|
1788
|
+
}
|
|
1789
|
+
if (!holds) gap = "\n\n";
|
|
1790
|
+
}
|
|
1791
|
+
out += gap;
|
|
1792
|
+
}
|
|
1793
|
+
out += value;
|
|
1794
|
+
before = { node, text: value };
|
|
1795
|
+
list2 = marker ? { node, marker } : void 0;
|
|
1796
|
+
});
|
|
1797
|
+
return blocks2.length === 0 ? String(doc.attrs.lead) : `${String(doc.attrs.lead)}${out}${String(doc.attrs.tail)}`;
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
// src/markdown/check.ts
|
|
1801
|
+
var BOM = "\uFEFF";
|
|
1802
|
+
var MOST_ISSUES = 20;
|
|
1803
|
+
function openMarkdown(text2) {
|
|
1804
|
+
try {
|
|
1805
|
+
return readAndCheck(text2);
|
|
1806
|
+
} catch {
|
|
1807
|
+
return {
|
|
1808
|
+
doc: schema.node("doc", null, schema.node("code_block", null, text2 ? schema.text(text2) : [])),
|
|
1809
|
+
format: { mark: false, ending: "\n" },
|
|
1810
|
+
editable: false,
|
|
1811
|
+
issues: [{ reason: "unsupported", line: 1 }]
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
function readAndCheck(text2) {
|
|
1816
|
+
const mark = text2.startsWith(BOM);
|
|
1817
|
+
const body = mark ? text2.slice(1) : text2;
|
|
1818
|
+
const lines = splitLines(body);
|
|
1819
|
+
const ending = lines.find((line) => line.ending !== "")?.ending || "\n";
|
|
1820
|
+
const issues = [];
|
|
1821
|
+
const other = lines.findIndex((line) => line.ending !== "" && line.ending !== ending);
|
|
1822
|
+
if (other !== -1) issues.push({ reason: "line-endings", line: other + 1 });
|
|
1823
|
+
const source = body.replace(/\r\n?/g, "\n");
|
|
1824
|
+
const { doc, unsupported } = parseMarkdown(source);
|
|
1825
|
+
for (const { reason, line } of unsupported) {
|
|
1826
|
+
if (!issues.some((issue) => issue.reason === reason && issue.line === line)) {
|
|
1827
|
+
issues.push({ reason, line });
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
const found = ["", ""].map((mark2) => source.indexOf(mark2)).filter((at) => at >= 0);
|
|
1831
|
+
const control = found.length > 0 ? Math.min(...found) : -1;
|
|
1832
|
+
if (control !== -1) {
|
|
1833
|
+
issues.push({ reason: "unsupported", line: source.slice(0, control).split("\n").length });
|
|
1834
|
+
}
|
|
1835
|
+
if (unsupported.length === 0 && control === -1) {
|
|
1836
|
+
const back = serializeMarkdown(doc);
|
|
1837
|
+
if (back !== source) {
|
|
1838
|
+
const before = source.split("\n");
|
|
1839
|
+
const after = back.split("\n");
|
|
1840
|
+
for (const run2 of diffSequences(before, after)) {
|
|
1841
|
+
if (run2.equal) continue;
|
|
1842
|
+
issues.push({
|
|
1843
|
+
reason: "formatting",
|
|
1844
|
+
line: Math.min(run2.aStart + 1, before.length),
|
|
1845
|
+
found: before.slice(run2.aStart, run2.aEnd).join("\n"),
|
|
1846
|
+
written: after.slice(run2.bStart, run2.bEnd).join("\n")
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
issues.sort((a, b) => a.line - b.line);
|
|
1852
|
+
return {
|
|
1853
|
+
doc,
|
|
1854
|
+
format: { mark, ending },
|
|
1855
|
+
editable: issues.length === 0,
|
|
1856
|
+
issues: issues.slice(0, MOST_ISSUES)
|
|
1857
|
+
};
|
|
1858
|
+
}
|
|
1859
|
+
function checkMarkdownRoundTrip(text2) {
|
|
1860
|
+
const { editable, issues } = openMarkdown(text2);
|
|
1861
|
+
return { editable, issues };
|
|
1862
|
+
}
|
|
1863
|
+
var REASONS = {
|
|
1864
|
+
html: "Raw HTML",
|
|
1865
|
+
footnote: "A footnote",
|
|
1866
|
+
reference: "A reference-style link or its definition",
|
|
1867
|
+
"line-endings": "Line breaks of more than one kind",
|
|
1868
|
+
formatting: "Formatting that would be rewritten",
|
|
1869
|
+
unsupported: "Something the rich view cannot hold"
|
|
1870
|
+
};
|
|
1871
|
+
function describeMarkdownIssue(issue) {
|
|
1872
|
+
return `Line ${issue.line}: ${REASONS[issue.reason]}.`;
|
|
1873
|
+
}
|
|
1874
|
+
export {
|
|
1875
|
+
checkMarkdownRoundTrip,
|
|
1876
|
+
describeMarkdownIssue
|
|
1877
|
+
};
|