md4x 0.0.12 → 0.0.16
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/lib/wasm/common.mjs +87 -0
- package/lib/wasm/defult.mjs +50 -0
- package/lib/wasm/unwasm.mjs +22 -0
- package/package.json +7 -4
- package/lib/unwasm.mjs +0 -19
- package/lib/wasm.mjs +0 -111
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// --- internal ---
|
|
2
|
+
|
|
3
|
+
let _instance;
|
|
4
|
+
|
|
5
|
+
export function _hasInstance() {
|
|
6
|
+
return !!_instance;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function _setInstance(instance) {
|
|
10
|
+
_instance = instance;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function _getExports() {
|
|
14
|
+
if (!_instance?.exports) {
|
|
15
|
+
throw new Error("md4x: WASM not initialized. Call `await init()` first.", {
|
|
16
|
+
cause: { instance: _instance },
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return _instance.exports;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const _imports = {
|
|
23
|
+
wasi_snapshot_preview1: {
|
|
24
|
+
fd_close: () => 0,
|
|
25
|
+
fd_seek: () => 0,
|
|
26
|
+
fd_write: () => 0,
|
|
27
|
+
proc_exit: () => {},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function render(exports, fn, input, ...extra) {
|
|
32
|
+
const { memory, md4x_alloc, md4x_free, md4x_result_ptr, md4x_result_size } =
|
|
33
|
+
exports;
|
|
34
|
+
const encoded = new TextEncoder().encode(input);
|
|
35
|
+
const ptr = md4x_alloc(encoded.length);
|
|
36
|
+
new Uint8Array(memory.buffer).set(encoded, ptr);
|
|
37
|
+
const ret = fn(ptr, encoded.length, ...extra);
|
|
38
|
+
md4x_free(ptr);
|
|
39
|
+
if (ret !== 0) {
|
|
40
|
+
throw new Error("md4x: render failed");
|
|
41
|
+
}
|
|
42
|
+
const outPtr = md4x_result_ptr();
|
|
43
|
+
const outSize = md4x_result_size();
|
|
44
|
+
const result = new TextDecoder().decode(
|
|
45
|
+
new Uint8Array(memory.buffer, outPtr, outSize),
|
|
46
|
+
);
|
|
47
|
+
md4x_free(outPtr);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function renderToHtml(input, opts) {
|
|
52
|
+
const flags = opts?.full ? 0x0008 : 0;
|
|
53
|
+
const exports = _getExports();
|
|
54
|
+
return render(exports, exports.md4x_to_html, input, flags);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function renderToAST(input) {
|
|
58
|
+
const exports = _getExports();
|
|
59
|
+
return render(exports, exports.md4x_to_ast, input);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function parseAST(input) {
|
|
63
|
+
return JSON.parse(renderToAST(input));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function renderToAnsi(input) {
|
|
67
|
+
const exports = _getExports();
|
|
68
|
+
return render(exports, exports.md4x_to_ansi, input);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function renderToMeta(input) {
|
|
72
|
+
const exports = _getExports();
|
|
73
|
+
return render(exports, exports.md4x_to_meta, input);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function renderToText(input) {
|
|
77
|
+
const exports = _getExports();
|
|
78
|
+
return render(exports, exports.md4x_to_text, input);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function parseMeta(input) {
|
|
82
|
+
const meta = JSON.parse(renderToMeta(input));
|
|
83
|
+
if (!meta.title && meta.headings?.[0]) {
|
|
84
|
+
meta.title = meta.headings[0].text;
|
|
85
|
+
}
|
|
86
|
+
return meta;
|
|
87
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export {
|
|
2
|
+
renderToHtml,
|
|
3
|
+
renderToAST,
|
|
4
|
+
parseAST,
|
|
5
|
+
renderToAnsi,
|
|
6
|
+
parseMeta,
|
|
7
|
+
renderToMeta,
|
|
8
|
+
renderToText,
|
|
9
|
+
} from "./common.mjs";
|
|
10
|
+
|
|
11
|
+
import { _setInstance, _imports, _hasInstance } from "./common.mjs";
|
|
12
|
+
|
|
13
|
+
export async function init(opts) {
|
|
14
|
+
if (_hasInstance()) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const input = opts?.wasm;
|
|
18
|
+
let bytes;
|
|
19
|
+
if (input instanceof ArrayBuffer || input instanceof Uint8Array) {
|
|
20
|
+
bytes = input;
|
|
21
|
+
} else if (input instanceof WebAssembly.Module) {
|
|
22
|
+
const { instance } = await WebAssembly.instantiate(input, {
|
|
23
|
+
wasi_snapshot_preview1: wasiStub,
|
|
24
|
+
});
|
|
25
|
+
_setInstance(instance);
|
|
26
|
+
return;
|
|
27
|
+
} else if (
|
|
28
|
+
input instanceof Response ||
|
|
29
|
+
(typeof input === "object" && typeof input.then === "function")
|
|
30
|
+
) {
|
|
31
|
+
const response = await input;
|
|
32
|
+
if (response instanceof Response) {
|
|
33
|
+
bytes = await response.arrayBuffer();
|
|
34
|
+
} else {
|
|
35
|
+
bytes = response;
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
const fsp = globalThis.process?.getBuiltinModule?.("fs/promises");
|
|
39
|
+
if (fsp) {
|
|
40
|
+
const wasmPath = new URL("../../build/md4x.wasm", import.meta.url);
|
|
41
|
+
bytes = await fsp.readFile(wasmPath);
|
|
42
|
+
} else {
|
|
43
|
+
bytes = await fetch(
|
|
44
|
+
await import("../../build/md4x.wasm?url").then((m) => m.default),
|
|
45
|
+
).then((r) => r.arrayBuffer());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const { instance } = await WebAssembly.instantiate(bytes, _imports);
|
|
49
|
+
_setInstance(instance);
|
|
50
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export {
|
|
2
|
+
renderToHtml,
|
|
3
|
+
renderToAST,
|
|
4
|
+
parseAST,
|
|
5
|
+
renderToAnsi,
|
|
6
|
+
parseMeta,
|
|
7
|
+
renderToMeta,
|
|
8
|
+
renderToText,
|
|
9
|
+
} from "./common.mjs";
|
|
10
|
+
|
|
11
|
+
import { _setInstance, _hasInstance, _imports } from "./common.mjs";
|
|
12
|
+
|
|
13
|
+
export async function init() {
|
|
14
|
+
if (_hasInstance()) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
let { default: instance } = await import("md4x/build/md4x.wasm?module");
|
|
18
|
+
if (instance instanceof WebAssembly.Module) {
|
|
19
|
+
instance = await WebAssembly.instantiate(instance, _imports);
|
|
20
|
+
}
|
|
21
|
+
_setInstance(instance);
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "md4x",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"repository": "pi0/md4x",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
"./wasm": {
|
|
9
|
-
"unwasm": "./lib/unwasm.mjs",
|
|
10
|
-
"default": "./lib/wasm.mjs"
|
|
9
|
+
"unwasm": "./lib/wasm/unwasm.mjs",
|
|
10
|
+
"default": "./lib/wasm/defult.mjs"
|
|
11
11
|
},
|
|
12
12
|
"./napi": "./lib/napi.mjs",
|
|
13
13
|
"./build/md4x.wasm": "./build/md4x.wasm",
|
|
14
14
|
".": {
|
|
15
15
|
"node": "./lib/napi.mjs",
|
|
16
|
-
"default":
|
|
16
|
+
"default": {
|
|
17
|
+
"unwasm": "./lib/wasm/unwasm.mjs",
|
|
18
|
+
"default": "./lib/wasm/defult.mjs"
|
|
19
|
+
}
|
|
17
20
|
}
|
|
18
21
|
},
|
|
19
22
|
"types": "./lib/types.d.mts",
|
package/lib/unwasm.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { init as _init } from "./wasm.mjs";
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
renderToHtml,
|
|
5
|
-
renderToAST,
|
|
6
|
-
parseAST,
|
|
7
|
-
renderToAnsi,
|
|
8
|
-
parseMeta,
|
|
9
|
-
renderToMeta,
|
|
10
|
-
renderToText,
|
|
11
|
-
} from "./wasm.mjs";
|
|
12
|
-
|
|
13
|
-
export async function init(opts) {
|
|
14
|
-
if (opts?.wasm) {
|
|
15
|
-
return _init(opts);
|
|
16
|
-
}
|
|
17
|
-
const mod = await import("md4x/build/md4x.wasm?module");
|
|
18
|
-
return _init({ wasm: mod.default });
|
|
19
|
-
}
|
package/lib/wasm.mjs
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// --- internal ---
|
|
2
|
-
|
|
3
|
-
let _instance;
|
|
4
|
-
|
|
5
|
-
function getExports() {
|
|
6
|
-
if (!_instance) {
|
|
7
|
-
throw new Error("md4x: WASM not initialized. Call `await init()` first.");
|
|
8
|
-
}
|
|
9
|
-
return _instance.exports;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const wasiStub = {
|
|
13
|
-
fd_close: () => 0,
|
|
14
|
-
fd_seek: () => 0,
|
|
15
|
-
fd_write: () => 0,
|
|
16
|
-
proc_exit: () => {},
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function render(exports, fn, input, ...extra) {
|
|
20
|
-
const { memory, md4x_alloc, md4x_free, md4x_result_ptr, md4x_result_size } =
|
|
21
|
-
exports;
|
|
22
|
-
const encoded = new TextEncoder().encode(input);
|
|
23
|
-
const ptr = md4x_alloc(encoded.length);
|
|
24
|
-
new Uint8Array(memory.buffer).set(encoded, ptr);
|
|
25
|
-
const ret = fn(ptr, encoded.length, ...extra);
|
|
26
|
-
md4x_free(ptr);
|
|
27
|
-
if (ret !== 0) {
|
|
28
|
-
throw new Error("md4x: render failed");
|
|
29
|
-
}
|
|
30
|
-
const outPtr = md4x_result_ptr();
|
|
31
|
-
const outSize = md4x_result_size();
|
|
32
|
-
const result = new TextDecoder().decode(
|
|
33
|
-
new Uint8Array(memory.buffer, outPtr, outSize),
|
|
34
|
-
);
|
|
35
|
-
md4x_free(outPtr);
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// --- exports ----
|
|
40
|
-
|
|
41
|
-
export async function init(opts) {
|
|
42
|
-
if (_instance) return;
|
|
43
|
-
const input = opts?.wasm;
|
|
44
|
-
let bytes;
|
|
45
|
-
if (input instanceof ArrayBuffer || input instanceof Uint8Array) {
|
|
46
|
-
bytes = input;
|
|
47
|
-
} else if (input instanceof WebAssembly.Module) {
|
|
48
|
-
const { instance } = await WebAssembly.instantiate(input, {
|
|
49
|
-
wasi_snapshot_preview1: wasiStub,
|
|
50
|
-
});
|
|
51
|
-
_instance = instance;
|
|
52
|
-
return;
|
|
53
|
-
} else if (
|
|
54
|
-
input instanceof Response ||
|
|
55
|
-
(typeof input === "object" && typeof input.then === "function")
|
|
56
|
-
) {
|
|
57
|
-
const response = await input;
|
|
58
|
-
if (response instanceof Response) {
|
|
59
|
-
bytes = await response.arrayBuffer();
|
|
60
|
-
} else {
|
|
61
|
-
bytes = response;
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
const fsp = globalThis.process?.getBuiltinModule?.("fs/promises");
|
|
65
|
-
if (fsp) {
|
|
66
|
-
const wasmPath = new URL("../build/md4x.wasm", import.meta.url);
|
|
67
|
-
bytes = await fsp.readFile(wasmPath);
|
|
68
|
-
} else {
|
|
69
|
-
bytes = await fetch(
|
|
70
|
-
await import("../build/md4x.wasm?url").then((m) => m.default),
|
|
71
|
-
).then((r) => r.arrayBuffer());
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const { instance } = await WebAssembly.instantiate(bytes, {
|
|
75
|
-
wasi_snapshot_preview1: wasiStub,
|
|
76
|
-
});
|
|
77
|
-
_instance = instance;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function renderToHtml(input, opts) {
|
|
81
|
-
const flags = opts?.full ? 0x0008 : 0;
|
|
82
|
-
return render(getExports(), getExports().md4x_to_html, input, flags);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function renderToAST(input) {
|
|
86
|
-
return render(getExports(), getExports().md4x_to_ast, input);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function parseAST(input) {
|
|
90
|
-
return JSON.parse(renderToAST(input));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function renderToAnsi(input) {
|
|
94
|
-
return render(getExports(), getExports().md4x_to_ansi, input);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function renderToMeta(input) {
|
|
98
|
-
return render(getExports(), getExports().md4x_to_meta, input);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function renderToText(input) {
|
|
102
|
-
return render(getExports(), getExports().md4x_to_text, input);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function parseMeta(input) {
|
|
106
|
-
const meta = JSON.parse(renderToMeta(input));
|
|
107
|
-
if (!meta.title && meta.headings?.[0]) {
|
|
108
|
-
meta.title = meta.headings[0].text;
|
|
109
|
-
}
|
|
110
|
-
return meta;
|
|
111
|
-
}
|