@seed-hypermedia/client 0.0.68 → 0.0.70
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/LICENSE +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +266 -0
- package/dist/auto-link.d.ts +3 -2
- package/dist/auto-link.mjs +21 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +170 -0
- package/dist/blobs.mjs +51 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.d.ts +7 -2
- package/dist/capability.mjs +14 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.d.ts +8 -7
- package/dist/change.mjs +24 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-37WRGUK4.mjs +125 -0
- package/dist/chunk-3KESF5XJ.mjs +297 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-4Q3SAL5R.mjs +75 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/chunk-BUPMCRR4.mjs +48 -0
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
- package/dist/chunk-DR3HJ6BV.mjs +25 -0
- package/dist/chunk-EU36RFR3.mjs +159 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-IT5LLTMP.mjs +209 -0
- package/dist/chunk-JT57MV7Y.mjs +136 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LVAHHQIK.mjs +41 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NYWCMQTH.mjs +412 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PJ2RFT5A.mjs +113 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-SHOAYNJY.mjs +289 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.mjs +15 -0
- package/dist/comment.d.ts +5 -4
- package/dist/comment.mjs +19 -0
- package/dist/contact.d.ts +5 -4
- package/dist/contact.mjs +15 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +53 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +132 -5453
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.d.ts +5 -4
- package/dist/ref.mjs +13 -0
- package/dist/resource-read.mjs +15 -0
- package/dist/signer.d.ts +42 -0
- package/dist/signer.mjs +9 -0
- package/dist/signing.d.ts +10 -2
- package/dist/signing.mjs +16 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +10 -6
- package/dist/vault.d.ts +14 -4
- package/dist/vault.mjs +6 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
import {
|
|
2
|
+
unpackHmId
|
|
3
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/blocks-to-markdown.ts
|
|
6
|
+
var FM_STRING_KEYS = [
|
|
7
|
+
"name",
|
|
8
|
+
"summary",
|
|
9
|
+
"displayAuthor",
|
|
10
|
+
"displayPublishTime",
|
|
11
|
+
"icon",
|
|
12
|
+
"cover",
|
|
13
|
+
"siteUrl",
|
|
14
|
+
"layout",
|
|
15
|
+
"seedExperimentalLogo",
|
|
16
|
+
"seedExperimentalHomeOrder",
|
|
17
|
+
"contentWidth",
|
|
18
|
+
"childrenType",
|
|
19
|
+
"importCategories",
|
|
20
|
+
"importTags"
|
|
21
|
+
];
|
|
22
|
+
var FM_BOOLEAN_KEYS = ["showOutline", "showActivity"];
|
|
23
|
+
function emitFrontmatter(metadata) {
|
|
24
|
+
const lines = [];
|
|
25
|
+
for (const key of FM_STRING_KEYS) {
|
|
26
|
+
const val = metadata[key];
|
|
27
|
+
if (val !== void 0 && val !== "") {
|
|
28
|
+
lines.push(`${key}: ${JSON.stringify(val)}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const key of FM_BOOLEAN_KEYS) {
|
|
32
|
+
const val = metadata[key];
|
|
33
|
+
if (val !== void 0) {
|
|
34
|
+
lines.push(`${key}: ${val}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (metadata.theme && typeof metadata.theme === "object") {
|
|
38
|
+
const entries = [];
|
|
39
|
+
if (metadata.theme.headerLayout !== void 0) {
|
|
40
|
+
entries.push(` headerLayout: ${JSON.stringify(metadata.theme.headerLayout)}`);
|
|
41
|
+
}
|
|
42
|
+
if (entries.length > 0) {
|
|
43
|
+
lines.push("theme:");
|
|
44
|
+
lines.push(...entries);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (lines.length === 0) return "---\n---\n";
|
|
48
|
+
return "---\n" + lines.join("\n") + "\n---\n";
|
|
49
|
+
}
|
|
50
|
+
function idComment(id) {
|
|
51
|
+
return `<!-- id:${id} -->`;
|
|
52
|
+
}
|
|
53
|
+
function appendIdToFirstLine(md, id) {
|
|
54
|
+
const newline = md.indexOf("\n");
|
|
55
|
+
if (newline === -1) {
|
|
56
|
+
return md ? `${md} ${idComment(id)}` : idComment(id);
|
|
57
|
+
}
|
|
58
|
+
return `${md.slice(0, newline)} ${idComment(id)}${md.slice(newline)}`;
|
|
59
|
+
}
|
|
60
|
+
function blocksToMarkdown(doc, options) {
|
|
61
|
+
const opts = { ipfsGateway: true, ...options };
|
|
62
|
+
const lines = [];
|
|
63
|
+
lines.push(emitFrontmatter(doc.metadata || {}));
|
|
64
|
+
for (const node of doc.content) {
|
|
65
|
+
const blockMd = blockNodeToMarkdown(node, 0, opts);
|
|
66
|
+
if (blockMd) {
|
|
67
|
+
lines.push(blockMd);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return lines.join("\n");
|
|
71
|
+
}
|
|
72
|
+
function blockNodeToMarkdown(node, depth, opts) {
|
|
73
|
+
var _a;
|
|
74
|
+
const block = node.block;
|
|
75
|
+
const children = node.children || [];
|
|
76
|
+
if (block.type === "Table") {
|
|
77
|
+
return tableToMarkdown(node, depth, (text, annotations) => applyAnnotations(text, annotations));
|
|
78
|
+
}
|
|
79
|
+
const childrenType = (_a = block.attributes) == null ? void 0 : _a.childrenType;
|
|
80
|
+
const isListContainer = block.type === "Paragraph" && !block.text && (childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote");
|
|
81
|
+
let result;
|
|
82
|
+
if (isListContainer) {
|
|
83
|
+
result = idComment(block.id);
|
|
84
|
+
} else {
|
|
85
|
+
result = blockToMarkdown(block, depth, opts);
|
|
86
|
+
}
|
|
87
|
+
for (const child of children) {
|
|
88
|
+
const childMd = blockNodeToMarkdown(child, depth + 1, opts);
|
|
89
|
+
if (childMd) {
|
|
90
|
+
if (childrenType === "Ordered") {
|
|
91
|
+
result += "\n" + indent(depth + 1) + "1. " + childMd.trim();
|
|
92
|
+
} else if (childrenType === "Unordered") {
|
|
93
|
+
result += "\n" + indent(depth + 1) + "- " + childMd.trim();
|
|
94
|
+
} else if (childrenType === "Blockquote") {
|
|
95
|
+
result += "\n" + indent(depth + 1) + "> " + childMd.trim();
|
|
96
|
+
} else {
|
|
97
|
+
result += "\n\n" + childMd;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
function blockToMarkdown(block, depth, opts) {
|
|
104
|
+
var _a, _b;
|
|
105
|
+
const ind = indent(depth);
|
|
106
|
+
const b = block;
|
|
107
|
+
const text = b.text || "";
|
|
108
|
+
const link = b.link || "";
|
|
109
|
+
const annotations = b.annotations;
|
|
110
|
+
const id = b.id;
|
|
111
|
+
switch (block.type) {
|
|
112
|
+
case "Paragraph": {
|
|
113
|
+
const rendered = ind + applyAnnotations(text, annotations);
|
|
114
|
+
return appendIdToFirstLine(rendered, id);
|
|
115
|
+
}
|
|
116
|
+
case "Heading": {
|
|
117
|
+
const level = Math.min(depth + 1, 6);
|
|
118
|
+
const hashes = "#".repeat(level);
|
|
119
|
+
const rendered = `${hashes} ${applyAnnotations(text, annotations)}`;
|
|
120
|
+
return appendIdToFirstLine(rendered, id);
|
|
121
|
+
}
|
|
122
|
+
case "Code": {
|
|
123
|
+
const lang = ((_a = b.attributes) == null ? void 0 : _a.language) || "";
|
|
124
|
+
return ind + "```" + lang + " " + idComment(id) + "\n" + ind + text + "\n" + ind + "```";
|
|
125
|
+
}
|
|
126
|
+
case "Math": {
|
|
127
|
+
return ind + "$$ " + idComment(id) + "\n" + ind + text + "\n" + ind + "$$";
|
|
128
|
+
}
|
|
129
|
+
case "Image": {
|
|
130
|
+
const altText = text || "image";
|
|
131
|
+
const imgUrl = formatMediaUrl(link, opts.ipfsGateway);
|
|
132
|
+
return ind + ` ${idComment(id)}`;
|
|
133
|
+
}
|
|
134
|
+
case "Video": {
|
|
135
|
+
const videoUrl = formatMediaUrl(link, opts.ipfsGateway);
|
|
136
|
+
return ind + `[Video](${videoUrl}) ${idComment(id)}`;
|
|
137
|
+
}
|
|
138
|
+
case "File": {
|
|
139
|
+
const fileName = ((_b = b.attributes) == null ? void 0 : _b.name) || "file";
|
|
140
|
+
const fileUrl = formatMediaUrl(link, opts.ipfsGateway);
|
|
141
|
+
return ind + `[${fileName}](${fileUrl}) ${idComment(id)}`;
|
|
142
|
+
}
|
|
143
|
+
case "Embed": {
|
|
144
|
+
return ind + `> [Embed: ${link}](${link}) ${idComment(id)}`;
|
|
145
|
+
}
|
|
146
|
+
case "WebEmbed": {
|
|
147
|
+
return ind + `[Web Embed](${link}) ${idComment(id)}`;
|
|
148
|
+
}
|
|
149
|
+
case "Button": {
|
|
150
|
+
const buttonText = text || "Button";
|
|
151
|
+
return ind + `[${buttonText}](${link}) ${idComment(id)}`;
|
|
152
|
+
}
|
|
153
|
+
case "Query": {
|
|
154
|
+
return ind + `<!-- Query block --> ${idComment(id)}`;
|
|
155
|
+
}
|
|
156
|
+
case "Nostr": {
|
|
157
|
+
return ind + `[Nostr: ${link}](${link}) ${idComment(id)}`;
|
|
158
|
+
}
|
|
159
|
+
default: {
|
|
160
|
+
if (text) {
|
|
161
|
+
return appendIdToFirstLine(ind + text, id);
|
|
162
|
+
}
|
|
163
|
+
return "";
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function normalizeTableNode(node) {
|
|
168
|
+
var _a, _b;
|
|
169
|
+
const columns = [];
|
|
170
|
+
const rows = [];
|
|
171
|
+
for (const child of node.children || []) {
|
|
172
|
+
const block = child.block;
|
|
173
|
+
if ((block == null ? void 0 : block.type) === "TableColumn") {
|
|
174
|
+
columns.push({ id: block.id });
|
|
175
|
+
} else if ((block == null ? void 0 : block.type) === "TableRow") {
|
|
176
|
+
const cells = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const cellNode of child.children || []) {
|
|
178
|
+
const cell = cellNode.block;
|
|
179
|
+
const columnId = (_a = cell == null ? void 0 : cell.attributes) == null ? void 0 : _a.columnId;
|
|
180
|
+
if (typeof columnId === "string" && columnId && !cells.has(columnId)) {
|
|
181
|
+
cells.set(columnId, { text: cell.text || "", annotations: cell.annotations });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
rows.push({
|
|
185
|
+
id: block.id,
|
|
186
|
+
// Position-0 invariant: only the first row may be the header row.
|
|
187
|
+
isHeader: rows.length === 0 && ((_b = block.attributes) == null ? void 0 : _b.isHeader) === true,
|
|
188
|
+
cells
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return { columns, rows };
|
|
193
|
+
}
|
|
194
|
+
function escapeCellText(s) {
|
|
195
|
+
return s.replace(/\|/g, "\\|").replace(/\n/g, "<br>");
|
|
196
|
+
}
|
|
197
|
+
function assembleTableMarkdown(tableId, columns, header, bodyRows, ind) {
|
|
198
|
+
const lines = [];
|
|
199
|
+
lines.push(ind + idComment(tableId));
|
|
200
|
+
const headerCells = columns.map((col, idx) => {
|
|
201
|
+
const text = header.cellTexts[idx] || "";
|
|
202
|
+
return (text ? text + " " : "") + `<!-- col:${col.id} -->`;
|
|
203
|
+
});
|
|
204
|
+
if (header.rowId) {
|
|
205
|
+
headerCells[headerCells.length - 1] += " " + idComment(header.rowId);
|
|
206
|
+
}
|
|
207
|
+
lines.push(ind + "| " + headerCells.join(" | ") + " |");
|
|
208
|
+
lines.push(ind + "|" + columns.map(() => " --- ").join("|") + "|");
|
|
209
|
+
for (const row of bodyRows) {
|
|
210
|
+
const cells = [...row.cellTexts];
|
|
211
|
+
const last = cells.length - 1;
|
|
212
|
+
cells[last] = (cells[last] ? cells[last] + " " : "") + idComment(row.rowId);
|
|
213
|
+
lines.push(ind + "| " + cells.join(" | ") + " |");
|
|
214
|
+
}
|
|
215
|
+
return lines.join("\n");
|
|
216
|
+
}
|
|
217
|
+
function tableToMarkdown(node, depth, renderCell) {
|
|
218
|
+
var _a;
|
|
219
|
+
const { columns, rows } = normalizeTableNode(node);
|
|
220
|
+
if (columns.length === 0) return "";
|
|
221
|
+
const renderRowCells = (row) => columns.map((col) => {
|
|
222
|
+
const cell = row == null ? void 0 : row.cells.get(col.id);
|
|
223
|
+
return cell ? escapeCellText(renderCell(cell.text, cell.annotations)) : "";
|
|
224
|
+
});
|
|
225
|
+
const headerRow = ((_a = rows[0]) == null ? void 0 : _a.isHeader) ? rows[0] : void 0;
|
|
226
|
+
const bodyRows = (headerRow ? rows.slice(1) : rows).map((row) => ({
|
|
227
|
+
rowId: row.id,
|
|
228
|
+
cellTexts: renderRowCells(row)
|
|
229
|
+
}));
|
|
230
|
+
return assembleTableMarkdown(
|
|
231
|
+
node.block.id,
|
|
232
|
+
columns,
|
|
233
|
+
{ rowId: headerRow == null ? void 0 : headerRow.id, cellTexts: renderRowCells(headerRow) },
|
|
234
|
+
bodyRows,
|
|
235
|
+
indent(depth)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
async function tableToResolvedMarkdown(node, depth, renderCell) {
|
|
239
|
+
var _a;
|
|
240
|
+
const { columns, rows } = normalizeTableNode(node);
|
|
241
|
+
if (columns.length === 0) return "";
|
|
242
|
+
const renderRowCells = (row) => Promise.all(
|
|
243
|
+
columns.map(async (col) => {
|
|
244
|
+
const cell = row == null ? void 0 : row.cells.get(col.id);
|
|
245
|
+
return cell ? escapeCellText(await renderCell(cell.text, cell.annotations)) : "";
|
|
246
|
+
})
|
|
247
|
+
);
|
|
248
|
+
const headerRow = ((_a = rows[0]) == null ? void 0 : _a.isHeader) ? rows[0] : void 0;
|
|
249
|
+
const bodyRows = await Promise.all(
|
|
250
|
+
(headerRow ? rows.slice(1) : rows).map(async (row) => ({
|
|
251
|
+
rowId: row.id,
|
|
252
|
+
cellTexts: await renderRowCells(row)
|
|
253
|
+
}))
|
|
254
|
+
);
|
|
255
|
+
return assembleTableMarkdown(
|
|
256
|
+
node.block.id,
|
|
257
|
+
columns,
|
|
258
|
+
{ rowId: headerRow == null ? void 0 : headerRow.id, cellTexts: await renderRowCells(headerRow) },
|
|
259
|
+
bodyRows,
|
|
260
|
+
indent(depth)
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
function applyAnnotations(text, annotations) {
|
|
264
|
+
if (!annotations || annotations.length === 0) {
|
|
265
|
+
return text;
|
|
266
|
+
}
|
|
267
|
+
const markers = [];
|
|
268
|
+
for (const ann of annotations) {
|
|
269
|
+
const starts = ann.starts || [];
|
|
270
|
+
const ends = ann.ends || [];
|
|
271
|
+
for (let i = 0; i < starts.length; i++) {
|
|
272
|
+
markers.push({ pos: starts[i], type: "open", annotation: ann });
|
|
273
|
+
if (ends[i] !== void 0) {
|
|
274
|
+
markers.push({ pos: ends[i], type: "close", annotation: ann });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
markers.sort((a, b) => {
|
|
279
|
+
if (a.pos !== b.pos) return a.pos - b.pos;
|
|
280
|
+
return a.type === "open" ? -1 : 1;
|
|
281
|
+
});
|
|
282
|
+
let result = "";
|
|
283
|
+
let lastPos = 0;
|
|
284
|
+
for (const marker of markers) {
|
|
285
|
+
result += text.slice(lastPos, marker.pos);
|
|
286
|
+
lastPos = marker.pos;
|
|
287
|
+
result += getAnnotationMarker(marker.annotation, marker.type);
|
|
288
|
+
}
|
|
289
|
+
result += text.slice(lastPos);
|
|
290
|
+
result = result.replace(/\uFFFC/g, "");
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
function getAnnotationMarker(ann, type) {
|
|
294
|
+
switch (ann.type) {
|
|
295
|
+
case "Bold":
|
|
296
|
+
return "**";
|
|
297
|
+
case "Italic":
|
|
298
|
+
return "_";
|
|
299
|
+
case "Strike":
|
|
300
|
+
return "~~";
|
|
301
|
+
case "Code":
|
|
302
|
+
return "`";
|
|
303
|
+
case "Underline":
|
|
304
|
+
return type === "open" ? "<u>" : "</u>";
|
|
305
|
+
case "Link":
|
|
306
|
+
if (type === "open") {
|
|
307
|
+
return "[";
|
|
308
|
+
} else {
|
|
309
|
+
return `](${ann.link || ""})`;
|
|
310
|
+
}
|
|
311
|
+
case "Embed": {
|
|
312
|
+
const link = "link" in ann ? ann.link || "" : "";
|
|
313
|
+
return type === "open" ? `<${link}` : ">";
|
|
314
|
+
}
|
|
315
|
+
default:
|
|
316
|
+
return "";
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function formatMediaUrl(url, useGateway) {
|
|
320
|
+
if (useGateway && url.startsWith("ipfs://")) {
|
|
321
|
+
const cid = url.slice(7);
|
|
322
|
+
return `https://ipfs.io/ipfs/${cid}`;
|
|
323
|
+
}
|
|
324
|
+
return url;
|
|
325
|
+
}
|
|
326
|
+
function indent(depth) {
|
|
327
|
+
return " ".repeat(depth);
|
|
328
|
+
}
|
|
329
|
+
function slugify(title) {
|
|
330
|
+
return title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60);
|
|
331
|
+
}
|
|
332
|
+
function draftFilename(slug, id) {
|
|
333
|
+
if (!slug) return `${id}.md`;
|
|
334
|
+
return `${slug}_${id}.md`;
|
|
335
|
+
}
|
|
336
|
+
function parseDraftFilename(filename) {
|
|
337
|
+
const lastDot = filename.lastIndexOf(".");
|
|
338
|
+
const ext = lastDot >= 0 ? filename.slice(lastDot) : "";
|
|
339
|
+
const base = lastDot >= 0 ? filename.slice(0, lastDot) : filename;
|
|
340
|
+
const lastUnderscore = base.lastIndexOf("_");
|
|
341
|
+
if (lastUnderscore >= 0 && ext === ".md") {
|
|
342
|
+
return { id: base.slice(lastUnderscore + 1), ext };
|
|
343
|
+
}
|
|
344
|
+
return { id: base, ext };
|
|
345
|
+
}
|
|
346
|
+
async function documentToResolvedMarkdown(doc, options) {
|
|
347
|
+
const ctx = {
|
|
348
|
+
client: options.client,
|
|
349
|
+
maxDepth: options.maxDepth ?? 2,
|
|
350
|
+
currentDepth: 0,
|
|
351
|
+
cache: /* @__PURE__ */ new Map()
|
|
352
|
+
};
|
|
353
|
+
return documentToResolvedMarkdownWithContext(doc, ctx);
|
|
354
|
+
}
|
|
355
|
+
async function commentToResolvedMarkdown(comment, options) {
|
|
356
|
+
return contentToResolvedMarkdown(comment.content || [], options);
|
|
357
|
+
}
|
|
358
|
+
async function contentToResolvedMarkdown(content, options) {
|
|
359
|
+
const ctx = {
|
|
360
|
+
client: options.client,
|
|
361
|
+
maxDepth: options.maxDepth ?? 2,
|
|
362
|
+
currentDepth: 0,
|
|
363
|
+
cache: /* @__PURE__ */ new Map()
|
|
364
|
+
};
|
|
365
|
+
return blockNodesToResolvedMarkdown(content, ctx);
|
|
366
|
+
}
|
|
367
|
+
async function documentToResolvedMarkdownWithContext(doc, ctx) {
|
|
368
|
+
const lines = [emitFrontmatter(doc.metadata || {})];
|
|
369
|
+
const body = await blockNodesToResolvedMarkdown(doc.content || [], ctx);
|
|
370
|
+
if (body) lines.push(body);
|
|
371
|
+
return lines.join("\n");
|
|
372
|
+
}
|
|
373
|
+
async function blockNodesToResolvedMarkdown(nodes, ctx) {
|
|
374
|
+
const lines = [];
|
|
375
|
+
for (const node of nodes) {
|
|
376
|
+
const blockMd = await resolvedBlockNodeToMarkdown(node, 0, ctx);
|
|
377
|
+
if (blockMd) lines.push(blockMd);
|
|
378
|
+
}
|
|
379
|
+
return lines.join("\n\n");
|
|
380
|
+
}
|
|
381
|
+
async function resolvedBlockNodeToMarkdown(node, depth, ctx) {
|
|
382
|
+
var _a;
|
|
383
|
+
const block = node.block;
|
|
384
|
+
const children = node.children || [];
|
|
385
|
+
if (block.type === "Table") {
|
|
386
|
+
return tableToResolvedMarkdown(node, depth, (text, annotations) => applyResolvedAnnotations(text, annotations, ctx));
|
|
387
|
+
}
|
|
388
|
+
const childrenType = (_a = block.attributes) == null ? void 0 : _a.childrenType;
|
|
389
|
+
const isListContainer = block.type === "Paragraph" && !block.text && (childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote");
|
|
390
|
+
let result = isListContainer ? resolvedIdComment(block.id) : await resolvedBlockToMarkdown(block, depth, ctx);
|
|
391
|
+
for (const child of children) {
|
|
392
|
+
const childMd = await resolvedBlockNodeToMarkdown(child, depth + 1, ctx);
|
|
393
|
+
if (!childMd) continue;
|
|
394
|
+
if (childrenType === "Ordered") result += "\n" + resolvedIndent(depth + 1) + "1. " + childMd.trim();
|
|
395
|
+
else if (childrenType === "Unordered") result += "\n" + resolvedIndent(depth + 1) + "- " + childMd.trim();
|
|
396
|
+
else if (childrenType === "Blockquote") result += "\n" + resolvedIndent(depth + 1) + "> " + childMd.trim();
|
|
397
|
+
else result += "\n\n" + childMd;
|
|
398
|
+
}
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
async function resolvedBlockToMarkdown(block, depth, ctx) {
|
|
402
|
+
var _a, _b;
|
|
403
|
+
const ind = resolvedIndent(depth);
|
|
404
|
+
const b = block;
|
|
405
|
+
const text = b.text || "";
|
|
406
|
+
const link = b.link || "";
|
|
407
|
+
const annotations = b.annotations;
|
|
408
|
+
const id = b.id;
|
|
409
|
+
switch (block.type) {
|
|
410
|
+
case "Paragraph":
|
|
411
|
+
return appendResolvedIdToFirstLine(ind + await applyResolvedAnnotations(text, annotations, ctx), id);
|
|
412
|
+
case "Heading": {
|
|
413
|
+
const hashes = "#".repeat(Math.min(depth + 1, 6));
|
|
414
|
+
return appendResolvedIdToFirstLine(`${hashes} ${await applyResolvedAnnotations(text, annotations, ctx)}`, id);
|
|
415
|
+
}
|
|
416
|
+
case "Code": {
|
|
417
|
+
const lang = ((_a = b.attributes) == null ? void 0 : _a.language) || "";
|
|
418
|
+
return ind + "```" + lang + " " + resolvedIdComment(id) + "\n" + ind + text + "\n" + ind + "```";
|
|
419
|
+
}
|
|
420
|
+
case "Math":
|
|
421
|
+
return ind + "$$ " + resolvedIdComment(id) + "\n" + ind + text + "\n" + ind + "$$";
|
|
422
|
+
case "Image":
|
|
423
|
+
return ind + `}) ${resolvedIdComment(id)}`;
|
|
424
|
+
case "Video":
|
|
425
|
+
return ind + `[Video](${formatResolvedMediaUrl(link)}) ${resolvedIdComment(id)}`;
|
|
426
|
+
case "File": {
|
|
427
|
+
const fileName = ((_b = b.attributes) == null ? void 0 : _b.name) || "file";
|
|
428
|
+
return ind + `[${fileName}](${formatResolvedMediaUrl(link)}) ${resolvedIdComment(id)}`;
|
|
429
|
+
}
|
|
430
|
+
case "Embed":
|
|
431
|
+
return appendResolvedIdToFirstLine(await resolveBlockEmbed(block, depth, ctx), id);
|
|
432
|
+
case "WebEmbed":
|
|
433
|
+
return ind + `[Web Embed](${link}) ${resolvedIdComment(id)}`;
|
|
434
|
+
case "Button":
|
|
435
|
+
return ind + `[${text || "Button"}](${link}) ${resolvedIdComment(id)}`;
|
|
436
|
+
case "Query":
|
|
437
|
+
return appendResolvedIdToFirstLine(await resolveQuery(block, depth, ctx), id);
|
|
438
|
+
case "Nostr":
|
|
439
|
+
return ind + `[Nostr: ${link}](${link}) ${resolvedIdComment(id)}`;
|
|
440
|
+
default:
|
|
441
|
+
return text ? appendResolvedIdToFirstLine(ind + text, id) : "";
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
async function resolveBlockEmbed(block, depth, ctx) {
|
|
445
|
+
var _a;
|
|
446
|
+
const ind = resolvedIndent(depth);
|
|
447
|
+
const link = block.link || "";
|
|
448
|
+
if (ctx.currentDepth >= ctx.maxDepth) return ind + `> [Embed: ${link}](${link})`;
|
|
449
|
+
const id = unpackHmId(link);
|
|
450
|
+
if (!id) return ind + `> [Embed: ${link}](${link})`;
|
|
451
|
+
try {
|
|
452
|
+
const resolved = await resolveEmbeddedResource(link, ctx);
|
|
453
|
+
if (!((_a = resolved.content) == null ? void 0 : _a.length)) return ind + `> [Embed: ${resolved.label}](${link})`;
|
|
454
|
+
const content = selectEmbeddedContent(resolved.content, id);
|
|
455
|
+
if (!content.length) return ind + `> [Embed: ${resolved.label}](${link})`;
|
|
456
|
+
const lines = [];
|
|
457
|
+
const metadata = [`embed: ${link}`, `title: ${resolved.label}`];
|
|
458
|
+
if (id.version) metadata.push(`version: ${id.version}`);
|
|
459
|
+
if (id.blockRef) metadata.push(`block: ${id.blockRef}${formatBlockRange(id)}`);
|
|
460
|
+
lines.push(ind + `<!-- ${metadata.join("; ")} -->`);
|
|
461
|
+
const nestedCtx = { ...ctx, currentDepth: ctx.currentDepth + 1 };
|
|
462
|
+
for (const node of content) {
|
|
463
|
+
const blockMd = await resolvedBlockNodeToMarkdown(node, depth, nestedCtx);
|
|
464
|
+
if (!blockMd) continue;
|
|
465
|
+
lines.push(blockMd);
|
|
466
|
+
}
|
|
467
|
+
lines.push(ind + `<!-- /embed: ${link} -->`);
|
|
468
|
+
return lines.join("\n");
|
|
469
|
+
} catch {
|
|
470
|
+
return ind + `> [Embed: ${link}](${link})`;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async function resolveQuery(block, depth, ctx) {
|
|
474
|
+
var _a, _b;
|
|
475
|
+
const ind = resolvedIndent(depth);
|
|
476
|
+
try {
|
|
477
|
+
const attrs = block.attributes;
|
|
478
|
+
const queryConfig = attrs == null ? void 0 : attrs.query;
|
|
479
|
+
let includes;
|
|
480
|
+
let sort;
|
|
481
|
+
let limit;
|
|
482
|
+
if (queryConfig == null ? void 0 : queryConfig.includes) {
|
|
483
|
+
includes = queryConfig.includes.map((inc) => ({
|
|
484
|
+
space: inc.space,
|
|
485
|
+
path: inc.path,
|
|
486
|
+
mode: inc.mode || "Children"
|
|
487
|
+
}));
|
|
488
|
+
sort = (_a = queryConfig.sort) == null ? void 0 : _a.map((s) => ({ term: s.term, reverse: s.reverse ?? false }));
|
|
489
|
+
limit = queryConfig.limit;
|
|
490
|
+
} else {
|
|
491
|
+
const space = (attrs == null ? void 0 : attrs.space) || "";
|
|
492
|
+
if (!space) return ind + "<!-- Query: no space specified -->";
|
|
493
|
+
limit = typeof (attrs == null ? void 0 : attrs.limit) === "number" ? attrs.limit : void 0;
|
|
494
|
+
includes = [
|
|
495
|
+
{
|
|
496
|
+
space,
|
|
497
|
+
path: attrs == null ? void 0 : attrs.path,
|
|
498
|
+
mode: (attrs == null ? void 0 : attrs.mode) || "Children"
|
|
499
|
+
}
|
|
500
|
+
];
|
|
501
|
+
}
|
|
502
|
+
const results = await ctx.client.request("Query", {
|
|
503
|
+
includes,
|
|
504
|
+
sort: sort || [{ term: "UpdateTime", reverse: true }],
|
|
505
|
+
limit: limit || 10
|
|
506
|
+
});
|
|
507
|
+
if (!((_b = results == null ? void 0 : results.results) == null ? void 0 : _b.length)) return ind + "<!-- Query: no results -->";
|
|
508
|
+
return results.results.map((doc) => {
|
|
509
|
+
var _a2, _b2;
|
|
510
|
+
const name = ((_a2 = doc.metadata) == null ? void 0 : _a2.name) || ((_b2 = doc.id.path) == null ? void 0 : _b2.join("/")) || doc.id.uid;
|
|
511
|
+
return ind + `- [${name}](${doc.id.id})`;
|
|
512
|
+
}).join("\n");
|
|
513
|
+
} catch (error) {
|
|
514
|
+
return ind + `<!-- Query error: ${error instanceof Error ? error.message : String(error)} -->`;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function selectEmbeddedContent(content, id) {
|
|
518
|
+
if (!id.blockRef) return content;
|
|
519
|
+
const target = findBlockById(content, id.blockRef);
|
|
520
|
+
if (!target) return [];
|
|
521
|
+
if (id.blockRange && "start" in id.blockRange && typeof id.blockRange.start === "number") {
|
|
522
|
+
return [sliceBlockText(target, id.blockRange.start, id.blockRange.end ?? id.blockRange.start)];
|
|
523
|
+
}
|
|
524
|
+
if (id.blockRange && "expanded" in id.blockRange && !id.blockRange.expanded) return [{ ...target, children: [] }];
|
|
525
|
+
return [target];
|
|
526
|
+
}
|
|
527
|
+
function sliceBlockText(node, start, end) {
|
|
528
|
+
const block = node.block;
|
|
529
|
+
if (typeof block.text !== "string") return { ...node, children: [] };
|
|
530
|
+
return { ...node, block: { ...block, text: block.text.slice(start, end) }, children: [] };
|
|
531
|
+
}
|
|
532
|
+
async function applyResolvedAnnotations(text, annotations, ctx) {
|
|
533
|
+
if (!(annotations == null ? void 0 : annotations.length)) return text;
|
|
534
|
+
const markers = [];
|
|
535
|
+
for (const ann of annotations) {
|
|
536
|
+
const starts = ann.starts || [];
|
|
537
|
+
const ends = ann.ends || [];
|
|
538
|
+
for (let i = 0; i < starts.length; i++) {
|
|
539
|
+
markers.push({ pos: starts[i], type: "open", annotation: ann });
|
|
540
|
+
if (ends[i] !== void 0) markers.push({ pos: ends[i], type: "close", annotation: ann });
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
markers.sort((a, b) => a.pos !== b.pos ? a.pos - b.pos : a.type === "open" ? -1 : 1);
|
|
544
|
+
let result = "";
|
|
545
|
+
let lastPos = 0;
|
|
546
|
+
for (const marker of markers) {
|
|
547
|
+
result += text.slice(lastPos, marker.pos);
|
|
548
|
+
lastPos = marker.pos;
|
|
549
|
+
result += await getResolvedAnnotationMarker(marker.annotation, marker.type, ctx);
|
|
550
|
+
}
|
|
551
|
+
result += text.slice(lastPos);
|
|
552
|
+
return result.replace(/\uFFFC/g, "");
|
|
553
|
+
}
|
|
554
|
+
async function getResolvedAnnotationMarker(ann, type, ctx) {
|
|
555
|
+
switch (ann.type) {
|
|
556
|
+
case "Bold":
|
|
557
|
+
return "**";
|
|
558
|
+
case "Italic":
|
|
559
|
+
return "_";
|
|
560
|
+
case "Strike":
|
|
561
|
+
return "~~";
|
|
562
|
+
case "Code":
|
|
563
|
+
return "`";
|
|
564
|
+
case "Underline":
|
|
565
|
+
return type === "open" ? "<u>" : "</u>";
|
|
566
|
+
case "Link":
|
|
567
|
+
return type === "open" ? "[" : `](${ann.link || ""})`;
|
|
568
|
+
case "Embed":
|
|
569
|
+
return resolveInlineEmbed(ann, type, ctx);
|
|
570
|
+
default:
|
|
571
|
+
return "";
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async function resolveInlineEmbed(ann, type, ctx) {
|
|
575
|
+
const link = "link" in ann ? ann.link || "" : "";
|
|
576
|
+
if (type === "close") return `](${link})`;
|
|
577
|
+
if (!link) return "[\u2197 embed";
|
|
578
|
+
try {
|
|
579
|
+
const resolved = await resolveReference(link, ctx);
|
|
580
|
+
const prefix = resolved.type === "account" ? "@" : "";
|
|
581
|
+
return `[${prefix}${resolved.label}`;
|
|
582
|
+
} catch {
|
|
583
|
+
return `[\u2197 ${fallbackLabel(link)}`;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async function resolveEmbeddedResource(link, ctx) {
|
|
587
|
+
var _a, _b;
|
|
588
|
+
const cacheKey = `embed:${link}`;
|
|
589
|
+
const cached = ctx.cache.get(cacheKey);
|
|
590
|
+
if (cached) return cached;
|
|
591
|
+
const id = unpackHmId(link);
|
|
592
|
+
if (!id) throw new Error("Invalid HM link");
|
|
593
|
+
const resource = await ctx.client.request("Resource", id);
|
|
594
|
+
let resolved;
|
|
595
|
+
if (resource.type === "document") {
|
|
596
|
+
resolved = {
|
|
597
|
+
label: ((_a = resource.document.metadata) == null ? void 0 : _a.name) || fallbackDocLabel(id),
|
|
598
|
+
type: "document",
|
|
599
|
+
content: resource.document.content || [],
|
|
600
|
+
version: id.version || resource.document.version,
|
|
601
|
+
latestVersion: resource.document.version
|
|
602
|
+
};
|
|
603
|
+
} else if (resource.type === "comment") {
|
|
604
|
+
let authorName = fallbackUid(resource.comment.author);
|
|
605
|
+
try {
|
|
606
|
+
const account = await ctx.client.request("Account", resource.comment.author);
|
|
607
|
+
if (account.type === "account" && ((_b = account.metadata) == null ? void 0 : _b.name)) authorName = account.metadata.name;
|
|
608
|
+
} catch {
|
|
609
|
+
}
|
|
610
|
+
resolved = {
|
|
611
|
+
label: `Comment by ${authorName}`,
|
|
612
|
+
type: "comment",
|
|
613
|
+
content: resource.comment.content || [],
|
|
614
|
+
version: id.version || resource.comment.version,
|
|
615
|
+
latestVersion: resource.comment.version,
|
|
616
|
+
author: resource.comment.author
|
|
617
|
+
};
|
|
618
|
+
} else {
|
|
619
|
+
resolved = { label: fallbackLabel(link), type: "unknown" };
|
|
620
|
+
}
|
|
621
|
+
ctx.cache.set(cacheKey, resolved);
|
|
622
|
+
return resolved;
|
|
623
|
+
}
|
|
624
|
+
async function resolveReference(link, ctx) {
|
|
625
|
+
var _a, _b, _c, _d, _e;
|
|
626
|
+
const cached = ctx.cache.get(link);
|
|
627
|
+
if (cached) return cached;
|
|
628
|
+
const id = unpackHmId(link);
|
|
629
|
+
if (!id) throw new Error("Invalid HM link");
|
|
630
|
+
let resolved;
|
|
631
|
+
const profileAccountUid = ((_a = id.path) == null ? void 0 : _a[0]) === ":profile" ? id.path[1] || id.uid : !((_b = id.path) == null ? void 0 : _b.length) ? id.uid : null;
|
|
632
|
+
if (profileAccountUid) {
|
|
633
|
+
try {
|
|
634
|
+
const account = await ctx.client.request("Account", profileAccountUid);
|
|
635
|
+
if (account.type === "account") {
|
|
636
|
+
resolved = { label: ((_c = account.metadata) == null ? void 0 : _c.name) || fallbackUid(profileAccountUid), type: "account" };
|
|
637
|
+
ctx.cache.set(link, resolved);
|
|
638
|
+
return resolved;
|
|
639
|
+
}
|
|
640
|
+
} catch {
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const resource = await ctx.client.request("Resource", id);
|
|
644
|
+
if (resource.type === "document") {
|
|
645
|
+
resolved = {
|
|
646
|
+
label: ((_d = resource.document.metadata) == null ? void 0 : _d.name) || fallbackDocLabel(id),
|
|
647
|
+
type: "document",
|
|
648
|
+
content: resource.document.content || [],
|
|
649
|
+
version: id.version || resource.document.version,
|
|
650
|
+
latestVersion: resource.document.version
|
|
651
|
+
};
|
|
652
|
+
} else if (resource.type === "comment") {
|
|
653
|
+
let authorName = fallbackUid(resource.comment.author);
|
|
654
|
+
try {
|
|
655
|
+
const account = await ctx.client.request("Account", resource.comment.author);
|
|
656
|
+
if (account.type === "account" && ((_e = account.metadata) == null ? void 0 : _e.name)) authorName = account.metadata.name;
|
|
657
|
+
} catch {
|
|
658
|
+
}
|
|
659
|
+
resolved = {
|
|
660
|
+
label: `Comment by ${authorName}`,
|
|
661
|
+
type: "comment",
|
|
662
|
+
content: resource.comment.content || [],
|
|
663
|
+
version: id.version || resource.comment.version,
|
|
664
|
+
latestVersion: resource.comment.version,
|
|
665
|
+
author: resource.comment.author
|
|
666
|
+
};
|
|
667
|
+
} else {
|
|
668
|
+
resolved = { label: fallbackLabel(link), type: "unknown" };
|
|
669
|
+
}
|
|
670
|
+
ctx.cache.set(link, resolved);
|
|
671
|
+
return resolved;
|
|
672
|
+
}
|
|
673
|
+
function findBlockById(content, blockId) {
|
|
674
|
+
for (const node of content) {
|
|
675
|
+
if (node.block.id === blockId) return node;
|
|
676
|
+
const found = findBlockById(node.children || [], blockId);
|
|
677
|
+
if (found) return found;
|
|
678
|
+
}
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
function resolvedIdComment(id) {
|
|
682
|
+
return `<!-- id:${id} -->`;
|
|
683
|
+
}
|
|
684
|
+
function appendResolvedIdToFirstLine(md, id) {
|
|
685
|
+
const newline = md.indexOf("\n");
|
|
686
|
+
if (newline === -1) return md ? `${md} ${resolvedIdComment(id)}` : resolvedIdComment(id);
|
|
687
|
+
return `${md.slice(0, newline)} ${resolvedIdComment(id)}${md.slice(newline)}`;
|
|
688
|
+
}
|
|
689
|
+
function formatResolvedMediaUrl(url) {
|
|
690
|
+
if (url.startsWith("ipfs://")) return `https://ipfs.io/ipfs/${url.slice(7)}`;
|
|
691
|
+
return url;
|
|
692
|
+
}
|
|
693
|
+
function formatBlockRange(id) {
|
|
694
|
+
const range = id.blockRange;
|
|
695
|
+
if (!range) return "";
|
|
696
|
+
if ("expanded" in range && range.expanded) return "+";
|
|
697
|
+
if ("start" in range) return `[${range.start}:${range.end}]`;
|
|
698
|
+
return "";
|
|
699
|
+
}
|
|
700
|
+
function fallbackDocLabel(id) {
|
|
701
|
+
var _a;
|
|
702
|
+
return ((_a = id.path) == null ? void 0 : _a.length) ? id.path.join("/") : fallbackUid(id.uid);
|
|
703
|
+
}
|
|
704
|
+
function fallbackLabel(link) {
|
|
705
|
+
const id = unpackHmId(link);
|
|
706
|
+
if (id) return fallbackDocLabel(id);
|
|
707
|
+
return link;
|
|
708
|
+
}
|
|
709
|
+
function fallbackUid(uid) {
|
|
710
|
+
return uid.length > 12 ? `${uid.slice(0, 12)}...` : uid;
|
|
711
|
+
}
|
|
712
|
+
function resolvedIndent(depth) {
|
|
713
|
+
return " ".repeat(depth);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
export {
|
|
717
|
+
emitFrontmatter,
|
|
718
|
+
blocksToMarkdown,
|
|
719
|
+
slugify,
|
|
720
|
+
draftFilename,
|
|
721
|
+
parseDraftFilename,
|
|
722
|
+
documentToResolvedMarkdown,
|
|
723
|
+
commentToResolvedMarkdown,
|
|
724
|
+
contentToResolvedMarkdown
|
|
725
|
+
};
|