Skip to content

Commit b69cd3d

Browse files
committed
chore: add JS formatting with biome
1 parent adeac8f commit b69cd3d

File tree

6 files changed

+144
-107
lines changed

6 files changed

+144
-107
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ README.md: lexers/*/*.go
1212
tokentype_string.go: types.go
1313
go generate
1414

15+
.PHONY: format-js
16+
format-js:
17+
biome format --write cmd/chromad/static/{index.js,chroma.js}
18+
1519
.PHONY: chromad
1620
chromad: build/chromad
1721

bin/.biome-1.9.4.pkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hermit

bin/biome

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.biome-1.9.4.pkg

biome.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.0.5/schema.json",
3+
"formatter": {
4+
"indentStyle": "space"
5+
}
6+
}

cmd/chromad/static/chroma.js

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,79 @@
11
// chroma.js - TinyGo WASM runtime initialization for Chroma syntax highlighter
22

33
// Import wasm_exec.js so that it initialises the Go WASM runtime.
4-
import './wasm_exec.js';
4+
import "./wasm_exec.js";
55

66
class ChromaWASM {
7-
constructor() {
8-
this.go = null;
9-
this.wasm = null;
10-
this.ready = false;
11-
this.readyPromise = this.init();
12-
}
7+
constructor() {
8+
this.go = null;
9+
this.wasm = null;
10+
this.ready = false;
11+
this.readyPromise = this.init();
12+
}
1313

14-
async init() {
15-
try {
16-
// Create a new Go instance
17-
this.go = new Go();
14+
async init() {
15+
try {
16+
// Create a new Go instance
17+
this.go = new Go();
1818

19-
// Load the WASM module
20-
const wasmResponse = await fetch('./static/chroma.wasm');
21-
if (!wasmResponse.ok) {
22-
throw new Error(`Failed to fetch chroma.wasm: ${wasmResponse.status}`);
23-
}
19+
// Load the WASM module
20+
const wasmResponse = await fetch("./static/chroma.wasm");
21+
if (!wasmResponse.ok) {
22+
throw new Error(`Failed to fetch chroma.wasm: ${wasmResponse.status}`);
23+
}
2424

25-
const wasmBytes = await wasmResponse.arrayBuffer();
26-
const wasmModule = await WebAssembly.instantiate(wasmBytes, this.go.importObject);
25+
const wasmBytes = await wasmResponse.arrayBuffer();
26+
const wasmModule = await WebAssembly.instantiate(
27+
wasmBytes,
28+
this.go.importObject,
29+
);
2730

28-
this.wasm = wasmModule.instance;
31+
this.wasm = wasmModule.instance;
2932

30-
// Run the Go program
31-
this.go.run(this.wasm);
33+
// Run the Go program
34+
this.go.run(this.wasm);
3235

33-
this.ready = true;
34-
console.log('Chroma WASM module initialized successfully');
35-
} catch (error) {
36-
console.error('Failed to initialize Chroma WASM module:', error);
37-
throw error;
38-
}
36+
this.ready = true;
37+
console.log("Chroma WASM module initialized successfully");
38+
} catch (error) {
39+
console.error("Failed to initialize Chroma WASM module:", error);
40+
throw error;
3941
}
42+
}
4043

41-
async waitForReady() {
42-
await this.readyPromise;
43-
if (!this.ready) {
44-
throw new Error('Chroma WASM module failed to initialize');
45-
}
44+
async waitForReady() {
45+
await this.readyPromise;
46+
if (!this.ready) {
47+
throw new Error("Chroma WASM module failed to initialize");
4648
}
49+
}
4750

48-
async highlight(source, lexer, formatter, withClasses) {
49-
await this.waitForReady();
51+
async highlight(source, lexer, formatter, withClasses) {
52+
await this.waitForReady();
5053

51-
if (typeof window.highlight !== 'function') {
52-
throw new Error('highlight function not available from WASM module');
53-
}
54+
if (typeof window.highlight !== "function") {
55+
throw new Error("highlight function not available from WASM module");
56+
}
5457

55-
try {
56-
return window.highlight(source, lexer, formatter, withClasses);
57-
} catch (error) {
58-
console.error('Error calling highlight function:', error);
59-
throw error;
60-
}
58+
try {
59+
return window.highlight(source, lexer, formatter, withClasses);
60+
} catch (error) {
61+
console.error("Error calling highlight function:", error);
62+
throw error;
6163
}
64+
}
6265
}
6366

64-
6567
export function isWasmSupported() {
6668
try {
67-
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
69+
if (
70+
typeof WebAssembly === "object" &&
71+
typeof WebAssembly.instantiate === "function"
72+
) {
6873
// The smallest possible WebAssembly module (magic number + version)
69-
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
74+
const module = new WebAssembly.Module(
75+
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00),
76+
);
7077
if (module instanceof WebAssembly.Module) {
7178
// Try to instantiate the module to ensure it's truly runnable
7279
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
@@ -78,6 +85,5 @@ export function isWasmSupported() {
7885
return false;
7986
}
8087

81-
8288
// Create global instance, null if WASM is not supported.
8389
export const chroma = isWasmSupported() ? new ChromaWASM() : null;

cmd/chromad/static/index.js

Lines changed: 79 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import * as Base64 from "./base64.js";
22
import { chroma } from "./chroma.js";
33

44
document.addEventListener("DOMContentLoaded", function () {
5-
var darkMode = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
6-
var style = document.createElement('style');
7-
var ref = document.querySelector('script');
5+
var darkMode =
6+
window.matchMedia &&
7+
window.matchMedia("(prefers-color-scheme: dark)").matches;
8+
var style = document.createElement("style");
9+
var ref = document.querySelector("script");
810
ref.parentNode.insertBefore(style, ref);
911

10-
var form = document.getElementById('chroma');
12+
var form = document.getElementById("chroma");
1113
var textArea = form.elements["text"];
1214
var styleSelect = form.elements["style"];
1315
var languageSelect = form.elements["language"];
@@ -16,49 +18,62 @@ document.addEventListener("DOMContentLoaded", function () {
1618
var output = document.getElementById("output");
1719
var htmlCheckbox = document.getElementById("html");
1820

19-
(document.querySelectorAll('.notification .delete') || []).forEach((el) => {
21+
(document.querySelectorAll(".notification .delete") || []).forEach((el) => {
2022
const notification = el.parentNode;
21-
el.addEventListener('click', () => {
23+
el.addEventListener("click", () => {
2224
notification.parentNode.removeChild(notification);
2325
});
2426
});
2527

2628
async function renderServer(formData) {
27-
return (await fetch("api/render", {
28-
method: 'POST',
29-
mode: 'cors',
30-
cache: 'no-cache',
31-
credentials: 'same-origin',
32-
headers: {
33-
'X-CSRF-Token': csrfToken,
34-
'Content-Type': 'application/json',
35-
},
36-
redirect: 'follow',
37-
referrer: 'no-referrer',
38-
body: JSON.stringify(formData),
39-
})).json();
29+
return (
30+
await fetch("api/render", {
31+
method: "POST",
32+
mode: "cors",
33+
cache: "no-cache",
34+
credentials: "same-origin",
35+
headers: {
36+
"X-CSRF-Token": csrfToken,
37+
"Content-Type": "application/json",
38+
},
39+
redirect: "follow",
40+
referrer: "no-referrer",
41+
body: JSON.stringify(formData),
42+
})
43+
).json();
4044
}
4145

4246
async function renderWasm(formData) {
43-
return await chroma.highlight(
44-
formData.text,
45-
formData.language,
46-
formData.style,
47-
formData.classes,
48-
);
47+
return await chroma.highlight(
48+
formData.text,
49+
formData.language,
50+
formData.style,
51+
formData.classes,
52+
);
4953
}
5054

5155
async function render(formData) {
52-
return chroma !== null
53-
? renderWasm(formData)
54-
: renderServer(formData);
56+
return chroma !== null ? renderWasm(formData) : renderServer(formData);
5557
}
5658

57-
5859
// https://stackoverflow.com/a/37697925/7980
5960
function handleTab(e) {
60-
var after, before, end, lastNewLine, changeLength, re, replace, selection, start, val;
61-
if ((e.charCode === 9 || e.keyCode === 9) && !e.altKey && !e.ctrlKey && !e.metaKey) {
61+
var after,
62+
before,
63+
end,
64+
lastNewLine,
65+
changeLength,
66+
re,
67+
replace,
68+
selection,
69+
start,
70+
val;
71+
if (
72+
(e.charCode === 9 || e.keyCode === 9) &&
73+
!e.altKey &&
74+
!e.ctrlKey &&
75+
!e.metaKey
76+
) {
6277
e.preventDefault();
6378
start = this.selectionStart;
6479
end = this.selectionEnd;
@@ -68,14 +83,14 @@ document.addEventListener("DOMContentLoaded", function () {
6883
replace = true;
6984
if (start !== end) {
7085
selection = val.substring(start, end);
71-
if (~selection.indexOf('\n')) {
86+
if (~selection.indexOf("\n")) {
7287
replace = false;
7388
changeLength = 0;
74-
lastNewLine = before.lastIndexOf('\n');
89+
lastNewLine = before.lastIndexOf("\n");
7590
if (!~lastNewLine) {
7691
selection = before + selection;
7792
changeLength = before.length;
78-
before = '';
93+
before = "";
7994
} else {
8095
selection = before.substring(lastNewLine) + selection;
8196
changeLength = before.length - lastNewLine;
@@ -87,9 +102,9 @@ document.addEventListener("DOMContentLoaded", function () {
87102
start--;
88103
changeLength--;
89104
}
90-
selection = selection.replace(re, '$1');
105+
selection = selection.replace(re, "$1");
91106
} else {
92-
selection = selection.replace(/(\n|^)/g, '$1\t');
107+
selection = selection.replace(/(\n|^)/g, "$1\t");
93108
start++;
94109
changeLength++;
95110
}
@@ -99,7 +114,7 @@ document.addEventListener("DOMContentLoaded", function () {
99114
}
100115
}
101116
if (replace && !e.shiftKey) {
102-
this.value = before + '\t' + after;
117+
this.value = before + "\t" + after;
103118
this.selectionStart = this.selectionEnd = start + 1;
104119
}
105120
}
@@ -109,7 +124,8 @@ document.addEventListener("DOMContentLoaded", function () {
109124
function debounce(func, wait, immediate) {
110125
var timeout;
111126
return function () {
112-
var context = this, args = arguments;
127+
var context = this;
128+
var args = arguments;
113129
var later = function () {
114130
timeout = null;
115131
if (!immediate) func.apply(context, args);
@@ -123,11 +139,11 @@ document.addEventListener("DOMContentLoaded", function () {
123139

124140
function getFormJSON() {
125141
return {
126-
"language": languageSelect.value,
127-
"style": styleSelect.value,
128-
"text": textArea.value,
129-
"classes": htmlCheckbox.checked,
130-
}
142+
language: languageSelect.value,
143+
style: styleSelect.value,
144+
text: textArea.value,
145+
classes: htmlCheckbox.checked,
146+
};
131147
}
132148

133149
async function update(event) {
@@ -145,12 +161,12 @@ document.addEventListener("DOMContentLoaded", function () {
145161
output.innerHTML = value.html;
146162
}
147163
} catch (error) {
148-
console.error('Error highlighting code:', error);
164+
console.error("Error highlighting code:", error);
149165
// Fallback: display plain text
150166
if (htmlCheckbox.checked) {
151167
output.innerText = textArea.value;
152168
} else {
153-
output.innerHTML = '<pre>' + textArea.value + '</pre>';
169+
output.innerHTML = "<pre>" + textArea.value + "</pre>";
154170
}
155171
}
156172

@@ -160,7 +176,7 @@ document.addEventListener("DOMContentLoaded", function () {
160176
}
161177

162178
function share(event) {
163-
let data = JSON.stringify(getFormJSON())
179+
let data = JSON.stringify(getFormJSON());
164180
data = Base64.encodeURI(data);
165181
location.hash = "#" + data;
166182
try {
@@ -172,26 +188,29 @@ document.addEventListener("DOMContentLoaded", function () {
172188
}
173189

174190
if (location.hash) {
175-
let json = Base64.decode(location.hash.substring(1))
191+
let json = Base64.decode(location.hash.substring(1));
176192
json = JSON.parse(json);
177193
textArea.value = json.text;
178194
languageSelect.value = json.language;
179195
styleSelect.value = json.style;
180196
htmlCheckbox.checked = json.classes;
181-
update(new Event('change'));
182-
} else if (darkMode) {
183-
styleSelect.value = "monokai";
184-
update(new Event("change"));
197+
update(new Event("change"));
198+
} else if (darkMode) {
199+
styleSelect.value = "monokai";
200+
update(new Event("change"));
185201
}
186202

187203
var eventHandler = (event) => update(event);
188-
var debouncedEventHandler = debounce(eventHandler, chroma === null ? 250 : 100);
189-
190-
languageSelect.addEventListener('change', eventHandler);
191-
styleSelect.addEventListener('change', eventHandler);
192-
htmlCheckbox.addEventListener('change', eventHandler);
193-
copyButton.addEventListener('click', share);
194-
195-
textArea.addEventListener('keydown', handleTab);
196-
textArea.addEventListener('change', debouncedEventHandler);
204+
var debouncedEventHandler = debounce(
205+
eventHandler,
206+
chroma === null ? 250 : 100,
207+
);
208+
209+
languageSelect.addEventListener("change", eventHandler);
210+
styleSelect.addEventListener("change", eventHandler);
211+
htmlCheckbox.addEventListener("change", eventHandler);
212+
copyButton.addEventListener("click", share);
213+
214+
textArea.addEventListener("keydown", handleTab);
215+
textArea.addEventListener("change", debouncedEventHandler);
197216
});

0 commit comments

Comments
 (0)