@seed-hypermedia/client 0.0.81 → 0.0.83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-link.d.ts +3 -4
- package/dist/auto-link.mjs +3 -3
- package/dist/block-diff.mjs +1 -1
- package/dist/blocks-to-markdown.d.ts +16 -11
- package/dist/blocks-to-markdown.mjs +2 -2
- package/dist/capability.mjs +2 -2
- package/dist/{chunk-MZENVC36.mjs → chunk-2GOQZ3DV.mjs} +1 -1
- package/dist/{chunk-OZKQOI2X.mjs → chunk-3KLY2B27.mjs} +51 -4
- package/dist/{chunk-D2LTD2KZ.mjs → chunk-4VD46ULS.mjs} +1 -1
- package/dist/{chunk-C42YWLL2.mjs → chunk-CRMBYTZY.mjs} +1 -1
- package/dist/{chunk-2OMSCQ56.mjs → chunk-E4PGX55A.mjs} +1 -1
- package/dist/{chunk-YH52VFVR.mjs → chunk-E6YSFT63.mjs} +304 -185
- package/dist/{chunk-IREIRKVP.mjs → chunk-IOXGTLKJ.mjs} +1 -1
- package/dist/{chunk-MZUGCZJ6.mjs → chunk-KMQYEO46.mjs} +38 -11
- package/dist/{chunk-LCKCG574.mjs → chunk-NVWWEHIS.mjs} +1 -1
- package/dist/{chunk-RCVLNXG6.mjs → chunk-QBYF2PVS.mjs} +1 -1
- package/dist/{chunk-55OXHA27.mjs → chunk-SIYJHDMJ.mjs} +4 -2
- package/dist/{chunk-WGOIXGRU.mjs → chunk-TUSTJXWH.mjs} +3 -3
- package/dist/{chunk-BO2QSSIU.mjs → chunk-V2UNLCDS.mjs} +1 -1
- package/dist/{chunk-AIQJ6BI3.mjs → chunk-WJRXPYFE.mjs} +1 -1
- package/dist/chunk-XB6E7EVC.mjs +872 -0
- package/dist/client.mjs +2 -2
- package/dist/comment.mjs +2 -2
- package/dist/document-state.mjs +2 -2
- package/dist/editorblock-to-hmblock.mjs +2 -2
- package/dist/hm-resolver.mjs +2 -2
- package/dist/hm-types.d.ts +216 -114
- package/dist/hm-types.mjs +5 -1
- package/dist/hmblock-to-editorblock.mjs +2 -2
- package/dist/index.mjs +15 -15
- package/dist/markdown-to-blocks.d.ts +27 -32
- package/dist/markdown-to-blocks.mjs +3 -1
- package/dist/pdf-to-blocks.mjs +3 -3
- package/dist/resource-read.mjs +4 -4
- package/dist/tei-to-blocks.mjs +2 -2
- package/dist/unicode.mjs +1 -1
- package/package.json +1 -1
- package/dist/chunk-H7L4TI6X.mjs +0 -705
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
// src/markdown-to-blocks.ts
|
|
2
|
+
import { parse as parseYaml } from "yaml";
|
|
3
|
+
var ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
|
|
4
|
+
function generateBlockId() {
|
|
5
|
+
let id = "";
|
|
6
|
+
for (let i = 0; i < 8; i++) {
|
|
7
|
+
id += ID_CHARS[Math.floor(Math.random() * ID_CHARS.length)];
|
|
8
|
+
}
|
|
9
|
+
return id;
|
|
10
|
+
}
|
|
11
|
+
var SPAN_STYLE_TO_TYPE = {
|
|
12
|
+
color: "TextColor",
|
|
13
|
+
"background-color": "BackgroundColor",
|
|
14
|
+
"font-size": "TextSize",
|
|
15
|
+
"font-family": "TextFamily"
|
|
16
|
+
};
|
|
17
|
+
var SPAN_TYPES = new Set(Object.values(SPAN_STYLE_TO_TYPE));
|
|
18
|
+
function decodeHtmlAttr(s) {
|
|
19
|
+
return s.replace(/"/g, '"').replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
20
|
+
}
|
|
21
|
+
function parseInlineFormatting(raw) {
|
|
22
|
+
const annotations = [];
|
|
23
|
+
const stack = [];
|
|
24
|
+
let text = "";
|
|
25
|
+
let i = 0;
|
|
26
|
+
const close = (span) => {
|
|
27
|
+
if (text.length > span.start) {
|
|
28
|
+
const ann = { type: span.type, starts: [span.start], ends: [text.length] };
|
|
29
|
+
if (span.link !== void 0) ann.link = span.link;
|
|
30
|
+
if (span.attributes) ann.attributes = span.attributes;
|
|
31
|
+
annotations.push(ann);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const toggle = (kind, family) => {
|
|
35
|
+
const idx = findLastIndex(stack, (s) => s.type === kind && s.family === family);
|
|
36
|
+
if (idx === -1) {
|
|
37
|
+
stack.push({ type: kind, start: text.length, family });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const above = stack.splice(idx + 1);
|
|
41
|
+
for (const s of above.reverse()) close(s);
|
|
42
|
+
close(stack.pop());
|
|
43
|
+
for (const s of above.reverse()) stack.push({ ...s, start: text.length });
|
|
44
|
+
};
|
|
45
|
+
const closeType = (pred) => {
|
|
46
|
+
const idx = findLastIndex(stack, pred);
|
|
47
|
+
if (idx === -1) return;
|
|
48
|
+
const above = stack.splice(idx + 1);
|
|
49
|
+
for (const s of above.reverse()) close(s);
|
|
50
|
+
close(stack.pop());
|
|
51
|
+
for (const s of above.reverse()) stack.push({ ...s, start: text.length });
|
|
52
|
+
};
|
|
53
|
+
while (i < raw.length) {
|
|
54
|
+
const ch = raw[i];
|
|
55
|
+
if (ch === "\\" && i + 1 < raw.length) {
|
|
56
|
+
text += raw[i + 1];
|
|
57
|
+
i += 2;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (ch === "`") {
|
|
61
|
+
let n = 0;
|
|
62
|
+
while (raw[i + n] === "`") n++;
|
|
63
|
+
const closer = findBacktickRun(raw, i + n, n);
|
|
64
|
+
if (closer !== -1) {
|
|
65
|
+
let inner = raw.slice(i + n, closer);
|
|
66
|
+
if (inner.length >= 2 && inner.startsWith(" ") && inner.endsWith(" ") && inner.trim() !== "") {
|
|
67
|
+
inner = inner.slice(1, -1);
|
|
68
|
+
}
|
|
69
|
+
inner = inner.replace(/<br\s*\/?>/g, "\n");
|
|
70
|
+
const start = text.length;
|
|
71
|
+
text += inner;
|
|
72
|
+
if (inner.length) annotations.push({ type: "Code", starts: [start], ends: [text.length] });
|
|
73
|
+
i = closer + n;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
text += raw.slice(i, i + n);
|
|
77
|
+
i += n;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (ch === "*" || ch === "_") {
|
|
81
|
+
let n = 0;
|
|
82
|
+
while (raw[i + n] === ch) n++;
|
|
83
|
+
i += n;
|
|
84
|
+
while (n > 0) {
|
|
85
|
+
if (n >= 2) {
|
|
86
|
+
toggle("Bold", ch);
|
|
87
|
+
n -= 2;
|
|
88
|
+
} else {
|
|
89
|
+
toggle("Italic", ch);
|
|
90
|
+
n -= 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (ch === "~" && raw[i + 1] === "~") {
|
|
96
|
+
toggle("Strike", "~");
|
|
97
|
+
i += 2;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (ch === "<") {
|
|
101
|
+
const br = /^<br\s*\/?>/.exec(raw.slice(i));
|
|
102
|
+
if (br) {
|
|
103
|
+
text += "\n";
|
|
104
|
+
i += br[0].length;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const tag = /^<(\/?)(u|mark)>/.exec(raw.slice(i));
|
|
108
|
+
if (tag) {
|
|
109
|
+
const type = tag[2] === "u" ? "Underline" : "Range";
|
|
110
|
+
if (tag[1]) closeType((s) => s.type === type);
|
|
111
|
+
else stack.push({ type, start: text.length, family: "html" });
|
|
112
|
+
i += tag[0].length;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const span = /^<span style="([a-z-]+):([^"]*)">/.exec(raw.slice(i));
|
|
116
|
+
if (span && SPAN_STYLE_TO_TYPE[span[1]]) {
|
|
117
|
+
stack.push({
|
|
118
|
+
type: SPAN_STYLE_TO_TYPE[span[1]],
|
|
119
|
+
start: text.length,
|
|
120
|
+
family: "html",
|
|
121
|
+
attributes: { value: decodeHtmlAttr(span[2]) }
|
|
122
|
+
});
|
|
123
|
+
i += span[0].length;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (raw.startsWith("</span>", i)) {
|
|
127
|
+
closeType((s) => SPAN_TYPES.has(s.type));
|
|
128
|
+
i += 7;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const end = raw.indexOf(">", i + 1);
|
|
132
|
+
if (end !== -1) {
|
|
133
|
+
const inner = raw.slice(i + 1, end);
|
|
134
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\S*$/.test(inner) && !/[\s<>]/.test(inner)) {
|
|
135
|
+
const start = text.length;
|
|
136
|
+
if (inner.startsWith("hm://")) {
|
|
137
|
+
text += "\uFFFC";
|
|
138
|
+
annotations.push({ type: "Embed", starts: [start], ends: [text.length], link: inner });
|
|
139
|
+
} else {
|
|
140
|
+
text += inner;
|
|
141
|
+
annotations.push({ type: "Link", starts: [start], ends: [text.length], link: inner });
|
|
142
|
+
}
|
|
143
|
+
i = end + 1;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (ch === "!" && raw[i + 1] === "[") {
|
|
149
|
+
const link = readLink(raw, i + 1);
|
|
150
|
+
if (link) {
|
|
151
|
+
const parsed = parseInlineFormatting(link.label);
|
|
152
|
+
appendParsed(parsed);
|
|
153
|
+
i = link.end;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (ch === "[") {
|
|
158
|
+
const link = readLink(raw, i);
|
|
159
|
+
if (link) {
|
|
160
|
+
const parsed = parseInlineFormatting(link.label);
|
|
161
|
+
const start = text.length;
|
|
162
|
+
appendParsed(parsed);
|
|
163
|
+
if (text.length > start) {
|
|
164
|
+
annotations.push({ type: "Link", starts: [start], ends: [text.length], link: link.url });
|
|
165
|
+
}
|
|
166
|
+
i = link.end;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
text += ch;
|
|
171
|
+
i++;
|
|
172
|
+
}
|
|
173
|
+
while (stack.length) close(stack.pop());
|
|
174
|
+
return { text, annotations: mergeAdjacentAnnotations(annotations) };
|
|
175
|
+
function appendParsed(parsed) {
|
|
176
|
+
const offset = text.length;
|
|
177
|
+
text += parsed.text;
|
|
178
|
+
for (const ann of parsed.annotations) {
|
|
179
|
+
annotations.push({
|
|
180
|
+
...ann,
|
|
181
|
+
starts: ann.starts.map((s) => s + offset),
|
|
182
|
+
ends: ann.ends.map((e) => e + offset)
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function findLastIndex(arr, pred) {
|
|
188
|
+
for (let i = arr.length - 1; i >= 0; i--) if (pred(arr[i])) return i;
|
|
189
|
+
return -1;
|
|
190
|
+
}
|
|
191
|
+
function findBacktickRun(s, from, n) {
|
|
192
|
+
let i = from;
|
|
193
|
+
while (i < s.length) {
|
|
194
|
+
if (s[i] === "`") {
|
|
195
|
+
let k = 0;
|
|
196
|
+
while (s[i + k] === "`") k++;
|
|
197
|
+
if (k === n) return i;
|
|
198
|
+
i += k;
|
|
199
|
+
} else {
|
|
200
|
+
i++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
function readLink(s, open) {
|
|
206
|
+
let depth = 0;
|
|
207
|
+
let i = open;
|
|
208
|
+
let close = -1;
|
|
209
|
+
while (i < s.length) {
|
|
210
|
+
const c = s[i];
|
|
211
|
+
if (c === "\\") {
|
|
212
|
+
i += 2;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (c === "`") {
|
|
216
|
+
let n = 0;
|
|
217
|
+
while (s[i + n] === "`") n++;
|
|
218
|
+
const closer = findBacktickRun(s, i + n, n);
|
|
219
|
+
i = closer === -1 ? i + n : closer + n;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (c === "[") depth++;
|
|
223
|
+
else if (c === "]") {
|
|
224
|
+
depth--;
|
|
225
|
+
if (depth === 0) {
|
|
226
|
+
close = i;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
i++;
|
|
231
|
+
}
|
|
232
|
+
if (close === -1 || s[close + 1] !== "(") return null;
|
|
233
|
+
const label = s.slice(open + 1, close);
|
|
234
|
+
let j = close + 2;
|
|
235
|
+
if (s[j] === "<") {
|
|
236
|
+
const gt = s.indexOf(">", j + 1);
|
|
237
|
+
if (gt === -1 || s[gt + 1] !== ")") return null;
|
|
238
|
+
return { label, url: s.slice(j + 1, gt), end: gt + 2 };
|
|
239
|
+
}
|
|
240
|
+
let parens = 0;
|
|
241
|
+
while (j < s.length) {
|
|
242
|
+
const c = s[j];
|
|
243
|
+
if (c === "\\") {
|
|
244
|
+
j += 2;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (c === "(") parens++;
|
|
248
|
+
else if (c === ")") {
|
|
249
|
+
if (parens === 0) return { label, url: s.slice(close + 2, j), end: j + 1 };
|
|
250
|
+
parens--;
|
|
251
|
+
}
|
|
252
|
+
j++;
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
function annotationKey(a) {
|
|
257
|
+
return a.type + "\0" + (a.link ?? "") + "\0" + (a.attributes ? JSON.stringify(sortKeys(a.attributes)) : "");
|
|
258
|
+
}
|
|
259
|
+
function mergeAdjacentAnnotations(annotations) {
|
|
260
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
261
|
+
for (const a of annotations) {
|
|
262
|
+
const key = annotationKey(a);
|
|
263
|
+
const entry = byKey.get(key) || { proto: a, ranges: [] };
|
|
264
|
+
for (let i = 0; i < a.starts.length; i++) {
|
|
265
|
+
const s = a.starts[i];
|
|
266
|
+
const e = a.ends[i];
|
|
267
|
+
if (e === void 0 || e <= s) continue;
|
|
268
|
+
entry.ranges.push({ s, e });
|
|
269
|
+
}
|
|
270
|
+
byKey.set(key, entry);
|
|
271
|
+
}
|
|
272
|
+
const out = [];
|
|
273
|
+
for (const { proto, ranges } of Array.from(byKey.values())) {
|
|
274
|
+
ranges.sort((a, b) => a.s - b.s || a.e - b.e);
|
|
275
|
+
const merged = [];
|
|
276
|
+
for (const r of ranges) {
|
|
277
|
+
const last = merged[merged.length - 1];
|
|
278
|
+
if (last && r.s <= last.e) last.e = Math.max(last.e, r.e);
|
|
279
|
+
else merged.push({ ...r });
|
|
280
|
+
}
|
|
281
|
+
for (const r of merged) {
|
|
282
|
+
const ann = { type: proto.type, starts: [r.s], ends: [r.e] };
|
|
283
|
+
if (proto.link !== void 0) ann.link = proto.link;
|
|
284
|
+
if (proto.attributes) ann.attributes = proto.attributes;
|
|
285
|
+
out.push(ann);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
out.sort((a, b) => a.starts[0] - b.starts[0] || b.ends[0] - a.ends[0] || a.type.localeCompare(b.type));
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
function sortKeys(value) {
|
|
292
|
+
if (Array.isArray(value)) return value.map(sortKeys);
|
|
293
|
+
if (value && typeof value === "object") {
|
|
294
|
+
const out = {};
|
|
295
|
+
for (const k of Object.keys(value).sort()) out[k] = sortKeys(value[k]);
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
return value;
|
|
299
|
+
}
|
|
300
|
+
var COMMENT_BODY = /<!--\s*id:([A-Za-z0-9_-]+)(?:\s+type:([A-Za-z0-9_-]+))?(?:\s+attrs:(\{.*\}))?\s*-->/;
|
|
301
|
+
var TRAILING_COMMENT_RE = new RegExp(" ?" + COMMENT_BODY.source + "\\s*$");
|
|
302
|
+
var STANDALONE_COMMENT_RE = new RegExp("^\\s*" + COMMENT_BODY.source + "\\s*$");
|
|
303
|
+
var END_RE = /^\s*<!--\s*end:([A-Za-z0-9_-]+)\s*-->\s*$/;
|
|
304
|
+
function commentFromMatch(m) {
|
|
305
|
+
const c = { id: m[1] };
|
|
306
|
+
if (m[2]) c.type = m[2];
|
|
307
|
+
if (m[3]) {
|
|
308
|
+
try {
|
|
309
|
+
const parsed = JSON.parse(m[3]);
|
|
310
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) c.attrs = parsed;
|
|
311
|
+
} catch {
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return c;
|
|
315
|
+
}
|
|
316
|
+
function stripBlockComment(s) {
|
|
317
|
+
const m = s.match(TRAILING_COMMENT_RE);
|
|
318
|
+
if (m) return { text: s.slice(0, m.index), comment: commentFromMatch(m) };
|
|
319
|
+
return { text: s };
|
|
320
|
+
}
|
|
321
|
+
function stripBlockId(s) {
|
|
322
|
+
const { text, comment } = stripBlockComment(s);
|
|
323
|
+
return { text, id: comment == null ? void 0 : comment.id };
|
|
324
|
+
}
|
|
325
|
+
var MARKER_RE = /^([-*+] |\d+[.)] |> )/;
|
|
326
|
+
function analyzeLine(raw) {
|
|
327
|
+
const lead = /^[ \t]*/.exec(raw)[0];
|
|
328
|
+
const indent = lead.replace(/\t/g, " ").length;
|
|
329
|
+
let rest = raw.slice(lead.length);
|
|
330
|
+
const blank = rest.trim() === "";
|
|
331
|
+
let marker = "";
|
|
332
|
+
let markerKind;
|
|
333
|
+
const m = MARKER_RE.exec(rest);
|
|
334
|
+
if (m && !blank) {
|
|
335
|
+
marker = m[1];
|
|
336
|
+
markerKind = marker === "> " ? "Blockquote" : /^\d/.test(marker) ? "Ordered" : "Unordered";
|
|
337
|
+
rest = rest.slice(marker.length);
|
|
338
|
+
}
|
|
339
|
+
return { raw, indent, marker, markerKind, contentCol: indent + marker.length, rest, blank };
|
|
340
|
+
}
|
|
341
|
+
function makeBlockNode(block, children = []) {
|
|
342
|
+
return { block, children };
|
|
343
|
+
}
|
|
344
|
+
function baseBlock(type, comment, text = "", annotations = []) {
|
|
345
|
+
const block = { type, id: (comment == null ? void 0 : comment.id) || generateBlockId(), text, annotations };
|
|
346
|
+
if ((comment == null ? void 0 : comment.attrs) && Object.keys(comment.attrs).length) block.attributes = { ...comment.attrs };
|
|
347
|
+
return block;
|
|
348
|
+
}
|
|
349
|
+
function createParagraphNode(rawText, comment) {
|
|
350
|
+
const { text, annotations } = parseInlineFormatting(rawText);
|
|
351
|
+
return makeBlockNode(baseBlock("Paragraph", comment, text, annotations));
|
|
352
|
+
}
|
|
353
|
+
function createHeadingNode(rawText, comment) {
|
|
354
|
+
const { text, annotations } = parseInlineFormatting(rawText);
|
|
355
|
+
return makeBlockNode(baseBlock("Heading", comment, text, annotations));
|
|
356
|
+
}
|
|
357
|
+
function createCodeNode(text, language, comment) {
|
|
358
|
+
const block = baseBlock("Code", comment, text);
|
|
359
|
+
if (language) block.language = language;
|
|
360
|
+
return makeBlockNode(block);
|
|
361
|
+
}
|
|
362
|
+
function createMathNode(text, comment) {
|
|
363
|
+
return makeBlockNode(baseBlock("Math", comment, text));
|
|
364
|
+
}
|
|
365
|
+
function createImageNode(alt, url, comment) {
|
|
366
|
+
const { text, annotations } = parseInlineFormatting(alt);
|
|
367
|
+
const block = baseBlock("Image", comment, text, annotations);
|
|
368
|
+
block.link = url;
|
|
369
|
+
return makeBlockNode(block);
|
|
370
|
+
}
|
|
371
|
+
function createTypedNode(visible, comment) {
|
|
372
|
+
const block = baseBlock(comment.type, comment);
|
|
373
|
+
const v = visible.trim();
|
|
374
|
+
const linkOnly = /^<([a-zA-Z][a-zA-Z0-9+.-]*:[^\s<>]*)>$/.exec(v);
|
|
375
|
+
const labeled = v.startsWith("[") ? readLink(v, 0) : null;
|
|
376
|
+
if (linkOnly) {
|
|
377
|
+
block.link = linkOnly[1];
|
|
378
|
+
} else if (labeled && labeled.end === v.length) {
|
|
379
|
+
const { text, annotations } = parseInlineFormatting(labeled.label);
|
|
380
|
+
block.text = text;
|
|
381
|
+
block.annotations = annotations;
|
|
382
|
+
block.link = labeled.url;
|
|
383
|
+
} else if (v) {
|
|
384
|
+
const { text, annotations } = parseInlineFormatting(v);
|
|
385
|
+
block.text = text;
|
|
386
|
+
block.annotations = annotations;
|
|
387
|
+
}
|
|
388
|
+
return makeBlockNode(block);
|
|
389
|
+
}
|
|
390
|
+
var COL_ID_RE = /<!--\s*col:([A-Za-z0-9_-]+)(?:\s+attrs:(\{.*?\}))?\s*-->/;
|
|
391
|
+
var CELL_ID_RE = /<!--\s*id:([A-Za-z0-9_-]+)\s*-->/;
|
|
392
|
+
function splitTableRow(line) {
|
|
393
|
+
const { text, id: afterPipeId } = stripBlockId(line.trim());
|
|
394
|
+
let s = text.trim();
|
|
395
|
+
if (s.startsWith("|")) s = s.slice(1);
|
|
396
|
+
const rawCells = [];
|
|
397
|
+
let cur = "";
|
|
398
|
+
for (let i = 0; i < s.length; i++) {
|
|
399
|
+
if (s[i] === "\\" && s[i + 1] === "|") {
|
|
400
|
+
cur += "|";
|
|
401
|
+
i++;
|
|
402
|
+
} else if (s[i] === "|") {
|
|
403
|
+
rawCells.push(cur.trim());
|
|
404
|
+
cur = "";
|
|
405
|
+
} else {
|
|
406
|
+
cur += s[i];
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const last = cur.trim();
|
|
410
|
+
if (last !== "" || rawCells.length === 0) rawCells.push(last);
|
|
411
|
+
let rowId = afterPipeId;
|
|
412
|
+
const cells = rawCells.map((raw) => {
|
|
413
|
+
let cellText = raw;
|
|
414
|
+
const idMatch = cellText.match(CELL_ID_RE);
|
|
415
|
+
if (idMatch) {
|
|
416
|
+
rowId = idMatch[1];
|
|
417
|
+
cellText = cellText.replace(idMatch[0], "");
|
|
418
|
+
}
|
|
419
|
+
let colId;
|
|
420
|
+
let colAttrs;
|
|
421
|
+
const colMatch = cellText.match(COL_ID_RE);
|
|
422
|
+
if (colMatch) {
|
|
423
|
+
colId = colMatch[1];
|
|
424
|
+
if (colMatch[2]) {
|
|
425
|
+
try {
|
|
426
|
+
colAttrs = JSON.parse(colMatch[2]);
|
|
427
|
+
} catch {
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
cellText = cellText.replace(colMatch[0], "");
|
|
431
|
+
}
|
|
432
|
+
return { text: cellText.trim(), colId, colAttrs };
|
|
433
|
+
});
|
|
434
|
+
return { cells, id: rowId };
|
|
435
|
+
}
|
|
436
|
+
function isSeparatorRow(cells) {
|
|
437
|
+
return cells.length > 0 && cells.every((c) => /^:?-+:?$/.test(c.text));
|
|
438
|
+
}
|
|
439
|
+
function createTableCellNode(rawText, columnId) {
|
|
440
|
+
const { text, annotations } = parseInlineFormatting(rawText);
|
|
441
|
+
return makeBlockNode({
|
|
442
|
+
type: "Paragraph",
|
|
443
|
+
id: generateBlockId(),
|
|
444
|
+
text,
|
|
445
|
+
annotations,
|
|
446
|
+
attributes: { columnId }
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
function createTableNode(lines, comment) {
|
|
450
|
+
var _a;
|
|
451
|
+
const rawRows = lines.map(splitTableRow);
|
|
452
|
+
let headerRow;
|
|
453
|
+
let bodyRows;
|
|
454
|
+
if (rawRows.length >= 2 && isSeparatorRow(rawRows[1].cells)) {
|
|
455
|
+
headerRow = rawRows[0];
|
|
456
|
+
bodyRows = rawRows.slice(2).filter((r) => !isSeparatorRow(r.cells));
|
|
457
|
+
} else {
|
|
458
|
+
bodyRows = rawRows.filter((r) => !isSeparatorRow(r.cells));
|
|
459
|
+
}
|
|
460
|
+
const headerCells = (headerRow == null ? void 0 : headerRow.cells) ?? [];
|
|
461
|
+
const colCount = Math.max(headerCells.length, ...bodyRows.map((r) => r.cells.length), 0);
|
|
462
|
+
const columnIds = [];
|
|
463
|
+
for (let idx = 0; idx < colCount; idx++) {
|
|
464
|
+
columnIds.push(((_a = headerCells[idx]) == null ? void 0 : _a.colId) || generateBlockId());
|
|
465
|
+
}
|
|
466
|
+
const columnNodes = columnIds.map((id, idx) => {
|
|
467
|
+
var _a2;
|
|
468
|
+
const block = { type: "TableColumn", id, text: "", annotations: [] };
|
|
469
|
+
const attrs = (_a2 = headerCells[idx]) == null ? void 0 : _a2.colAttrs;
|
|
470
|
+
if (attrs && Object.keys(attrs).length) block.attributes = { ...attrs };
|
|
471
|
+
return makeBlockNode(block);
|
|
472
|
+
});
|
|
473
|
+
const rowNodes = [];
|
|
474
|
+
const hasHeaderRow = headerCells.some((c) => c.text !== "");
|
|
475
|
+
if (headerRow && hasHeaderRow) {
|
|
476
|
+
const cells = columnIds.map((columnId, idx) => {
|
|
477
|
+
var _a2;
|
|
478
|
+
return createTableCellNode(((_a2 = headerCells[idx]) == null ? void 0 : _a2.text) ?? "", columnId);
|
|
479
|
+
});
|
|
480
|
+
rowNodes.push(
|
|
481
|
+
makeBlockNode(
|
|
482
|
+
{
|
|
483
|
+
type: "TableRow",
|
|
484
|
+
id: headerRow.id || generateBlockId(),
|
|
485
|
+
text: "",
|
|
486
|
+
annotations: [],
|
|
487
|
+
attributes: { isHeader: true }
|
|
488
|
+
},
|
|
489
|
+
cells
|
|
490
|
+
)
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
for (const row of bodyRows) {
|
|
494
|
+
const cells = columnIds.map((columnId, idx) => {
|
|
495
|
+
var _a2;
|
|
496
|
+
return createTableCellNode(((_a2 = row.cells[idx]) == null ? void 0 : _a2.text) ?? "", columnId);
|
|
497
|
+
});
|
|
498
|
+
rowNodes.push(
|
|
499
|
+
makeBlockNode(
|
|
500
|
+
{
|
|
501
|
+
type: "TableRow",
|
|
502
|
+
id: row.id || generateBlockId(),
|
|
503
|
+
text: "",
|
|
504
|
+
annotations: []
|
|
505
|
+
},
|
|
506
|
+
cells
|
|
507
|
+
)
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
return makeBlockNode(baseBlock("Table", comment), [...columnNodes, ...rowNodes]);
|
|
511
|
+
}
|
|
512
|
+
function coerceString(value) {
|
|
513
|
+
if (value == null) return void 0;
|
|
514
|
+
if (value instanceof Date) {
|
|
515
|
+
return value.toISOString().split("T")[0];
|
|
516
|
+
}
|
|
517
|
+
if (typeof value === "string") return value;
|
|
518
|
+
return String(value);
|
|
519
|
+
}
|
|
520
|
+
var METADATA_STRING_KEYS = [
|
|
521
|
+
"name",
|
|
522
|
+
"summary",
|
|
523
|
+
"displayAuthor",
|
|
524
|
+
"displayPublishTime",
|
|
525
|
+
"icon",
|
|
526
|
+
"cover",
|
|
527
|
+
"siteUrl",
|
|
528
|
+
"layout",
|
|
529
|
+
"seedExperimentalLogo",
|
|
530
|
+
"importCategories",
|
|
531
|
+
"importTags",
|
|
532
|
+
"seedExperimentalHomeOrder",
|
|
533
|
+
"contentWidth",
|
|
534
|
+
"childrenType"
|
|
535
|
+
];
|
|
536
|
+
var METADATA_BOOLEAN_KEYS = ["showOutline", "showActivity"];
|
|
537
|
+
function parseFrontmatter(markdown) {
|
|
538
|
+
const trimmed = markdown.replace(/^/, "");
|
|
539
|
+
if (!trimmed.startsWith("---")) {
|
|
540
|
+
return { content: markdown, metadata: {} };
|
|
541
|
+
}
|
|
542
|
+
const firstLineEnd = trimmed.indexOf("\n");
|
|
543
|
+
if (firstLineEnd === -1 || trimmed.slice(0, firstLineEnd).trim() !== "---") {
|
|
544
|
+
return { content: markdown, metadata: {} };
|
|
545
|
+
}
|
|
546
|
+
const closeRe = /\n---[ \t]*(?:\n|$)/g;
|
|
547
|
+
closeRe.lastIndex = firstLineEnd;
|
|
548
|
+
const closeMatch = closeRe.exec(trimmed);
|
|
549
|
+
if (!closeMatch) {
|
|
550
|
+
return { content: markdown, metadata: {} };
|
|
551
|
+
}
|
|
552
|
+
const yamlBlock = trimmed.slice(firstLineEnd + 1, closeMatch.index);
|
|
553
|
+
const rest = trimmed.slice(closeMatch.index + closeMatch[0].length);
|
|
554
|
+
const metadata = {};
|
|
555
|
+
try {
|
|
556
|
+
const parsed = yamlBlock.trim() === "" ? {} : parseYaml(yamlBlock);
|
|
557
|
+
if (!parsed || typeof parsed !== "object") {
|
|
558
|
+
return { content: rest, metadata };
|
|
559
|
+
}
|
|
560
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
561
|
+
if (value === void 0) continue;
|
|
562
|
+
if (key === "title") continue;
|
|
563
|
+
metadata[key] = value;
|
|
564
|
+
}
|
|
565
|
+
if (metadata.name === void 0 && parsed["title"] != null) {
|
|
566
|
+
metadata.name = coerceString(parsed["title"]);
|
|
567
|
+
}
|
|
568
|
+
for (const key of METADATA_STRING_KEYS) {
|
|
569
|
+
if (metadata[key] !== void 0 && typeof metadata[key] !== "string") metadata[key] = coerceString(metadata[key]);
|
|
570
|
+
}
|
|
571
|
+
for (const key of METADATA_BOOLEAN_KEYS) {
|
|
572
|
+
if (metadata[key] !== void 0 && typeof metadata[key] !== "boolean") metadata[key] = Boolean(metadata[key]);
|
|
573
|
+
}
|
|
574
|
+
} catch {
|
|
575
|
+
return { content: markdown, metadata: {} };
|
|
576
|
+
}
|
|
577
|
+
return { content: rest, metadata };
|
|
578
|
+
}
|
|
579
|
+
var HEADING_RE = /^(#{1,6})\s+(.*)$/;
|
|
580
|
+
var isListType = (t) => t === "Unordered" || t === "Ordered" || t === "Blockquote";
|
|
581
|
+
var FENCE_RE = /^(`{3,})(.*)$/;
|
|
582
|
+
var IMAGE_START_RE = /^!\[/;
|
|
583
|
+
function startsSpecialBlock(rest) {
|
|
584
|
+
return HEADING_RE.test(rest) || FENCE_RE.test(rest) || rest.startsWith("$$") || IMAGE_START_RE.test(rest) || rest.startsWith("|") || STANDALONE_COMMENT_RE.test(rest) || END_RE.test(rest);
|
|
585
|
+
}
|
|
586
|
+
function parseMarkdown(markdown) {
|
|
587
|
+
var _a;
|
|
588
|
+
const { content, metadata } = parseFrontmatter(markdown);
|
|
589
|
+
const lines = content.split("\n");
|
|
590
|
+
const infos = lines.map(analyzeLine);
|
|
591
|
+
const root = { list: [], childIndent: 0 };
|
|
592
|
+
const stack = [root];
|
|
593
|
+
const top = () => stack[stack.length - 1];
|
|
594
|
+
function adjustStack(info, headingLevel) {
|
|
595
|
+
while (stack.length > 1) {
|
|
596
|
+
const frame = top();
|
|
597
|
+
if (info.indent < frame.childIndent) {
|
|
598
|
+
stack.pop();
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (info.indent === frame.childIndent) {
|
|
602
|
+
if (frame.shorthand) {
|
|
603
|
+
if (info.markerKind === frame.shorthand) break;
|
|
604
|
+
stack.pop();
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
if (frame.headingLevel !== void 0 && headingLevel !== void 0 && headingLevel <= frame.headingLevel) {
|
|
608
|
+
stack.pop();
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
function isImplicitListStart(frame, kind) {
|
|
617
|
+
if (!frame.node) return true;
|
|
618
|
+
const parentType = frame.node.block.childrenType;
|
|
619
|
+
if (parentType && parentType !== kind) return true;
|
|
620
|
+
return frame.list.length > 0 && frame.markerRun !== kind;
|
|
621
|
+
}
|
|
622
|
+
function nextNonBlank(from) {
|
|
623
|
+
for (let j = from; j < infos.length; j++) if (!infos[j].blank) return infos[j];
|
|
624
|
+
return void 0;
|
|
625
|
+
}
|
|
626
|
+
let i = 0;
|
|
627
|
+
while (i < infos.length) {
|
|
628
|
+
const info = infos[i];
|
|
629
|
+
if (info.blank) {
|
|
630
|
+
i++;
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
const endMatch = END_RE.exec(info.rest);
|
|
634
|
+
if (endMatch && !info.marker) {
|
|
635
|
+
const idx = findLastIndex(stack, (f) => {
|
|
636
|
+
var _a2;
|
|
637
|
+
return ((_a2 = f.node) == null ? void 0 : _a2.block.id) === endMatch[1];
|
|
638
|
+
});
|
|
639
|
+
if (idx > 0) stack.length = idx;
|
|
640
|
+
i++;
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const headingMatch = HEADING_RE.exec(info.rest);
|
|
644
|
+
adjustStack(info, headingMatch ? headingMatch[1].length : void 0);
|
|
645
|
+
let frame = top();
|
|
646
|
+
if (!info.markerKind && !frame.shorthand && ((_a = frame.node) == null ? void 0 : _a.block.type) === "Heading" && frame.list.length > 0 && isListType(frame.node.block.childrenType)) {
|
|
647
|
+
const container = makeBlockNode(
|
|
648
|
+
{
|
|
649
|
+
type: "Paragraph",
|
|
650
|
+
id: generateBlockId(),
|
|
651
|
+
text: "",
|
|
652
|
+
annotations: [],
|
|
653
|
+
childrenType: frame.node.block.childrenType
|
|
654
|
+
},
|
|
655
|
+
frame.list.splice(0)
|
|
656
|
+
);
|
|
657
|
+
frame.list.push(container);
|
|
658
|
+
frame.node.block.childrenType = "Group";
|
|
659
|
+
frame.markerRun = void 0;
|
|
660
|
+
}
|
|
661
|
+
if (info.markerKind && !frame.shorthand && isImplicitListStart(frame, info.markerKind)) {
|
|
662
|
+
const prev = frame.list[frame.list.length - 1];
|
|
663
|
+
const canNest = prev && prev.block.type === "Paragraph" && prev.block.text !== "" && prev.children.length === 0 && !prev.block.childrenType;
|
|
664
|
+
let target;
|
|
665
|
+
if (canNest) {
|
|
666
|
+
target = prev;
|
|
667
|
+
} else {
|
|
668
|
+
target = makeBlockNode({ type: "Paragraph", id: generateBlockId(), text: "", annotations: [] });
|
|
669
|
+
frame.list.push(target);
|
|
670
|
+
frame.markerRun = void 0;
|
|
671
|
+
}
|
|
672
|
+
target.block.childrenType = info.markerKind;
|
|
673
|
+
if (stack.length > 1 && top().node === target) stack.pop();
|
|
674
|
+
stack.push({ node: target, list: target.children, childIndent: info.indent, shorthand: info.markerKind });
|
|
675
|
+
frame = top();
|
|
676
|
+
}
|
|
677
|
+
const parsed = parseBlockAt(i);
|
|
678
|
+
i = parsed.next;
|
|
679
|
+
const node = parsed.node;
|
|
680
|
+
frame.list.push(node);
|
|
681
|
+
frame.markerRun = info.markerKind;
|
|
682
|
+
if (info.markerKind && frame.node && !frame.node.block.childrenType) {
|
|
683
|
+
frame.node.block.childrenType = info.markerKind;
|
|
684
|
+
}
|
|
685
|
+
if (headingMatch) {
|
|
686
|
+
stack.push({ node, list: node.children, childIndent: info.contentCol, headingLevel: headingMatch[1].length });
|
|
687
|
+
} else if (parsed.standalone && !info.marker) {
|
|
688
|
+
const following = nextNonBlank(i);
|
|
689
|
+
if (following && following.indent === info.indent && following.markerKind) {
|
|
690
|
+
stack.push({ node, list: node.children, childIndent: info.indent, shorthand: following.markerKind });
|
|
691
|
+
} else {
|
|
692
|
+
stack.push({ node, list: node.children, childIndent: info.indent + 2 });
|
|
693
|
+
}
|
|
694
|
+
} else {
|
|
695
|
+
stack.push({ node, list: node.children, childIndent: info.marker ? info.contentCol : info.indent + 2 });
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
const finalize = (nodes) => {
|
|
699
|
+
for (const n of nodes) {
|
|
700
|
+
if (n.children.length && !n.block.childrenType && n.block.type !== "Table" && n.block.type !== "TableRow") {
|
|
701
|
+
n.block.childrenType = "Group";
|
|
702
|
+
}
|
|
703
|
+
finalize(n.children);
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
finalize(root.list);
|
|
707
|
+
return { tree: root.list, metadata };
|
|
708
|
+
function parseBlockAt(start) {
|
|
709
|
+
const info = infos[start];
|
|
710
|
+
const rest = info.rest;
|
|
711
|
+
const standalone = STANDALONE_COMMENT_RE.exec(rest);
|
|
712
|
+
if (standalone) {
|
|
713
|
+
const comment2 = commentFromMatch(standalone);
|
|
714
|
+
const following = nextNonBlank(start + 1);
|
|
715
|
+
if (following && following.indent >= info.indent && following.rest.startsWith("|") && !following.marker) {
|
|
716
|
+
const tableLines = [];
|
|
717
|
+
let j2 = start + 1;
|
|
718
|
+
while (j2 < infos.length && infos[j2].rest.startsWith("|") && !infos[j2].blank) {
|
|
719
|
+
tableLines.push(infos[j2].rest);
|
|
720
|
+
j2++;
|
|
721
|
+
}
|
|
722
|
+
return { node: createTableNode(tableLines, comment2), next: j2, standalone: false };
|
|
723
|
+
}
|
|
724
|
+
const type = comment2.type || "Paragraph";
|
|
725
|
+
return { node: makeBlockNode(baseBlock(type, comment2)), next: start + 1, standalone: true };
|
|
726
|
+
}
|
|
727
|
+
const fence = FENCE_RE.exec(rest);
|
|
728
|
+
if (fence) {
|
|
729
|
+
const fenceLen = fence[1].length;
|
|
730
|
+
const { text: language, comment: comment2 } = stripBlockComment(fence[2].trim());
|
|
731
|
+
const bodyLines = [];
|
|
732
|
+
let j2 = start + 1;
|
|
733
|
+
while (j2 < infos.length) {
|
|
734
|
+
const l = infos[j2];
|
|
735
|
+
const closing = /^(`{3,})\s*$/.exec(l.raw.trim());
|
|
736
|
+
if (closing && closing[1].length >= fenceLen && l.indent <= info.contentCol) break;
|
|
737
|
+
bodyLines.push(stripIndent(l.raw, info.contentCol));
|
|
738
|
+
j2++;
|
|
739
|
+
}
|
|
740
|
+
j2++;
|
|
741
|
+
return { node: createCodeNode(bodyLines.join("\n"), language.trim(), comment2), next: j2, standalone: false };
|
|
742
|
+
}
|
|
743
|
+
if (rest.startsWith("$$")) {
|
|
744
|
+
const { comment: comment2 } = stripBlockComment(rest.slice(2).trim());
|
|
745
|
+
const bodyLines = [];
|
|
746
|
+
let j2 = start + 1;
|
|
747
|
+
while (j2 < infos.length && infos[j2].raw.trim() !== "$$") {
|
|
748
|
+
bodyLines.push(stripIndent(infos[j2].raw, info.contentCol));
|
|
749
|
+
j2++;
|
|
750
|
+
}
|
|
751
|
+
j2++;
|
|
752
|
+
return { node: createMathNode(bodyLines.join("\n"), comment2), next: j2, standalone: false };
|
|
753
|
+
}
|
|
754
|
+
const heading = HEADING_RE.exec(rest);
|
|
755
|
+
if (heading) {
|
|
756
|
+
const { text: text2, comment: comment2 } = stripBlockComment(heading[2]);
|
|
757
|
+
if (comment2 == null ? void 0 : comment2.type) return { node: createTypedNode(text2, comment2), next: start + 1, standalone: false };
|
|
758
|
+
return { node: createHeadingNode(text2, comment2), next: start + 1, standalone: false };
|
|
759
|
+
}
|
|
760
|
+
if (rest.startsWith("|")) {
|
|
761
|
+
const tableLines = [];
|
|
762
|
+
let j2 = start;
|
|
763
|
+
while (j2 < infos.length && infos[j2].rest.startsWith("|") && !infos[j2].blank) {
|
|
764
|
+
tableLines.push(infos[j2].rest);
|
|
765
|
+
j2++;
|
|
766
|
+
}
|
|
767
|
+
return { node: createTableNode(tableLines), next: j2, standalone: false };
|
|
768
|
+
}
|
|
769
|
+
const { text: visible, comment } = stripBlockComment(rest);
|
|
770
|
+
if (comment == null ? void 0 : comment.type) {
|
|
771
|
+
return { node: createTypedNode(visible, comment), next: start + 1, standalone: false };
|
|
772
|
+
}
|
|
773
|
+
if (IMAGE_START_RE.test(visible)) {
|
|
774
|
+
const link = readLink(visible, 1);
|
|
775
|
+
if (link && visible.slice(link.end).trim() === "") {
|
|
776
|
+
let url = link.url;
|
|
777
|
+
if (url && !url.match(/^[a-zA-Z][a-zA-Z0-9+.-]*:/)) url = `file://${url}`;
|
|
778
|
+
return { node: createImageNode(link.label, url, comment), next: start + 1, standalone: false };
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
let text = visible;
|
|
782
|
+
let j = start + 1;
|
|
783
|
+
if (!comment) {
|
|
784
|
+
while (j < infos.length) {
|
|
785
|
+
const l = infos[j];
|
|
786
|
+
if (l.blank || l.marker || l.indent !== info.contentCol || startsSpecialBlock(l.rest)) break;
|
|
787
|
+
const { text: more, comment: c } = stripBlockComment(l.rest);
|
|
788
|
+
if (c) break;
|
|
789
|
+
text += " " + more;
|
|
790
|
+
j++;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return { node: createParagraphNode(text, comment), next: j, standalone: false };
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
function stripIndent(line, n) {
|
|
797
|
+
let k = 0;
|
|
798
|
+
while (k < n && line[k] === " ") k++;
|
|
799
|
+
return line.slice(k);
|
|
800
|
+
}
|
|
801
|
+
function markdownBlockNodesToHMBlockNodes(nodes) {
|
|
802
|
+
return nodes.map((node) => {
|
|
803
|
+
const { block } = node;
|
|
804
|
+
const attributes = { ...block.attributes };
|
|
805
|
+
if (block.childrenType !== void 0) {
|
|
806
|
+
attributes.childrenType = block.childrenType;
|
|
807
|
+
}
|
|
808
|
+
if (block.language !== void 0) {
|
|
809
|
+
attributes.language = block.language;
|
|
810
|
+
}
|
|
811
|
+
const hmBlock = {
|
|
812
|
+
type: block.type,
|
|
813
|
+
id: block.id,
|
|
814
|
+
text: block.text,
|
|
815
|
+
annotations: block.annotations.map((a) => ({
|
|
816
|
+
type: a.type,
|
|
817
|
+
starts: a.starts,
|
|
818
|
+
ends: a.ends,
|
|
819
|
+
...a.link !== void 0 ? { link: a.link } : {},
|
|
820
|
+
...a.attributes !== void 0 ? { attributes: a.attributes } : {}
|
|
821
|
+
})),
|
|
822
|
+
attributes
|
|
823
|
+
};
|
|
824
|
+
if (block.link !== void 0) {
|
|
825
|
+
hmBlock.link = block.link;
|
|
826
|
+
}
|
|
827
|
+
return {
|
|
828
|
+
block: hmBlock,
|
|
829
|
+
children: node.children.length > 0 ? markdownBlockNodesToHMBlockNodes(node.children) : void 0
|
|
830
|
+
};
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
function flattenToOperations(tree, parentId = "") {
|
|
834
|
+
const ops = [];
|
|
835
|
+
const blockIds = [];
|
|
836
|
+
for (const node of tree) {
|
|
837
|
+
const block = {
|
|
838
|
+
...node.block.attributes,
|
|
839
|
+
type: node.block.type,
|
|
840
|
+
id: node.block.id,
|
|
841
|
+
text: node.block.text,
|
|
842
|
+
annotations: node.block.annotations
|
|
843
|
+
};
|
|
844
|
+
if (node.block.language !== void 0) {
|
|
845
|
+
block["language"] = node.block.language;
|
|
846
|
+
}
|
|
847
|
+
if (node.block.childrenType !== void 0) {
|
|
848
|
+
block["childrenType"] = node.block.childrenType;
|
|
849
|
+
}
|
|
850
|
+
if (node.block.link !== void 0) {
|
|
851
|
+
block["link"] = node.block.link;
|
|
852
|
+
}
|
|
853
|
+
ops.push({ type: "ReplaceBlock", block });
|
|
854
|
+
blockIds.push(node.block.id);
|
|
855
|
+
if (node.children.length > 0) {
|
|
856
|
+
ops.push(...flattenToOperations(node.children, node.block.id));
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
if (blockIds.length > 0) {
|
|
860
|
+
ops.push({ type: "MoveBlocks", blocks: blockIds, parent: parentId });
|
|
861
|
+
}
|
|
862
|
+
return ops;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
export {
|
|
866
|
+
parseInlineFormatting,
|
|
867
|
+
mergeAdjacentAnnotations,
|
|
868
|
+
parseFrontmatter,
|
|
869
|
+
parseMarkdown,
|
|
870
|
+
markdownBlockNodesToHMBlockNodes,
|
|
871
|
+
flattenToOperations
|
|
872
|
+
};
|