Skip to content

Commit 70c212a

Browse files
authored
fix: handle externalRef in pretext.rng schema compilation
The upstream pretext.rng now uses <externalRef> elements for PreFigure diagram support, referencing pf-adapter.rng, pf-preamble-adapter.rng, and pf_schema.rng. salve-annos has a bug where InternalSimplifier.parse is called from step1 with only a filePath URL (per the Parser type interface), leaving schemaText undefined and crashing the XML parser. Fixes: 1. Patch InternalSimplifier.prototype.parse in compile-grammar.mjs to load the resource via resourceLoader when schemaText is undefined. 2. Update getSchemas.js to also download pf-adapter.rng, pf-preamble-adapter.rng, and pf_schema.rng (dependencies of pretext.rng). 3. Commit pf_schema.rng and updated schema files to repo.
1 parent e0fcf7d commit 70c212a

7 files changed

Lines changed: 2407 additions & 15 deletions

File tree

packages/schema/assets/pretext.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/schema/scripts/compile-grammar.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,33 @@
1111
import fs from "fs";
1212
import path from "path";
1313
import { fileURLToPath, pathToFileURL } from "url";
14+
import { createRequire } from "module";
1415
import { convertRNGToPattern, writeTreeToJSON } from "salve-annos";
1516

17+
// salve-annos bug workaround: InternalSimplifier.parse is passed to step1 as a
18+
// Parser callback (filePath: URL) => Promise<Element>, but the implementation
19+
// expects three arguments (filePath, schemaResource, schemaText). When step1
20+
// calls parse(url) for externalRef/include elements, schemaText is undefined,
21+
// causing a crash. Patch the prototype to load the resource when schemaText is
22+
// not supplied.
23+
const _require = createRequire(import.meta.url);
24+
const internal = _require(
25+
"salve-annos/lib/salve/conversion/schema-simplifiers/internal.js",
26+
);
27+
const origParse = internal.InternalSimplifier.prototype.parse;
28+
internal.InternalSimplifier.prototype.parse = async function patchedParse(
29+
filePath,
30+
schemaResource,
31+
schemaText,
32+
) {
33+
if (schemaText === undefined) {
34+
const res = await this.options.resourceLoader.load(filePath);
35+
schemaResource = res;
36+
schemaText = await res.getText();
37+
}
38+
return origParse.call(this, filePath, schemaResource, schemaText);
39+
};
40+
1641
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
1742
const workspaceRoot = path.resolve(scriptDir, "../../..");
1843
const extensionSchemaDir = path.join(

0 commit comments

Comments
 (0)