Read: ids, refs and link targets
ids.ts: converting between hm URLs, gateway URLs, site URLs, in-app refs and routes, plus media URLs.

src/read/ids.ts is a pure module (no clock, no randomness, no fetch) that defines how the app names a document. Every package that builds a link to a document, an author or an image imports it.

The ref

A ref is uid or uid/path/segments: no version, no fragment (L1-L4). The doc route /d/:ref carries it URL-encoded. The same parameter may instead hold a whole URL (a deep link or a typed address); linkOfRef tells the two apart. See Deep links and URL handling and the glossary.

Functions

Function

Input to output

refFromUrl(url)

hm://uid/a/b?v=…#blk or https://host/hm/uid/a/b to uid/a/b. Anything else, an empty path, or a gateway page in STATIC_HM_PATHS (api, download, connect, register, device-link, profile, contact, create-site) gives null. Segments are percent-decoded once; a malformed escape is kept as is.

blockRefFromUrl(url)

The block id in #blockId or #blockId[1:5] (also cut at +), or "".

linkOfRef(ref)

The URL when the route parameter is one (decoded once if it arrived as scheme%3A…), else "".

siteUrlParts(url)

https://host/a/b to {origin, path}. null for /hm/…, for STATIC_SITE_PATHS (api, ipfs, assets, _next, favicon.ico), and when the last segment looks like a file (. plus 2 to 5 letters or digits).

hostOf(url)

Lowercased host, "" if not a URL.

parseRef(ref)

{uid, path[]}. Decodes only when the ref has no literal / but contains %2f, so a plain ref keeps literal percent escapes.

refOf(uid, path)

Joins, dropping empty segments.

hmUrl(ref)

hm://uid/a/b with each segment encodeURIComponent-ed. This is the id sent to the read API.

docHref, authorHref, directoryHref

/d/{ref}, /a/{uid}, /dir/{uid}, each encoded.

linkTarget(url, server = "")

Where a link inside a document goes, see below.

shortId(uid)

z6Mkm3…aXq8 (first 6, last 4) for ids longer than 12 characters. Secondary text only.

mediaUrl(server, url, size)

ipfs://cid to {server}/hm/api/image/{cid}?size= followed by S, M or L, or {server}/ipfs/{cid} when size is "". http(s) URLs pass through, anything else is "".

linkTarget rules

    A document link (refFromUrl succeeds) stays in the app. An hm:// link becomes /d/{ref}; an https://host/hm/… gateway link becomes /d/{the whole URL}, so it is later read from its own host.

    A page of the current server's site (same host as server, and siteUrlParts accepts it) becomes /d/{the whole URL}.

    Otherwise only http(s)://, mailto:, tel:, a root-relative path and #fragment are returned unchanged. Every other scheme returns "", so the renderer writes plain text instead of a link. The edge-case test "document links cannot execute active URL schemes" pins this.

Relative /d/… hrefs are further rewritten by the root contract's readScopedHref so they stay on the visible reader's server, see Root contract wiring.

Who uses it

Tests

    markdown.test.ts ids group: 3 tests (URLs to refs, round trip through the route, media and short ids).

    deep-links.test.ts: 7 tests, the first three on linkOfRef, siteUrlParts and linkTarget.

    edge-cases.test.ts: literal percent escapes in refs, gateway contact pages, active URL schemes.

Known limits

    mediaUrl does not trim a trailing slash from server.

    A site page whose last path segment contains a dot followed by 2 to 5 alphanumerics is treated as a file and is not opened in the reader.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime