OpenQuarryGitHub

Install the openquarry command from npm

The openquarry package on npm installs the command and the library's validators, schemas and types. @framers/openquarry is the same library without the command.

openquarry runs on Node 18 or later and is on npm under the MIT License.

Start a project

npx openquarry init starts a folder of notes or a course, and npx openquarry check validates its frontmatter and manifests. Both are commands of the openquarry package.

Notes

Makes a knowledge base with a fabric manifest, one weave, one loom and a first note.

Shell
npx openquarry init my-notes
cd my-notes
npx openquarry check

A course

Makes a course with a weave of kind course, a loom of kind unit and a first lesson.

Shell
npx openquarry init my-course --template course
cd my-course
npx openquarry check

Check a project

check prints one line for each file it checks and lists any errors under it. It exits with 1 when a file is invalid, which fails a CI job.

Shell
npx openquarry check

Add a note or a lesson

new writes a Markdown file with a new id, a slug made from the title, version: 1.0.0 and its contentType.

Shell
npx openquarry new note "Scale and distance" --in weaves/notes/getting-started
Shell
npx openquarry new lesson "Reading contour lines" --in weaves/my-course/first-unit

Print a new id

A note or a lesson written by hand needs its own id, and this command prints a new UUID.

Shell
npx openquarry id

The command needs Node 18 or later. init copies a starter that ships inside the package, so it works offline. It refuses a folder that already holds files unless you pass --force, and it never overwrites a file. The package's README describes every option.

Copy a starter with degit

degit copies the same two starters straight from GitHub. Each starter's check script validates its files with @framers/openquarry.

Notes
npx degit framerslab/openquarry/templates/notes my-notes
cd my-notes
npm install
npm run check
A course
npx degit framerslab/openquarry/templates/course my-course
cd my-course
npm install
npm run check

The degit commands need Node 20 or later. npx degit copies the starter's folder from GitHub without its history.

Install

Install openquarry with your package manager. It runs on Node 18 or later, as an ES module or from CommonJS. For the library without the command, install @framers/openquarry the same way.

npm
npm install openquarry
pnpm
pnpm add openquarry
yarn
yarn add openquarry

Most examples below parse YAML with the yaml package, which npm install yaml adds.

What it exports

Each validator takes a parsed value and returns valid and errors, plus warnings when it has advice. Each line in errors starts with the path of the problem in the value. validateBlocksIndex also takes { normalize: true } and returns the index under the thread names as value.

Validators

Schemas

Types

Compatibility exports

  • strandSchema: deprecated alias for threadSchema.
  • StrandFrontmatter: deprecated alias for ThreadFrontmatter.
  • KnowledgeNodeType: deprecated type for the original stored node values. Use FabricNodeType for new nodes.

Version 1 compatibility

Files

Start a project documents the openquarry command, and From the command line checks one file with the library alone, in one Node command.

Check a note's or a lesson's frontmatter

Parse the YAML between the two --- lines, then pass it to validateThreadFrontmatter. This lesson leaves out version, so the result names it.

lesson.mjs
import { parse } from 'yaml'
import { validateThreadFrontmatter } from 'openquarry'

const frontmatter = parse(`
id: 70c967bf-2dba-4399-aef1-671174eb904f
slug: scale-and-distance
title: Scale and distance
contentType: lesson
`)

const result = validateThreadFrontmatter(frontmatter)
console.log(result.valid)
for (const error of result.errors) console.log(error)
Prints
false
/ must have required property 'version'

Check fabric.yaml, weave.yaml and loom.yaml

A fabric can have a fabric.yaml, a weave has a weave.yaml and a loom a loom.yaml, and each file has its own validator. A kind is one lowercase word, so Course fails.

folders.mjs
import { validateFabricManifest, validateLoom, validateWeave } from 'openquarry'

console.log(validateFabricManifest({ format: 'openquarry', version: 2 }).valid)
console.log(validateLoom({ slug: 'the-map', title: 'The map', kind: 'unit' }).valid)

const weave = validateWeave({ slug: 'reading-a-map', title: 'Reading a map', kind: 'Course' })
for (const error of weave.errors) console.log(error)
Prints
true
true
/kind must match pattern "^[a-z][a-z0-9-]*$"

Check a catalogue

validateCatalogue checks a catalogue.yaml against its schema, then four rules a schema cannot hold. Each id is listed once, every link names something the catalogue lists, an entry's home is one of its subjects, and no chain of links makes a circle.

