@gmb/bitmark-parser 6.1.3 → 6.1.4
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/README.md +46 -0
- package/dist/browser/bitmark-parser.min.js +4 -2
- package/dist/browser/bitmark-parser.min.js.map +1 -1
- package/dist/browser/bitmark_wasm_bg.wasm +0 -0
- package/dist/browser/cjs/bitmark_wasm_bg.wasm +0 -0
- package/dist/browser/cjs/index.cjs +1091 -1
- package/dist/browser/cjs/index.cjs.map +1 -1
- package/dist/browser/cjs/index.d.cts +1761 -1
- package/dist/browser/esm/bitmark_wasm_bg.wasm +0 -0
- package/dist/browser/esm/index.d.ts +1761 -1
- package/dist/browser/esm/index.js +1090 -1
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/esm/worker-entry.js.map +1 -1
- package/dist/index.cjs +1100 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +191 -1
- package/dist/index.d.ts +191 -1
- package/dist/index.js +1092 -3
- package/dist/index.js.map +1 -1
- package/dist/legacy.cjs +1170 -0
- package/dist/legacy.cjs.map +1 -0
- package/dist/legacy.d.cts +1568 -0
- package/dist/legacy.d.ts +1568 -0
- package/dist/legacy.js +1095 -0
- package/dist/legacy.js.map +1 -0
- package/package.json +13 -7
- package/wasm/bitmark_wasm.d.ts +10 -0
- package/wasm/bitmark_wasm.js +39 -0
- package/wasm/bitmark_wasm_bg.wasm +0 -0
- package/wasm/bitmark_wasm_bg.wasm.d.ts +1 -0
- package/wasm/package.json +1 -1
package/dist/legacy.js
ADDED
|
@@ -0,0 +1,1095 @@
|
|
|
1
|
+
// src/legacy-entry.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
|
|
4
|
+
// src/node/legacy-init.ts
|
|
5
|
+
import * as fs from "fs";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
|
|
8
|
+
// src/legacy/runtime.ts
|
|
9
|
+
var wasmModule;
|
|
10
|
+
function setLegacyWasm(wasm2) {
|
|
11
|
+
wasmModule = wasm2;
|
|
12
|
+
}
|
|
13
|
+
function getLegacyWasm() {
|
|
14
|
+
if (!wasmModule) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
"legacy API not initialised: the WASM module has not been provided (browser consumers must call init() first)"
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return wasmModule;
|
|
20
|
+
}
|
|
21
|
+
var fileIo;
|
|
22
|
+
function setLegacyFileIo(io) {
|
|
23
|
+
fileIo = io;
|
|
24
|
+
}
|
|
25
|
+
function getLegacyFileIo() {
|
|
26
|
+
return fileIo;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/node/legacy-init.ts
|
|
30
|
+
function initLegacyNode(wasm2) {
|
|
31
|
+
setLegacyWasm(wasm2);
|
|
32
|
+
setLegacyFileIo({
|
|
33
|
+
existsSync: (p) => fs.existsSync(p),
|
|
34
|
+
readFileSync: (p, encoding) => fs.readFileSync(p, { encoding }),
|
|
35
|
+
writeFile: (p, data, opts) => {
|
|
36
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
37
|
+
fs.writeFileSync(p, data, {
|
|
38
|
+
encoding: opts.encoding ?? "utf8",
|
|
39
|
+
flag: opts.append ? "a" : "w"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/generated/package_info.ts
|
|
46
|
+
var PACKAGE_INFO = {
|
|
47
|
+
"name": "@gmb/bitmark-parser",
|
|
48
|
+
"version": "6.1.4",
|
|
49
|
+
"author": "Get More Brain Ltd <info@getmorebrain.com>",
|
|
50
|
+
"license": "ISC",
|
|
51
|
+
"description": "A parser for bitmark text, powered by WebAssembly."
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/legacy/errors.ts
|
|
55
|
+
var LegacyUnsupportedError = class extends Error {
|
|
56
|
+
constructor(message) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.name = "LegacyUnsupportedError";
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
function unsupportedMember(member, reason) {
|
|
62
|
+
throw new LegacyUnsupportedError(
|
|
63
|
+
`'${member}' is not supported by the @gmb/bitmark-parser legacy API` + (reason ? `: ${reason}` : "")
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
function unsupportedOption(option, reason) {
|
|
67
|
+
throw new LegacyUnsupportedError(
|
|
68
|
+
`Option '${option}' is not supported by the @gmb/bitmark-parser legacy API` + (reason ? `: ${reason}` : "")
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/legacy/translate.ts
|
|
73
|
+
var V3_ONLY = "bitmark v2 output is not supported (this parser is bitmark v3 only)";
|
|
74
|
+
function validateVersionOptions(opts) {
|
|
75
|
+
if (opts.bitmarkVersion === 2) unsupportedOption("bitmarkVersion: 2", V3_ONLY);
|
|
76
|
+
}
|
|
77
|
+
function validateBitmarkOptions(o) {
|
|
78
|
+
if (o.explicitTextFormat) unsupportedOption("bitmarkOptions.explicitTextFormat");
|
|
79
|
+
if (o.prettifyJson !== void 0 && o.prettifyJson !== false) {
|
|
80
|
+
unsupportedOption("bitmarkOptions.prettifyJson", "embedded-JSON body prettification is not supported");
|
|
81
|
+
}
|
|
82
|
+
if (o.cardSetVersion === 1) unsupportedOption("bitmarkOptions.cardSetVersion: 1", V3_ONLY);
|
|
83
|
+
if (o.debugGenerationInline) unsupportedOption("bitmarkOptions.debugGenerationInline");
|
|
84
|
+
if (o.noBreakscaping) unsupportedOption("bitmarkOptions.noBreakscaping");
|
|
85
|
+
}
|
|
86
|
+
function validateJsonOptions(o) {
|
|
87
|
+
if (o.textAsPlainText) unsupportedOption("jsonOptions.textAsPlainText", V3_ONLY);
|
|
88
|
+
if (o.excludeUnknownProperties) unsupportedOption("jsonOptions.excludeUnknownProperties");
|
|
89
|
+
if (o.debugGenerationInline) unsupportedOption("jsonOptions.debugGenerationInline");
|
|
90
|
+
}
|
|
91
|
+
function prettyIndent(prettify) {
|
|
92
|
+
if (prettify === true) return { pretty: true, indent: 2 };
|
|
93
|
+
if (typeof prettify === "number" && prettify > 0) return { pretty: true, indent: prettify };
|
|
94
|
+
return { pretty: false, indent: 2 };
|
|
95
|
+
}
|
|
96
|
+
function spacesAround(v) {
|
|
97
|
+
if (v === void 0 || v === true) return 1;
|
|
98
|
+
if (v === false) return 0;
|
|
99
|
+
return Math.max(0, Math.min(10, v));
|
|
100
|
+
}
|
|
101
|
+
function mapTextFormat(f) {
|
|
102
|
+
switch (f) {
|
|
103
|
+
case "text":
|
|
104
|
+
case "latex":
|
|
105
|
+
case "json":
|
|
106
|
+
case "xml":
|
|
107
|
+
return "plainText";
|
|
108
|
+
default:
|
|
109
|
+
return "bitmark++";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/legacy/pseudo-ast.ts
|
|
114
|
+
var PSEUDO_AST_MARKER = /* @__PURE__ */ Symbol.for("@gmb/bitmark-parser/legacy-pseudo-ast");
|
|
115
|
+
function makePseudoAst(bits) {
|
|
116
|
+
const ast = { bits };
|
|
117
|
+
Object.defineProperty(ast, PSEUDO_AST_MARKER, { value: true, enumerable: false });
|
|
118
|
+
return ast;
|
|
119
|
+
}
|
|
120
|
+
function isPseudoAst(v) {
|
|
121
|
+
return v !== null && typeof v === "object" && (PSEUDO_AST_MARKER in v || Array.isArray(v.bits));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/legacy/builder.ts
|
|
125
|
+
var BUILT_BIT_PARSER = /* @__PURE__ */ Symbol.for("@gmb/bitmark-parser/legacy-built-bit-parser");
|
|
126
|
+
var DATA_KEY_TO_JSON_KEY = {
|
|
127
|
+
bitType: "type",
|
|
128
|
+
textFormat: "format",
|
|
129
|
+
flashcards: "cards",
|
|
130
|
+
markConfig: "marks"
|
|
131
|
+
};
|
|
132
|
+
var DEPRECATED_TEXT_FORMATS = {
|
|
133
|
+
"bitmark--": "bitmark++",
|
|
134
|
+
"bitmark+": "bitmark++"
|
|
135
|
+
};
|
|
136
|
+
var TAG_TEXT_KEYS = [
|
|
137
|
+
"title",
|
|
138
|
+
"subtitle",
|
|
139
|
+
"item",
|
|
140
|
+
"lead",
|
|
141
|
+
"pageNumber",
|
|
142
|
+
"marginNumber",
|
|
143
|
+
"hint",
|
|
144
|
+
"instruction",
|
|
145
|
+
"caption"
|
|
146
|
+
];
|
|
147
|
+
var Builder = class {
|
|
148
|
+
/**
|
|
149
|
+
* Build bitmark AST envelope (bpg-compatible `buildBitmark`).
|
|
150
|
+
*/
|
|
151
|
+
buildBitmark(data) {
|
|
152
|
+
const ast = makePseudoAst(data.bits ?? []);
|
|
153
|
+
if (data.errors !== void 0) {
|
|
154
|
+
ast.errors = data.errors;
|
|
155
|
+
}
|
|
156
|
+
return ast;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Build a bit (bpg-compatible `buildBit`).
|
|
160
|
+
*
|
|
161
|
+
* Accepts the full bpg data shape (flat, ~250 optional fields, values in
|
|
162
|
+
* the JSON model types). Returns the normalized bit JSON. Throws
|
|
163
|
+
* `No config found for bit: X` for unknown bit types, as bpg does.
|
|
164
|
+
*
|
|
165
|
+
* NOTE (documented divergences from bpg):
|
|
166
|
+
* - the return value is bit JSON, not bpg's internal AST node model;
|
|
167
|
+
* - `resourceType` (an inference hint in bpg) is not carried into the
|
|
168
|
+
* JSON — the attached `resource` object is authoritative;
|
|
169
|
+
* - more than one entry in `resources` passes through under a
|
|
170
|
+
* `resources` key (kept visible, not silently dropped) — the canonical
|
|
171
|
+
* single attachment is `resource`.
|
|
172
|
+
*/
|
|
173
|
+
buildBit(data, _options) {
|
|
174
|
+
const wasm2 = getLegacyWasm();
|
|
175
|
+
const src = { ...data };
|
|
176
|
+
const bitType = src.bitType;
|
|
177
|
+
this.assertKnownBitType(bitType);
|
|
178
|
+
const bit = {};
|
|
179
|
+
bit.type = bitType;
|
|
180
|
+
if (src.isCommented) {
|
|
181
|
+
bit.type = "_comment";
|
|
182
|
+
bit.originalType = bitType;
|
|
183
|
+
}
|
|
184
|
+
delete src.bitType;
|
|
185
|
+
delete src.isCommented;
|
|
186
|
+
const textFormat = src.textFormat;
|
|
187
|
+
if (textFormat !== void 0) {
|
|
188
|
+
bit.format = DEPRECATED_TEXT_FORMATS[textFormat] ?? textFormat;
|
|
189
|
+
delete src.textFormat;
|
|
190
|
+
}
|
|
191
|
+
delete src.resourceType;
|
|
192
|
+
const body = src.body;
|
|
193
|
+
if (body !== void 0 && body !== null && typeof body === "object" && "body" in body) {
|
|
194
|
+
bit.body = body.body;
|
|
195
|
+
delete src.body;
|
|
196
|
+
}
|
|
197
|
+
if (src.example !== void 0) {
|
|
198
|
+
bit.isExample = true;
|
|
199
|
+
bit.example = src.example;
|
|
200
|
+
delete src.example;
|
|
201
|
+
delete src.__isDefaultExample;
|
|
202
|
+
} else if (src.__isDefaultExample) {
|
|
203
|
+
bit.isExample = true;
|
|
204
|
+
delete src.__isDefaultExample;
|
|
205
|
+
}
|
|
206
|
+
const resources = src.resources;
|
|
207
|
+
if (resources !== void 0) {
|
|
208
|
+
if (Array.isArray(resources)) {
|
|
209
|
+
if (resources.length === 1) bit.resource = resources[0];
|
|
210
|
+
else if (resources.length > 1) bit.resources = resources;
|
|
211
|
+
} else {
|
|
212
|
+
bit.resource = resources;
|
|
213
|
+
}
|
|
214
|
+
delete src.resources;
|
|
215
|
+
}
|
|
216
|
+
const parser = src.parser;
|
|
217
|
+
delete src.parser;
|
|
218
|
+
const extraProperties = src.extraProperties;
|
|
219
|
+
delete src.extraProperties;
|
|
220
|
+
for (const [key, value] of Object.entries(src)) {
|
|
221
|
+
if (value === void 0) continue;
|
|
222
|
+
bit[DATA_KEY_TO_JSON_KEY[key] ?? key] = value;
|
|
223
|
+
}
|
|
224
|
+
if (extraProperties) {
|
|
225
|
+
for (const [key, value] of Object.entries(extraProperties)) {
|
|
226
|
+
if (!(key in bit)) bit[key] = value;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
this.parseStringTexts(bit);
|
|
230
|
+
const out = wasm2.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1);
|
|
231
|
+
if (out.startsWith("error:")) {
|
|
232
|
+
throw new Error(out.slice("error:".length).trim());
|
|
233
|
+
}
|
|
234
|
+
const entries = JSON.parse(out);
|
|
235
|
+
const built = entries[0]?.bit ?? bit;
|
|
236
|
+
for (const [key, value] of Object.entries(bit)) {
|
|
237
|
+
if (key in built) continue;
|
|
238
|
+
if (value === false || value === "" || value === null || Array.isArray(value) && value.length === 0) {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
built[key] = value;
|
|
242
|
+
}
|
|
243
|
+
if (parser !== void 0) {
|
|
244
|
+
Object.defineProperty(built, BUILT_BIT_PARSER, { value: parser, enumerable: false });
|
|
245
|
+
}
|
|
246
|
+
return built;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Build a highlight body bit (bpg-compatible `buildHighlight`).
|
|
250
|
+
* Shapes the HighlightJson body-bit; unset optionals are omitted.
|
|
251
|
+
*/
|
|
252
|
+
buildHighlight(_context, data) {
|
|
253
|
+
if (!data) return void 0;
|
|
254
|
+
const src = { ...data };
|
|
255
|
+
const attrs = src.attrs;
|
|
256
|
+
if (attrs) Object.assign(src, attrs);
|
|
257
|
+
const texts = Array.isArray(src.texts) ? src.texts.filter((t) => t != null).map((t) => ({
|
|
258
|
+
text: t.text ?? "",
|
|
259
|
+
isCorrect: t.isCorrect ?? false,
|
|
260
|
+
isHighlighted: t.isHighlighted ?? false,
|
|
261
|
+
...stripUndefined(t, ["text", "isCorrect", "isHighlighted"])
|
|
262
|
+
})) : [];
|
|
263
|
+
const node = {
|
|
264
|
+
type: "highlight",
|
|
265
|
+
texts,
|
|
266
|
+
prefix: src.prefix ?? "",
|
|
267
|
+
postfix: src.postfix ?? "",
|
|
268
|
+
...stripUndefined(src, ["type", "texts", "prefix", "postfix", "attrs"]),
|
|
269
|
+
attrs: {}
|
|
270
|
+
};
|
|
271
|
+
return node;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* bpg's builder parses string-valued JsonText fields as bitmark text
|
|
275
|
+
* (inline marks become TextAst marks); the composer would otherwise treat
|
|
276
|
+
* them as literal text. Converts top-level JsonText fields in place.
|
|
277
|
+
* Nested (card-content) strings are left literal — documented divergence.
|
|
278
|
+
*/
|
|
279
|
+
parseStringTexts(bit) {
|
|
280
|
+
const wasm2 = getLegacyWasm();
|
|
281
|
+
const toAst = (s, location) => {
|
|
282
|
+
if (s === "") return s;
|
|
283
|
+
const out = wasm2.convert_text(s, "textToAst", "bitmark++", location);
|
|
284
|
+
try {
|
|
285
|
+
return JSON.parse(out);
|
|
286
|
+
} catch {
|
|
287
|
+
return s;
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const format = bit.format;
|
|
291
|
+
const bitmarkBody = format === void 0 || format === "bitmark++" || format === "bitmark+";
|
|
292
|
+
if (typeof bit.body === "string" && bitmarkBody) {
|
|
293
|
+
bit.body = toAst(bit.body, "body");
|
|
294
|
+
}
|
|
295
|
+
for (const key of TAG_TEXT_KEYS) {
|
|
296
|
+
const v = bit[key];
|
|
297
|
+
if (typeof v === "string") bit[key] = toAst(v, "tag");
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/** Throw bpg's exact unknown-bit-type error (`Config.getBitConfig` parity). */
|
|
301
|
+
assertKnownBitType(bitType) {
|
|
302
|
+
const wasm2 = getLegacyWasm();
|
|
303
|
+
let known = false;
|
|
304
|
+
if (bitType !== void 0) {
|
|
305
|
+
try {
|
|
306
|
+
const info = JSON.parse(wasm2.info("bit", "json", bitType, false, 2));
|
|
307
|
+
known = info.error === void 0;
|
|
308
|
+
} catch {
|
|
309
|
+
known = false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (!known) {
|
|
313
|
+
throw new Error(`No config found for bit: ${bitType}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
function stripUndefined(src, exclude) {
|
|
318
|
+
const out = {};
|
|
319
|
+
for (const [k, v] of Object.entries(src)) {
|
|
320
|
+
if (v !== void 0 && !exclude.includes(k)) out[k] = v;
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/legacy/bpg-class.ts
|
|
326
|
+
var BitmarkParserGenerator = class {
|
|
327
|
+
/**
|
|
328
|
+
* Get the version of the library.
|
|
329
|
+
*
|
|
330
|
+
* Reports the @gmb/bitmark-parser version (not a bpg version string).
|
|
331
|
+
*/
|
|
332
|
+
version() {
|
|
333
|
+
return PACKAGE_INFO.version;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Convert bitmark from bitmark to JSON, or JSON to bitmark (bpg-compatible).
|
|
337
|
+
*
|
|
338
|
+
* Input detection order matches bpg: AST → JSON → assume bitmark.
|
|
339
|
+
* Default output: bitmark input → json; json/ast input → bitmark.
|
|
340
|
+
*/
|
|
341
|
+
convert(input, options) {
|
|
342
|
+
const opts = Object.assign({}, options);
|
|
343
|
+
const jsonOptions = Object.assign({}, opts.jsonOptions);
|
|
344
|
+
const bitmarkOptions = Object.assign({}, opts.bitmarkOptions);
|
|
345
|
+
validateVersionOptions(opts);
|
|
346
|
+
validateBitmarkOptions(bitmarkOptions);
|
|
347
|
+
validateJsonOptions(jsonOptions);
|
|
348
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
349
|
+
const wasm2 = getLegacyWasm();
|
|
350
|
+
const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
|
|
351
|
+
const outputFormat = opts.outputFormat ?? (isBitmark ? "json" : "bitmark");
|
|
352
|
+
const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
|
|
353
|
+
const { pretty, indent } = prettyIndent(jsonOptions.prettify);
|
|
354
|
+
const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
|
|
355
|
+
const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", pretty, indent, spaces);
|
|
356
|
+
this.throwOnError(out);
|
|
357
|
+
if (outputFormat === "bitmark" || outputFormat === "text") {
|
|
358
|
+
return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
359
|
+
}
|
|
360
|
+
if (outputFormat === "json") {
|
|
361
|
+
if (opts.outputFile) return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
362
|
+
if (pretty || jsonOptions.stringify) return out;
|
|
363
|
+
return JSON.parse(out);
|
|
364
|
+
}
|
|
365
|
+
const ast = makePseudoAst(JSON.parse(out));
|
|
366
|
+
if (opts.outputFile) {
|
|
367
|
+
return this.deliver(JSON.stringify(ast), opts.outputFile, opts.fileOptions);
|
|
368
|
+
}
|
|
369
|
+
return ast;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Upgrade (prettify / canonicalize) bitmark or JSON to the latest version
|
|
373
|
+
* of itself (bpg-compatible `upgrade`).
|
|
374
|
+
*/
|
|
375
|
+
upgrade(input, options) {
|
|
376
|
+
const opts = Object.assign({}, options);
|
|
377
|
+
const jsonOptions = Object.assign({}, opts.jsonOptions);
|
|
378
|
+
const bitmarkOptions = Object.assign({}, opts.bitmarkOptions);
|
|
379
|
+
validateVersionOptions(opts);
|
|
380
|
+
validateBitmarkOptions(bitmarkOptions);
|
|
381
|
+
validateJsonOptions(jsonOptions);
|
|
382
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
383
|
+
const wasm2 = getLegacyWasm();
|
|
384
|
+
const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
|
|
385
|
+
const { pretty, indent } = prettyIndent(jsonOptions.prettify);
|
|
386
|
+
const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
|
|
387
|
+
const out = wasm2.canonicalize(source, isBitmark ? "bitmark" : "json", "optimized", pretty, indent, spaces);
|
|
388
|
+
this.throwOnError(out);
|
|
389
|
+
if (isBitmark) return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
390
|
+
if (opts.outputFile) return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
391
|
+
if (pretty || jsonOptions.stringify) return out;
|
|
392
|
+
return JSON.parse(out);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Get information about the bitmark language (bpg-compatible `info`).
|
|
396
|
+
*/
|
|
397
|
+
info(options) {
|
|
398
|
+
const opts = Object.assign({}, options);
|
|
399
|
+
const wasm2 = getLegacyWasm();
|
|
400
|
+
const type = opts.type ?? "list";
|
|
401
|
+
const format = opts.outputFormat ?? "text";
|
|
402
|
+
const { pretty, indent } = prettyIndent(opts.prettify);
|
|
403
|
+
const coreFormat = format === "pojo" ? "json" : format;
|
|
404
|
+
const out = wasm2.info(type, coreFormat, opts.bit ?? "", pretty, indent);
|
|
405
|
+
this.throwOnError(out);
|
|
406
|
+
if (format === "pojo") return JSON.parse(out);
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
/** Not supported: bpg-internal development tool. */
|
|
410
|
+
generateConfig(_options) {
|
|
411
|
+
unsupportedMember("BitmarkParserGenerator.generateConfig", "bpg-internal development tool");
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Convert between HTML tables and bitmark tables (bpg-compatible
|
|
415
|
+
* `convertHtmlTable`).
|
|
416
|
+
*
|
|
417
|
+
* Backed by the core's schema-driven html mapping: HTML input has each
|
|
418
|
+
* `<table>` wrapped in the core's `<bitmark-bit>` envelope and parsed;
|
|
419
|
+
* bits input is emitted as html and the `<table>` fragments extracted
|
|
420
|
+
* (the envelope stripped). The markup *inside* `<table>` follows this
|
|
421
|
+
* parser's html mapping, which may differ from bpg's in detail.
|
|
422
|
+
*/
|
|
423
|
+
convertHtmlTable(input, options) {
|
|
424
|
+
const opts = Object.assign({}, options);
|
|
425
|
+
const jsonOptions = Object.assign({}, opts.jsonOptions);
|
|
426
|
+
const bitmarkOptions = Object.assign({}, opts.bitmarkOptions);
|
|
427
|
+
validateBitmarkOptions(bitmarkOptions);
|
|
428
|
+
validateJsonOptions(jsonOptions);
|
|
429
|
+
if (opts.keepUnknownTags) unsupportedOption("keepUnknownTags");
|
|
430
|
+
if (opts.noBreakscaping) unsupportedOption("noBreakscaping");
|
|
431
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
432
|
+
const wasm2 = getLegacyWasm();
|
|
433
|
+
const inStr = typeof input === "string" ? this.maybeReadFile(input, void 0) : "";
|
|
434
|
+
const isHtmlInput = opts.inputFormat === "html" || opts.inputFormat === void 0 && typeof input === "string" && /<table[\s>]/iu.test(inStr);
|
|
435
|
+
if (!isHtmlInput) {
|
|
436
|
+
const { source, isBitmark } = this.resolveInput(input, void 0);
|
|
437
|
+
const out2 = wasm2.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1);
|
|
438
|
+
this.throwOnError(out2);
|
|
439
|
+
return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
|
|
440
|
+
}
|
|
441
|
+
const bitType = opts.tableFormat === "table" ? "table" : "table-extended";
|
|
442
|
+
const wrapped = extractHtmlTables(inStr).map((t) => `<bitmark-bit data-type="${bitType}" data-format="bitmark++">${t}</bitmark-bit>`).join("");
|
|
443
|
+
const outputFormat = opts.outputFormat ?? "bitmark";
|
|
444
|
+
const { pretty, indent } = prettyIndent(jsonOptions.prettify);
|
|
445
|
+
const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
|
|
446
|
+
if (outputFormat === "html") {
|
|
447
|
+
const out2 = wasm2.convert(wrapped, "html", "html", "optimized", false, 2, 1);
|
|
448
|
+
this.throwOnError(out2);
|
|
449
|
+
return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
|
|
450
|
+
}
|
|
451
|
+
const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
|
|
452
|
+
const out = wasm2.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces);
|
|
453
|
+
this.throwOnError(out);
|
|
454
|
+
if (outputFormat === "bitmark" || outputFormat === "text") {
|
|
455
|
+
return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
456
|
+
}
|
|
457
|
+
if (outputFormat === "json") {
|
|
458
|
+
if (opts.outputFile) return this.deliver(out, opts.outputFile, opts.fileOptions);
|
|
459
|
+
if (pretty || jsonOptions.stringify) return out;
|
|
460
|
+
return JSON.parse(out);
|
|
461
|
+
}
|
|
462
|
+
const ast = makePseudoAst(JSON.parse(out));
|
|
463
|
+
if (opts.outputFile) return this.deliver(JSON.stringify(ast), opts.outputFile, opts.fileOptions);
|
|
464
|
+
return ast;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Create a bit-JSON pseudo-AST from bitmark or JSON input (PLAN-126 §3.1).
|
|
468
|
+
*
|
|
469
|
+
* NOTE: the returned structure is the bit JSON (wrapper entries), not
|
|
470
|
+
* bpg's internal TS AST node model. It round-trips through `convert` but
|
|
471
|
+
* is not walkable with bpg's `Ast`/`NodeType`.
|
|
472
|
+
*/
|
|
473
|
+
createAst(input, options) {
|
|
474
|
+
const opts = Object.assign({}, options);
|
|
475
|
+
const wasm2 = getLegacyWasm();
|
|
476
|
+
const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
|
|
477
|
+
const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1);
|
|
478
|
+
this.throwOnError(out);
|
|
479
|
+
return makePseudoAst(JSON.parse(out));
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Breakscape text (bpg-compatible `breakscapeText`).
|
|
483
|
+
*/
|
|
484
|
+
breakscapeText(input, options) {
|
|
485
|
+
if (!input) return input;
|
|
486
|
+
const opts = Object.assign({}, options);
|
|
487
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
488
|
+
const wasm2 = getLegacyWasm();
|
|
489
|
+
const inStr = this.maybeReadFile(input, opts.inputFormat);
|
|
490
|
+
const res = wasm2.breakscape_text(inStr, mapTextFormat(opts.textFormat), opts.textLocation ?? "body");
|
|
491
|
+
return this.deliver(res, opts.outputFile, opts.fileOptions);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Unbreakscape text (bpg-compatible `unbreakscapeText`).
|
|
495
|
+
*/
|
|
496
|
+
unbreakscapeText(input, options) {
|
|
497
|
+
if (!input) return input;
|
|
498
|
+
const opts = Object.assign({}, options);
|
|
499
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
500
|
+
const wasm2 = getLegacyWasm();
|
|
501
|
+
const inStr = this.maybeReadFile(input, opts.inputFormat);
|
|
502
|
+
const res = wasm2.unbreakscape_text(inStr, mapTextFormat(opts.textFormat), opts.textLocation ?? "body");
|
|
503
|
+
return this.deliver(res, opts.outputFile, opts.fileOptions);
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Convert bitmark text ↔ text AST (bpg-compatible `convertText`).
|
|
507
|
+
*
|
|
508
|
+
* Direction is auto-detected: TextAst (JSON) input generates bitmark
|
|
509
|
+
* text; anything else parses as bitmark text and returns the TextAst
|
|
510
|
+
* (plain JS object; a string when `jsonOptions.prettify` is set).
|
|
511
|
+
*/
|
|
512
|
+
convertText(input, options) {
|
|
513
|
+
const opts = Object.assign({}, options);
|
|
514
|
+
const jsonOptions = Object.assign({}, opts.jsonOptions);
|
|
515
|
+
this.checkBrowserFileWrite(opts.outputFile);
|
|
516
|
+
const wasm2 = getLegacyWasm();
|
|
517
|
+
const isBitmarkText = opts.textFormat === void 0 || mapTextFormat(opts.textFormat) === "bitmark++";
|
|
518
|
+
const textLocation = opts.textLocation ?? "body";
|
|
519
|
+
let inStr;
|
|
520
|
+
let ast;
|
|
521
|
+
if (typeof input === "string") {
|
|
522
|
+
inStr = this.maybeReadFile(input, opts.inputFormat);
|
|
523
|
+
ast = this.parseTextAstString(inStr);
|
|
524
|
+
} else {
|
|
525
|
+
inStr = JSON.stringify(input);
|
|
526
|
+
ast = Array.isArray(input) ? input : void 0;
|
|
527
|
+
}
|
|
528
|
+
if (ast !== void 0) {
|
|
529
|
+
const out2 = isBitmarkText ? wasm2.convert_text(JSON.stringify(ast), "astToText", "bitmark++", textLocation) : wasm2.convert_text(JSON.stringify(ast), "astToPlainText", "plainText", textLocation);
|
|
530
|
+
this.throwOnError(out2);
|
|
531
|
+
return this.deliver(out2, opts.outputFile, opts.fileOptions);
|
|
532
|
+
}
|
|
533
|
+
const out = wasm2.convert_text(inStr, "textToAst", isBitmarkText ? "bitmark++" : "plainText", "body");
|
|
534
|
+
this.throwOnError(out);
|
|
535
|
+
const { pretty, indent } = prettyIndent(jsonOptions.prettify);
|
|
536
|
+
const outStr = pretty ? wasm2.prettify(out, indent) : out;
|
|
537
|
+
if (opts.outputFile) return this.deliver(outStr, opts.outputFile, opts.fileOptions);
|
|
538
|
+
if (pretty) return outStr;
|
|
539
|
+
return JSON.parse(out);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Convert a text AST to plain text (bpg-compatible `textAstToPlainText`).
|
|
543
|
+
*/
|
|
544
|
+
textAstToPlainText(textAst, _options) {
|
|
545
|
+
const wasm2 = getLegacyWasm();
|
|
546
|
+
const out = wasm2.convert_text(
|
|
547
|
+
typeof textAst === "string" ? textAst : JSON.stringify(textAst),
|
|
548
|
+
"astToPlainText",
|
|
549
|
+
"bitmark++",
|
|
550
|
+
"body"
|
|
551
|
+
);
|
|
552
|
+
this.throwOnError(out);
|
|
553
|
+
return out;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Extract plain text from bitmark or bitmark JSON (bpg-compatible).
|
|
557
|
+
*/
|
|
558
|
+
extractPlainText(input, options) {
|
|
559
|
+
const inputFormat = options?.inputFormat;
|
|
560
|
+
const wasm2 = getLegacyWasm();
|
|
561
|
+
if (inputFormat === "plainText") {
|
|
562
|
+
return typeof input === "string" ? String(input) : "";
|
|
563
|
+
}
|
|
564
|
+
if (inputFormat === "bitmarkText") {
|
|
565
|
+
const ast = wasm2.convert_text(String(input), "textToAst", "bitmark++", "body");
|
|
566
|
+
const out2 = wasm2.convert_text(ast, "astToPlainText", "bitmark++", "body");
|
|
567
|
+
this.throwOnError(out2);
|
|
568
|
+
return out2;
|
|
569
|
+
}
|
|
570
|
+
if (typeof input === "string" && this.parseTextAstString(input) === void 0 && !wasm2.is_bitmark(input)) {
|
|
571
|
+
const looksJson = (() => {
|
|
572
|
+
try {
|
|
573
|
+
JSON.parse(input);
|
|
574
|
+
return true;
|
|
575
|
+
} catch {
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
})();
|
|
579
|
+
if (!looksJson) {
|
|
580
|
+
return this.extractPlainText(input, { inputFormat: "bitmarkText" });
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
const { source, isBitmark } = this.resolveInput(input, void 0);
|
|
584
|
+
const out = wasm2.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1);
|
|
585
|
+
this.throwOnError(out);
|
|
586
|
+
return out;
|
|
587
|
+
}
|
|
588
|
+
/** If `s` parses as a TextAst JSON array, return it. */
|
|
589
|
+
parseTextAstString(s) {
|
|
590
|
+
const t = s.trim();
|
|
591
|
+
if (!t.startsWith("[")) return void 0;
|
|
592
|
+
try {
|
|
593
|
+
const v = JSON.parse(t);
|
|
594
|
+
if (Array.isArray(v) && v.every((n) => n !== null && typeof n === "object" && "type" in n)) {
|
|
595
|
+
return v;
|
|
596
|
+
}
|
|
597
|
+
} catch {
|
|
598
|
+
}
|
|
599
|
+
return void 0;
|
|
600
|
+
}
|
|
601
|
+
// ------------------------------------------------------------------
|
|
602
|
+
// Internal helpers
|
|
603
|
+
// ------------------------------------------------------------------
|
|
604
|
+
/**
|
|
605
|
+
* Resolve bpg input (string | file path | JSON value | pseudo-AST) to a
|
|
606
|
+
* source string for the WASM core, classified per bpg's detection order:
|
|
607
|
+
* AST → JSON → assume bitmark (PLAN-126 F-10).
|
|
608
|
+
*/
|
|
609
|
+
resolveInput(input, inputFormat) {
|
|
610
|
+
if (typeof input !== "string") {
|
|
611
|
+
const entries = this.jsonEntries(input);
|
|
612
|
+
return {
|
|
613
|
+
source: JSON.stringify(entries ? entries.map((b) => this.toWrapperEntry(b)) : input),
|
|
614
|
+
isBitmark: false
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
const inStr = this.maybeReadFile(input, inputFormat);
|
|
618
|
+
const wasm2 = getLegacyWasm();
|
|
619
|
+
if (wasm2.is_bitmark(inStr)) return { source: inStr, isBitmark: true };
|
|
620
|
+
try {
|
|
621
|
+
const parsed = JSON.parse(inStr);
|
|
622
|
+
const entries = this.jsonEntries(parsed);
|
|
623
|
+
if (entries) {
|
|
624
|
+
return {
|
|
625
|
+
source: JSON.stringify(entries.map((b) => this.toWrapperEntry(b))),
|
|
626
|
+
isBitmark: false
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
} catch {
|
|
630
|
+
return { source: inStr, isBitmark: true };
|
|
631
|
+
}
|
|
632
|
+
return { source: inStr, isBitmark: false };
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Normalise the JSON shapes bpg accepts to a bits/entries array:
|
|
636
|
+
* wrapper array, `{ bits: [...] }` AST envelope, single `{ bit: ... }`
|
|
637
|
+
* wrapper, or a bare bit object (`{ type: ... }`).
|
|
638
|
+
*/
|
|
639
|
+
jsonEntries(v) {
|
|
640
|
+
if (Array.isArray(v)) return v;
|
|
641
|
+
if (v !== null && typeof v === "object") {
|
|
642
|
+
const o = v;
|
|
643
|
+
if (Array.isArray(o.bits)) return o.bits;
|
|
644
|
+
if ("bit" in o) return [o];
|
|
645
|
+
if (typeof o.type === "string") return [{ bit: o }];
|
|
646
|
+
}
|
|
647
|
+
return void 0;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Normalise a pseudo-AST bits entry to a `{ bit, parser? }` wrapper.
|
|
651
|
+
* `Builder.buildBit` returns bare bit JSON (with any parser metadata
|
|
652
|
+
* attached under a non-enumerable symbol); parsed forward output is
|
|
653
|
+
* already wrapper-shaped.
|
|
654
|
+
*/
|
|
655
|
+
toWrapperEntry(entry) {
|
|
656
|
+
if (entry !== null && typeof entry === "object" && "bit" in entry) return entry;
|
|
657
|
+
const wrapper = { bit: entry };
|
|
658
|
+
const parser = entry?.[BUILT_BIT_PARSER];
|
|
659
|
+
if (parser !== void 0) wrapper.parser = parser;
|
|
660
|
+
return wrapper;
|
|
661
|
+
}
|
|
662
|
+
/** bpg parity: read `input` as a file when it names one (Node only). */
|
|
663
|
+
maybeReadFile(input, inputFormat) {
|
|
664
|
+
if (inputFormat === "string") return input;
|
|
665
|
+
const io = getLegacyFileIo();
|
|
666
|
+
if (!io) return input;
|
|
667
|
+
try {
|
|
668
|
+
if (io.existsSync(input)) return io.readFileSync(input, "utf8");
|
|
669
|
+
} catch {
|
|
670
|
+
}
|
|
671
|
+
return input;
|
|
672
|
+
}
|
|
673
|
+
/** bpg parity: writing a file is impossible in the browser. */
|
|
674
|
+
checkBrowserFileWrite(outputFile) {
|
|
675
|
+
if (outputFile && !getLegacyFileIo()) {
|
|
676
|
+
throw new Error("Cannot write to file in browser environment");
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
/** Return `data`, or write it to `outputFile` (returning void), as bpg does. */
|
|
680
|
+
deliver(data, outputFile, fileOptions) {
|
|
681
|
+
if (!outputFile) return data;
|
|
682
|
+
const io = getLegacyFileIo();
|
|
683
|
+
if (!io) throw new Error("Cannot write to file in browser environment");
|
|
684
|
+
io.writeFile(outputFile, data, {
|
|
685
|
+
append: fileOptions?.append,
|
|
686
|
+
encoding: fileOptions?.encoding ?? "utf8"
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
/** The core reports failures as `error:`-prefixed strings; bpg throws. */
|
|
690
|
+
throwOnError(out) {
|
|
691
|
+
if (out.startsWith("error:")) {
|
|
692
|
+
throw new Error(out.slice("error:".length).trim());
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
function extractHtmlTables(html) {
|
|
697
|
+
const tables = [];
|
|
698
|
+
const lower = html.toLowerCase();
|
|
699
|
+
let i = 0;
|
|
700
|
+
for (; ; ) {
|
|
701
|
+
const start = lower.indexOf("<table", i);
|
|
702
|
+
if (start === -1) break;
|
|
703
|
+
let depth = 0;
|
|
704
|
+
let j = start;
|
|
705
|
+
let end = -1;
|
|
706
|
+
while (j < html.length) {
|
|
707
|
+
const open = lower.indexOf("<table", j);
|
|
708
|
+
const close = lower.indexOf("</table", j);
|
|
709
|
+
if (close === -1) break;
|
|
710
|
+
if (open !== -1 && open < close) {
|
|
711
|
+
depth++;
|
|
712
|
+
j = open + "<table".length;
|
|
713
|
+
} else {
|
|
714
|
+
depth--;
|
|
715
|
+
const closeEnd = lower.indexOf(">", close);
|
|
716
|
+
j = closeEnd === -1 ? html.length : closeEnd + 1;
|
|
717
|
+
if (depth === 0) {
|
|
718
|
+
end = j;
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
if (end === -1) break;
|
|
724
|
+
tables.push(html.slice(start, end));
|
|
725
|
+
i = end;
|
|
726
|
+
}
|
|
727
|
+
return tables;
|
|
728
|
+
}
|
|
729
|
+
function bitmarkTextParse(text) {
|
|
730
|
+
const out = getLegacyWasm().convert_text(text, "textToAst", "bitmark++", "body");
|
|
731
|
+
if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
|
|
732
|
+
return JSON.parse(out);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// src/legacy/resource-builder.ts
|
|
736
|
+
var RESOURCE_SPECS = {
|
|
737
|
+
imageResource: { type: "image", key: "image", valueField: "src" },
|
|
738
|
+
imageLinkResource: { type: "image-link", key: "imageLink", valueField: "url" },
|
|
739
|
+
audioResource: { type: "audio", key: "audio", valueField: "src" },
|
|
740
|
+
audioEmbedResource: { type: "audio-embed", key: "audioEmbed", valueField: "src" },
|
|
741
|
+
audioLinkResource: { type: "audio-link", key: "audioLink", valueField: "url" },
|
|
742
|
+
videoResource: { type: "video", key: "video", valueField: "src" },
|
|
743
|
+
videoEmbedResource: { type: "video-embed", key: "videoEmbed", valueField: "url" },
|
|
744
|
+
videoLinkResource: { type: "video-link", key: "videoLink", valueField: "url" },
|
|
745
|
+
stillImageFilmEmbedResource: {
|
|
746
|
+
type: "still-image-film-embed",
|
|
747
|
+
key: "stillImageFilmEmbed",
|
|
748
|
+
valueField: "url"
|
|
749
|
+
},
|
|
750
|
+
stillImageFilmLinkResource: {
|
|
751
|
+
type: "still-image-film-link",
|
|
752
|
+
key: "stillImageFilmLink",
|
|
753
|
+
valueField: "url"
|
|
754
|
+
},
|
|
755
|
+
articleResource: { type: "article", key: "article", valueField: "body" },
|
|
756
|
+
documentResource: { type: "document", key: "document", valueField: "url" },
|
|
757
|
+
documentEmbedResource: { type: "document-embed", key: "documentEmbed", valueField: "url" },
|
|
758
|
+
documentLinkResource: { type: "document-link", key: "documentLink", valueField: "url" },
|
|
759
|
+
documentDownloadResource: {
|
|
760
|
+
type: "document-download",
|
|
761
|
+
key: "documentDownload",
|
|
762
|
+
valueField: "url"
|
|
763
|
+
},
|
|
764
|
+
appLinkResource: { type: "app-link", key: "appLink", valueField: "url" },
|
|
765
|
+
websiteLinkResource: { type: "website-link", key: "websiteLink", valueField: "url" }
|
|
766
|
+
};
|
|
767
|
+
function buildResource(spec) {
|
|
768
|
+
return (_context, data, _typeAlias) => {
|
|
769
|
+
if (!data) return void 0;
|
|
770
|
+
const { value, ...rest } = data;
|
|
771
|
+
const inner = { [spec.valueField]: value ?? "" };
|
|
772
|
+
for (const [k, v] of Object.entries(rest)) {
|
|
773
|
+
if (v !== void 0) inner[k] = v;
|
|
774
|
+
}
|
|
775
|
+
return { type: spec.type, [spec.key]: inner };
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
var SPEC_BY_TYPE = Object.fromEntries(
|
|
779
|
+
Object.values(RESOURCE_SPECS).map((s) => [s.type, s])
|
|
780
|
+
);
|
|
781
|
+
var ResourceBuilder = class {
|
|
782
|
+
/**
|
|
783
|
+
* Normalise existing ResourceJson wrapper(s) (bpg-compatible).
|
|
784
|
+
* Unknown resource types are dropped (bpg parity: it skips invalid types).
|
|
785
|
+
*/
|
|
786
|
+
resourceFromResourceJson(_context, resource) {
|
|
787
|
+
if (!resource) return void 0;
|
|
788
|
+
const single = !Array.isArray(resource);
|
|
789
|
+
const list = single ? [resource] : resource;
|
|
790
|
+
const nodes = [];
|
|
791
|
+
for (const wrapper of list) {
|
|
792
|
+
const spec = SPEC_BY_TYPE[wrapper.type];
|
|
793
|
+
if (!spec) continue;
|
|
794
|
+
const inner = wrapper[spec.key] ?? {};
|
|
795
|
+
const { [spec.valueField]: value, ...rest } = inner;
|
|
796
|
+
const built = buildResource(spec)(void 0, { value, ...rest });
|
|
797
|
+
if (built) nodes.push(built);
|
|
798
|
+
}
|
|
799
|
+
if (nodes.length === 0) return void 0;
|
|
800
|
+
return single ? nodes[0] : nodes;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Build a resource wrapper from a bare ResourceDataJson (bpg-compatible).
|
|
804
|
+
* `data` may also be a plain string (used as the url/src/body value).
|
|
805
|
+
*/
|
|
806
|
+
resourceFromResourceDataJson(context, type, data) {
|
|
807
|
+
if (!data) return void 0;
|
|
808
|
+
const spec = SPEC_BY_TYPE[type];
|
|
809
|
+
if (!spec) return void 0;
|
|
810
|
+
const asString = typeof data === "string" ? data : void 0;
|
|
811
|
+
const obj = typeof data === "object" ? data : {};
|
|
812
|
+
const { url, src, body, ...rest } = obj;
|
|
813
|
+
const value = url ?? src ?? body ?? asString;
|
|
814
|
+
return buildResource(spec)(context, { value, ...rest });
|
|
815
|
+
}
|
|
816
|
+
imageResource = buildResource(RESOURCE_SPECS.imageResource);
|
|
817
|
+
imageLinkResource = buildResource(RESOURCE_SPECS.imageLinkResource);
|
|
818
|
+
audioResource = buildResource(RESOURCE_SPECS.audioResource);
|
|
819
|
+
audioEmbedResource = buildResource(RESOURCE_SPECS.audioEmbedResource);
|
|
820
|
+
audioLinkResource = buildResource(RESOURCE_SPECS.audioLinkResource);
|
|
821
|
+
videoResource = buildResource(RESOURCE_SPECS.videoResource);
|
|
822
|
+
videoEmbedResource = buildResource(RESOURCE_SPECS.videoEmbedResource);
|
|
823
|
+
videoLinkResource = buildResource(RESOURCE_SPECS.videoLinkResource);
|
|
824
|
+
stillImageFilmEmbedResource = buildResource(
|
|
825
|
+
RESOURCE_SPECS.stillImageFilmEmbedResource
|
|
826
|
+
);
|
|
827
|
+
stillImageFilmLinkResource = buildResource(
|
|
828
|
+
RESOURCE_SPECS.stillImageFilmLinkResource
|
|
829
|
+
);
|
|
830
|
+
articleResource = buildResource(RESOURCE_SPECS.articleResource);
|
|
831
|
+
documentResource = buildResource(RESOURCE_SPECS.documentResource);
|
|
832
|
+
documentEmbedResource = buildResource(RESOURCE_SPECS.documentEmbedResource);
|
|
833
|
+
documentLinkResource = buildResource(RESOURCE_SPECS.documentLinkResource);
|
|
834
|
+
documentDownloadResource = buildResource(
|
|
835
|
+
RESOURCE_SPECS.documentDownloadResource
|
|
836
|
+
);
|
|
837
|
+
appLinkResource = buildResource(RESOURCE_SPECS.appLinkResource);
|
|
838
|
+
websiteLinkResource = buildResource(RESOURCE_SPECS.websiteLinkResource);
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
// src/legacy/options.ts
|
|
842
|
+
var Input = {
|
|
843
|
+
/** Input is as a string */
|
|
844
|
+
string: "string",
|
|
845
|
+
/** Input is as a file path */
|
|
846
|
+
file: "file"
|
|
847
|
+
};
|
|
848
|
+
var Output = {
|
|
849
|
+
/** Output bitmark string */
|
|
850
|
+
bitmark: "bitmark",
|
|
851
|
+
/** Output JSON as a plain JS object, or a file */
|
|
852
|
+
json: "json",
|
|
853
|
+
/** Output AST as a plain JS object, or a file */
|
|
854
|
+
ast: "ast",
|
|
855
|
+
/** Output plain text as a string, or a file */
|
|
856
|
+
text: "text"
|
|
857
|
+
};
|
|
858
|
+
var InputFormat = {
|
|
859
|
+
bitmark: "bitmark",
|
|
860
|
+
bitmarkText: "bitmarkText",
|
|
861
|
+
plainText: "plainText",
|
|
862
|
+
html: "html"
|
|
863
|
+
};
|
|
864
|
+
var BitmarkVersion = {
|
|
865
|
+
v2: 2,
|
|
866
|
+
v3: 3
|
|
867
|
+
};
|
|
868
|
+
var CardSetVersion = {
|
|
869
|
+
v1: 1,
|
|
870
|
+
v2: 2
|
|
871
|
+
};
|
|
872
|
+
var BitmarkParserType = {
|
|
873
|
+
peggy: "peggy"
|
|
874
|
+
};
|
|
875
|
+
var InfoType = {
|
|
876
|
+
list: "list",
|
|
877
|
+
deprecated: "deprecated",
|
|
878
|
+
all: "all",
|
|
879
|
+
bit: "bit"
|
|
880
|
+
};
|
|
881
|
+
var InfoFormat = {
|
|
882
|
+
text: "text",
|
|
883
|
+
json: "json",
|
|
884
|
+
pojo: "pojo"
|
|
885
|
+
};
|
|
886
|
+
var BodyTextFormat = {
|
|
887
|
+
plainText: "text",
|
|
888
|
+
latex: "latex",
|
|
889
|
+
json: "json",
|
|
890
|
+
xml: "xml",
|
|
891
|
+
bitmarkPlusPlus: "bitmark++"
|
|
892
|
+
};
|
|
893
|
+
var TextLocation = {
|
|
894
|
+
tag: "tag",
|
|
895
|
+
body: "body"
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
// src/legacy/enums-extra.ts
|
|
899
|
+
var TextMarkType = {
|
|
900
|
+
bold: "bold",
|
|
901
|
+
light: "light",
|
|
902
|
+
italic: "italic",
|
|
903
|
+
highlight: "highlight",
|
|
904
|
+
highlightOrange: "highlightOrange",
|
|
905
|
+
highlightYellow: "highlightYellow",
|
|
906
|
+
highlightGreen: "highlightGreen",
|
|
907
|
+
highlightBlue: "highlightBlue",
|
|
908
|
+
highlightPurple: "highlightPurple",
|
|
909
|
+
highlightPink: "highlightPink",
|
|
910
|
+
highlightBrown: "highlightBrown",
|
|
911
|
+
highlightBlack: "highlightBlack",
|
|
912
|
+
highlightWhite: "highlightWhite",
|
|
913
|
+
highlightGray: "highlightGray",
|
|
914
|
+
strike: "strike",
|
|
915
|
+
subscript: "subscript",
|
|
916
|
+
superscript: "superscript",
|
|
917
|
+
ins: "ins",
|
|
918
|
+
del: "del",
|
|
919
|
+
underline: "underline",
|
|
920
|
+
doubleUnderline: "doubleUnderline",
|
|
921
|
+
circle: "circle",
|
|
922
|
+
languageEmRed: "languageEmRed",
|
|
923
|
+
languageEmOrange: "languageEmOrange",
|
|
924
|
+
languageEmYellow: "languageEmYellow",
|
|
925
|
+
languageEmGreen: "languageEmGreen",
|
|
926
|
+
languageEmBlue: "languageEmBlue",
|
|
927
|
+
languageEmPurple: "languageEmPurple",
|
|
928
|
+
languageEmPink: "languageEmPink",
|
|
929
|
+
languageEmBrown: "languageEmBrown",
|
|
930
|
+
languageEmBlack: "languageEmBlack",
|
|
931
|
+
languageEmWhite: "languageEmWhite",
|
|
932
|
+
languageEmGray: "languageEmGray",
|
|
933
|
+
languageEm: "languageEm",
|
|
934
|
+
userUnderline: "userUnderline",
|
|
935
|
+
userDoubleUnderline: "userDoubleUnderline",
|
|
936
|
+
userStrike: "userStrike",
|
|
937
|
+
userCircle: "userCircle",
|
|
938
|
+
userHighlight: "userHighlight",
|
|
939
|
+
notranslate: "notranslate",
|
|
940
|
+
link: "link",
|
|
941
|
+
ref: "ref",
|
|
942
|
+
xref: "xref",
|
|
943
|
+
extref: "extref",
|
|
944
|
+
footnote: "footnote",
|
|
945
|
+
footnoteStar: "footnote*",
|
|
946
|
+
symbol: "symbol",
|
|
947
|
+
var: "var",
|
|
948
|
+
code: "code",
|
|
949
|
+
timer: "timer",
|
|
950
|
+
duration: "duration",
|
|
951
|
+
color: "color",
|
|
952
|
+
colorPicker: "colorPicker",
|
|
953
|
+
comment: "comment"
|
|
954
|
+
};
|
|
955
|
+
var TextNodeType = {
|
|
956
|
+
text: "text",
|
|
957
|
+
hardBreak: "hardBreak",
|
|
958
|
+
paragraph: "paragraph",
|
|
959
|
+
heading: "heading",
|
|
960
|
+
section: "section",
|
|
961
|
+
noBulletList: "noBulletList",
|
|
962
|
+
bulletList: "bulletList",
|
|
963
|
+
orderedList: "orderedList",
|
|
964
|
+
orderedListRoman: "orderedListRoman",
|
|
965
|
+
orderedListRomanLower: "orderedListRomanLower",
|
|
966
|
+
letteredList: "letteredList",
|
|
967
|
+
letteredListLower: "letteredListLower",
|
|
968
|
+
taskList: "taskList",
|
|
969
|
+
listItem: "listItem",
|
|
970
|
+
taskItem: "taskItem",
|
|
971
|
+
image: "image",
|
|
972
|
+
imageInline: "imageInline",
|
|
973
|
+
codeBlock: "codeBlock",
|
|
974
|
+
latex: "latex",
|
|
975
|
+
gap: "gap",
|
|
976
|
+
select: "select",
|
|
977
|
+
highlight: "highlight",
|
|
978
|
+
mark: "mark"
|
|
979
|
+
};
|
|
980
|
+
function bitNameToKey(name) {
|
|
981
|
+
return name.replace(/-([a-z0-9])/gu, (_, c) => c.toUpperCase());
|
|
982
|
+
}
|
|
983
|
+
var bitTypeCache;
|
|
984
|
+
function loadBitTypes() {
|
|
985
|
+
if (!bitTypeCache) {
|
|
986
|
+
const wasm2 = getLegacyWasm();
|
|
987
|
+
const all = JSON.parse(wasm2.info("all", "json", "", false, 2));
|
|
988
|
+
const map = { _standard: "_standard" };
|
|
989
|
+
for (const { name } of all) {
|
|
990
|
+
map[bitNameToKey(name)] = name;
|
|
991
|
+
}
|
|
992
|
+
bitTypeCache = map;
|
|
993
|
+
}
|
|
994
|
+
return bitTypeCache;
|
|
995
|
+
}
|
|
996
|
+
var BitType = new Proxy(
|
|
997
|
+
{},
|
|
998
|
+
{
|
|
999
|
+
get: (_t, prop) => typeof prop === "string" ? loadBitTypes()[prop] : void 0,
|
|
1000
|
+
has: (_t, prop) => typeof prop === "string" && prop in loadBitTypes(),
|
|
1001
|
+
ownKeys: () => Reflect.ownKeys(loadBitTypes()),
|
|
1002
|
+
getOwnPropertyDescriptor: (_t, prop) => typeof prop === "string" && prop in loadBitTypes() ? { value: loadBitTypes()[prop], enumerable: true, configurable: true, writable: false } : void 0
|
|
1003
|
+
}
|
|
1004
|
+
);
|
|
1005
|
+
function unsupportedEnum(name, reason) {
|
|
1006
|
+
return new Proxy(
|
|
1007
|
+
{},
|
|
1008
|
+
{
|
|
1009
|
+
get: (_t, prop) => {
|
|
1010
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === Symbol.toStringTag) {
|
|
1011
|
+
return () => `[unsupported ${name}]`;
|
|
1012
|
+
}
|
|
1013
|
+
return unsupportedMember(name, reason);
|
|
1014
|
+
},
|
|
1015
|
+
has: () => unsupportedMember(name, reason),
|
|
1016
|
+
ownKeys: () => unsupportedMember(name, reason)
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
var NodeType = unsupportedEnum(
|
|
1021
|
+
"NodeType",
|
|
1022
|
+
"it names bpg's internal AST nodes; the legacy AST is bit JSON (see createAst)"
|
|
1023
|
+
);
|
|
1024
|
+
var BoundingBoxIndex = unsupportedEnum(
|
|
1025
|
+
"BoundingBoxIndex",
|
|
1026
|
+
"bpg-internal bounding-box utility"
|
|
1027
|
+
);
|
|
1028
|
+
|
|
1029
|
+
// src/legacy/stubs.ts
|
|
1030
|
+
function stubClass(name, reason) {
|
|
1031
|
+
return class {
|
|
1032
|
+
constructor() {
|
|
1033
|
+
unsupportedMember(name, reason);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
var WALKER_REASON = "the legacy AST is bit JSON, not bpg's internal node model; walking it with bpg NodeType names would silently misbehave";
|
|
1038
|
+
var INTERNAL_REASON = "internal class of bpg's TS implementation";
|
|
1039
|
+
var Ast = stubClass("Ast", WALKER_REASON);
|
|
1040
|
+
var BitmarkGenerator = stubClass("BitmarkGenerator", INTERNAL_REASON);
|
|
1041
|
+
var BitmarkStringGenerator = stubClass("BitmarkStringGenerator", INTERNAL_REASON);
|
|
1042
|
+
var BitmarkFileGenerator = stubClass("BitmarkFileGenerator", INTERNAL_REASON);
|
|
1043
|
+
var JsonGenerator = stubClass("JsonGenerator", INTERNAL_REASON);
|
|
1044
|
+
var JsonStringGenerator = stubClass("JsonStringGenerator", INTERNAL_REASON);
|
|
1045
|
+
var JsonFileGenerator = stubClass("JsonFileGenerator", INTERNAL_REASON);
|
|
1046
|
+
var HtmlTableGenerator = stubClass("HtmlTableGenerator", INTERNAL_REASON);
|
|
1047
|
+
var HtmlTableParser = stubClass("HtmlTableParser", INTERNAL_REASON);
|
|
1048
|
+
var BitmarkParser = stubClass("BitmarkParser", INTERNAL_REASON);
|
|
1049
|
+
var JsonParser = stubClass("JsonParser", INTERNAL_REASON);
|
|
1050
|
+
var StringWriter = stubClass("StringWriter", INTERNAL_REASON);
|
|
1051
|
+
var FileWriter = stubClass("FileWriter", INTERNAL_REASON);
|
|
1052
|
+
var StreamWriter = stubClass("StreamWriter", INTERNAL_REASON);
|
|
1053
|
+
|
|
1054
|
+
// src/legacy-entry.ts
|
|
1055
|
+
var require2 = createRequire(import.meta.url);
|
|
1056
|
+
var wasm = require2("../wasm/bitmark_wasm.js");
|
|
1057
|
+
initLegacyNode(wasm);
|
|
1058
|
+
export {
|
|
1059
|
+
Ast,
|
|
1060
|
+
BitType,
|
|
1061
|
+
BitmarkFileGenerator,
|
|
1062
|
+
BitmarkGenerator,
|
|
1063
|
+
BitmarkParser,
|
|
1064
|
+
BitmarkParserGenerator,
|
|
1065
|
+
BitmarkParserType,
|
|
1066
|
+
BitmarkStringGenerator,
|
|
1067
|
+
BitmarkVersion,
|
|
1068
|
+
BodyTextFormat,
|
|
1069
|
+
BoundingBoxIndex,
|
|
1070
|
+
Builder,
|
|
1071
|
+
CardSetVersion,
|
|
1072
|
+
FileWriter,
|
|
1073
|
+
HtmlTableGenerator,
|
|
1074
|
+
HtmlTableParser,
|
|
1075
|
+
InfoFormat,
|
|
1076
|
+
InfoType,
|
|
1077
|
+
Input,
|
|
1078
|
+
InputFormat,
|
|
1079
|
+
JsonFileGenerator,
|
|
1080
|
+
JsonGenerator,
|
|
1081
|
+
JsonParser,
|
|
1082
|
+
JsonStringGenerator,
|
|
1083
|
+
LegacyUnsupportedError,
|
|
1084
|
+
NodeType,
|
|
1085
|
+
Output,
|
|
1086
|
+
ResourceBuilder,
|
|
1087
|
+
StreamWriter,
|
|
1088
|
+
StringWriter,
|
|
1089
|
+
TextLocation,
|
|
1090
|
+
TextMarkType,
|
|
1091
|
+
TextNodeType,
|
|
1092
|
+
bitmarkTextParse,
|
|
1093
|
+
isPseudoAst
|
|
1094
|
+
};
|
|
1095
|
+
//# sourceMappingURL=legacy.js.map
|