Skip to content

Commit

Permalink
Fix sysdelim and systeme expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
siefkenj committed Mar 19, 2024
1 parent fa71152 commit 73ae463
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### v1.7.1

- Types fix for `@unified-latex/unified-latex-types`
- Fixed AST when expanding `\sysdelim` macros for rendering `\systeme{}` macros with KaTeX

### v1.7.0

Expand Down
12 changes: 9 additions & 3 deletions packages/unified-latex-to-hast/libs/pre-html-subs/katex-subs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ export const katexSpecificMacroReplacements: Record<
});

// If we have information about the sysdelims, then apply them
if (node._renderInfo?.sysdelims) {
const [frontDelim, backDelim]: [Ast.Node, Ast.Node] = node
if (node?._renderInfo?.sysdelims) {
const [frontDelim, backDelim]: [Ast.Node[], Ast.Node[]] = node
._renderInfo?.sysdelims as any;

return [LEFT, frontDelim, ret, RIGHT, backDelim];
return [
LEFT,
...(frontDelim || []),
ret,
RIGHT,
...(backDelim || []),
];
}

return [LEFT, DEFAULT_LEFT_DELIM, ret, RIGHT, DEFAULT_RIGHT_DELIM];
Expand Down
132 changes: 132 additions & 0 deletions packages/unified-latex-to-hast/tests/katex-subs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { describe, it, expect } from "vitest";
import util from "util";
import { parse } from "@unified-latex/unified-latex-util-parse";
import { replaceNode } from "@unified-latex/unified-latex-util-replace";
import {
attachNeededRenderInfo,
katexSpecificMacroReplacements,
} from "../libs/pre-html-subs/katex-subs";
import { match } from "@unified-latex/unified-latex-util-match";
import { Macro, Node } from "@unified-latex/unified-latex-types/index";
import { trimRenderInfo } from "@unified-latex/unified-latex-util-render-info";

// Make console.log pretty-print by default
const origLog = console.log;
console.log = (...args) => {
origLog(...args.map((x) => util.inspect(x, false, 10, true)));
};

describe("unified-latex-to-hast:katex-subs", () => {
it("can expand systeme", () => {
const parsed = trimRenderInfo(
parse("$\\sysdelim{[}{.}x=\\systeme{a&b\\\\x&y}$")
);
const isKatexMacro = match.createMacroMatcher(
katexSpecificMacroReplacements
);
attachNeededRenderInfo(parsed);
replaceNode(parsed, (node: Macro) => {
if (isKatexMacro(node)) {
return katexSpecificMacroReplacements[node.content](node);
}
});

//trimRenderInfo(parsed);

expect(parsed).toEqual({
content: [
{
content: [
{
content: "x",
type: "string",
},
{
content: "=",
type: "string",
},
{
content: "left",
type: "macro",
},
{
content: "[",
type: "string",
},
{
args: [
{
closeMark: "}",
content: [
{
content: "r",
type: "string",
},
],
openMark: "{",
type: "argument",
},
],
content: [
{
content: "a",
type: "string",
},
{
content: "&",
type: "string",
},
{
content: "b",
type: "string",
},
{
args: [
{
closeMark: "",
content: [],
openMark: "",
type: "argument",
},
{
closeMark: "",
content: [],
openMark: "",
type: "argument",
},
],
content: "\\",
type: "macro",
},
{
content: "x",
type: "string",
},
{
content: "&",
type: "string",
},
{
content: "y",
type: "string",
},
],
env: "array",
type: "environment",
},
{
content: "right",
type: "macro",
},
{
content: ".",
type: "string",
},
],
type: "inlinemath",
},
],
type: "root",
});
});
});
4 changes: 4 additions & 0 deletions packages/unified-latex-types/libs/info-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export type MacroInfo = {
* @type {boolean}
*/
tikzPathCommand?: boolean;
/**
* If `\sysdelims` is present, this contains the global information about the delimiters.
*/
sysdelims?: (Ast.Node[] | null)[];
};
/**
* The macro signature as an xparse argument specification string.
Expand Down
2 changes: 1 addition & 1 deletion packages/unified-latex-types/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig(({ mode }) => {
const plugins =
mode === "commonjs"
? []
: [dts({ rollupTypes: true }), packageReadmeAndPackageJson()];
: [dts({ rollupTypes: false }), packageReadmeAndPackageJson()];
console.log(`Building in mode: ${mode}.\n`);

return {
Expand Down

0 comments on commit 73ae463

Please sign in to comment.