@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.
@@ -0,0 +1,107 @@
1
+ import { Diagnostic, CompletionItem, Range, Position } from 'vscode-languageserver-types';
2
+ export type { Diagnostic, CompletionItem, Range, Position };
3
+ /**
4
+ * A grammar produced by salve from a (precompiled or freshly-converted) RELAX NG
5
+ * schema. Kept opaque here so consumers do not need to depend on salve's types
6
+ * directly.
7
+ */
8
+ export interface Grammar {
9
+ newWalker(nameResolver?: unknown): unknown;
10
+ }
11
+ /**
12
+ * The category of a raw validation error, derived from salve's error class.
13
+ * Rule predicates and message templates key off this.
14
+ */
15
+ export type SchemaErrorKind = "element-not-allowed" | "attribute-not-allowed" | "attribute-value-invalid" | "choice-not-satisfied" | "text-not-allowed" | "unexpected-end" | "xinclude-missing" | "xinclude-circular" | "well-formedness" | "duplicate-id" | "dangling-reference" | "other";
16
+ /**
17
+ * A normalized validation error, produced before rule/severity/message
18
+ * customization is applied. This is the shape rule predicates receive.
19
+ */
20
+ export interface SchemaError {
21
+ kind: SchemaErrorKind;
22
+ /** The raw message produced by the underlying engine. */
23
+ message: string;
24
+ /** The element or attribute local name involved (when applicable). */
25
+ name?: string;
26
+ /** The namespace URI involved (when applicable). */
27
+ ns?: string;
28
+ /** Names offered as alternatives (e.g. for choice errors). */
29
+ alternatives?: string[];
30
+ /**
31
+ * Local name of the immediate enclosing element at the point of the error.
32
+ * For a misplaced child element this is the element it appears inside; for an
33
+ * invalid attribute it is the element carrying the attribute. Undefined at the
34
+ * document root (e.g. unexpected-end errors).
35
+ */
36
+ parent?: string;
37
+ /**
38
+ * The chain of open elements at the point of the error, outermost first and
39
+ * ending with {@link parent}. Enables context-aware rules (e.g. only relax an
40
+ * element when it appears inside a specific ancestor).
41
+ */
42
+ ancestors?: string[];
43
+ /** Location of the error in the (mapped-back) source document. */
44
+ range: Range;
45
+ /** URI of the document the error belongs to (after XInclude mapping). */
46
+ uri: string;
47
+ }
48
+ /**
49
+ * A rule that customizes how a raw {@link SchemaError} becomes an LSP
50
+ * {@link Diagnostic}: overriding severity, rewriting the message, tagging it
51
+ * with a stable rule id, or filtering it out entirely.
52
+ */
53
+ export interface Rule {
54
+ /** Stable identifier reported as the diagnostic `code`. */
55
+ id: string;
56
+ /** Returns true if this rule applies to the given error. */
57
+ match: (error: SchemaError) => boolean;
58
+ /** Severity to report. Defaults to the ruleset default (Error). */
59
+ severity?: DiagnosticSeverityValue;
60
+ /** Rewrites the diagnostic message. Receives the raw error. */
61
+ message?: (error: SchemaError) => string;
62
+ /** When true, the error is suppressed rather than reported. */
63
+ suppress?: boolean;
64
+ }
65
+ /** Mirrors `vscode-languageserver-types` DiagnosticSeverity numeric values. */
66
+ export type DiagnosticSeverityValue = 1 | 2 | 3 | 4;
67
+ export interface Ruleset {
68
+ rules: Rule[];
69
+ /** Severity applied when no rule overrides it. Defaults to Error (1). */
70
+ defaultSeverity?: DiagnosticSeverityValue;
71
+ /** Diagnostic `source` label. Defaults to "pretext". */
72
+ source?: string;
73
+ }
74
+ /** Reads the contents of an absolute file path, or returns undefined if absent. */
75
+ export type FileReader = (absolutePath: string) => string | undefined;
76
+ export interface ValidateOptions {
77
+ /** URI of the document being validated (used for reporting + XInclude base). */
78
+ uri?: string;
79
+ /** Cancels in-flight validation. */
80
+ signal?: AbortSignal;
81
+ /** Resolve `xi:include` references before validating. Default: true. */
82
+ resolveXIncludes?: boolean;
83
+ /** Reads included files. Defaults to a Node `fs` reader. */
84
+ readFile?: FileReader;
85
+ /** Rule customization applied to raw errors. Defaults to {@link defaultRuleset}. */
86
+ ruleset?: Ruleset;
87
+ }
88
+ export interface ValidationResult {
89
+ /** Diagnostics for the primary document ({@link ValidateOptions.uri}). */
90
+ diagnostics: Diagnostic[];
91
+ /** All diagnostics keyed by document URI (primary + XIncluded files). */
92
+ diagnosticsByUri: Record<string, Diagnostic[]>;
93
+ }
94
+ export interface CompletionContext {
95
+ /** Full source text of the document. */
96
+ text: string;
97
+ /** Cursor position (0-based line and character). */
98
+ position: Position;
99
+ /** The compiled grammar to consult. */
100
+ grammar: Grammar;
101
+ /**
102
+ * URI of the document, used to cache and incrementally extend walker state
103
+ * across repeated completion requests on the same (growing) document.
104
+ * Completions still work without it; caching is simply skipped.
105
+ */
106
+ uri?: string;
107
+ }
@@ -0,0 +1,9 @@
1
+ import { ValidateOptions, ValidationResult, Grammar } from './types';
2
+ import { Severity } from './rules';
3
+ /**
4
+ * Validate an XML document against a compiled PreTeXt grammar. Resolves
5
+ * `xi:include`s first (unless disabled) and maps errors in included files back
6
+ * to their originating file/line.
7
+ */
8
+ export declare function validateDocument(source: string, grammar: Grammar, options?: ValidateOptions): ValidationResult;
9
+ export { Severity };
@@ -0,0 +1,42 @@
1
+ import { FileReader } from './types';
2
+ /** Where a line in the merged document originated. */
3
+ export interface OriginEntry {
4
+ /** URI of the source file this line came from. */
5
+ uri: string;
6
+ /** 0-based line number within that source file. */
7
+ line: number;
8
+ /** Number of characters prepended to this line during merging (column shift). */
9
+ columnShift: number;
10
+ }
11
+ /** A problem with an `xi:include` element itself (missing target, cycle). */
12
+ export interface IncludeProblem {
13
+ kind: "xinclude-missing" | "xinclude-circular";
14
+ message: string;
15
+ /** URI of the file containing the offending include. */
16
+ uri: string;
17
+ /** 0-based line of the include element. */
18
+ line: number;
19
+ /** 0-based start column of the include element. */
20
+ column: number;
21
+ /** Length of the matched include element text. */
22
+ length: number;
23
+ }
24
+ export interface ResolvedDocument {
25
+ /** The inlined document text with all includes expanded. */
26
+ text: string;
27
+ /** Per-line origin, indexed by 0-based line in {@link text}. */
28
+ origin: OriginEntry[];
29
+ /** Problems encountered resolving includes. */
30
+ problems: IncludeProblem[];
31
+ }
32
+ /** Default reader backed by Node's `fs`. */
33
+ export declare function defaultFileReader(): FileReader;
34
+ /**
35
+ * Expand every `xi:include` in `text`, producing a single merged document plus a
36
+ * per-line origin map so validation errors in included content can be mapped
37
+ * back to the file and line they actually came from.
38
+ *
39
+ * Handles nested includes and detects cycles and missing targets (reported as
40
+ * {@link IncludeProblem}s rather than throwing).
41
+ */
42
+ export declare function resolveXIncludes(text: string, documentUri: string, readFile?: FileReader): ResolvedDocument;
@@ -0,0 +1,11 @@
1
+ const baseConfig = require('../../.eslintrc.json');
2
+
3
+ module.exports = [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.json'],
7
+ languageOptions: {
8
+ parser: require('jsonc-eslint-parser'),
9
+ },
10
+ },
11
+ ];
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/index.cjs');
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./dist/index.d.ts";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/index.js';
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@pretextbook/schema",
3
+ "version": "0.0.1",
4
+ "description": "RELAX NG validation and context-aware completions for PreTeXt documents",
5
+ "author": "Oscar Levin",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/PreTeXtBook/pretext-tools.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/PreTeXtBook/pretext-tools/issues"
13
+ },
14
+ "homepage": "https://github.com/PreTeXtBook/pretext-tools/tree/main/packages/schema#readme",
15
+ "private": false,
16
+ "type": "module",
17
+ "main": "./index.cjs",
18
+ "module": "./index.js",
19
+ "types": "./index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./index.d.ts",
23
+ "import": "./index.js",
24
+ "require": "./index.cjs",
25
+ "default": "./index.js"
26
+ },
27
+ "./compile": {
28
+ "types": "./compile.d.ts",
29
+ "import": "./compile.js",
30
+ "require": "./compile.cjs",
31
+ "default": "./compile.js"
32
+ }
33
+ },
34
+ "sideEffects": false,
35
+ "files": [
36
+ "*.js",
37
+ "*.cjs",
38
+ "*.d.ts",
39
+ "*.d.cts",
40
+ "assets",
41
+ "dist"
42
+ ],
43
+ "scripts": {
44
+ "build": "vite build",
45
+ "compile-grammar": "node ./scripts/compile-grammar.mjs",
46
+ "test": "vitest run"
47
+ },
48
+ "dependencies": {
49
+ "salve-annos": "^1.2.4",
50
+ "saxes": "^6.0.0",
51
+ "vscode-languageserver-types": "^3.17.5",
52
+ "xregexp": "^4.4.1"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ }
57
+ }