This commit is contained in:
Max Richter
2025-09-26 12:42:06 +02:00
parent deae5acac8
commit ae5cd8481a
17 changed files with 649 additions and 233 deletions

View File

@@ -1,74 +1,99 @@
import { readable } from 'svelte/store';
import { readable } from "svelte/store";
declare global {
interface Window {
Go: {
new (): {
run: (inst: WebAssembly.Instance) => Promise<void>;
importObject: WebAssembly.Imports;
};
};
markaParseFile: (input: string) => string;
markaParseFileWithTemplate: (markdown: string, template: string) => string;
markaListTemplates: () => string;
markaGetTemplate: (name: string) => string;
}
interface Window {
Go: {
new(): {
run: (inst: WebAssembly.Instance) => Promise<void>;
importObject: WebAssembly.Imports;
};
};
markaMatchBlocks: (input: string) => unknown;
markaParseFile: (input: string) => string;
markaParseFileWithTemplate: (markdown: string, template: string) => string;
markaListTemplates: () => string;
markaGetTemplate: (name: string) => string;
}
}
export const wasmReady = readable(false, (set) => {
if (typeof window === 'undefined') {
return;
}
if (typeof window === "undefined") {
return;
}
const loadWasm = async () => {
const go = new window.Go();
try {
const result = await WebAssembly.instantiateStreaming(fetch('/main.wasm'), go.importObject);
go.run(result.instance);
set(true);
} catch (error) {
console.error('Error loading wasm module:', error);
}
};
const loadWasm = async () => {
const go = new window.Go();
try {
const result = await WebAssembly.instantiateStreaming(
fetch("/main.wasm"),
go.importObject,
);
go.run(result.instance);
set(true);
} catch (error) {
console.error("Error loading wasm module:", error);
}
};
if (document.readyState === 'complete') {
loadWasm();
} else {
window.addEventListener('load', loadWasm);
}
if (document.readyState === "complete") {
loadWasm();
} else {
window.addEventListener("load", loadWasm);
}
});
export interface ParseResult {
data: unknown;
timings: { [key: string]: number };
}
export type ParseResultSuccess = {
data: unknown;
timings: { [key: string]: number };
};
export type ParseResultError = {
error: string;
};
export type ParseResult = ParseResultSuccess | ParseResultError;
export function parseMarkdown(markdown: string): ParseResult {
if (typeof window.markaParseFile !== 'function') {
throw new Error('Wasm module not ready');
}
const result = window.markaParseFile(markdown);
return JSON.parse(result);
if (typeof window.markaParseFile !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaParseFile(markdown);
if (result.error) return result;
return JSON.parse(result);
}
export function parseMarkdownWithTemplate(markdown: string, template: string): ParseResult {
if (typeof window.markaParseFileWithTemplate !== 'function') {
throw new Error('Wasm module not ready');
}
const result = window.markaParseFileWithTemplate(markdown, template);
return JSON.parse(result);
export function matchBlocks(markdown: string): ParseResult {
if (typeof window.markaMatchBlocks !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaMatchBlocks(markdown) as ParseResult;
if (result.error) return result;
return JSON.parse(result);
}
export function parseMarkdownWithTemplate(
markdown: string,
template: string,
): ParseResult {
if (typeof window.markaParseFileWithTemplate !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaParseFileWithTemplate(markdown, template);
if (result.error) return result;
return JSON.parse(result);
}
export function listTemplates(): string[] {
if (typeof window.markaListTemplates !== 'function') {
throw new Error('Wasm module not ready');
}
const result = window.markaListTemplates();
return JSON.parse(result);
if (typeof window.markaListTemplates !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaListTemplates();
return JSON.parse(result);
}
export function getTemplate(name: string): string {
if (typeof window.markaGetTemplate !== 'function') {
throw new Error('Wasm module not ready');
}
return window.markaGetTemplate(name);
if (typeof window.markaGetTemplate !== "function") {
throw new Error("Wasm module not ready");
}
return window.markaGetTemplate(name);
}