@oxidoc/highlight 0.1.0-beta.2
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 +32 -0
- package/oxidoc_highlight.d.ts +45 -0
- package/oxidoc_highlight.js +259 -0
- package/oxidoc_highlight_bg.wasm +0 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# oxidoc-highlight
|
|
2
|
+
|
|
3
|
+
Lightweight, zero-dependency syntax highlighting that emits HTML with `<span class="tok-*">` tokens. Designed for documentation engines where you want fast, predictable output without pulling in tree-sitter or heavyweight grammars.
|
|
4
|
+
|
|
5
|
+
## Supported Languages
|
|
6
|
+
|
|
7
|
+
Bash, C, CSS, Diff, Go, HTML, Java, JavaScript/JSX, JSON, Markdown, PHP, Python, RDX, Rust, SQL, TOML, TypeScript/TSX, XML, YAML
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```rust
|
|
12
|
+
let html = oxidoc_highlight::highlight("let x = 42;", "rust");
|
|
13
|
+
// → <span class="tok-keyword">let</span> x <span class="tok-operator">=</span> <span class="tok-number">42</span><span class="tok-punctuation">;</span>
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```rust
|
|
17
|
+
// Check language support
|
|
18
|
+
assert!(oxidoc_highlight::is_supported("rust"));
|
|
19
|
+
let langs = oxidoc_highlight::supported_languages();
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Wasm
|
|
23
|
+
|
|
24
|
+
Enable the `wasm` feature to get `wasm-bindgen` exports:
|
|
25
|
+
|
|
26
|
+
```toml
|
|
27
|
+
oxidoc-highlight = { version = "0.1", features = ["wasm"] }
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or [MIT License](LICENSE-MIT) at your option.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export function highlight(code: string, lang: string): string;
|
|
5
|
+
|
|
6
|
+
export function isSupported(lang: string): boolean;
|
|
7
|
+
|
|
8
|
+
export function supportedLanguages(): string[];
|
|
9
|
+
|
|
10
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
11
|
+
|
|
12
|
+
export interface InitOutput {
|
|
13
|
+
readonly memory: WebAssembly.Memory;
|
|
14
|
+
readonly highlight: (a: number, b: number, c: number, d: number) => [number, number];
|
|
15
|
+
readonly isSupported: (a: number, b: number) => number;
|
|
16
|
+
readonly supportedLanguages: () => [number, number];
|
|
17
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
18
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
19
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
20
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
21
|
+
readonly __externref_drop_slice: (a: number, b: number) => void;
|
|
22
|
+
readonly __wbindgen_start: () => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
29
|
+
* a precompiled `WebAssembly.Module`.
|
|
30
|
+
*
|
|
31
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
32
|
+
*
|
|
33
|
+
* @returns {InitOutput}
|
|
34
|
+
*/
|
|
35
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
39
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
40
|
+
*
|
|
41
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
42
|
+
*
|
|
43
|
+
* @returns {Promise<InitOutput>}
|
|
44
|
+
*/
|
|
45
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/* @ts-self-types="./oxidoc_highlight.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} code
|
|
5
|
+
* @param {string} lang
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
export function highlight(code, lang) {
|
|
9
|
+
let deferred3_0;
|
|
10
|
+
let deferred3_1;
|
|
11
|
+
try {
|
|
12
|
+
const ptr0 = passStringToWasm0(code, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
13
|
+
const len0 = WASM_VECTOR_LEN;
|
|
14
|
+
const ptr1 = passStringToWasm0(lang, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
15
|
+
const len1 = WASM_VECTOR_LEN;
|
|
16
|
+
const ret = wasm.highlight(ptr0, len0, ptr1, len1);
|
|
17
|
+
deferred3_0 = ret[0];
|
|
18
|
+
deferred3_1 = ret[1];
|
|
19
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
20
|
+
} finally {
|
|
21
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} lang
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export function isSupported(lang) {
|
|
30
|
+
const ptr0 = passStringToWasm0(lang, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
31
|
+
const len0 = WASM_VECTOR_LEN;
|
|
32
|
+
const ret = wasm.isSupported(ptr0, len0);
|
|
33
|
+
return ret !== 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @returns {string[]}
|
|
38
|
+
*/
|
|
39
|
+
export function supportedLanguages() {
|
|
40
|
+
const ret = wasm.supportedLanguages();
|
|
41
|
+
var v1 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice();
|
|
42
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
|
|
43
|
+
return v1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function __wbg_get_imports() {
|
|
47
|
+
const import0 = {
|
|
48
|
+
__proto__: null,
|
|
49
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
50
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
51
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
52
|
+
return ret;
|
|
53
|
+
},
|
|
54
|
+
__wbindgen_init_externref_table: function() {
|
|
55
|
+
const table = wasm.__wbindgen_externrefs;
|
|
56
|
+
const offset = table.grow(4);
|
|
57
|
+
table.set(0, undefined);
|
|
58
|
+
table.set(offset + 0, undefined);
|
|
59
|
+
table.set(offset + 1, null);
|
|
60
|
+
table.set(offset + 2, true);
|
|
61
|
+
table.set(offset + 3, false);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
__proto__: null,
|
|
66
|
+
"./oxidoc_highlight_bg.js": import0,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getArrayJsValueFromWasm0(ptr, len) {
|
|
71
|
+
ptr = ptr >>> 0;
|
|
72
|
+
const mem = getDataViewMemory0();
|
|
73
|
+
const result = [];
|
|
74
|
+
for (let i = ptr; i < ptr + 4 * len; i += 4) {
|
|
75
|
+
result.push(wasm.__wbindgen_externrefs.get(mem.getUint32(i, true)));
|
|
76
|
+
}
|
|
77
|
+
wasm.__externref_drop_slice(ptr, len);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let cachedDataViewMemory0 = null;
|
|
82
|
+
function getDataViewMemory0() {
|
|
83
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
84
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
85
|
+
}
|
|
86
|
+
return cachedDataViewMemory0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getStringFromWasm0(ptr, len) {
|
|
90
|
+
ptr = ptr >>> 0;
|
|
91
|
+
return decodeText(ptr, len);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let cachedUint8ArrayMemory0 = null;
|
|
95
|
+
function getUint8ArrayMemory0() {
|
|
96
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
97
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
98
|
+
}
|
|
99
|
+
return cachedUint8ArrayMemory0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
103
|
+
if (realloc === undefined) {
|
|
104
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
105
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
106
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
107
|
+
WASM_VECTOR_LEN = buf.length;
|
|
108
|
+
return ptr;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let len = arg.length;
|
|
112
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
113
|
+
|
|
114
|
+
const mem = getUint8ArrayMemory0();
|
|
115
|
+
|
|
116
|
+
let offset = 0;
|
|
117
|
+
|
|
118
|
+
for (; offset < len; offset++) {
|
|
119
|
+
const code = arg.charCodeAt(offset);
|
|
120
|
+
if (code > 0x7F) break;
|
|
121
|
+
mem[ptr + offset] = code;
|
|
122
|
+
}
|
|
123
|
+
if (offset !== len) {
|
|
124
|
+
if (offset !== 0) {
|
|
125
|
+
arg = arg.slice(offset);
|
|
126
|
+
}
|
|
127
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
128
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
129
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
130
|
+
|
|
131
|
+
offset += ret.written;
|
|
132
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
WASM_VECTOR_LEN = offset;
|
|
136
|
+
return ptr;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
140
|
+
cachedTextDecoder.decode();
|
|
141
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
142
|
+
let numBytesDecoded = 0;
|
|
143
|
+
function decodeText(ptr, len) {
|
|
144
|
+
numBytesDecoded += len;
|
|
145
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
146
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
147
|
+
cachedTextDecoder.decode();
|
|
148
|
+
numBytesDecoded = len;
|
|
149
|
+
}
|
|
150
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const cachedTextEncoder = new TextEncoder();
|
|
154
|
+
|
|
155
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
156
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
157
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
158
|
+
view.set(buf);
|
|
159
|
+
return {
|
|
160
|
+
read: arg.length,
|
|
161
|
+
written: buf.length
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let WASM_VECTOR_LEN = 0;
|
|
167
|
+
|
|
168
|
+
let wasmModule, wasm;
|
|
169
|
+
function __wbg_finalize_init(instance, module) {
|
|
170
|
+
wasm = instance.exports;
|
|
171
|
+
wasmModule = module;
|
|
172
|
+
cachedDataViewMemory0 = null;
|
|
173
|
+
cachedUint8ArrayMemory0 = null;
|
|
174
|
+
wasm.__wbindgen_start();
|
|
175
|
+
return wasm;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function __wbg_load(module, imports) {
|
|
179
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
180
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
181
|
+
try {
|
|
182
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
183
|
+
} catch (e) {
|
|
184
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
185
|
+
|
|
186
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
187
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
188
|
+
|
|
189
|
+
} else { throw e; }
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const bytes = await module.arrayBuffer();
|
|
194
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
195
|
+
} else {
|
|
196
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
197
|
+
|
|
198
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
199
|
+
return { instance, module };
|
|
200
|
+
} else {
|
|
201
|
+
return instance;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function expectedResponseType(type) {
|
|
206
|
+
switch (type) {
|
|
207
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function initSync(module) {
|
|
214
|
+
if (wasm !== undefined) return wasm;
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
if (module !== undefined) {
|
|
218
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
219
|
+
({module} = module)
|
|
220
|
+
} else {
|
|
221
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const imports = __wbg_get_imports();
|
|
226
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
227
|
+
module = new WebAssembly.Module(module);
|
|
228
|
+
}
|
|
229
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
230
|
+
return __wbg_finalize_init(instance, module);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function __wbg_init(module_or_path) {
|
|
234
|
+
if (wasm !== undefined) return wasm;
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
if (module_or_path !== undefined) {
|
|
238
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
239
|
+
({module_or_path} = module_or_path)
|
|
240
|
+
} else {
|
|
241
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (module_or_path === undefined) {
|
|
246
|
+
module_or_path = new URL('oxidoc_highlight_bg.wasm', import.meta.url);
|
|
247
|
+
}
|
|
248
|
+
const imports = __wbg_get_imports();
|
|
249
|
+
|
|
250
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
251
|
+
module_or_path = fetch(module_or_path);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
255
|
+
|
|
256
|
+
return __wbg_finalize_init(instance, module);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oxidoc/highlight",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "Lightweight syntax highlighting for documentation engines",
|
|
5
|
+
"version": "0.1.0-beta.2",
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/oxidoc-lab/oxidoc-highlight"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"oxidoc_highlight_bg.wasm",
|
|
13
|
+
"oxidoc_highlight.js",
|
|
14
|
+
"oxidoc_highlight.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "oxidoc_highlight.js",
|
|
17
|
+
"homepage": "https://github.com/oxidoc-lab/oxidoc-highlight",
|
|
18
|
+
"types": "oxidoc_highlight.d.ts",
|
|
19
|
+
"sideEffects": [
|
|
20
|
+
"./snippets/*"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"syntax-highlighting",
|
|
24
|
+
"highlighter",
|
|
25
|
+
"code",
|
|
26
|
+
"wasm",
|
|
27
|
+
"documentation"
|
|
28
|
+
]
|
|
29
|
+
}
|