From 283041fb42f8fe89207117e88510f96e97f62a6c Mon Sep 17 00:00:00 2001 From: magyarblip Date: Thu, 13 Mar 2025 13:00:48 -0400 Subject: [PATCH 1/2] add support for markdownlint, clean README --- .markdownlint.yml | 1 + README.md | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 .markdownlint.yml diff --git a/.markdownlint.yml b/.markdownlint.yml new file mode 100644 index 0000000..fb41791 --- /dev/null +++ b/.markdownlint.yml @@ -0,0 +1 @@ + MD033: false diff --git a/README.md b/README.md index 8dc0363..e3b219f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ applications. ### NPM -``` +```sh npm install @digitalbazaar/cborld ``` @@ -34,7 +34,7 @@ npm install @digitalbazaar/cborld To install locally (for development): -``` +```sh git clone https://github.com/digitalbazaar/cborld.git cd cborld npm install @@ -113,6 +113,7 @@ const jsonldDocument = await cborld.decode({cborldBytes, documentLoader}); ### encode(options) ⇒ Promise<Uint8Array> + Encodes a given JSON-LD document into a CBOR-LD byte array. **Kind**: global function @@ -178,6 +179,7 @@ Encodes a given JSON-LD document into a CBOR-LD byte array. ### decode(options) ⇒ Promise<object> + Decodes a CBOR-LD byte array into a JSON-LD document. **Kind**: global function @@ -244,6 +246,7 @@ Decodes a CBOR-LD byte array into a JSON-LD document. ### diagnosticFunction : function + A diagnostic function that is called with diagnostic information. Typically set to `console.log` when debugging. @@ -264,6 +267,7 @@ set to `console.log` when debugging. ### documentLoaderFunction ⇒ string + Fetches a resource given a URL and returns it as a string. **Kind**: global typedef @@ -305,7 +309,7 @@ If editing the README, please conform to the ## Commercial Support Commercial support for this library is available upon request from -Digital Bazaar: support@digitalbazaar.com +Digital Bazaar: ## License From 13c10ed18e6981c228fce72834384d2a12f98f34 Mon Sep 17 00:00:00 2001 From: magyarblip Date: Thu, 13 Mar 2025 13:10:55 -0400 Subject: [PATCH 2/2] Fix README so usage examples work with standard JsonLdDocumentLoader --- README.md | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e3b219f..4438c97 100644 --- a/README.md +++ b/README.md @@ -50,31 +50,52 @@ CBOR-LD data. To encode a JSON-LD document as CBOR-LD: ```js -import {encode} from '@digitalbazaar/cborld'; +import { encode } from '@digitalbazaar/cborld'; +import { JsonLdDocumentLoader } from 'jsonld-document-loader'; + +// Establish JSON-LD context +const CONTEXT_URL = 'https://example.com/my-context/v1'; +const CONTEXT = { + '@context': { + ex: 'https://example.com/my-context/v1#', + type: '@type', + Example: 'ex:Example', + label: 'ex:label', + content: 'ex:content' + } +}; +const loader = new JsonLdDocumentLoader(); +loader.addStatic(CONTEXT_URL, CONTEXT) +const myLoader = loader.build(); -const jsonldDocument = { - '@context': 'https://www.w3.org/ns/activitystreams', +const myDocument = { + '@context': CONTEXT_URL, type: 'Note', - summary: 'CBOR-LD', + label: 'CBOR-LD', content: 'CBOR-LD is awesome!' }; // encode a JSON-LD Javascript object into CBOR-LD bytes -// Note: user must provide their own JSON-LD `documentLoader` -const cborldBytes = await encode({jsonldDocument, documentLoader}); +const outputCborldBytes = await encode({ + jsonldDocument: myDocument, + documentLoader: myLoader +}); ``` To decode a CBOR-LD document to JSON-LD: ```js -import {decode} from '@digitalbazaar/cborld'; +import { decode } from '@digitalbazaar/cborld'; // get the CBOR-LD bytes -const cborldBytes = await fs.promises.readFile('out.cborld'); +const inputCcborldBytes = await fs.promises.readFile('out.cborld'); // decode the CBOR-LD bytes into a Javascript object -// Note: user must provide their own JSON-LD `documentLoader` -const jsonldDocument = await cborld.decode({cborldBytes, documentLoader}); +// Note: reuses JSON-LD `myLoader` from encode example above +const outputJsonldDocument = await cborld.decode({ + cborldBytes: inputCcborldBytes, + documentLoader: myLoader +}); ``` ## API