@hatua/document 0.0.0 → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pedro Gomes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,36 @@
1
+ import { WorkflowDefinition, workflowDefinition } from '@hatua/schema';
2
+ import { Document } from 'yaml';
3
+ export * from './lex';
4
+ /**
5
+ * Owns the YAML document. This layer holds the source of truth (ADR-0001) —
6
+ * the parsed representation, not a typed object graph. Canvas edits and text
7
+ * edits both land here, so the user's comments, key order and style survive.
8
+ *
9
+ * There are two layers, and the distinction matters:
10
+ *
11
+ * CST — concrete syntax tree. Byte-exact: stringifying an unmodified CST
12
+ * reproduces the input character for character, whitespace included.
13
+ * AST — the `Document` API. Ergonomic (getIn/setIn), and it preserves
14
+ * comments, but it NORMALISES some whitespace: three spaces before an
15
+ * inline comment come back as one.
16
+ *
17
+ * We keep both. Because Hatua does not own the file, an untouched document must
18
+ * come back byte-identical; once edited, the AST's serialisation is the best
19
+ * available and still keeps every comment.
20
+ */
21
+ export interface WorkflowDocument {
22
+ /** Ergonomic view. Mutating it is how you edit the document. */
23
+ readonly ast: Document;
24
+ /**
25
+ * Typed projection. Throws if the document is not a valid Workflow
26
+ * Definition — use `validate()` first when the source may be malformed, and
27
+ * `toString()`/`ast` to drive Text Mode over a document that does not parse
28
+ * into a workflow yet.
29
+ */
30
+ toJSON(): WorkflowDefinition;
31
+ /** Non-throwing counterpart to `toJSON()`. */
32
+ validate(): ReturnType<typeof workflowDefinition.safeParse>;
33
+ /** Byte-identical to the input while untouched; comment-preserving once edited. */
34
+ toString(): string;
35
+ }
36
+ export declare function parseWorkflow(source: string): WorkflowDocument;
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { CST as e, Composer as t, Lexer as n, Parser as r } from "yaml";
2
+ import { workflowDefinition as i } from "@hatua/schema";
3
+ //#region src/lex.ts
4
+ var a = (e) => e.charCodeAt(0) < 32 && e !== "\n" && e !== " ", o = /* @__PURE__ */ new Set([
5
+ ":",
6
+ "-",
7
+ "?",
8
+ ",",
9
+ "[",
10
+ "]",
11
+ "{",
12
+ "}",
13
+ "---",
14
+ "..."
15
+ ]), s = /* @__PURE__ */ new Set([
16
+ "true",
17
+ "false",
18
+ "True",
19
+ "False",
20
+ "TRUE",
21
+ "FALSE",
22
+ "yes",
23
+ "no",
24
+ "on",
25
+ "off"
26
+ ]), c = /* @__PURE__ */ new Set([
27
+ "null",
28
+ "Null",
29
+ "NULL",
30
+ "~"
31
+ ]), l = (e) => e.startsWith("\"") || e.startsWith("'") || e.startsWith("|") || e.startsWith(">") ? "string" : s.has(e) ? "boolean" : c.has(e) ? "null" : /^[-+]?(\d[\d_]*)(\.\d*)?([eE][-+]?\d+)?$/.test(e) || /^0[xob][\dA-Fa-f_]+$/.test(e) ? "number" : "scalar";
32
+ function u(e) {
33
+ let t = [], r = 0, i = null, s = (e, n, r) => {
34
+ n > e && t.push({
35
+ from: e,
36
+ to: n,
37
+ kind: r
38
+ });
39
+ };
40
+ for (let c of new n().lex(e)) {
41
+ if (a(c)) continue;
42
+ let e = r;
43
+ r += c.length;
44
+ let n = r;
45
+ if (c.startsWith("#")) {
46
+ s(e, n, "comment"), i = null;
47
+ continue;
48
+ }
49
+ if (c.trim() === "") {
50
+ s(e, n, "space");
51
+ continue;
52
+ }
53
+ if (o.has(c)) {
54
+ if (c === ":" && i !== null) {
55
+ let e = t[i];
56
+ e && (e.kind = "key");
57
+ }
58
+ s(e, n, "punctuation"), i = null;
59
+ continue;
60
+ }
61
+ if (/^[&*!%]/.test(c)) {
62
+ s(e, n, "annotation"), i = null;
63
+ continue;
64
+ }
65
+ i = t.length, s(e, n, l(c));
66
+ }
67
+ return s(r, e.length, "scalar"), t;
68
+ }
69
+ //#endregion
70
+ //#region src/index.ts
71
+ function d(n) {
72
+ let a = [...new r().parse(n)], o = [...new t({ keepSourceTokens: !0 }).compose(a)], [s] = o;
73
+ if (!s) throw Error("No YAML document found in source");
74
+ if (o.length > 1) throw Error(`A Workflow Definition is a single YAML document; this source holds ${o.length}. Split the file, or edit it as text outside Hatua.`);
75
+ let c = String(s), l = a.map((t) => e.stringify(t)).join("");
76
+ return {
77
+ ast: s,
78
+ toString: () => {
79
+ let e = String(s);
80
+ return e === c ? l : e;
81
+ },
82
+ validate: () => i.safeParse(s.toJS()),
83
+ toJSON: () => {
84
+ let e = i.safeParse(s.toJS());
85
+ if (!e.success) throw Error(`Not a valid Workflow Definition: ${e.error.issues[0]?.message}`);
86
+ return e.data;
87
+ }
88
+ };
89
+ }
90
+ //#endregion
91
+ export { u as lexYaml, d as parseWorkflow };
package/dist/lex.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The source text, split into the pieces a reader tells apart by eye.
3
+ *
4
+ * Here rather than in the renderer because this package owns the YAML seam:
5
+ * `yaml` is its dependency and its whole reason for existing is that nothing
6
+ * else has to know how a YAML document is taken apart. A second package
7
+ * importing the lexer would be a second answer to "what is this text made of".
8
+ *
9
+ * ## Why the lexer and not the parser
10
+ *
11
+ * `parseWorkflow` refuses a source it cannot compose, and refusing is right —
12
+ * there is nothing to edit through. But a reader typing in **Text Mode** spends
13
+ * most of their keystrokes on text that does not parse, and colour that flickered
14
+ * off between two valid states would be worse than no colour at all. A lexer has
15
+ * no such state: it splits what is there and says nothing about whether it means
16
+ * anything.
17
+ *
18
+ * ## Offsets rather than substrings
19
+ *
20
+ * The lexer yields the source in order, so the spans below reassemble it exactly.
21
+ * Handing back offsets keeps that checkable — `spans.map(text.slice)` IS the
22
+ * source — and lets a caller cut a span finer without re-lexing, which is what
23
+ * a **Template** inside a scalar needs.
24
+ */
25
+ export type SpanKind =
26
+ /** A `#` comment, to the end of its line. */
27
+ 'comment'
28
+ /** The scalar on the left of a `:`. */
29
+ | 'key'
30
+ /** A quoted scalar, or a block scalar's body. */
31
+ | 'string' | 'number' | 'boolean' | 'null'
32
+ /** An unquoted scalar that is none of the above. */
33
+ | 'scalar'
34
+ /** `:`, `-`, `,`, brackets, braces, and the document markers. */
35
+ | 'punctuation'
36
+ /** `&anchor`, `*alias`, `!!tag`, `%DIRECTIVE`. */
37
+ | 'annotation'
38
+ /** Whitespace and newlines, carried so the spans reassemble the source. */
39
+ | 'space';
40
+ export interface Span {
41
+ /** Index into the source, inclusive. */
42
+ from: number;
43
+ /** Index into the source, exclusive. */
44
+ to: number;
45
+ kind: SpanKind;
46
+ }
47
+ /**
48
+ * Split `source` into spans covering every character exactly once.
49
+ *
50
+ * A lexer error is not raised: `yaml`'s lexer is total over any string, and the
51
+ * point of using it here is that half-typed input has an answer. What it cannot
52
+ * classify comes back as `scalar`, which renders as ordinary text.
53
+ */
54
+ export declare function lexYaml(source: string): Span[];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,17 +1,35 @@
1
1
  {
2
2
  "name": "@hatua/document",
3
- "version": "0.0.0",
4
- "description": "Placeholder reserving the name. Nothing is published here; see 0.1.0 and later.",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Internal to @hatua/react — published because it is an external of its build, not a supported API. Owns the YAML document: parse, surgical edit, serialise, source positions.",
5
6
  "license": "MIT",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/pedromvgomes/hatua.git",
9
10
  "directory": "source/packages/document"
10
11
  },
12
+ "sideEffects": false,
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
11
22
  "publishConfig": {
12
23
  "access": "public"
13
24
  },
14
- "files": [
15
- "README.md"
16
- ]
25
+ "dependencies": {
26
+ "yaml": "^2.8.1",
27
+ "@hatua/schema": "0.1.0"
28
+ },
29
+ "scripts": {
30
+ "build": "vite build",
31
+ "typecheck": "tsc --noEmit",
32
+ "test": "vitest run --passWithNoTests",
33
+ "test:coverage": "vitest run --coverage --coverage.reporter=text-summary --coverage.reporter=json-summary --coverage.reporter=lcovonly --passWithNoTests"
34
+ }
17
35
  }
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # @hatua/document
2
-
3
- This version reserves the package name and contains no code.
4
-
5
- Install `0.1.0` or later.