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"; 2 import type { LiveLoader } from "astro/loaders"; 3 import type { 4 CollectionFilter, ··· 10 import { 11 getLeafletDocuments, 12 getSingleLeafletDocument, 13 leafletBlocksToHTML, 14 leafletDocumentRecordToView, 15 LiveLoaderError, 16 resolveMiniDoc, 17 uriToRkey, 18 } from "./utils.js"; 19 - import { Client, simpleFetchHandler } from "@atcute/client"; 20 21 export function leafletLiveLoader( 22 options: LiveLeafletLoaderOptions, ··· 35 ); 36 } 37 38 - // not a valid did 39 - if (!isDid(repo)) { 40 - throw new LiveLoaderError("invalid did", "INVALID_DID"); 41 } 42 43 return {
··· 1 + import { Client, simpleFetchHandler } from "@atcute/client"; 2 + import { isHandle } from "@atcute/lexicons/syntax"; 3 import type { LiveLoader } from "astro/loaders"; 4 import type { 5 CollectionFilter, ··· 11 import { 12 getLeafletDocuments, 13 getSingleLeafletDocument, 14 + isPlcDid, 15 leafletBlocksToHTML, 16 leafletDocumentRecordToView, 17 LiveLoaderError, 18 resolveMiniDoc, 19 uriToRkey, 20 } from "./utils.js"; 21 22 export function leafletLiveLoader( 23 options: LiveLeafletLoaderOptions, ··· 36 ); 37 } 38 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 + } 48 } 49 50 return {
+11 -3
lib/leaftlet-static-loader.ts
··· 1 import { Client, simpleFetchHandler } from "@atcute/client"; 2 import { isDid } from "@atproto/did"; 3 import type { Loader, LoaderContext } from "astro/loaders"; 4 import { LeafletDocumentSchema } from "schema.js"; ··· 8 } from "types.js"; 9 import { 10 getLeafletDocuments, 11 leafletBlocksToHTML, 12 leafletDocumentRecordToView, 13 LiveLoaderError, ··· 27 ); 28 } 29 30 - // not a valid did 31 - if (!isDid(repo)) { 32 - throw new LiveLoaderError("invalid did", "INVALID_DID"); 33 } 34 35 return {
··· 1 import { Client, simpleFetchHandler } from "@atcute/client"; 2 + import { isHandle } from "@atcute/lexicons/syntax"; 3 import { isDid } from "@atproto/did"; 4 import type { Loader, LoaderContext } from "astro/loaders"; 5 import { LeafletDocumentSchema } from "schema.js"; ··· 9 } from "types.js"; 10 import { 11 getLeafletDocuments, 12 + isPlcDid, 13 leafletBlocksToHTML, 14 leafletDocumentRecordToView, 15 LiveLoaderError, ··· 29 ); 30 } 31 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 + } 41 } 42 43 return {
+6
lib/types.ts
··· 78 text: string; 79 facet?: Exclude<Facet["features"], { $type: string }>; 80 }
··· 78 text: string; 79 facet?: Exclude<Facet["features"], { $type: string }>; 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 import type {} from "@atcute/atproto"; 2 - import { is } from "@atcute/lexicons"; 3 import { AtUri, UnicodeString } from "@atproto/api"; 4 import katex from "katex"; 5 import sanitizeHTML from "sanitize-html"; ··· 14 PubLeafletPagesLinearDocument, 15 } from "./lexicons/index.js"; 16 import type { 17 Facet, 18 GetLeafletDocumentsParams, 19 GetSingleLeafletDocumentParams, ··· 358 359 return `<li>${parseBlocks({ block: { block: item.content }, did })}${children}</li>`; 360 }
··· 1 import type {} from "@atcute/atproto"; 2 + import { type Handle, is } from "@atcute/lexicons"; 3 import { AtUri, UnicodeString } from "@atproto/api"; 4 import katex from "katex"; 5 import sanitizeHTML from "sanitize-html"; ··· 14 PubLeafletPagesLinearDocument, 15 } from "./lexicons/index.js"; 16 import type { 17 + Did, 18 Facet, 19 GetLeafletDocumentsParams, 20 GetSingleLeafletDocumentParams, ··· 359 360 return `<li>${parseBlocks({ block: { block: item.content }, did })}${children}</li>`; 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 "@atcute/client": "^4.0.3", 61 "@atcute/lexicons": "^1.1.0", 62 "@atproto/api": "^0.16.2", 63 - "@atproto/did": "^0.1.5", 64 "katex": "^0.16.22", 65 "sanitize-html": "^2.17.0" 66 }
··· 60 "@atcute/client": "^4.0.3", 61 "@atcute/lexicons": "^1.1.0", 62 "@atproto/api": "^0.16.2", 63 "katex": "^0.16.22", 64 "sanitize-html": "^2.17.0" 65 }
-10
pnpm-lock.yaml
··· 17 '@atproto/api': 18 specifier: ^0.16.2 19 version: 0.16.2 20 - '@atproto/did': 21 - specifier: ^0.1.5 22 - version: 0.1.5 23 katex: 24 specifier: ^0.16.22 25 version: 0.16.22 ··· 101 102 '@atproto/common-web@0.4.2': 103 resolution: {integrity: sha512-vrXwGNoFGogodjQvJDxAeP3QbGtawgZute2ed1XdRO0wMixLk3qewtikZm06H259QDJVu6voKC5mubml+WgQUw==} 104 - 105 - '@atproto/did@0.1.5': 106 - resolution: {integrity: sha512-8+1D08QdGE5TF0bB0vV8HLVrVZJeLNITpRTUVEoABNMRaUS7CoYSVb0+JNQDeJIVmqMjOL8dOjvCUDkp3gEaGQ==} 107 108 '@atproto/lexicon@0.4.12': 109 resolution: {integrity: sha512-fcEvEQ1GpQYF5igZ4IZjPWEoWVpsEF22L9RexxLS3ptfySXLflEyH384e7HITzO/73McDeaJx3lqHIuqn9ulnw==} ··· 2512 graphemer: 1.4.0 2513 multiformats: 9.9.0 2514 uint8arrays: 3.0.0 2515 - zod: 3.25.76 2516 - 2517 - '@atproto/did@0.1.5': 2518 - dependencies: 2519 zod: 3.25.76 2520 2521 '@atproto/lexicon@0.4.12':
··· 17 '@atproto/api': 18 specifier: ^0.16.2 19 version: 0.16.2 20 katex: 21 specifier: ^0.16.22 22 version: 0.16.22 ··· 98 99 '@atproto/common-web@0.4.2': 100 resolution: {integrity: sha512-vrXwGNoFGogodjQvJDxAeP3QbGtawgZute2ed1XdRO0wMixLk3qewtikZm06H259QDJVu6voKC5mubml+WgQUw==} 101 102 '@atproto/lexicon@0.4.12': 103 resolution: {integrity: sha512-fcEvEQ1GpQYF5igZ4IZjPWEoWVpsEF22L9RexxLS3ptfySXLflEyH384e7HITzO/73McDeaJx3lqHIuqn9ulnw==} ··· 2506 graphemer: 1.4.0 2507 multiformats: 9.9.0 2508 uint8arrays: 3.0.0 2509 zod: 3.25.76 2510 2511 '@atproto/lexicon@0.4.12':