- Implemented in-house, zero-dependency utility modules in `pi-utils` covering DOM manipulation, markdown parsing, templating, browser automation helpers, and terminal buffers. - Migrated packages across the repository to consume the new internal utilities and `omptype` schema validators instead of external dependencies. - Removed multiple external runtime and development dependencies including Zod, Marked, LRU cache, Turndown, and Puppeteer browser packages.
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { Lexer, Marked, type TokenizerAndRendererExtension } from "../src/marked";
|
|
import goldens from "./fixtures/marked/goldens.json";
|
|
|
|
describe("marked compatibility", () => {
|
|
for (const golden of goldens) {
|
|
test(`matches marked tokens and HTML for ${golden.name}`, () => {
|
|
expect([...Lexer.lex(golden.source)]).toEqual(golden.tokens);
|
|
expect(new Marked().parse(golden.source)).toBe(golden.html);
|
|
});
|
|
}
|
|
|
|
test("runs block and inline tokenizer/renderer extensions", () => {
|
|
const latexBlock: TokenizerAndRendererExtension = {
|
|
name: "latexBlock",
|
|
level: "block",
|
|
start(src) {
|
|
const index = src.indexOf("$$\n");
|
|
return index === -1 ? undefined : index;
|
|
},
|
|
tokenizer(src) {
|
|
const match = /^\$\$\n([\s\S]+?)\n\$\$(?:\n|$)/.exec(src);
|
|
return match ? { type: "latexBlock", raw: match[0], text: match[1] } : undefined;
|
|
},
|
|
renderer(token) {
|
|
return `<math>${token.text}</math>\n`;
|
|
},
|
|
};
|
|
const inlineLatex: TokenizerAndRendererExtension = {
|
|
name: "latex",
|
|
level: "inline",
|
|
start(src) {
|
|
const index = src.indexOf("$");
|
|
return index === -1 ? undefined : index;
|
|
},
|
|
tokenizer(src) {
|
|
const match = /^\$([^\n$]+)\$/.exec(src);
|
|
return match ? { type: "latex", raw: match[0], text: match[1] } : undefined;
|
|
},
|
|
renderer(token) {
|
|
return `<i>${token.text}</i>`;
|
|
},
|
|
};
|
|
const marked = new Marked().use({ extensions: [latexBlock, inlineLatex] });
|
|
|
|
expect([...marked.lexer("before $x_i$\n\n$$\ny^2\n$$\n")]).toEqual([
|
|
{
|
|
type: "paragraph",
|
|
raw: "before $x_i$",
|
|
text: "before $x_i$",
|
|
tokens: [
|
|
{ type: "text", raw: "before ", text: "before ", escaped: false },
|
|
{ type: "latex", raw: "$x_i$", text: "x_i" },
|
|
],
|
|
},
|
|
{ type: "space", raw: "\n\n" },
|
|
{ type: "latexBlock", raw: "$$\ny^2\n$$\n", text: "y^2" },
|
|
]);
|
|
expect(marked.parse("before $x_i$\n\n$$\ny^2\n$$\n")).toBe("<p>before <i>x_i</i></p>\n<math>y^2</math>\n");
|
|
});
|
|
});
|