fix: most of the template blocks

This commit is contained in:
Max Richter
2025-09-30 19:28:56 +02:00
parent d35f3e5e2e
commit 2a1572f99d
20 changed files with 210 additions and 187 deletions

View File

@@ -1,20 +1,6 @@
import { readable } from "svelte/store";
declare global {
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") {
@@ -22,7 +8,7 @@ export const wasmReady = readable(false, (set) => {
}
const loadWasm = async () => {
const go = new window.Go();
const go = new globalThis.Go();
try {
const result = await WebAssembly.instantiateStreaming(
fetch("/main.wasm"),
@@ -38,7 +24,7 @@ export const wasmReady = readable(false, (set) => {
if (document.readyState === "complete") {
loadWasm();
} else {
window.addEventListener("load", loadWasm);
globalThis.addEventListener("load", loadWasm);
}
});
@@ -54,46 +40,75 @@ export type ParseResultError = {
export type ParseResult = ParseResultSuccess | ParseResultError;
export function parseMarkdown(markdown: string): ParseResult {
if (typeof window.markaParseFile !== "function") {
if (typeof globalThis.marka?.parseFile !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaParseFile(markdown);
if (result.error) return result;
return JSON.parse(result);
const resultString = globalThis.marka.parseFile(markdown);
return JSON.parse(resultString);
}
export function matchBlocks(markdown: string): ParseResult {
if (typeof window.markaMatchBlocks !== "function") {
export function compileTemplate(templateSource: string) {
if (typeof globalThis.marka?.compileTemplate !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaMatchBlocks(markdown) as ParseResult;
if (result.error) return result;
return JSON.parse(result);
const resultString = globalThis.marka.compileTemplate(templateSource);
const result = JSON.parse(resultString);
console.log({ result });
return result;
}
export function matchBlocks(markdown: string, template: string): ParseResult {
if (typeof globalThis.marka?.matchBlocks !== "function") {
throw new Error("Wasm module not ready");
}
const resultString = globalThis.marka.matchBlocks(markdown, template);
return JSON.parse(resultString);
}
export function parseMarkdownWithTemplate(
markdown: string,
template: string,
): ParseResult {
if (typeof window.markaParseFileWithTemplate !== "function") {
if (typeof globalThis.marka?.parseFileWithTemplate !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaParseFileWithTemplate(markdown, template);
if (result.error) return result;
return JSON.parse(result);
const resultString = globalThis.marka.parseFileWithTemplate(
markdown,
template,
);
return JSON.parse(resultString);
}
export function listTemplates(): string[] {
if (typeof window.markaListTemplates !== "function") {
if (typeof globalThis.marka?.listTemplates !== "function") {
throw new Error("Wasm module not ready");
}
const result = window.markaListTemplates();
return JSON.parse(result);
const resultString = globalThis.marka.listTemplates();
return JSON.parse(resultString);
}
export function getTemplate(name: string): string {
if (typeof window.markaGetTemplate !== "function") {
if (typeof globalThis.marka?.getTemplate !== "function") {
throw new Error("Wasm module not ready");
}
return window.markaGetTemplate(name);
return globalThis.marka.getTemplate(name);
}
export function detectType(markdown: string): string | ParseResultError {
if (typeof globalThis.marka?.detectType !== "function") {
throw new Error("Wasm module not ready");
}
const result = globalThis.marka.detectType(markdown);
try {
// If the result is a JSON string with an error, parse and return it
const parsed = JSON.parse(result);
if (parsed.error) {
return parsed;
}
} catch (e) {
// Otherwise, it's a plain string for success
return result;
}
return result;
}