feat: allow for handle or did to be used for repo parameter option (#14)

* feat: allow for handle or did to be used for

* chore: remove @atproto/did from deps

* docs: update changeset with code example

authored by Dane Miller and committed by GitHub 5524ce50 6d70cc64

+17
.changeset/true-terms-start.md
··· 1 + --- 2 + "@nulfrost/leaflet-loader-astro": minor 3 + --- 4 + 5 + Added the ability to use a handle or did when specifying a repo for leafletStaticLoader and leafletLiveLoader 6 + 7 + 8 + ```ts 9 + import { defineLiveCollection} from "astro:content"; 10 + import { leafletLiveLoader } from "leaflet-loader-astro"; 11 + 12 + const documents = defineLiveCollection({ 13 + loader: leafletLiveLoader({ repo: "dane.computer" }), // or repo: did:plc:qttsv4e7pu2jl3ilanfgc3zn, both work! 14 + }); 15 + 16 + export const collections = { documents }; 17 + ```
+12 -5
lib/leaflet-live-loader.ts
··· 1 - import { isDid } from "@atproto/did"; 1 + import { Client, simpleFetchHandler } from "@atcute/client"; 2 + import { isHandle } from "@atcute/lexicons/syntax"; 2 3 import type { LiveLoader } from "astro/loaders"; 3 4 import type { 4 5 CollectionFilter, ··· 10 11 import { 11 12 getLeafletDocuments, 12 13 getSingleLeafletDocument, 14 + isPlcDid, 13 15 leafletBlocksToHTML, 14 16 leafletDocumentRecordToView, 15 17 LiveLoaderError, 16 18 resolveMiniDoc, 17 19 uriToRkey, 18 20 } from "./utils.js"; 19 - import { Client, simpleFetchHandler } from "@atcute/client"; 20 21 21 22 export function leafletLiveLoader( 22 23 options: LiveLeafletLoaderOptions, ··· 35 36 ); 36 37 } 37 38 38 - // not a valid did 39 - if (!isDid(repo)) { 40 - throw new LiveLoaderError("invalid did", "INVALID_DID"); 39 + // not a valid handle, check if valid did 40 + if (!isHandle(repo)) { 41 + // not a valid handle or did, throw 42 + if (!isPlcDid(repo)) { 43 + throw new LiveLoaderError( 44 + "invalid handle or did", 45 + "INVALID_HANDLE_OR_DID", 46 + ); 47 + } 41 48 } 42 49 43 50 return {
+11 -3
lib/leaftlet-static-loader.ts
··· 1 1 import { Client, simpleFetchHandler } from "@atcute/client"; 2 + import { isHandle } from "@atcute/lexicons/syntax"; 2 3 import { isDid } from "@atproto/did"; 3 4 import type { Loader, LoaderContext } from "astro/loaders"; 4 5 import { LeafletDocumentSchema } from "schema.js"; ··· 8 9 } from "types.js"; 9 10 import { 10 11 getLeafletDocuments, 12 + isPlcDid, 11 13 leafletBlocksToHTML, 12 14 leafletDocumentRecordToView, 13 15 LiveLoaderError, ··· 27 29 ); 28 30 } 29 31 30 - // not a valid did 31 - if (!isDid(repo)) { 32 - throw new LiveLoaderError("invalid did", "INVALID_DID"); 32 + // not a valid handle, check if valid did 33 + if (!isHandle(repo)) { 34 + // not a valid handle or did, throw 35 + if (!isPlcDid(repo)) { 36 + throw new LiveLoaderError( 37 + "invalid handle or did", 38 + "INVALID_HANDLE_OR_DID", 39 + ); 40 + } 33 41 } 34 42 35 43 return {
+6
lib/types.ts
··· 78 78 text: string; 79 79 facet?: Exclude<Facet["features"], { $type: string }>; 80 80 } 81 + 82 + // yoinked from: https://github.com/mary-ext/atcute/blob/trunk/packages/lexicons/lexicons/lib/syntax/handle.ts 83 + /** 84 + * represents a decentralized identifier (DID). 85 + */ 86 + export type Did<Method extends string = string> = `did:${Method}:${string}`;
+9 -1
lib/utils.ts
··· 1 1 import type {} from "@atcute/atproto"; 2 - import { is } from "@atcute/lexicons"; 2 + import { type Handle, is } from "@atcute/lexicons"; 3 3 import { AtUri, UnicodeString } from "@atproto/api"; 4 4 import katex from "katex"; 5 5 import sanitizeHTML from "sanitize-html"; ··· 14 14 PubLeafletPagesLinearDocument, 15 15 } from "./lexicons/index.js"; 16 16 import type { 17 + Did, 17 18 Facet, 18 19 GetLeafletDocumentsParams, 19 20 GetSingleLeafletDocumentParams, ··· 358 359 359 360 return `<li>${parseBlocks({ block: { block: item.content }, did })}${children}</li>`; 360 361 } 362 + 363 + // yoinked from: https://github.com/mary-ext/atcute/blob/trunk/packages/lexicons/lexicons/lib/syntax/handle.ts 364 + const PLC_DID_RE = /^did:plc:([a-z2-7]{24})$/; 365 + 366 + export const isPlcDid = (input: string): input is Did<"plc"> => { 367 + return input.length === 32 && PLC_DID_RE.test(input); 368 + };
-1
package.json
··· 60 60 "@atcute/client": "^4.0.3", 61 61 "@atcute/lexicons": "^1.1.0", 62 62 "@atproto/api": "^0.16.2", 63 - "@atproto/did": "^0.1.5", 64 63 "katex": "^0.16.22", 65 64 "sanitize-html": "^2.17.0" 66 65 }
-10
pnpm-lock.yaml
··· 17 17 '@atproto/api': 18 18 specifier: ^0.16.2 19 19 version: 0.16.2 20 - '@atproto/did': 21 - specifier: ^0.1.5 22 - version: 0.1.5 23 20 katex: 24 21 specifier: ^0.16.22 25 22 version: 0.16.22 ··· 101 98 102 99 '@atproto/common-web@0.4.2': 103 100 resolution: {integrity: sha512-vrXwGNoFGogodjQvJDxAeP3QbGtawgZute2ed1XdRO0wMixLk3qewtikZm06H259QDJVu6voKC5mubml+WgQUw==} 104 - 105 - '@atproto/did@0.1.5': 106 - resolution: {integrity: sha512-8+1D08QdGE5TF0bB0vV8HLVrVZJeLNITpRTUVEoABNMRaUS7CoYSVb0+JNQDeJIVmqMjOL8dOjvCUDkp3gEaGQ==} 107 101 108 102 '@atproto/lexicon@0.4.12': 109 103 resolution: {integrity: sha512-fcEvEQ1GpQYF5igZ4IZjPWEoWVpsEF22L9RexxLS3ptfySXLflEyH384e7HITzO/73McDeaJx3lqHIuqn9ulnw==} ··· 2512 2506 graphemer: 1.4.0 2513 2507 multiformats: 9.9.0 2514 2508 uint8arrays: 3.0.0 2515 - zod: 3.25.76 2516 - 2517 - '@atproto/did@0.1.5': 2518 - dependencies: 2519 2509 zod: 3.25.76 2520 2510 2521 2511 '@atproto/lexicon@0.4.12':