@seed-hypermedia/client 0.0.44 → 0.0.46
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/blocks-to-markdown.d.ts +13 -1
- package/dist/{chunk-XHVG24AF.mjs → chunk-SYBWJKDJ.mjs} +1 -0
- package/dist/hm-types.d.ts +539 -0
- package/dist/hm-types.mjs +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.mjs +435 -2
- package/dist/resource-read.d.ts +23 -0
- package/package.json +1 -1
- package/src/index.ts +13 -1
package/dist/hm-types.mjs
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -24,11 +24,14 @@ export { codePointLength, entityQueryPathToHmIdPath, getHMQueryString, hmIdPathT
|
|
|
24
24
|
export type { HMRequest, HMSigner, UnpackedHypermediaId } from './hm-types';
|
|
25
25
|
export { resolveHypermediaUrl, resolveId } from './hm-resolver';
|
|
26
26
|
export type { DomainResolverFn, DomainIdChangedCallback, ResolveOptions, ResolvedUrl } from './hm-resolver';
|
|
27
|
+
export { resolveIdWithClient } from './resource-read';
|
|
28
|
+
export type { ResolveIdWithClientOptions, ResolvedIdWithClient } from './resource-read';
|
|
27
29
|
export { fileToIpfsBlobs, filesToIpfsBlobs, resolveFileLinksInBlocks, hasFileLinks } from './file-to-ipfs';
|
|
28
30
|
export type { CollectedBlob } from './file-to-ipfs';
|
|
29
31
|
export { parseMarkdown, flattenToOperations, parseInlineFormatting, parseFrontmatter, markdownBlockNodesToHMBlockNodes, } from './markdown-to-blocks';
|
|
30
32
|
export type { BlockNode, SeedBlock, Annotation } from './markdown-to-blocks';
|
|
31
|
-
export { blocksToMarkdown, emitFrontmatter, slugify, draftFilename, parseDraftFilename } from './blocks-to-markdown';
|
|
33
|
+
export { blocksToMarkdown, emitFrontmatter, slugify, draftFilename, parseDraftFilename, documentToResolvedMarkdown, commentToResolvedMarkdown, contentToResolvedMarkdown, } from './blocks-to-markdown';
|
|
34
|
+
export type { ResolvedMarkdownOptions } from './blocks-to-markdown';
|
|
32
35
|
export type { BlocksToMarkdownOptions } from './blocks-to-markdown';
|
|
33
36
|
export { createBlocksMap, matchBlockIds, computeReplaceOps, hmBlockNodeToBlockNode } from './block-diff';
|
|
34
37
|
export type { APIBlockNode, APIBlock } from './block-diff';
|
package/dist/index.mjs
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
serializeBlockRange,
|
|
18
18
|
toNumber,
|
|
19
19
|
unpackHmId
|
|
20
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-SYBWJKDJ.mjs";
|
|
21
21
|
|
|
22
22
|
// src/capability.ts
|
|
23
23
|
import { encode as cborEncode2 } from "@ipld/dag-cbor";
|
|
@@ -2351,6 +2351,68 @@ function resolveProfilePath(siteUid, pathSegments) {
|
|
|
2351
2351
|
};
|
|
2352
2352
|
}
|
|
2353
2353
|
|
|
2354
|
+
// src/resource-read.ts
|
|
2355
|
+
async function resolveIdWithClient(rawId, options = {}) {
|
|
2356
|
+
const commentId = extractCommentIdFromReadableUrl(rawId);
|
|
2357
|
+
const parsed = commentId ? commentIdToHmId(commentId) : unpackHmId(rawId);
|
|
2358
|
+
if (parsed) {
|
|
2359
|
+
if (isWebUrl(rawId)) {
|
|
2360
|
+
const origin2 = new URL(rawId).origin;
|
|
2361
|
+
return { id: parsed, client: createSeedClient(origin2), serverUrl: origin2 };
|
|
2362
|
+
}
|
|
2363
|
+
const client = options.client ?? createSeedClient(normalizeServerUrl(options.serverUrl || "https://hyper.media"));
|
|
2364
|
+
return { id: parsed, client, serverUrl: client.baseUrl };
|
|
2365
|
+
}
|
|
2366
|
+
const id = await resolveId(rawId, { domainResolver: options.domainResolver });
|
|
2367
|
+
const origin = new URL(rawId).origin;
|
|
2368
|
+
return { id, client: createSeedClient(origin), serverUrl: origin };
|
|
2369
|
+
}
|
|
2370
|
+
var COMMENT_VIEW_TERMS = /* @__PURE__ */ new Set([":comments", ":comment", ":discussions"]);
|
|
2371
|
+
function extractCommentIdFromReadableUrl(rawId) {
|
|
2372
|
+
if (isWebUrl(rawId)) {
|
|
2373
|
+
const url = new URL(rawId);
|
|
2374
|
+
const panelCommentId = extractCommentIdFromPanel(url.searchParams.get("panel"));
|
|
2375
|
+
if (panelCommentId) return panelCommentId;
|
|
2376
|
+
return extractCommentIdFromPath(url.pathname.split("/").filter(Boolean));
|
|
2377
|
+
}
|
|
2378
|
+
const [withoutHash] = rawId.split("#");
|
|
2379
|
+
const [withoutQuery] = withoutHash.split("?");
|
|
2380
|
+
const parsed = unpackHmId(withoutQuery);
|
|
2381
|
+
return (parsed == null ? void 0 : parsed.path) ? extractCommentIdFromPath(parsed.path) : null;
|
|
2382
|
+
}
|
|
2383
|
+
function extractCommentIdFromPanel(panel) {
|
|
2384
|
+
if (!panel) return null;
|
|
2385
|
+
const [viewTerm, first, second] = panel.split("/");
|
|
2386
|
+
if (viewTerm !== "comments" && viewTerm !== "comment" && viewTerm !== "discussions") return null;
|
|
2387
|
+
if (first && second) return `${first}/${second}`;
|
|
2388
|
+
return first || null;
|
|
2389
|
+
}
|
|
2390
|
+
function extractCommentIdFromPath(pathParts) {
|
|
2391
|
+
if (pathParts.length >= 3) {
|
|
2392
|
+
const thirdToLast = pathParts[pathParts.length - 3];
|
|
2393
|
+
if (thirdToLast && COMMENT_VIEW_TERMS.has(thirdToLast)) {
|
|
2394
|
+
return `${pathParts[pathParts.length - 2]}/${pathParts[pathParts.length - 1]}`;
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
if (pathParts.length >= 2) {
|
|
2398
|
+
const secondToLast = pathParts[pathParts.length - 2];
|
|
2399
|
+
if (secondToLast && COMMENT_VIEW_TERMS.has(secondToLast)) return pathParts[pathParts.length - 1] || null;
|
|
2400
|
+
}
|
|
2401
|
+
return null;
|
|
2402
|
+
}
|
|
2403
|
+
function commentIdToHmId(commentId) {
|
|
2404
|
+
const parsed = unpackHmId(`hm://${commentId}`);
|
|
2405
|
+
if (!parsed) throw new Error(`Invalid comment ID: ${commentId}`);
|
|
2406
|
+
return parsed;
|
|
2407
|
+
}
|
|
2408
|
+
function isWebUrl(rawId) {
|
|
2409
|
+
return rawId.startsWith("http://") || rawId.startsWith("https://");
|
|
2410
|
+
}
|
|
2411
|
+
function normalizeServerUrl(serverUrl) {
|
|
2412
|
+
const withScheme = serverUrl.startsWith("http://") || serverUrl.startsWith("https://") ? serverUrl : `https://${serverUrl}`;
|
|
2413
|
+
return withScheme.replace(/\/+$/, "");
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2354
2416
|
// src/file-to-ipfs.ts
|
|
2355
2417
|
import { MemoryBlockstore } from "blockstore-core/memory";
|
|
2356
2418
|
import { importer as unixFSImporter } from "ipfs-unixfs-importer";
|
|
@@ -2839,7 +2901,7 @@ var METADATA_STRING_KEYS = [
|
|
|
2839
2901
|
"importTags"
|
|
2840
2902
|
];
|
|
2841
2903
|
var METADATA_BOOLEAN_KEYS = ["showOutline", "showActivity"];
|
|
2842
|
-
var METADATA_ENUM_KEYS = ["seedExperimentalHomeOrder", "contentWidth"];
|
|
2904
|
+
var METADATA_ENUM_KEYS = ["seedExperimentalHomeOrder", "contentWidth", "childrenType"];
|
|
2843
2905
|
function parseFrontmatter(markdown) {
|
|
2844
2906
|
const trimmed = markdown.trimStart();
|
|
2845
2907
|
if (!trimmed.startsWith("---")) {
|
|
@@ -3009,6 +3071,7 @@ var FM_STRING_KEYS = [
|
|
|
3009
3071
|
"seedExperimentalLogo",
|
|
3010
3072
|
"seedExperimentalHomeOrder",
|
|
3011
3073
|
"contentWidth",
|
|
3074
|
+
"childrenType",
|
|
3012
3075
|
"importCategories",
|
|
3013
3076
|
"importTags"
|
|
3014
3077
|
];
|
|
@@ -3237,6 +3300,372 @@ function parseDraftFilename(filename) {
|
|
|
3237
3300
|
}
|
|
3238
3301
|
return { id: base, ext };
|
|
3239
3302
|
}
|
|
3303
|
+
async function documentToResolvedMarkdown(doc, options) {
|
|
3304
|
+
const ctx = {
|
|
3305
|
+
client: options.client,
|
|
3306
|
+
maxDepth: options.maxDepth ?? 2,
|
|
3307
|
+
currentDepth: 0,
|
|
3308
|
+
cache: /* @__PURE__ */ new Map()
|
|
3309
|
+
};
|
|
3310
|
+
return documentToResolvedMarkdownWithContext(doc, ctx);
|
|
3311
|
+
}
|
|
3312
|
+
async function commentToResolvedMarkdown(comment, options) {
|
|
3313
|
+
return contentToResolvedMarkdown(comment.content || [], options);
|
|
3314
|
+
}
|
|
3315
|
+
async function contentToResolvedMarkdown(content, options) {
|
|
3316
|
+
const ctx = {
|
|
3317
|
+
client: options.client,
|
|
3318
|
+
maxDepth: options.maxDepth ?? 2,
|
|
3319
|
+
currentDepth: 0,
|
|
3320
|
+
cache: /* @__PURE__ */ new Map()
|
|
3321
|
+
};
|
|
3322
|
+
return blockNodesToResolvedMarkdown(content, ctx);
|
|
3323
|
+
}
|
|
3324
|
+
async function documentToResolvedMarkdownWithContext(doc, ctx) {
|
|
3325
|
+
const lines = [emitFrontmatter(doc.metadata || {})];
|
|
3326
|
+
const body = await blockNodesToResolvedMarkdown(doc.content || [], ctx);
|
|
3327
|
+
if (body) lines.push(body);
|
|
3328
|
+
return lines.join("\n");
|
|
3329
|
+
}
|
|
3330
|
+
async function blockNodesToResolvedMarkdown(nodes, ctx) {
|
|
3331
|
+
const lines = [];
|
|
3332
|
+
for (const node of nodes) {
|
|
3333
|
+
const blockMd = await resolvedBlockNodeToMarkdown(node, 0, ctx);
|
|
3334
|
+
if (blockMd) lines.push(blockMd);
|
|
3335
|
+
}
|
|
3336
|
+
return lines.join("\n\n");
|
|
3337
|
+
}
|
|
3338
|
+
async function resolvedBlockNodeToMarkdown(node, depth, ctx) {
|
|
3339
|
+
var _a;
|
|
3340
|
+
const block = node.block;
|
|
3341
|
+
const children = node.children || [];
|
|
3342
|
+
const childrenType = (_a = block.attributes) == null ? void 0 : _a.childrenType;
|
|
3343
|
+
const isListContainer = block.type === "Paragraph" && !block.text && (childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote");
|
|
3344
|
+
let result = isListContainer ? resolvedIdComment(block.id) : await resolvedBlockToMarkdown(block, depth, ctx);
|
|
3345
|
+
for (const child of children) {
|
|
3346
|
+
const childMd = await resolvedBlockNodeToMarkdown(child, depth + 1, ctx);
|
|
3347
|
+
if (!childMd) continue;
|
|
3348
|
+
if (childrenType === "Ordered") result += "\n" + resolvedIndent(depth + 1) + "1. " + childMd.trim();
|
|
3349
|
+
else if (childrenType === "Unordered") result += "\n" + resolvedIndent(depth + 1) + "- " + childMd.trim();
|
|
3350
|
+
else if (childrenType === "Blockquote") result += "\n" + resolvedIndent(depth + 1) + "> " + childMd.trim();
|
|
3351
|
+
else result += "\n\n" + childMd;
|
|
3352
|
+
}
|
|
3353
|
+
return result;
|
|
3354
|
+
}
|
|
3355
|
+
async function resolvedBlockToMarkdown(block, depth, ctx) {
|
|
3356
|
+
var _a, _b;
|
|
3357
|
+
const ind = resolvedIndent(depth);
|
|
3358
|
+
const b = block;
|
|
3359
|
+
const text = b.text || "";
|
|
3360
|
+
const link = b.link || "";
|
|
3361
|
+
const annotations = b.annotations;
|
|
3362
|
+
const id = b.id;
|
|
3363
|
+
switch (block.type) {
|
|
3364
|
+
case "Paragraph":
|
|
3365
|
+
return appendResolvedIdToFirstLine(ind + await applyResolvedAnnotations(text, annotations, ctx), id);
|
|
3366
|
+
case "Heading": {
|
|
3367
|
+
const hashes = "#".repeat(Math.min(depth + 1, 6));
|
|
3368
|
+
return appendResolvedIdToFirstLine(`${hashes} ${await applyResolvedAnnotations(text, annotations, ctx)}`, id);
|
|
3369
|
+
}
|
|
3370
|
+
case "Code": {
|
|
3371
|
+
const lang = ((_a = b.attributes) == null ? void 0 : _a.language) || "";
|
|
3372
|
+
return ind + "```" + lang + " " + resolvedIdComment(id) + "\n" + ind + text + "\n" + ind + "```";
|
|
3373
|
+
}
|
|
3374
|
+
case "Math":
|
|
3375
|
+
return ind + "$$ " + resolvedIdComment(id) + "\n" + ind + text + "\n" + ind + "$$";
|
|
3376
|
+
case "Image":
|
|
3377
|
+
return ind + `}) ${resolvedIdComment(id)}`;
|
|
3378
|
+
case "Video":
|
|
3379
|
+
return ind + `[Video](${formatResolvedMediaUrl(link)}) ${resolvedIdComment(id)}`;
|
|
3380
|
+
case "File": {
|
|
3381
|
+
const fileName = ((_b = b.attributes) == null ? void 0 : _b.name) || "file";
|
|
3382
|
+
return ind + `[${fileName}](${formatResolvedMediaUrl(link)}) ${resolvedIdComment(id)}`;
|
|
3383
|
+
}
|
|
3384
|
+
case "Embed":
|
|
3385
|
+
return appendResolvedIdToFirstLine(await resolveBlockEmbed(block, depth, ctx), id);
|
|
3386
|
+
case "WebEmbed":
|
|
3387
|
+
return ind + `[Web Embed](${link}) ${resolvedIdComment(id)}`;
|
|
3388
|
+
case "Button":
|
|
3389
|
+
return ind + `[${text || "Button"}](${link}) ${resolvedIdComment(id)}`;
|
|
3390
|
+
case "Query":
|
|
3391
|
+
return appendResolvedIdToFirstLine(await resolveQuery(block, depth, ctx), id);
|
|
3392
|
+
case "Nostr":
|
|
3393
|
+
return ind + `[Nostr: ${link}](${link}) ${resolvedIdComment(id)}`;
|
|
3394
|
+
default:
|
|
3395
|
+
return text ? appendResolvedIdToFirstLine(ind + text, id) : "";
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
async function resolveBlockEmbed(block, depth, ctx) {
|
|
3399
|
+
var _a;
|
|
3400
|
+
const ind = resolvedIndent(depth);
|
|
3401
|
+
const link = block.link || "";
|
|
3402
|
+
if (ctx.currentDepth >= ctx.maxDepth) return ind + `> [Embed: ${link}](${link})`;
|
|
3403
|
+
const id = unpackHmId(link);
|
|
3404
|
+
if (!id) return ind + `> [Embed: ${link}](${link})`;
|
|
3405
|
+
try {
|
|
3406
|
+
const resolved = await resolveEmbeddedResource(link, ctx);
|
|
3407
|
+
if (!((_a = resolved.content) == null ? void 0 : _a.length)) return ind + `> [Embed: ${resolved.label}](${link})`;
|
|
3408
|
+
const content = selectEmbeddedContent(resolved.content, id);
|
|
3409
|
+
if (!content.length) return ind + `> [Embed: ${resolved.label}](${link})`;
|
|
3410
|
+
const lines = [];
|
|
3411
|
+
const metadata = [`embed: ${link}`, `title: ${resolved.label}`];
|
|
3412
|
+
if (id.version) metadata.push(`version: ${id.version}`);
|
|
3413
|
+
if (id.blockRef) metadata.push(`block: ${id.blockRef}${formatBlockRange(id)}`);
|
|
3414
|
+
lines.push(ind + `<!-- ${metadata.join("; ")} -->`);
|
|
3415
|
+
const nestedCtx = { ...ctx, currentDepth: ctx.currentDepth + 1 };
|
|
3416
|
+
for (const node of content) {
|
|
3417
|
+
const blockMd = await resolvedBlockNodeToMarkdown(node, depth, nestedCtx);
|
|
3418
|
+
if (!blockMd) continue;
|
|
3419
|
+
lines.push(blockMd);
|
|
3420
|
+
}
|
|
3421
|
+
lines.push(ind + `<!-- /embed: ${link} -->`);
|
|
3422
|
+
return lines.join("\n");
|
|
3423
|
+
} catch {
|
|
3424
|
+
return ind + `> [Embed: ${link}](${link})`;
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
async function resolveQuery(block, depth, ctx) {
|
|
3428
|
+
var _a, _b;
|
|
3429
|
+
const ind = resolvedIndent(depth);
|
|
3430
|
+
try {
|
|
3431
|
+
const attrs = block.attributes;
|
|
3432
|
+
const queryConfig = attrs == null ? void 0 : attrs.query;
|
|
3433
|
+
let includes;
|
|
3434
|
+
let sort;
|
|
3435
|
+
let limit;
|
|
3436
|
+
if (queryConfig == null ? void 0 : queryConfig.includes) {
|
|
3437
|
+
includes = queryConfig.includes.map((inc) => ({
|
|
3438
|
+
space: inc.space,
|
|
3439
|
+
path: inc.path,
|
|
3440
|
+
mode: inc.mode || "Children"
|
|
3441
|
+
}));
|
|
3442
|
+
sort = (_a = queryConfig.sort) == null ? void 0 : _a.map((s) => ({ term: s.term, reverse: s.reverse ?? false }));
|
|
3443
|
+
limit = queryConfig.limit;
|
|
3444
|
+
} else {
|
|
3445
|
+
const space = (attrs == null ? void 0 : attrs.space) || "";
|
|
3446
|
+
if (!space) return ind + "<!-- Query: no space specified -->";
|
|
3447
|
+
limit = typeof (attrs == null ? void 0 : attrs.limit) === "number" ? attrs.limit : void 0;
|
|
3448
|
+
includes = [
|
|
3449
|
+
{
|
|
3450
|
+
space,
|
|
3451
|
+
path: attrs == null ? void 0 : attrs.path,
|
|
3452
|
+
mode: (attrs == null ? void 0 : attrs.mode) || "Children"
|
|
3453
|
+
}
|
|
3454
|
+
];
|
|
3455
|
+
}
|
|
3456
|
+
const results = await ctx.client.request("Query", {
|
|
3457
|
+
includes,
|
|
3458
|
+
sort: sort || [{ term: "UpdateTime", reverse: true }],
|
|
3459
|
+
limit: limit || 10
|
|
3460
|
+
});
|
|
3461
|
+
if (!((_b = results == null ? void 0 : results.results) == null ? void 0 : _b.length)) return ind + "<!-- Query: no results -->";
|
|
3462
|
+
return results.results.map((doc) => {
|
|
3463
|
+
var _a2, _b2;
|
|
3464
|
+
const name = ((_a2 = doc.metadata) == null ? void 0 : _a2.name) || ((_b2 = doc.id.path) == null ? void 0 : _b2.join("/")) || doc.id.uid;
|
|
3465
|
+
return ind + `- [${name}](${doc.id.id})`;
|
|
3466
|
+
}).join("\n");
|
|
3467
|
+
} catch (error) {
|
|
3468
|
+
return ind + `<!-- Query error: ${error instanceof Error ? error.message : String(error)} -->`;
|
|
3469
|
+
}
|
|
3470
|
+
}
|
|
3471
|
+
function selectEmbeddedContent(content, id) {
|
|
3472
|
+
if (!id.blockRef) return content;
|
|
3473
|
+
const target = findBlockById(content, id.blockRef);
|
|
3474
|
+
if (!target) return [];
|
|
3475
|
+
if (id.blockRange && "start" in id.blockRange && typeof id.blockRange.start === "number") {
|
|
3476
|
+
return [sliceBlockText(target, id.blockRange.start, id.blockRange.end ?? id.blockRange.start)];
|
|
3477
|
+
}
|
|
3478
|
+
if (id.blockRange && "expanded" in id.blockRange && !id.blockRange.expanded) return [{ ...target, children: [] }];
|
|
3479
|
+
return [target];
|
|
3480
|
+
}
|
|
3481
|
+
function sliceBlockText(node, start, end) {
|
|
3482
|
+
const block = node.block;
|
|
3483
|
+
if (typeof block.text !== "string") return { ...node, children: [] };
|
|
3484
|
+
return { ...node, block: { ...block, text: block.text.slice(start, end) }, children: [] };
|
|
3485
|
+
}
|
|
3486
|
+
async function applyResolvedAnnotations(text, annotations, ctx) {
|
|
3487
|
+
if (!(annotations == null ? void 0 : annotations.length)) return text;
|
|
3488
|
+
const markers = [];
|
|
3489
|
+
for (const ann of annotations) {
|
|
3490
|
+
const starts = ann.starts || [];
|
|
3491
|
+
const ends = ann.ends || [];
|
|
3492
|
+
for (let i = 0; i < starts.length; i++) {
|
|
3493
|
+
markers.push({ pos: starts[i], type: "open", annotation: ann });
|
|
3494
|
+
if (ends[i] !== void 0) markers.push({ pos: ends[i], type: "close", annotation: ann });
|
|
3495
|
+
}
|
|
3496
|
+
}
|
|
3497
|
+
markers.sort((a, b) => a.pos !== b.pos ? a.pos - b.pos : a.type === "open" ? -1 : 1);
|
|
3498
|
+
let result = "";
|
|
3499
|
+
let lastPos = 0;
|
|
3500
|
+
for (const marker of markers) {
|
|
3501
|
+
result += text.slice(lastPos, marker.pos);
|
|
3502
|
+
lastPos = marker.pos;
|
|
3503
|
+
result += await getResolvedAnnotationMarker(marker.annotation, marker.type, ctx);
|
|
3504
|
+
}
|
|
3505
|
+
result += text.slice(lastPos);
|
|
3506
|
+
return result.replace(/\uFFFC/g, "");
|
|
3507
|
+
}
|
|
3508
|
+
async function getResolvedAnnotationMarker(ann, type, ctx) {
|
|
3509
|
+
switch (ann.type) {
|
|
3510
|
+
case "Bold":
|
|
3511
|
+
return "**";
|
|
3512
|
+
case "Italic":
|
|
3513
|
+
return "_";
|
|
3514
|
+
case "Strike":
|
|
3515
|
+
return "~~";
|
|
3516
|
+
case "Code":
|
|
3517
|
+
return "`";
|
|
3518
|
+
case "Underline":
|
|
3519
|
+
return type === "open" ? "<u>" : "</u>";
|
|
3520
|
+
case "Link":
|
|
3521
|
+
return type === "open" ? "[" : `](${ann.link || ""})`;
|
|
3522
|
+
case "Embed":
|
|
3523
|
+
return resolveInlineEmbed(ann, type, ctx);
|
|
3524
|
+
default:
|
|
3525
|
+
return "";
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
async function resolveInlineEmbed(ann, type, ctx) {
|
|
3529
|
+
const link = "link" in ann ? ann.link || "" : "";
|
|
3530
|
+
if (type === "close") return `](${link})`;
|
|
3531
|
+
if (!link) return "[\u2197 embed";
|
|
3532
|
+
try {
|
|
3533
|
+
const resolved = await resolveReference(link, ctx);
|
|
3534
|
+
const prefix = resolved.type === "account" ? "@" : "";
|
|
3535
|
+
return `[${prefix}${resolved.label}`;
|
|
3536
|
+
} catch {
|
|
3537
|
+
return `[\u2197 ${fallbackLabel(link)}`;
|
|
3538
|
+
}
|
|
3539
|
+
}
|
|
3540
|
+
async function resolveEmbeddedResource(link, ctx) {
|
|
3541
|
+
var _a, _b;
|
|
3542
|
+
const cacheKey = `embed:${link}`;
|
|
3543
|
+
const cached = ctx.cache.get(cacheKey);
|
|
3544
|
+
if (cached) return cached;
|
|
3545
|
+
const id = unpackHmId(link);
|
|
3546
|
+
if (!id) throw new Error("Invalid HM link");
|
|
3547
|
+
const resource = await ctx.client.request("Resource", id);
|
|
3548
|
+
let resolved;
|
|
3549
|
+
if (resource.type === "document") {
|
|
3550
|
+
resolved = {
|
|
3551
|
+
label: ((_a = resource.document.metadata) == null ? void 0 : _a.name) || fallbackDocLabel(id),
|
|
3552
|
+
type: "document",
|
|
3553
|
+
content: resource.document.content || [],
|
|
3554
|
+
version: id.version || resource.document.version,
|
|
3555
|
+
latestVersion: resource.document.version
|
|
3556
|
+
};
|
|
3557
|
+
} else if (resource.type === "comment") {
|
|
3558
|
+
let authorName = fallbackUid(resource.comment.author);
|
|
3559
|
+
try {
|
|
3560
|
+
const account = await ctx.client.request("Account", resource.comment.author);
|
|
3561
|
+
if (account.type === "account" && ((_b = account.metadata) == null ? void 0 : _b.name)) authorName = account.metadata.name;
|
|
3562
|
+
} catch {
|
|
3563
|
+
}
|
|
3564
|
+
resolved = {
|
|
3565
|
+
label: `Comment by ${authorName}`,
|
|
3566
|
+
type: "comment",
|
|
3567
|
+
content: resource.comment.content || [],
|
|
3568
|
+
version: id.version || resource.comment.version,
|
|
3569
|
+
latestVersion: resource.comment.version,
|
|
3570
|
+
author: resource.comment.author
|
|
3571
|
+
};
|
|
3572
|
+
} else {
|
|
3573
|
+
resolved = { label: fallbackLabel(link), type: "unknown" };
|
|
3574
|
+
}
|
|
3575
|
+
ctx.cache.set(cacheKey, resolved);
|
|
3576
|
+
return resolved;
|
|
3577
|
+
}
|
|
3578
|
+
async function resolveReference(link, ctx) {
|
|
3579
|
+
var _a, _b, _c, _d, _e;
|
|
3580
|
+
const cached = ctx.cache.get(link);
|
|
3581
|
+
if (cached) return cached;
|
|
3582
|
+
const id = unpackHmId(link);
|
|
3583
|
+
if (!id) throw new Error("Invalid HM link");
|
|
3584
|
+
let resolved;
|
|
3585
|
+
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;
|
|
3586
|
+
if (profileAccountUid) {
|
|
3587
|
+
try {
|
|
3588
|
+
const account = await ctx.client.request("Account", profileAccountUid);
|
|
3589
|
+
if (account.type === "account") {
|
|
3590
|
+
resolved = { label: ((_c = account.metadata) == null ? void 0 : _c.name) || fallbackUid(profileAccountUid), type: "account" };
|
|
3591
|
+
ctx.cache.set(link, resolved);
|
|
3592
|
+
return resolved;
|
|
3593
|
+
}
|
|
3594
|
+
} catch {
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
const resource = await ctx.client.request("Resource", id);
|
|
3598
|
+
if (resource.type === "document") {
|
|
3599
|
+
resolved = {
|
|
3600
|
+
label: ((_d = resource.document.metadata) == null ? void 0 : _d.name) || fallbackDocLabel(id),
|
|
3601
|
+
type: "document",
|
|
3602
|
+
content: resource.document.content || [],
|
|
3603
|
+
version: id.version || resource.document.version,
|
|
3604
|
+
latestVersion: resource.document.version
|
|
3605
|
+
};
|
|
3606
|
+
} else if (resource.type === "comment") {
|
|
3607
|
+
let authorName = fallbackUid(resource.comment.author);
|
|
3608
|
+
try {
|
|
3609
|
+
const account = await ctx.client.request("Account", resource.comment.author);
|
|
3610
|
+
if (account.type === "account" && ((_e = account.metadata) == null ? void 0 : _e.name)) authorName = account.metadata.name;
|
|
3611
|
+
} catch {
|
|
3612
|
+
}
|
|
3613
|
+
resolved = {
|
|
3614
|
+
label: `Comment by ${authorName}`,
|
|
3615
|
+
type: "comment",
|
|
3616
|
+
content: resource.comment.content || [],
|
|
3617
|
+
version: id.version || resource.comment.version,
|
|
3618
|
+
latestVersion: resource.comment.version,
|
|
3619
|
+
author: resource.comment.author
|
|
3620
|
+
};
|
|
3621
|
+
} else {
|
|
3622
|
+
resolved = { label: fallbackLabel(link), type: "unknown" };
|
|
3623
|
+
}
|
|
3624
|
+
ctx.cache.set(link, resolved);
|
|
3625
|
+
return resolved;
|
|
3626
|
+
}
|
|
3627
|
+
function findBlockById(content, blockId) {
|
|
3628
|
+
for (const node of content) {
|
|
3629
|
+
if (node.block.id === blockId) return node;
|
|
3630
|
+
const found = findBlockById(node.children || [], blockId);
|
|
3631
|
+
if (found) return found;
|
|
3632
|
+
}
|
|
3633
|
+
return null;
|
|
3634
|
+
}
|
|
3635
|
+
function resolvedIdComment(id) {
|
|
3636
|
+
return `<!-- id:${id} -->`;
|
|
3637
|
+
}
|
|
3638
|
+
function appendResolvedIdToFirstLine(md, id) {
|
|
3639
|
+
const newline = md.indexOf("\n");
|
|
3640
|
+
if (newline === -1) return md ? `${md} ${resolvedIdComment(id)}` : resolvedIdComment(id);
|
|
3641
|
+
return `${md.slice(0, newline)} ${resolvedIdComment(id)}${md.slice(newline)}`;
|
|
3642
|
+
}
|
|
3643
|
+
function formatResolvedMediaUrl(url) {
|
|
3644
|
+
if (url.startsWith("ipfs://")) return `https://ipfs.io/ipfs/${url.slice(7)}`;
|
|
3645
|
+
return url;
|
|
3646
|
+
}
|
|
3647
|
+
function formatBlockRange(id) {
|
|
3648
|
+
const range = id.blockRange;
|
|
3649
|
+
if (!range) return "";
|
|
3650
|
+
if ("expanded" in range && range.expanded) return "+";
|
|
3651
|
+
if ("start" in range) return `[${range.start}:${range.end}]`;
|
|
3652
|
+
return "";
|
|
3653
|
+
}
|
|
3654
|
+
function fallbackDocLabel(id) {
|
|
3655
|
+
var _a;
|
|
3656
|
+
return ((_a = id.path) == null ? void 0 : _a.length) ? id.path.join("/") : fallbackUid(id.uid);
|
|
3657
|
+
}
|
|
3658
|
+
function fallbackLabel(link) {
|
|
3659
|
+
const id = unpackHmId(link);
|
|
3660
|
+
if (id) return fallbackDocLabel(id);
|
|
3661
|
+
return link;
|
|
3662
|
+
}
|
|
3663
|
+
function fallbackUid(uid) {
|
|
3664
|
+
return uid.length > 12 ? `${uid.slice(0, 12)}...` : uid;
|
|
3665
|
+
}
|
|
3666
|
+
function resolvedIndent(depth) {
|
|
3667
|
+
return " ".repeat(depth);
|
|
3668
|
+
}
|
|
3240
3669
|
|
|
3241
3670
|
// src/block-diff.ts
|
|
3242
3671
|
function createBlocksMap(nodes, parentId = "") {
|
|
@@ -4447,8 +4876,10 @@ export {
|
|
|
4447
4876
|
blocksToMarkdown,
|
|
4448
4877
|
codePointLength,
|
|
4449
4878
|
commentRecordIdFromBlob,
|
|
4879
|
+
commentToResolvedMarkdown,
|
|
4450
4880
|
computeReplaceOps,
|
|
4451
4881
|
contactRecordIdFromBlob,
|
|
4882
|
+
contentToResolvedMarkdown,
|
|
4452
4883
|
createAutoLinkOps,
|
|
4453
4884
|
createBlocksMap,
|
|
4454
4885
|
createCapability,
|
|
@@ -4467,6 +4898,7 @@ export {
|
|
|
4467
4898
|
deleteContact,
|
|
4468
4899
|
documentContainsLinkToChild,
|
|
4469
4900
|
documentHasSelfQuery,
|
|
4901
|
+
documentToResolvedMarkdown,
|
|
4470
4902
|
draftFilename,
|
|
4471
4903
|
editorBlockToHMBlock,
|
|
4472
4904
|
editorBlocksToHMBlockNodes,
|
|
@@ -4504,6 +4936,7 @@ export {
|
|
|
4504
4936
|
resolveFileLinksInBlocks,
|
|
4505
4937
|
resolveHypermediaUrl,
|
|
4506
4938
|
resolveId,
|
|
4939
|
+
resolveIdWithClient,
|
|
4507
4940
|
serializeBlockRange,
|
|
4508
4941
|
shouldAutoLinkParent,
|
|
4509
4942
|
signDocumentChange,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type SeedClient } from './client';
|
|
2
|
+
import { type DomainResolverFn } from './hm-resolver';
|
|
3
|
+
import { type UnpackedHypermediaId } from './hm-types';
|
|
4
|
+
/** Options for resolving a user-supplied hypermedia ID or web URL. */
|
|
5
|
+
export type ResolveIdWithClientOptions = {
|
|
6
|
+
client?: SeedClient;
|
|
7
|
+
serverUrl?: string;
|
|
8
|
+
domainResolver?: DomainResolverFn;
|
|
9
|
+
};
|
|
10
|
+
/** Resolved hypermedia ID with the Seed client that should be used to read it. */
|
|
11
|
+
export type ResolvedIdWithClient = {
|
|
12
|
+
id: UnpackedHypermediaId;
|
|
13
|
+
client: SeedClient;
|
|
14
|
+
serverUrl: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Resolves an hm:// ID, gateway URL, or Seed site URL and returns the matching Seed client.
|
|
18
|
+
*
|
|
19
|
+
* This is the shared implementation used by CLI-like document readers:
|
|
20
|
+
* - hm:// IDs use the caller's configured client/server.
|
|
21
|
+
* - web URLs resolve through the documented OPTIONS/header flow and then read from that URL's origin.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveIdWithClient(rawId: string, options?: ResolveIdWithClientOptions): Promise<ResolvedIdWithClient>;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -59,6 +59,8 @@ export type {HMRequest, HMSigner, UnpackedHypermediaId} from './hm-types'
|
|
|
59
59
|
|
|
60
60
|
export {resolveHypermediaUrl, resolveId} from './hm-resolver'
|
|
61
61
|
export type {DomainResolverFn, DomainIdChangedCallback, ResolveOptions, ResolvedUrl} from './hm-resolver'
|
|
62
|
+
export {resolveIdWithClient} from './resource-read'
|
|
63
|
+
export type {ResolveIdWithClientOptions, ResolvedIdWithClient} from './resource-read'
|
|
62
64
|
|
|
63
65
|
export {fileToIpfsBlobs, filesToIpfsBlobs, resolveFileLinksInBlocks, hasFileLinks} from './file-to-ipfs'
|
|
64
66
|
export type {CollectedBlob} from './file-to-ipfs'
|
|
@@ -71,7 +73,17 @@ export {
|
|
|
71
73
|
markdownBlockNodesToHMBlockNodes,
|
|
72
74
|
} from './markdown-to-blocks'
|
|
73
75
|
export type {BlockNode, SeedBlock, Annotation} from './markdown-to-blocks'
|
|
74
|
-
export {
|
|
76
|
+
export {
|
|
77
|
+
blocksToMarkdown,
|
|
78
|
+
emitFrontmatter,
|
|
79
|
+
slugify,
|
|
80
|
+
draftFilename,
|
|
81
|
+
parseDraftFilename,
|
|
82
|
+
documentToResolvedMarkdown,
|
|
83
|
+
commentToResolvedMarkdown,
|
|
84
|
+
contentToResolvedMarkdown,
|
|
85
|
+
} from './blocks-to-markdown'
|
|
86
|
+
export type {ResolvedMarkdownOptions} from './blocks-to-markdown'
|
|
75
87
|
export type {BlocksToMarkdownOptions} from './blocks-to-markdown'
|
|
76
88
|
|
|
77
89
|
export {createBlocksMap, matchBlockIds, computeReplaceOps, hmBlockNodeToBlockNode} from './block-diff'
|