Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sysdelm and systeme expansion #93

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 14 additions & 1 deletion packages/unified-latex-prettier/libs/printer/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,20 @@ export function printLatexAst(
case "whitespace":
return line;
default:
console.warn("Printing unknown type", node);
console.warn(`Printing unknown type ${readableType(node)}`, node);
return printRaw(node);
}
}

/**
* Get a printable type for an object.
*/
function readableType(obj: any): string {
if (obj == null) {
return "null";
}
if (Array.isArray(obj)) {
return "array";
}
return typeof obj;
}
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