Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import type { Diagnostic } from "@codemirror/lint";import type { EditorState } from "@codemirror/state";import type { FileId, TypstDiagnostic, TypstState } from "@typbase/engine";
import { forEachDiagnostic, linter } from "@codemirror/lint";import { LRUCache } from "lru-cache";
import type { TextRef, TypstRequestHandler } from "./types";
import { parseBackticks } from "./highlight";
/** * Latest Typst diagnostics per page path. The WYSIWYG plugin caches the * diagnostics of every compile here, so the linter source can reuse them * instead of compiling the same text a second time. */const diagnosticsCache = new LRUCache<string, { text: string; diagnostics: TypstDiagnostic[] }>({ max: 2,});
export const TYPST_LINT_MARK = "cm-typst-lint";
export function rememberDiagnostics( path: string, text: string, diagnostics: TypstDiagnostic[],): void { diagnosticsCache.set(path, { text, diagnostics });}
function cachedDiagnostics(path: string, text: string): TypstDiagnostic[] | undefined { const entry = diagnosticsCache.get(path);
return entry && entry.text === text ? entry.diagnostics : undefined;}
/** Converts engine diagnostics into CodeMirror lint diagnostics. */export function toLintDiagnostics(typstDiagnostics: TypstDiagnostic[]): Diagnostic[] { return typstDiagnostics.map((diagnostic) => ({ from: diagnostic.range.start, to: diagnostic.range.end, severity: diagnostic.severity, message: diagnostic.message, markClass: TYPST_LINT_MARK, renderMessage() { const fragment = document.createDocumentFragment(); const paragraph = document.createElement("p"); parseBackticks(diagnostic.message, paragraph); fragment.append(paragraph);
if (diagnostic.hints.length) { const list = document.createElement("ul"); list.className = "typst-hints";
for (const hint of diagnostic.hints) { const item = document.createElement("li"); parseBackticks(hint, item); list.append(item); }
fragment.append(list); }
return fragment; }, }));}
export function mergeTypstDiagnostics( state: EditorState, typstDiagnostics: TypstDiagnostic[],): Diagnostic[] { const foreign: Diagnostic[] = []; forEachDiagnostic(state, (diagnostic, from, to) => { if (diagnostic.markClass !== TYPST_LINT_MARK) foreign.push({ ...diagnostic, from, to }); });
return [...foreign, ...toLintDiagnostics(typstDiagnostics)];}
export interface TypstLinterOptions { onRequests?: TypstRequestHandler; onPanic?: (fileId: FileId) => void; /** Called after a diagnostics compile succeeds. */ onCompile?: () => void;}
/** * Typst diagnostics as a CodeMirror linter source. */export const typstLinter = ( fileId: FileId, spaceId: string, path: string, prelude: TextRef, typstState: TypstState, options: TypstLinterOptions = {},) => linter( async (view) => { const text = view.state.doc.toString(); const cached = cachedDiagnostics(path, text); if (cached) return toLintDiagnostics(cached);
let result: ReturnType<TypstState["diagnosePaged"]>; try { result = typstState.diagnosePaged(fileId, text, prelude.value); } catch (error) { console.error("[typst] diagnostics compile panicked:", error); options.onPanic?.(fileId);
return []; }
if (result.requests.length > 0 && options.onRequests) { const updated = await options.onRequests(result.requests, spaceId); if (updated) result = typstState.diagnosePaged(fileId, text, prelude.value); }
rememberDiagnostics(path, text, result.diagnostics); options.onCompile?.();
return toLintDiagnostics(result.diagnostics); }, { delay: 400 }, );