Skip to content

Open-source, local-first AI agent for real work.

2.9 KB
Raw Blame
import type { Extension } from "mdast-util-from-markdown";
import type { Tokenizer } from "micromark-util-types";
import type { Processor } from "unified";

declare module "micromark-util-types" {
	interface TokenTypeMap {
		vettaMath: "vettaMath";
		vettaMathData: "vettaMathData";
	}
}

type State = ReturnType<Tokenizer> | undefined;

/** micromark's line ending codes: CRLF, LF, CR. */
const isLineEnding = (code: number | null): boolean => code !== null && code < -2;

/**
 * Line endings must sit in their own `lineEnding` token: micromark splits text into
 * per-line chunks, and a token that swallows a line ending corrupts that split
 * (a RangeError from its splice buffer, or a hang) once the formula spans lines.
 */
const tokenize: Tokenizer = (effects, ok, nok) => {
	let closing = 41;
	let count = 0;
	return start;
	function start(code: number | null) {
		effects.enter("vettaMath");
		effects.consume(code);
		return delimiter;
	}
	function delimiter(code: number | null) {
		if (code !== 40 && code !== 91) return nok(code);
		closing = code === 40 ? 41 : 93;
		effects.consume(code);
		return between;
	}
	function between(code: number | null): State {
		if (code === null) return nok(code);
		if (isLineEnding(code)) {
			effects.enter("lineEnding");
			effects.consume(code);
			effects.exit("lineEnding");
			return between;
		}
		effects.enter("vettaMathData");
		return data(code);
	}
	function data(code: number | null): State {
		if (code === null || isLineEnding(code)) {
			effects.exit("vettaMathData");
			return between(code);
		}
		if (++count > 16_000) return nok(code);
		effects.consume(code);
		return code === 92 ? afterBackslash : data;
	}
	function afterBackslash(code: number | null): State {
		if (code === closing) {
			effects.consume(code);
			effects.exit("vettaMathData");
			effects.exit("vettaMath");
			return ok;
		}
		if (code === 40 || code === 91) return nok(code);
		// A doubled slash is TeX's line break, not a closing delimiter.
		if (code === 92) {
			effects.consume(code);
			return data;
		}
		return data(code);
	}
};

/** Tokenize before Markdown escaping; fenced/inline code and link destinations remain untouched. */
export function remarkMathAliases(this: Processor) {
	const data = this.data();
	data.micromarkExtensions ??= [];
	data.micromarkExtensions.push({ text: { 92: { name: "vettaMath", tokenize } } });
	const extension: Extension = {
		enter: {
			vettaMath(token) {
				const raw = this.sliceSerialize(token);
				this.enter(
					{
						type: "inlineMath",
						value: raw.slice(2, -2),
						data: {
							hName: "code",
							hProperties: { className: ["language-math", raw[1] === "[" ? "math-display" : "math-inline"] },
							hChildren: [{ type: "text", value: raw.slice(2, -2) }],
						},
					},
					token,
				);
			},
		},
		exit: {
			vettaMath(token) {
				this.exit(token);
			},
		},
	};
	data.fromMarkdownExtensions ??= [];
	data.fromMarkdownExtensions.push(extension);
}

import type {} from "remark-parse";