Skip to content

Commit

Permalink
Merge branch 'master' into redesign-2024
Browse files Browse the repository at this point in the history
# Conflicts:
#	.env
#	package.json
#	src/app/components/elements/panels/UserBetaFeatures.tsx
  • Loading branch information
jsharkey13 committed Feb 11, 2025
2 parents b6bad3e + 48e677d commit 5aee6ba
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def ask_to_run_command(command,
stdout_data.append(decoded_chunk) # Store for later

return_code = proc.wait()
output = ''.join(stdout_data)
output = ''.join(stdout_data).strip()
if return_code == 0:
return output
elif expected_nonzero_exit_codes and return_code in expected_nonzero_exit_codes:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"html-to-text": "^9.0.5",
"identity-obj-proxy": "3.0.0",
"immer": "^9.0.21",
"inequality": "^1.2.2",
"inequality": "^1.2.3",
"inequality-grammar": "1.3.5",
"isaac-graph-sketcher": "0.13.7",
"js-cookie": "^3.0.5",
Expand Down
1 change: 1 addition & 0 deletions src/IsaacAppTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export interface BooleanNotation {

export interface DisplaySettings {
HIDE_QUESTION_ATTEMPTS?: boolean;
PREFER_MATHML?: boolean;
}

export interface UserConsent {
Expand Down
24 changes: 14 additions & 10 deletions src/app/components/elements/markup/latexRendering.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {useContext} from "react";
import {selectors, useAppSelector, useGetSegueEnvironmentQuery} from "../../../state";
import {FigureNumberingContext, FigureNumbersById, PotentialUser} from "../../../../IsaacAppTypes";
import {useGetSegueEnvironmentQuery} from "../../../state";
import {FigureNumberingContext, FigureNumbersById} from "../../../../IsaacAppTypes";
import he from "he";
import {dropZoneRegex, renderA11yString, BOOLEAN_NOTATION, isAda, useUserPreferences} from "../../../services";
import {BOOLEAN_NOTATION, dropZoneRegex, isAda, renderA11yString, useUserPreferences} from "../../../services";
import katex, {KatexOptions} from "katex";
import 'katex/dist/contrib/mhchem.mjs';
import {Immutable} from "immer";

type MathJaxMacro = string|[string, number];

Expand Down Expand Up @@ -220,7 +219,13 @@ const ENDREF = "==ENDREF==";
const REF_REGEXP = new RegExp(REF + "(.*?)" + ENDREF, "g");
const SR_REF_REGEXP = new RegExp("start text, " + REF_REGEXP.source + ", end text,", "g");

export function katexify(html: string, user: Immutable<PotentialUser> | null, booleanNotation : BOOLEAN_NOTATION | undefined, showScreenReaderHoverText: boolean, figureNumbers: FigureNumbersById) {
export function katexify(
html: string,
booleanNotation : BOOLEAN_NOTATION | undefined,
showScreenReaderHoverText: boolean,
preferMathML: boolean,
figureNumbers: FigureNumbersById
) {
start.lastIndex = 0;
let match: RegExpExecArray | null;
let output = "";
Expand Down Expand Up @@ -287,8 +292,8 @@ export function katexify(html: string, user: Immutable<PotentialUser> | null, bo
// Until https://github.com/KaTeX/KaTeX/issues/3668 is resolved, apply suggested fix ourselves, awfully:
katexRenderResult = katexRenderResult.replaceAll("color:transparent;", "color:transparent;visibility:hidden;");

// If katex-a11y fails, generate MathML using KaTeX for accessibility
if (screenReaderText) {
// If katex-a11y fails, or MathML preferred, generate MathML using KaTeX for accessibility:
if (!preferMathML && screenReaderText) {
katexRenderResult = katexRenderResult.replace('<span class="katex">',
`<span class="katex"><span class="visually-hidden" aria-label="${screenReaderText}" role="text"></span>`);
} else {
Expand Down Expand Up @@ -333,10 +338,9 @@ export function katexify(html: string, user: Immutable<PotentialUser> | null, bo

// A hook wrapper around katexify that gets its required parameters from the current redux state and existing figure numbering context
export const useRenderKatex = () => {
const user = useAppSelector(selectors.user.orNull);
const {data: segueEnvironment} = useGetSegueEnvironmentQuery();
const {preferredBooleanNotation} = useUserPreferences();
const {preferredBooleanNotation, preferMathML} = useUserPreferences();
const figureNumbers = useContext(FigureNumberingContext);

return (markup: string) => katexify(markup, user, preferredBooleanNotation && BOOLEAN_NOTATION[preferredBooleanNotation], segueEnvironment === "DEV", figureNumbers);
return (markup: string) => katexify(markup, preferredBooleanNotation && BOOLEAN_NOTATION[preferredBooleanNotation], segueEnvironment === "DEV", preferMathML ?? false, figureNumbers);
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const InequalityMenuTab = ({menu, latexTitle, subMenu, className, isSubMenu = fa
if (!menuContext) return null;
const {activeMenu: [activeMenu, activeSubMenu], openNewMenuTab} = menuContext;

const navigate = () => openNewMenuTab([menu, subMenu ?? null]);
const active = activeMenu === menu && (isSubMenu ? activeSubMenu === subMenu : true);
const navigate = () => openNewMenuTab([menu, (!active || isSubMenu) && subMenu ? subMenu : null]);

return <li className={classNames(active ? "active" : "inactive", className)} onClick={navigate} onKeyUp={navigate}>
{isSubMenu ? <VShape/> : <TabShape/>}<Markup encoding={"latex"}>{`$${latexTitle}$`}</Markup>
Expand Down
13 changes: 12 additions & 1 deletion src/app/components/elements/panels/UserBetaFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const RevisionModeInput = ({displaySettings, setDisplaySettings}: Revisio
return <StyledCheckbox checked={displaySettings.HIDE_QUESTION_ATTEMPTS ?? false}
onChange={e => {
setDisplaySettings((oldDs) => ({...oldDs, HIDE_QUESTION_ATTEMPTS: e.target.checked}));
}}
}}
color={siteSpecific("primary", "")}
label={<p>Hide previous question attempts</p>}
id={"hide-previous-q-attempts"}
Expand All @@ -36,6 +36,17 @@ export const UserBetaFeatures = ({ displaySettings, setDisplaySettings, consentS
<b><RevisionModeInput {...{displaySettings, setDisplaySettings}}/></b>
<p>{`This feature lets you answer questions ${siteSpecific("that you have answered before, without seeing your old answer.", "again, even if you've answered them before.")} It's useful if you are reviewing a topic before a test or exam.`}</p>
</>
<>
<b><StyledCheckbox checked={displaySettings.PREFER_MATHML ?? false}
onChange={e => {
setDisplaySettings((oldDs) => ({...oldDs, PREFER_MATHML: e.target.checked}));
}}
color={siteSpecific("primary", "")}
label={<p>Use MathML for accessible maths</p>}
id={"prefer-mathml"}
/></b>
<p>{`With this setting you can toggle between using alternative text or MathML for mathematical equations.`}</p>
</>
{isAda && <>
<StyledCheckbox checked={consentSettings.OPENAI ?? false}
onChange={e => {
Expand Down
11 changes: 8 additions & 3 deletions src/app/services/userPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { AppState, useAppSelector } from '../state';
interface UseUserPreferencesReturnType {
preferredProgrammingLanguage?: PROGRAMMING_LANGUAGE;
preferredBooleanNotation?: BOOLEAN_NOTATION;
preferMathML?: boolean;
}

export function useUserPreferences(): UseUserPreferencesReturnType {
const {examBoard} = useUserViewingContext();

const {PROGRAMMING_LANGUAGE: programmingLanguage, BOOLEAN_NOTATION: booleanNotation} =
const {PROGRAMMING_LANGUAGE: programmingLanguage, BOOLEAN_NOTATION: booleanNotation, DISPLAY_SETTING: displaySettings} =
useAppSelector((state: AppState) => state?.userPreferences) || {};

// Programming language preference -
Expand All @@ -28,8 +29,12 @@ export function useUserPreferences(): UseUserPreferencesReturnType {
preferredBooleanNotation = examBoardBooleanNotationMap[examBoard];
}

// Accessibility preferences:
const preferMathML = displaySettings?.PREFER_MATHML;

return {
preferredProgrammingLanguage,
preferredBooleanNotation
preferredBooleanNotation,
preferMathML
};
}
}
2 changes: 1 addition & 1 deletion src/app/state/slices/api/segueInfoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const updatedIsaacApi = isaacApi.injectEndpoints({

getContentVersion: build.query<string, void>({
query: () => ({
url: "/info/content_versions/live_version",
url: "/admin/live_version",
method: "GET",
}),
onQueryStarted: onQueryLifecycleEvents({
Expand Down
14 changes: 7 additions & 7 deletions src/test/components/TrustedHtml.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('TrustedHtml LaTeX locator', () => {
it('can find basic delimiters', () => {
delimiters.forEach(([open, displayMode, close]) => {
const testcase = html[0] + wrapIn(open, math[0], close) + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + LATEX + html[1]);
// @ts-ignore
Expand All @@ -48,7 +48,7 @@ describe('TrustedHtml LaTeX locator', () => {
it("unbalanced delimiters don't break everything but instead are just skipped", () => {
delimiters.forEach(([open, , ]) => {
const testcase = html[0] + wrapIn(open, math[0], "") + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + open + math[0] + html[1]);
expect(katex.renderToString).not.toHaveBeenCalled();
Expand All @@ -59,7 +59,7 @@ describe('TrustedHtml LaTeX locator', () => {
nestedDollars.forEach((dollarMath) => {
delimiters.forEach(([open, displayMode, close]) => {
const testcase = html[0] + wrapIn(open, dollarMath, close) + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + LATEX + html[1]);
// @ts-ignore
Expand All @@ -77,7 +77,7 @@ describe('TrustedHtml LaTeX locator', () => {
it('can render environments', () => {
const env = "\\begin{aligned}" + math[0] + "\\end{aligned}";
const testcase = html[0] + env + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + LATEX + html[1]);
// @ts-ignore
Expand All @@ -93,7 +93,7 @@ describe('TrustedHtml LaTeX locator', () => {
it('missing refs show an inline error', () => {
const ref = "\\ref{foo[234o89tdgfiuno34£\"$%^Y}";
const testcase = html[0] + ref + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + "unknown reference " + ref + html[1]);
expect(katex.renderToString).not.toHaveBeenCalled();
Expand All @@ -102,7 +102,7 @@ describe('TrustedHtml LaTeX locator', () => {
it('found refs show their figure number', () => {
const ref = "\\ref{foo}";
const testcase = html[0] + ref + html[1];
const result = katexify(testcase, null, undefined, false, {foo: 42});
const result = katexify(testcase, undefined, false, false, {foo: 42});

const expectedFigureRef = "Figure" + "&nbsp;" + "42";
const expectedFigureRefWithFormatting = `<strong class="text-theme figure-reference">${expectedFigureRef}</strong>`;
Expand All @@ -114,7 +114,7 @@ describe('TrustedHtml LaTeX locator', () => {
const escapedDollar = "\\$";
const unescapedDollar = "$";
const testcase = html[0] + escapedDollar + html[1];
const result = katexify(testcase, null, undefined, false, {});
const result = katexify(testcase, undefined, false, false, {});

expect(result).toEqual(html[0] + unescapedDollar + html[1]);
expect(katex.renderToString).not.toHaveBeenCalled();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6351,10 +6351,10 @@ [email protected]:
moo "^0.5.2"
nearley "^2.20.1"

inequality@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/inequality/-/inequality-1.2.2.tgz#8b71d2a322b9e8fdce8a63a0495272f9736ac90a"
integrity sha512-VUPfa0KbqjSeqWVL/ZjOzP3OE2KoSTyWrVbWQ6Z1E0vMvyX9QzY8igKty2/YAAWpwqu9x1aGz104haxYieV8rQ==
inequality@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/inequality/-/inequality-1.2.3.tgz#5050360e5a188a5581000d518454bf3571a62e56"
integrity sha512-nzkJ3DCxHmuJbM8L+6q8P9pPK0vALlac9ShXdXZdK3Vyvo78PEKR6h5OQLfNBJkaHMvsKUQRSrKh9Zn6Ok978Q==

inflight@^1.0.4:
version "1.0.6"
Expand Down

0 comments on commit 5aee6ba

Please sign in to comment.