@latex-kit/schema 0.2.0 → 0.3.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/docid.js CHANGED
@@ -55,7 +55,15 @@ function writeDocId(file, docId) {
55
55
  }
56
56
  if (ext === '.yaml' || ext === '.yml') {
57
57
  const doc = (0, yaml_1.parseDocument)(text);
58
- doc.set('docId', docId);
58
+ if (doc.has('docId') || !(0, yaml_1.isMap)(doc.contents)) {
59
+ doc.set('docId', docId);
60
+ }
61
+ else {
62
+ // right after `template:`, where people look for it
63
+ const items = doc.contents.items;
64
+ const at = items.findIndex((p) => (0, yaml_1.isScalar)(p.key) && p.key.value === 'template');
65
+ items.splice(at + 1, 0, doc.createPair('docId', docId));
66
+ }
59
67
  (0, fs_1.writeFileSync)(file, doc.toString());
60
68
  return;
61
69
  }
package/esm/docid.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { randomBytes } from 'crypto';
2
2
  import { readFileSync, writeFileSync } from 'fs';
3
3
  import { extname } from 'path';
4
- import { parseDocument } from 'yaml';
4
+ import { isMap, isScalar, parseDocument } from 'yaml';
5
5
  const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
6
6
  /**
7
7
  * A new document ID: `PREFIX-YYYYMMDD-XXXXXXXX` (8 random Crockford base-32
@@ -49,7 +49,15 @@ export function writeDocId(file, docId) {
49
49
  }
50
50
  if (ext === '.yaml' || ext === '.yml') {
51
51
  const doc = parseDocument(text);
52
- doc.set('docId', docId);
52
+ if (doc.has('docId') || !isMap(doc.contents)) {
53
+ doc.set('docId', docId);
54
+ }
55
+ else {
56
+ // right after `template:`, where people look for it
57
+ const items = doc.contents.items;
58
+ const at = items.findIndex((p) => isScalar(p.key) && p.key.value === 'template');
59
+ items.splice(at + 1, 0, doc.createPair('docId', docId));
60
+ }
53
61
  writeFileSync(file, doc.toString());
54
62
  return;
55
63
  }
package/esm/load.d.ts CHANGED
@@ -3,6 +3,12 @@ export declare class LoadError extends Error {
3
3
  readonly file: string;
4
4
  constructor(message: string, file: string);
5
5
  }
6
+ /**
7
+ * Resolve these module names to fixed paths when loading project files, e.g.
8
+ * `{ 'latex-kit': '/…/latex-kit' }`. The CLI uses it so schema.ts and records
9
+ * can `import { z } from 'latex-kit'` even in a folder with no node_modules.
10
+ */
11
+ export declare function setModuleAliases(map: Record<string, string>): void;
6
12
  /** Load a TypeScript or JavaScript module and return all its exports. */
7
13
  export declare function loadModule(file: string): Promise<Record<string, unknown>>;
8
14
  /** Load a data file: JSON, YAML, or the default export of a TS/JS module. */
package/esm/load.js CHANGED
@@ -13,9 +13,19 @@ export class LoadError extends Error {
13
13
  }
14
14
  }
15
15
  let jiti = null;
16
+ let aliases = {};
17
+ /**
18
+ * Resolve these module names to fixed paths when loading project files, e.g.
19
+ * `{ 'latex-kit': '/…/latex-kit' }`. The CLI uses it so schema.ts and records
20
+ * can `import { z } from 'latex-kit'` even in a folder with no node_modules.
21
+ */
22
+ export function setModuleAliases(map) {
23
+ aliases = { ...map };
24
+ jiti = null;
25
+ }
16
26
  function loader() {
17
27
  // fsCache off: never write a transpile cache into the user's project
18
- jiti ??= createJiti(join(process.cwd(), 'latex-kit.js'), { fsCache: false, moduleCache: false, interopDefault: false });
28
+ jiti ??= createJiti(join(process.cwd(), 'latex-kit.js'), { fsCache: false, moduleCache: false, interopDefault: false, alias: aliases });
19
29
  return jiti;
20
30
  }
21
31
  /** Load a TypeScript or JavaScript module and return all its exports. */
package/load.d.ts CHANGED
@@ -3,6 +3,12 @@ export declare class LoadError extends Error {
3
3
  readonly file: string;
4
4
  constructor(message: string, file: string);
5
5
  }
6
+ /**
7
+ * Resolve these module names to fixed paths when loading project files, e.g.
8
+ * `{ 'latex-kit': '/…/latex-kit' }`. The CLI uses it so schema.ts and records
9
+ * can `import { z } from 'latex-kit'` even in a folder with no node_modules.
10
+ */
11
+ export declare function setModuleAliases(map: Record<string, string>): void;
6
12
  /** Load a TypeScript or JavaScript module and return all its exports. */
7
13
  export declare function loadModule(file: string): Promise<Record<string, unknown>>;
8
14
  /** Load a data file: JSON, YAML, or the default export of a TS/JS module. */
package/load.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LoadError = exports.DATA_EXTENSIONS = void 0;
4
+ exports.setModuleAliases = setModuleAliases;
4
5
  exports.loadModule = loadModule;
5
6
  exports.loadFile = loadFile;
6
7
  const fs_1 = require("fs");
@@ -19,9 +20,19 @@ class LoadError extends Error {
19
20
  }
20
21
  exports.LoadError = LoadError;
21
22
  let jiti = null;
23
+ let aliases = {};
24
+ /**
25
+ * Resolve these module names to fixed paths when loading project files, e.g.
26
+ * `{ 'latex-kit': '/…/latex-kit' }`. The CLI uses it so schema.ts and records
27
+ * can `import { z } from 'latex-kit'` even in a folder with no node_modules.
28
+ */
29
+ function setModuleAliases(map) {
30
+ aliases = { ...map };
31
+ jiti = null;
32
+ }
22
33
  function loader() {
23
34
  // fsCache off: never write a transpile cache into the user's project
24
- jiti ??= (0, jiti_1.createJiti)((0, path_1.join)(process.cwd(), 'latex-kit.js'), { fsCache: false, moduleCache: false, interopDefault: false });
35
+ jiti ??= (0, jiti_1.createJiti)((0, path_1.join)(process.cwd(), 'latex-kit.js'), { fsCache: false, moduleCache: false, interopDefault: false, alias: aliases });
25
36
  return jiti;
26
37
  }
27
38
  /** Load a TypeScript or JavaScript module and return all its exports. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latex-kit/schema",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Typed data for latex-kit documents: zod schemas, shared types, references, document IDs",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "main": "index.js",
@@ -44,5 +44,5 @@
44
44
  "yaml": "^2.9.1",
45
45
  "zod": "^4.6.7"
46
46
  },
47
- "gitHead": "0da7d546606627d73d38647b02b29cd4df4addd4"
47
+ "gitHead": "023ede48131a32e9250ec48b0dde17e71c6a1536"
48
48
  }