@gmb/bitmark-parser 6.1.4 → 6.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/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import { createRequire } from "module";
10
10
  // src/generated/package_info.ts
11
11
  var PACKAGE_INFO = {
12
12
  "name": "@gmb/bitmark-parser",
13
- "version": "6.1.4",
13
+ "version": "6.3.0",
14
14
  "author": "Get More Brain Ltd <info@getmorebrain.com>",
15
15
  "license": "ISC",
16
16
  "description": "A parser for bitmark text, powered by WebAssembly."
@@ -50,7 +50,7 @@ function buildUnits(wasm2, input, inputIsBitmark) {
50
50
  if (isError(out)) throw new Error(out);
51
51
  const envelopes = JSON.parse(out);
52
52
  return envelopes.map((env, index) => ({
53
- view: { format: "json", index, total: envelopes.length, bit: env.bit },
53
+ view: { format: "json", index, total: envelopes.length, bit: env.bit ?? {} },
54
54
  // Per-bit transform consumes a one-bit JSON document.
55
55
  source: JSON.stringify([env])
56
56
  }));
@@ -316,16 +316,14 @@ function validateVersionOptions(opts) {
316
316
  }
317
317
  function validateBitmarkOptions(o) {
318
318
  if (o.explicitTextFormat) unsupportedOption("bitmarkOptions.explicitTextFormat");
319
- if (o.prettifyJson !== void 0 && o.prettifyJson !== false) {
320
- unsupportedOption("bitmarkOptions.prettifyJson", "embedded-JSON body prettification is not supported");
319
+ if (o.cardSetVersion === 1) {
320
+ unsupportedOption("bitmarkOptions.cardSetVersion: 1", V3_ONLY);
321
321
  }
322
- if (o.cardSetVersion === 1) unsupportedOption("bitmarkOptions.cardSetVersion: 1", V3_ONLY);
323
322
  if (o.debugGenerationInline) unsupportedOption("bitmarkOptions.debugGenerationInline");
324
323
  if (o.noBreakscaping) unsupportedOption("bitmarkOptions.noBreakscaping");
325
324
  }
326
325
  function validateJsonOptions(o) {
327
326
  if (o.textAsPlainText) unsupportedOption("jsonOptions.textAsPlainText", V3_ONLY);
328
- if (o.excludeUnknownProperties) unsupportedOption("jsonOptions.excludeUnknownProperties");
329
327
  if (o.debugGenerationInline) unsupportedOption("jsonOptions.debugGenerationInline");
330
328
  }
331
329
  function prettyIndent(prettify) {
@@ -361,6 +359,14 @@ function isPseudoAst(v) {
361
359
  return v !== null && typeof v === "object" && (PSEUDO_AST_MARKER in v || Array.isArray(v.bits));
362
360
  }
363
361
 
362
+ // src/legacy/generated/resource-collection-keys.ts
363
+ var RESOURCE_COLLECTION_KEYS = {
364
+ "extractor-image": { key: "images", resourceType: "image" },
365
+ "extractor-image-collapsible": { key: "images", resourceType: "image" },
366
+ "images-logo-grave": { key: "logos", resourceType: "image" },
367
+ "prototype-images": { key: "images", resourceType: "image" }
368
+ };
369
+
364
370
  // src/legacy/builder.ts
365
371
  var BUILT_BIT_PARSER = /* @__PURE__ */ Symbol.for("@gmb/bitmark-parser/legacy-built-bit-parser");
366
372
  var DATA_KEY_TO_JSON_KEY = {
@@ -405,10 +411,14 @@ var Builder = class {
405
411
  * NOTE (documented divergences from bpg):
406
412
  * - the return value is bit JSON, not bpg's internal AST node model;
407
413
  * - `resourceType` (an inference hint in bpg) is not carried into the
408
- * JSON — the attached `resource` object is authoritative;
409
- * - more than one entry in `resources` passes through under a
410
- * `resources` key (kept visible, not silently dropped) — the canonical
411
- * single attachment is `resource`.
414
+ * JSON — the attached `resource` object is authoritative.
415
+ *
416
+ * `resources` follows bpg's JsonGenerator routing: on a bit whose config
417
+ * maps a repeatable resource tag group onto a collection key (e.g.
418
+ * `images` / `logos` — see the generated RESOURCE_COLLECTION_KEYS), the
419
+ * matching-type entries land under that key; on every other bit the first
420
+ * entry becomes the canonical single `resource` (as bpg: "if there is
421
+ * more than one resource, use the first one").
412
422
  */
413
423
  buildBit(data, _options) {
414
424
  const wasm2 = getLegacyWasm();
@@ -444,12 +454,15 @@ var Builder = class {
444
454
  delete src.__isDefaultExample;
445
455
  }
446
456
  const resources = src.resources;
447
- if (resources !== void 0) {
448
- if (Array.isArray(resources)) {
449
- if (resources.length === 1) bit.resource = resources[0];
450
- else if (resources.length > 1) bit.resources = resources;
451
- } else {
452
- bit.resource = resources;
457
+ if (resources !== void 0 && bitType !== void 0) {
458
+ const list = Array.isArray(resources) ? resources : [resources];
459
+ const collection = RESOURCE_COLLECTION_KEYS[bitType];
460
+ if (collection) {
461
+ bit[collection.key] = list.filter(
462
+ (r) => r !== null && typeof r === "object" && r.type === collection.resourceType
463
+ );
464
+ } else if (list.length >= 1) {
465
+ bit.resource = list[0];
453
466
  }
454
467
  delete src.resources;
455
468
  }
@@ -467,7 +480,7 @@ var Builder = class {
467
480
  }
468
481
  }
469
482
  this.parseStringTexts(bit);
470
- const out = wasm2.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1);
483
+ const out = wasm2.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1, false);
471
484
  if (out.startsWith("error:")) {
472
485
  throw new Error(out.slice("error:".length).trim());
473
486
  }
@@ -591,8 +604,10 @@ var BitmarkParserGenerator = class {
591
604
  const outputFormat = opts.outputFormat ?? (isBitmark ? "json" : "bitmark");
592
605
  const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
593
606
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
607
+ const emit = coreFormat === "bitmark" ? prettyIndent(bitmarkOptions.prettifyJson) : { pretty, indent };
594
608
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
595
- const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", pretty, indent, spaces);
609
+ const includeUnknown = !jsonOptions.excludeUnknownProperties;
610
+ const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", emit.pretty, emit.indent, spaces, includeUnknown);
596
611
  this.throwOnError(out);
597
612
  if (outputFormat === "bitmark" || outputFormat === "text") {
598
613
  return this.deliver(out, opts.outputFile, opts.fileOptions);
@@ -622,7 +637,9 @@ var BitmarkParserGenerator = class {
622
637
  this.checkBrowserFileWrite(opts.outputFile);
623
638
  const wasm2 = getLegacyWasm();
624
639
  const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
625
- const { pretty, indent } = prettyIndent(jsonOptions.prettify);
640
+ const { pretty, indent } = prettyIndent(
641
+ isBitmark ? bitmarkOptions.prettifyJson : jsonOptions.prettify
642
+ );
626
643
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
627
644
  const out = wasm2.canonicalize(source, isBitmark ? "bitmark" : "json", "optimized", pretty, indent, spaces);
628
645
  this.throwOnError(out);
@@ -674,7 +691,7 @@ var BitmarkParserGenerator = class {
674
691
  const isHtmlInput = opts.inputFormat === "html" || opts.inputFormat === void 0 && typeof input === "string" && /<table[\s>]/iu.test(inStr);
675
692
  if (!isHtmlInput) {
676
693
  const { source, isBitmark } = this.resolveInput(input, void 0);
677
- const out2 = wasm2.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1);
694
+ const out2 = wasm2.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1, false);
678
695
  this.throwOnError(out2);
679
696
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
680
697
  }
@@ -684,12 +701,12 @@ var BitmarkParserGenerator = class {
684
701
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
685
702
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
686
703
  if (outputFormat === "html") {
687
- const out2 = wasm2.convert(wrapped, "html", "html", "optimized", false, 2, 1);
704
+ const out2 = wasm2.convert(wrapped, "html", "html", "optimized", false, 2, 1, false);
688
705
  this.throwOnError(out2);
689
706
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
690
707
  }
691
708
  const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
692
- const out = wasm2.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces);
709
+ const out = wasm2.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces, false);
693
710
  this.throwOnError(out);
694
711
  if (outputFormat === "bitmark" || outputFormat === "text") {
695
712
  return this.deliver(out, opts.outputFile, opts.fileOptions);
@@ -714,7 +731,7 @@ var BitmarkParserGenerator = class {
714
731
  const opts = Object.assign({}, options);
715
732
  const wasm2 = getLegacyWasm();
716
733
  const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
717
- const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1);
734
+ const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1, true);
718
735
  this.throwOnError(out);
719
736
  return makePseudoAst(JSON.parse(out));
720
737
  }
@@ -821,7 +838,7 @@ var BitmarkParserGenerator = class {
821
838
  }
822
839
  }
823
840
  const { source, isBitmark } = this.resolveInput(input, void 0);
824
- const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1);
841
+ const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1, false);
825
842
  this.throwOnError(out);
826
843
  return out;
827
844
  }
@@ -1291,6 +1308,11 @@ var StringWriter = stubClass("StringWriter", INTERNAL_REASON);
1291
1308
  var FileWriter = stubClass("FileWriter", INTERNAL_REASON);
1292
1309
  var StreamWriter = stubClass("StreamWriter", INTERNAL_REASON);
1293
1310
 
1311
+ // src/types.ts
1312
+ function patchEntry(path2, op, value) {
1313
+ return value === void 0 ? { path: path2, op } : { path: path2, op, value };
1314
+ }
1315
+
1294
1316
  // src/index.ts
1295
1317
  var require2 = createRequire(import.meta.url);
1296
1318
  var wasm = require2("../wasm/bitmark_wasm.js");
@@ -1301,8 +1323,27 @@ function version() {
1301
1323
  function lex(input, options = {}) {
1302
1324
  return wasm.lex(input, options.stage ?? "lex");
1303
1325
  }
1304
- function parse(input, options = {}) {
1305
- return wasm.parse(input, options.mode ?? "optimized");
1326
+ function bitmarkToObjects(input, options = {}) {
1327
+ const out = wasm.convert(
1328
+ input,
1329
+ "bitmark",
1330
+ "json",
1331
+ options.mode ?? "optimized",
1332
+ false,
1333
+ 2,
1334
+ 1,
1335
+ options.includeUnknownProperties ?? false
1336
+ );
1337
+ if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1338
+ return JSON.parse(out);
1339
+ }
1340
+ function objectsToBitmark(bits, options = {}) {
1341
+ const entries = bits.map(
1342
+ (b) => b !== null && typeof b === "object" && "bit" in b && typeof b.bit === "object" ? b : { bit: b }
1343
+ );
1344
+ const out = wasm.convert(JSON.stringify(entries), "json", "bitmark", options.mode ?? "optimized", false, 2, 1, false);
1345
+ if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1346
+ return out;
1306
1347
  }
1307
1348
  function convert(input, options = {}) {
1308
1349
  return wasm.convert(
@@ -1312,16 +1353,10 @@ function convert(input, options = {}) {
1312
1353
  options.mode ?? "optimized",
1313
1354
  options.pretty ?? false,
1314
1355
  options.indent ?? 2,
1315
- options.spacesAroundValues ?? 1
1356
+ options.spacesAroundValues ?? 1,
1357
+ options.includeUnknownProperties ?? false
1316
1358
  );
1317
1359
  }
1318
- function optimize(input, options = {}) {
1319
- return wasm.optimize(input, options.inputFormat ?? "auto", options.pretty ?? false, options.indent ?? 2);
1320
- }
1321
- function fullize(input, options = {}) {
1322
- return wasm.fullize(input, options.inputFormat ?? "auto", options.pretty ?? false, options.indent ?? 2);
1323
- }
1324
- var expand = fullize;
1325
1360
  function canonicalize(input, options = {}) {
1326
1361
  return wasm.canonicalize(
1327
1362
  input,
@@ -1377,22 +1412,17 @@ function info(options = {}) {
1377
1412
  const indent = options.indent ?? 2;
1378
1413
  return wasm.info(infoType, format, bit, pretty, indent);
1379
1414
  }
1380
- function generate(input, options = {}) {
1381
- return wasm.convert(input, "json", "bitmark", options.mode ?? "optimized", false, 2, 1);
1382
- }
1383
1415
  export {
1416
+ bitmarkToObjects,
1384
1417
  breakscapeText,
1385
1418
  canonicalize,
1386
1419
  convert,
1387
1420
  countBits,
1388
- expand,
1389
- fullize,
1390
- generate,
1391
1421
  info,
1392
1422
  legacy_exports as legacy,
1393
1423
  lex,
1394
- optimize,
1395
- parse,
1424
+ objectsToBitmark,
1425
+ patchEntry,
1396
1426
  splitBits,
1397
1427
  transform,
1398
1428
  transformParallel,