@pretextbook/schema 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # @pretextbook/schema
2
+
3
+ RELAX NG validation and context-aware completions for [PreTeXt](https://pretextbook.org)
4
+ documents, built on the [`salve-annos`](https://www.npmjs.com/package/salve-annos)
5
+ engine.
6
+
7
+ This package powers the diagnostics and (as a fallback) the completions in the
8
+ PreTeXt Tools VS Code extension's language server, but it has no dependency on
9
+ VS Code and can be used standalone.
10
+
11
+ ## What it does
12
+
13
+ - **Validation** — checks a document against the compiled PreTeXt grammar and
14
+ returns LSP-shaped `Diagnostic`s with precise ranges. Errors are recovered, so
15
+ a single pass reports every problem, not just the first.
16
+ - **XInclude awareness** — resolves `xi:include` references before validating and
17
+ maps errors in an included file back to the file and line they came from.
18
+ - **Completions** — uses salve's `walker.possible()` to offer exactly the
19
+ elements/attributes allowed at the cursor, respecting the content model.
20
+ - **Customizable rules** — a small rule layer lets you override severities,
21
+ rewrite messages, tag diagnostics with stable ids, or suppress them.
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ import {
27
+ loadGrammarFromJSON,
28
+ validateDocument,
29
+ getCompletions,
30
+ } from "@pretextbook/schema";
31
+ import fs from "fs";
32
+
33
+ // Load the precompiled grammar (generated at build time — see below).
34
+ const grammar = loadGrammarFromJSON(
35
+ fs.readFileSync("assets/pretext.json", "utf8"),
36
+ );
37
+
38
+ const result = validateDocument(source, grammar, {
39
+ uri: "file:///book/main.ptx",
40
+ });
41
+ for (const [uri, diagnostics] of Object.entries(result.diagnosticsByUri)) {
42
+ // publish `diagnostics` for `uri`
43
+ }
44
+
45
+ const items = getCompletions({ text: source, position, grammar });
46
+ ```
47
+
48
+ ### Compiling a grammar
49
+
50
+ `loadGrammarFromJSON` is the fast runtime path. To produce that JSON from a
51
+ `.rng` file (e.g. after the upstream schema changes), use the build-time entry
52
+ point, which pulls in salve's Node-only conversion machinery:
53
+
54
+ ```ts
55
+ import { compileRngToJSON } from "@pretextbook/schema/compile";
56
+ const { json, warnings } = await compileRngToJSON("schema/pretext.rng");
57
+ ```
58
+
59
+ The monorepo wires this up via `scripts/compile-grammar.mjs`, run as part of
60
+ `npm run refresh:schemas` and the extension build. It writes `pretext.json`
61
+ alongside the `.rng` files the extension ships.
62
+
63
+ ## Custom rules
64
+
65
+ ```ts
66
+ import { validateDocument, Severity } from "@pretextbook/schema";
67
+
68
+ const result = validateDocument(source, grammar, {
69
+ ruleset: {
70
+ defaultSeverity: Severity.Error,
71
+ rules: [
72
+ {
73
+ id: "text-not-allowed",
74
+ match: (e) => e.kind === "text-not-allowed",
75
+ severity: Severity.Warning,
76
+ },
77
+ ],
78
+ },
79
+ });
80
+ ```
81
+
82
+ Each raw error is normalized to a `SchemaError` (`kind`, `name`, `alternatives`,
83
+ `range`, `uri`); the first matching rule decides its severity, message, and
84
+ whether it is suppressed.
85
+
86
+ ## Notes
87
+
88
+ - The engine is `salve-annos`, a maintained fork of `salve` that fixes the
89
+ Windows `file://` handling and tracks modern Node/TypeScript.
90
+ - The experimental schema (`pretext-dev.rng`) currently has dangling refs
91
+ upstream and may fail to compile; the build treats that as non-fatal and falls
92
+ back to the stable grammar.