catalogue.mjs
import { validateCatalogue } from 'openquarry'

const result = validateCatalogue({
  id: 'field-skills',
  title: 'Field skills',
  subjects: [{ id: 'navigation', title: 'Navigation', broader: [] }],
  entries: [
    {
      slug: 'reading-a-map',
      home: 'outdoors',
      subjects: ['navigation'],
      requires: ['compass-basics'],
    },
  ],
})
for (const error of result.errors) console.log(error)
Prints
/entries/0/home must be one of the entry's subjects: "navigation"
/entries/0/requires/0 names an entry the catalogue does not list: "compass-basics"

Check a saved blocks index

A blocks index lists tagged blocks in threads and uses threadPath in tag references. Save this script as blocks.mjs and run node blocks.mjs blocks-index.json on a generated index. Version 1 compatibility explains the older field names.

blocks.mjs
import { readFileSync } from 'node:fs'
import { validateBlocksIndex } from 'openquarry'

const index = JSON.parse(readFileSync(process.argv[2], 'utf8'))
const result = validateBlocksIndex(index)
console.log(result.valid)
Prints
true

Use the JSON Schemas in your own validator

Each schema is exported as an object, JSON Schema draft 2020-12. Compile it with Ajv's 2020 build and ajv-formats (npm install ajv ajv-formats), or with any validator that reads draft 2020-12.

schema.mjs
import Ajv2020 from 'ajv/dist/2020.js'
import addFormats from 'ajv-formats'
import { loomSchema } from 'openquarry'

const ajv = new Ajv2020({ allErrors: true, strict: false })
addFormats(ajv)
const validate = ajv.compile(loomSchema)

console.log(validate({ slug: 'the-map', title: 'The map', kind: 'unit' }))
console.log(validate({ slug: 'the-map' }))
console.log(validate.errors[0].message)
Prints
true
false
must have required property 'title'

Catch a wrong value with the TypeScript types

The types describe every file the schemas check. A value the format does not allow, such as contentType: 'lecture', fails type checking before the code runs.

lesson.ts
import { validateThreadFrontmatter, type ThreadFrontmatter } from 'openquarry'

const lesson: ThreadFrontmatter = {
  id: '70c967bf-2dba-4399-aef1-671174eb904f',
  slug: 'scale-and-distance',
  title: 'Scale and distance',
  version: '1.0.0',
  contentType: 'lesson',
  difficulty: 'beginner',
  teaches: ['field-skills/skill/map-scale'],
}

console.log(validateThreadFrontmatter(lesson).valid)
Prints
true

Load the package from CommonJS

The package ships an ES module build and a CommonJS build, so require works too.

check.cjs
const { validateLoom } = require('openquarry')

console.log(validateLoom({ slug: 'the-map', title: 'The map', kind: 'unit' }))
Prints
{ valid: true, errors: [] }

Read the YAML sources

The package ships each schema's YAML source, byte for byte, under @framers/openquarry/schema/. These files come from @framers/openquarry, so install it to import them.

sources.mjs
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { parse } from 'yaml'

const require = createRequire(import.meta.url)
const file = require.resolve('@framers/openquarry/schema/weave.schema.yaml')
console.log(parse(readFileSync(file, 'utf8')).title)
Prints
Weave

Check files in your editor

Every schema has a stable address on this site. Editors built on the YAML language server read a first-line comment that names a schema, then check the file as you type.

weave.yaml
# yaml-language-server: $schema=https://openquarry.dev/schema/v1/weave.schema.json
slug: reading-a-map
title: Reading a map
kind: course

From the command line

npx openquarry check validates a whole project, as Check a project shows. To check one file with the library alone, this Node command reads a lesson saved as scale-and-distance.md, prints the result and exits with 1 when the frontmatter is invalid.

Shell
npm install openquarry yaml
node --input-type=module -e '
  import { readFileSync } from "node:fs";
  import { parse } from "yaml";
  import { validateThreadFrontmatter } from "openquarry";

  const text = readFileSync(process.argv[1], "utf8");
  const match = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(text);
  if (!match) throw new Error("Missing YAML frontmatter");

  const result = validateThreadFrontmatter(parse(match[1]));
  console.log(result);
  process.exitCode = result.valid ? 0 : 1;
' scale-and-distance.md

What the validators check