@ameyanagi/reshiki 0.0.0-stage → 0.12.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/src/index.js ADDED
@@ -0,0 +1,56 @@
1
+ // The public API: a thin layer over the wasm-bindgen exports that loads the
2
+ // module once, takes text or bytes, and returns text for text formats.
3
+ import load, * as wasm from "../dist/wasm/reshiki_web.js";
4
+ import { fontBytes } from "./fonts.js";
5
+
6
+ const BINARY = new Set(["png", "pdf", "cdx"]);
7
+ const encoder = new TextEncoder();
8
+ const decoder = new TextDecoder();
9
+ let loading;
10
+
11
+ export function init(module) {
12
+ // A failed load is forgotten, so a later call can try again.
13
+ loading ??= load(module === undefined ? undefined : { module_or_path: module }).catch((error) => {
14
+ loading = undefined;
15
+ throw error;
16
+ });
17
+ return loading.then(() => undefined);
18
+ }
19
+
20
+ const bytes = (data) => (typeof data === "string" ? encoder.encode(data) : data);
21
+
22
+ export async function convert(data, from, to) {
23
+ await init();
24
+ const output = await wasm.convert(bytes(data), from, to);
25
+ return BINARY.has(to) ? output : decoder.decode(output);
26
+ }
27
+
28
+ export async function renderSvg(data, format) {
29
+ await init();
30
+ return wasm.renderSvg(bytes(data), format);
31
+ }
32
+
33
+ export async function renderPng(data, format, options = {}) {
34
+ await init();
35
+ return wasm.renderPng(bytes(data), format, options.maxWidth, options.maxHeight);
36
+ }
37
+
38
+ export async function smiles(data, format) {
39
+ await init();
40
+ return wasm.smiles(bytes(data), format);
41
+ }
42
+
43
+ export async function inchi(data, format) {
44
+ await init();
45
+ return wasm.inchi(bytes(data), format);
46
+ }
47
+
48
+ export async function addFont(font) {
49
+ await init();
50
+ return wasm.addFont(await fontBytes(font));
51
+ }
52
+
53
+ export async function version() {
54
+ await init();
55
+ return wasm.version();
56
+ }