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

[Locked Labels] Count lone unescaped $ as regular dollar signs #1875

Open
wants to merge 6 commits into
base: labels-util-function
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .changeset/small-owls-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus": patch
"@khanacademy/perseus-editor": patch
---

[Locked Labels] Count lone unescaped \$ as regular dollar signs in TeX
15 changes: 15 additions & 0 deletions packages/perseus/src/widgets/interactive-graphs/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,19 @@ describe("replaceOutsideTeX", () => {

expect(convertedString).toEqual("\\text{\\\\}");
});

test.each`
input | expectedOutput
${"$"} | ${"\\text{\\$}"}
${"$$"} | ${""}
${"\\$"} | ${"\\text{\\$}"}
${"$1$"} | ${"1"}
${"$1"} | ${"\\text{\\$1}"}
${"1$"} | ${"\\text{1\\$}"}
${"$$1$"} | ${"\\text{1\\$}"}
${"$1$$"} | ${"1\\text{\\$}"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice set of test cases :)

`("counts lone unescaped $ as TeX", ({input, expectedOutput}) => {
const convertedString = replaceOutsideTeX(input);
expect(convertedString).toEqual(expectedOutput);
});
});
27 changes: 18 additions & 9 deletions packages/perseus/src/widgets/interactive-graphs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function replaceOutsideTeX(mathString: string) {
}

type ParsedNode = {
type: "math" | "text";
type: "math" | "text" | "unescapedDollar";
content: string;
};

Expand All @@ -118,14 +118,23 @@ function condenseTextNodes(nodes: ParsedNode[] | undefined): Array<ParsedNode> {

let currentText = "";
for (const node of nodes) {
if (node.type === "math") {
if (currentText) {
result.push({type: "text", content: currentText});
currentText = "";
}
result.push(node);
} else {
currentText += node.content;
switch (node.type) {
case "math":
if (currentText) {
result.push({type: "text", content: currentText});
currentText = "";
}
result.push(node);
break;
case "unescapedDollar":
// If the unescaped dollar had a closing pair to define
// math, it would have been caught by the "math" case above.
// Since this unescaped dollar is caught here, we can
// assume it is alone and used as as a literal dollar sign.
currentText += "$";
break;
default:
currentText += node.content;
}
}

Expand Down
Loading